I am trying to export the content of my RTE developed with Lexical in HTML format. To do so, I have a button with an handleClick
function which is supposed to console.log
the content of the RTE in HTML.
When I try to export the content as a stringified JSON, there is no problem, I can see my content, for example:
{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"test content","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}
However as soon as I try to convert the content to HTML, I keep having an empty string.
Any idea what I could be doing wrong here? Here is the function supposed to export the content to HTML:
import { $generateHtmlFromNodes } from '@lexical/html';
const handleClick = (editor: LexicalEditor) => {
editor.update(() => {
const editorState = editor.getEditorState();
const jsonString = JSON.stringify(editorState);
console.log('jsonString', jsonString);
const htmlString = $generateHtmlFromNodes(editor);
console.log('htmlString', htmlString);
});
};
Thank you
$generateHtmlFromNodes
implementation – Crosstie