Replies: 1 comment
-
For context, there's simplified version of my function that converts HTML to serialized editor state: import { $getRoot, $getSelection } from "lexical";
import { createHeadlessEditor } from "@lexical/headless";
import { $createServerBlockNode, LinkNode, ServerBlockNode } from "@payloadcms/richtext-lexical";
import { JSDOM } from "jsdom";
import { $generateNodesFromDOM } from "@lexical/html";
// ↓ This causes error 💣💥
import { $createLinkNode } from "@payloadcms/richtext-lexical/client";
const BASE_URL = "https://example.com";
function htmlToLexical(html: string) {
const editor = createHeadlessEditor({
nodes: [ServerBlockNode, LinkNode],
html: {
import: {
figure: () => ({
conversion(element) {
const imageElement = element.querySelector("img");
const captionElement = element.querySelector("figcaption");
const node = $createServerBlockNode({
blockName: "figure",
blockType: "figure",
imageSrc: imageElement?.src,
caption: captionElement?.textContent,
});
return { node };
},
}),
a: () => ({
priority: 1,
conversion(element) {
// Some preparation/processing/validation logic
const anchorElement = element as HTMLAnchorElement;
const url = new URL(anchorElement.href, BASE_URL);
const node = $createLinkNode({
fields: {
linkType: "custom",
newTab: anchorElement.target === "_blank",
url: url.toString(),
},
});
return { node };
},
}),
},
},
});
editor.update(
() => {
const dom = new JSDOM(html);
const nodes = $generateNodesFromDOM(editor, dom.window.document);
$getRoot().select();
const selection = $getSelection();
if (selection === null) {
throw new Error("Selection is null");
}
selection.insertNodes(nodes);
},
{ discrete: true },
);
return editor.getEditorState().toJSON();
}
console.log(
JSON.stringify(
htmlToLexical(`
<p><a href="/">Home</a>
<figure>
<img src="image.webp" />
<figcatption>
Nice image
</figcaption>
</figure>
`),
null,
2,
),
); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm converting HTML from legacy website into lexical editor state in Node script. I was successful in mapping some components into
BlockNode
with$createServerBlockNode
:However, there are no such server equivalents for other built-in nodes like, for example,
LinkNode
. There's$createLinkNode
in"@payloadcms/richtext-lexical/client"
, but when I attempt to import this module, I get following error:I'm not sure whether this is bug or not. Since I'm importing client module, I suppose it's intended for bundlers. On the other hand, there are no server-side equivalents like
$createServerLinkNode
or something.I suppose that
LinkNode
itself is not client dependent since it does not have sever node variant of itself. But something else that is imported or reexported in@payloadcms/richtext-lexical/client
is, which causes trouble.Beta Was this translation helpful? Give feedback.
All reactions