HTML serializer plugin for Plate rich text editor framework enabling bidirectional conversion between Slate nodes and HTML format
—
HTML serialization functionality for converting Plate editor content (Slate nodes) into HTML strings. This module provides comprehensive control over HTML output formatting, class preservation, and rendering customization.
Main function for converting Slate node structures to HTML with extensive customization options.
/**
* Convert Slate Nodes into HTML string
* @param editor - Plate editor instance
* @param options - Serialization configuration options
* @returns HTML string representation of the nodes
*/
function serializeHTMLFromNodes(
editor: PlateEditor,
options: SerializeHTMLOptions
): string;
interface SerializeHTMLOptions {
/** Plugins with renderElement or renderLeaf for custom rendering */
plugins: PlatePlugin[];
/** Slate nodes to convert to HTML */
nodes: TDescendant[];
/** Enable stripping data attributes (default: true) */
stripDataAttributes?: boolean;
/** List of className prefixes to preserve from being stripped out */
preserveClassNames?: string[];
/** Slate props to provide if the rendering depends on slate hooks */
slateProps?: Partial<SlateProps>;
/** Whether to strip whitespaces from serialized HTML (default: true) */
stripWhitespace?: boolean;
}Usage Examples:
import { serializeHTMLFromNodes } from "@udecode/plate-html-serializer";
// Basic serialization
const nodes = [
{
type: 'p',
children: [{ text: 'Hello world' }]
}
];
const html = serializeHTMLFromNodes(editor, {
plugins: myPlugins,
nodes: nodes
});
// Result: "<p>Hello world</p>"
// Advanced serialization with options
const htmlWithOptions = serializeHTMLFromNodes(editor, {
plugins: myPlugins,
nodes: complexNodes,
stripDataAttributes: false, // Keep data attributes
preserveClassNames: ['my-custom-'], // Preserve classes starting with 'my-custom-'
stripWhitespace: false, // Keep whitespace formatting
slateProps: { // Additional Slate context
value: editor.children,
editor: editor
}
});
// Serializing with custom class preservation
const styledHtml = serializeHTMLFromNodes(editor, {
plugins: myPlugins,
nodes: styledNodes,
preserveClassNames: ['highlight-', 'theme-', 'slate-'], // Keep these class prefixes
stripDataAttributes: true // Remove slate data attributes
});Low-level utilities for HTML string processing and DOM manipulation.
/**
* Convert HTML string into HTML element
* @param rawHtml - HTML string to convert
* @param stripWhitespace - Whether to strip whitespace (default: true)
* @returns HTMLElement with parsed content
*/
function htmlStringToDOMNode(
rawHtml: string,
stripWhitespace?: boolean
): HTMLElement;Usage Example:
import { htmlStringToDOMNode } from "@udecode/plate-html-serializer";
// Convert HTML string to DOM element
const htmlString = '<div><p>Content</p><p>More content</p></div>';
const domElement = htmlStringToDOMNode(htmlString, true);
// Use with deserialization
const slateNodes = deserializeHTMLElement(editor, {
plugins: myPlugins,
element: domElement
});The serialization process follows these steps:
data-slate-node, data-slate-type, etc.)data-testid)Control which CSS classes are preserved in the final HTML output:
// Preserve all classes starting with specific prefixes
const html = serializeHTMLFromNodes(editor, {
plugins: myPlugins,
nodes: nodes,
preserveClassNames: [
'slate-', // Keep Slate's default classes
'editor-', // Keep custom editor classes
'highlight-' // Keep syntax highlighting classes
]
});Choose whether to strip Slate-specific data attributes:
// Keep all data attributes (useful for debugging)
const htmlWithData = serializeHTMLFromNodes(editor, {
plugins: myPlugins,
nodes: nodes,
stripDataAttributes: false
});
// Strip data attributes for clean output (default)
const cleanHtml = serializeHTMLFromNodes(editor, {
plugins: myPlugins,
nodes: nodes,
stripDataAttributes: true
});Control whitespace processing in the output:
// Preserve whitespace formatting
const formattedHtml = serializeHTMLFromNodes(editor, {
plugins: myPlugins,
nodes: nodes,
stripWhitespace: false
});
// Remove extra whitespace for compact output (default)
const compactHtml = serializeHTMLFromNodes(editor, {
plugins: myPlugins,
nodes: nodes,
stripWhitespace: true
});The serializer integrates with Plate's plugin system to provide customizable rendering:
// Plugin with custom serialization
const myPlugin = {
key: 'my-element',
serialize: {
element: ({ element, children }) => {
if (element.type === 'my-element') {
return <div className="custom-element">{children}</div>;
}
}
},
renderElement: ({ element, children }) => {
if (element.type === 'my-element') {
return <div className="custom-element">{children}</div>;
}
}
};
// Use plugin in serialization
const html = serializeHTMLFromNodes(editor, {
plugins: [myPlugin],
nodes: nodes
});Install with Tessl CLI
npx tessl i tessl/npm-udecode--plate-html-serializer