当我意识到我无法将一些词汇内容存储为没有默认词汇类的纯 HTML 标签时,我几乎完成了 Lexical 的工作。 例如: 存储为:
<strong class="editor-text-bold editor-text-italic" data-lexical-text="true">Hi there</strong>
代替
<strong><i>Hi there</i></strong>
有没有办法将数据存储为纯 HTML,以便我可以直接加载任何 HTML 作为富文本?
我目前用来从 Lexical 生成 HTML 的代码:
const [editor] = useLexicalComposerContext();
const handleClick = (editor) => {
editor.update(() => {
const htmlString = $generateHtmlFromNodes(editor, null);
const html = DOMPurify.sanitize(htmlString, {
FORBID_ATTR: ["class", "dir"],
});
console.log(html);
setPreview(html);
});
};