@lexical/html
HTML
This package exports utility functions for converting Lexical
-> HTML
and HTML
-> Lexical
. These same functions are also used in the lexical-clipboard
package for copy and paste.
Full documentation can be found here.
Exporting
const htmlString = $generateHtmlFromNodes(editor, selection | null);
Importing
First we need to parse the HTML string into a DOM instance.
const parser = new DOMParser();
const dom = parser.parseFromString(htmlString, textHtmlMimeType);
const dom = new JSDOM(htmlString);
And once you have the DOM instance.
const nodes = $generateNodesFromDOM(editor, dom);
const editor = createEditor({ ...config, nodes });
const selection = $getSelection();
selection.insertNodes(nodes);