HTML serializer plugin for Plate rich text editor framework enabling bidirectional conversion between Slate nodes and HTML format
npx @tessl/cli install tessl/npm-udecode--plate-html-serializer@7.0.0The Plate HTML Serializer provides bidirectional conversion between Plate editor content (Slate nodes) and HTML format for the Plate rich text editor framework. It enables seamless integration between HTML content and Plate's internal Slate data structure, supporting both serialization (Slate → HTML) and deserialization (HTML → Slate).
npm install @udecode/plate-html-serializerimport {
// Plugin functions
createDeserializeHTMLPlugin,
withDeserializeHTML,
// Serialization functions
serializeHTMLFromNodes,
htmlStringToDOMNode,
// Deserialization functions
deserializeHTMLToDocumentFragment,
deserializeHTMLElement,
deserializeHTMLNode,
deserializeHTMLToElement,
deserializeHTMLToText,
deserializeHTMLToFragment,
deserializeHTMLToMarks,
deserializeHTMLToBreak,
// Constants
htmlDeserializerId
} from "@udecode/plate-html-serializer";For CommonJS:
const {
// Plugin functions
createDeserializeHTMLPlugin,
withDeserializeHTML,
// Serialization functions
serializeHTMLFromNodes,
htmlStringToDOMNode,
// Deserialization functions
deserializeHTMLToDocumentFragment,
deserializeHTMLElement,
deserializeHTMLNode,
deserializeHTMLToElement,
deserializeHTMLToText,
deserializeHTMLToFragment,
deserializeHTMLToMarks,
deserializeHTMLToBreak,
// Constants
htmlDeserializerId
} = require("@udecode/plate-html-serializer");import { createPlateEditor } from "@udecode/plate-core";
import {
createDeserializeHTMLPlugin,
serializeHTMLFromNodes,
deserializeHTMLToDocumentFragment
} from "@udecode/plate-html-serializer";
// Create editor with HTML deserialization support
const editor = createPlateEditor({
plugins: [
createDeserializeHTMLPlugin({
plugins: [] // Add your Plate plugins here for deserialization rules
})
]
});
// Serialize Slate nodes to HTML
const nodes = [{ type: 'p', children: [{ text: 'Hello world' }] }];
const html = serializeHTMLFromNodes(editor, {
plugins: [], // Add your Plate plugins here for rendering
nodes: nodes,
stripDataAttributes: true,
stripWhitespace: true
});
console.log(html); // "<p>Hello world</p>"
// Deserialize HTML string to Slate nodes
const htmlString = '<p>Hello <strong>world</strong></p>';
const slateNodes = deserializeHTMLToDocumentFragment(editor, {
plugins: [], // Add your Plate plugins here for deserialization rules
element: htmlString,
stripWhitespace: true
});
console.log(slateNodes);
// [{ type: 'p', children: [{ text: 'Hello ' }, { text: 'world', bold: true }] }]The Plate HTML Serializer is built around two core modules:
Core functionality for converting HTML content into Slate node structures, including plugin integration and low-level processing utilities.
function createDeserializeHTMLPlugin(
options?: WithDeserializeHTMLOptions
): PlatePlugin;
function withDeserializeHTML(
options?: WithDeserializeHTMLOptions
): WithOverride;
function deserializeHTMLElement<T = {}>(
editor: PlateEditor<T>,
options: { plugins: PlatePlugin<T>[]; element: HTMLElement; }
): DeserializeHTMLReturn;
function deserializeHTMLNode<T = {}>(
editor: PlateEditor<T>,
plugins: PlatePlugin<T>[]
): (node: HTMLElement | ChildNode) => DeserializeHTMLReturn;
function deserializeHTMLToElement<T = {}>(
editor: PlateEditor<T>,
options: {
plugins: PlatePlugin<T>[];
element: HTMLElement;
children: DeserializeHTMLChildren[];
}
): TElement | undefined;
function deserializeHTMLToText(
node: HTMLElement | ChildNode
): string | null;
function deserializeHTMLToFragment(options: {
element: HTMLElement;
children: DeserializeHTMLChildren[];
}): TDescendant[] | undefined;
function deserializeHTMLToMarks<T = {}>(
editor: PlateEditor<T>,
props: DeserializeMarksProps<T>
): TDescendant[];
function deserializeHTMLToBreak(
node: HTMLElement | ChildNode
): string | undefined;
interface WithDeserializeHTMLOptions {
plugins?: PlatePlugin[];
}
const htmlDeserializerId: "HTML Deserializer";Convert Slate nodes to HTML strings with extensive customization options for rendering, class preservation, and formatting.
function serializeHTMLFromNodes(
editor: PlateEditor,
options: SerializeHTMLOptions
): string;
function htmlStringToDOMNode(
rawHtml: string,
stripWhitespace?: boolean
): HTMLElement;Low-level utilities for processing HTML elements, fragments, and document structures into Slate-compatible formats.
function deserializeHTMLToDocumentFragment<T = {}>(
editor: PlateEditor<T>,
options: DocumentFragmentOptions<T>
): TDescendant[];// Deserialization types
type DeserializeHTMLChildren = ChildNode | TDescendant | string | null;
type DeserializeHTMLReturn =
| string
| null
| TDescendant[]
| TElement
| DeserializeHTMLChildren[];
type DeserializedHTMLElement = TDescendant;
interface DeserializeMarksProps<T = {}> {
plugins: PlatePlugin<T>[];
element: HTMLElement;
children: DeserializeHTMLChildren[];
}
interface WithDeserializeHTMLOptions {
plugins?: PlatePlugin[];
}
interface DocumentFragmentOptions<T> {
plugins: PlatePlugin<T>[];
element: HTMLElement | string;
stripWhitespace?: boolean;
}
// Serialization types
interface SerializeHTMLOptions {
plugins: PlatePlugin[];
nodes: TDescendant[];
stripDataAttributes?: boolean;
preserveClassNames?: string[];
slateProps?: Partial<SlateProps>;
stripWhitespace?: boolean;
}