-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lexical Editor: Link Feature: Missing _target
attribute, incorrect href
attribute in HTML conversion
#6504
Comments
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.
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
|
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; |
This issue has been automatically locked. |
Link to reproduction
No response
Payload Version
2.0.0
Node Version
18.19.0
Next.js Version
N/A
Describe the Bug
First the definite bug; if "Open in new tab" is selected when creating a link in Lexical Editor the
_target
attribute is not applied, this can be seen in the code:Second, maybe not a bug, but not really covered in the documentation; When linking to an internal collection the
href
is the ID of the collection document, not an actual URL.It looks like there are some attempts to correctly find the
href
, but I'm not sure how to make it work. I am using aslug
field, but also tried using aurl
field with both relative URLs and full (http://...
) URLs, to no avail.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 usepayload.findByID
within the converter causes an infinite loop that I am unable to track down.Reproduction Steps
Apologies for not creating a reproduction, I don't think this warrants a full reproduction, but I can work on that if it is determined to be needed.
payload.config.ts
Text Block:
Relevant fields on link target schema:
Attempt at writing my own converter:
Adapters and Plugins
richtext-lexical
The text was updated successfully, but these errors were encountered: