Are there any examples to convert a html string to lexical to save it in the db?
Finding hard to understand the documentation.
Hey, what exactly is unclear in the documentation? Maybe I can improve it
export function HtmlToLexical({ htmlString }: HtmlProps) {
const parser = new DOMParser();
const dom = parser.parseFromString(htmlString, textHtmlMimeType);
// Once you have the DOM instance it's easy to generate LexicalNodes.
const nodes = $generateNodesFromDOM(editor, dom);
// Select the root
$getRoot().select();
// Insert them at a selection.
$insertNodes(nodes);}
i want to create a function to convert my html String to the lexical format so i want to return the text in lexical json format so i can save the text in my db
how do you use the method above though?
Here's a better example:
https://payloadcms.com/docs/rich-text/lexical#html-lexicalAll the functions prefixed with $ have to be wrapped inside of an
editor.update()
call back - otherwise they won't work
what do i have to fill the const yourEditorConfig; // <= your editor config here with?
and how do i return the json after the convertion
what do i have to fill the const yourEditorConfig; // <= your editor config here with?
You can use
sanitizeEditorConfig(defaultEditorConfig)
to get the default editor config and just use that one.
and how do i return the json after the convertion
const editorJSON = headlessEditor.getEditorState().toJSON()
thank you!
Star
Discord
online
Get dedicated engineering support directly from the Payload team..