slate-serializers
A collection of serializers to convert Slate JSON objects to various formats and vice versa. Designed to work in both Node.js and browser environments.
Getting started
Install only the packages you need. Each serializer is published under @slate-serializers/ on npm. The umbrella package slate-serializers re-exports the HTML and DOM serializers and their default configs—useful if you want a single dependency for server-side HTML ↔ Slate workflows.
DOM types and constructors
Serializer configs use the same DOM model as htmlparser2 / domhandler, but you should not add domhandler as an app dependency for types or constructors. Import types such as ChildNode with import type { ChildNode } from '@slate-serializers/html' (or slate-serializers; see PR #215). Import the Element and Text constructors from the same packages when you build nodes in markTransforms / elementTransforms (see PR #218), e.g. import { Element, Text } from '@slate-serializers/html'.
Packages at a glance
| npm package | Role |
|---|---|
| @slate-serializers/html | slateToHtml, htmlToSlate, and shared configs (depends on DOM internally). |
| @slate-serializers/dom | slateToDom — htmlparser2 DOM nodes before stringifying to HTML. Use when you need to inspect or tweak the DOM first. Docs |
| @slate-serializers/react | <SlateToReact /> for React output: same top-level keys as slateToHtml, plus elementTransforms for React nodes. |
| @slate-serializers/template | slateToTemplate — top-level nodes as HTML strings or custom serializers (e.g. JSX). Docs |
| @slate-serializers/utilities | Small helpers (e.g. style object handling) used across the monorepo; rarely needed directly unless you extend serializers. |
| slate-serializers | Meta package: re-exports htmlToSlate, slateToHtml, slateToDom, and related configs from the HTML/DOM packages. |
Install
Minimal examples
HTML round-trip:
React output:
Slate version
Serializers target Slate's modern data model (≥ 0.50). This demo is built with Slate ~0.101 and slate-react ~0.101. The library README notes historical testing on older releases; for compatibility detail and parser choices, see Engineering decisions in the monorepo.
slateToHtmlConvert Slate JSON to HTML.
htmlToSlateConvert HTML to Slate JSON.
slateToReactConvert Slate JSON to React.
slateToTemplateConvert Slate JSON to an array of HTML strings mixed with custom serializers (e.g. JSX).
slateToDomConvert Slate JSON to a
htmlparser2DOM before serializing to HTML—useful when you need to manipulate the tree first.