Giter Club home page Giter Club logo

Comments (2)

AlessioGr avatar AlessioGr commented on July 24, 2024 1

Thank you for the detailed issue! This has been fixed by #6544. Keep in mind that the fix will only be available in the latest v3 beta (3.0.0-beta.37) and won't be backported to 2.0.

Target attributes have been added back to all default link converters.

I also tried (naively) to build my own implementation, but node.fields.doc.value is always the ID, never the full document, and trying to use payload.findByID within the converter causes an infinite loop that I am unable to track down.

Building an HTML converter for this was the right move, as there is no way for a default converter to know what the final URL of the internal link will be. The infinite loop from payload.findByID could have be due to 2 issues:

  1. I have noticed that the default converters list was growing with every field initialization. This could have caused lag. #6544 fixed this
  2. In 2.0, the payload object was not passed through to the converter functions. Using the imported one can cause issues. In 3.0, payload is now passed through as an argument. I have tried your provided example which uses payload.findByID, and it works perfectly!

from payload.

dlford avatar dlford commented on July 24, 2024

Thank you @AlessioGr!

I was able to apply your filtering logic to the converters passed in to my converter to resolve the issue for the time being. Looking forward to 3.0!

import {
  HTMLConverter,
  LinkNode,
  SerializedLinkNode,
  convertLexicalNodesToHTML,
} from '@payloadcms/richtext-lexical';
import payload from 'payload';

const LinkHTMLConverter: HTMLConverter<SerializedLinkNode> = {
  converter: async function ({ converters, node, parent }) {
    const foundNodeTypes: string[] = [];
    const filteredConverters: HTMLConverter[] = [];
    for (const converter of converters) {
      if (!converter.nodeTypes?.length) {
        continue;
      }
      const newConverter: HTMLConverter = {
        converter: converter.converter,
        nodeTypes: [...converter.nodeTypes],
      };
      newConverter.nodeTypes = newConverter.nodeTypes.filter(
        (nodeType) => {
          if (foundNodeTypes.includes(nodeType)) {
            return false;
          }
          foundNodeTypes.push(nodeType);
          return true;
        },
      );

      if (newConverter.nodeTypes.length) {
        filteredConverters.push(newConverter);
      }
    }

    const childrenText = await convertLexicalNodesToHTML({
      converters: filteredConverters,
      lexicalNodes: node.children,
      parent: {
        ...node,
        parent,
      },
    });

    const rel: string = node.fields.newTab
      ? ' rel="noopener noreferrer"'
      : '';

    const target: string = node.fields.newTab ? ' target="_blank"' : '';

    let href: string = '';
    if (node.fields.linkType === 'custom') {
      href = node.fields.url;
    } else if (
      !!node.fields.doc?.value &&
      node.fields.doc?.relationTo &&
      typeof node.fields.doc?.value === 'string' &&
      !node.fields.doc.value.startsWith('http')
    ) {
      const document = await payload.findByID({
        id: node.fields.doc.value,
        collection: node.fields.doc.relationTo,
      });
      href = `/${document.slug}`;
    } else {
      href = (node.fields.doc?.value as string) ?? '';
    }

    return `<a href="${href}"${target}${rel}>${childrenText}</a>`;
  },
  nodeTypes: [LinkNode.getType()],
};

export default LinkHTMLConverter;

from payload.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.