htmlToSlate
All output.json file content on this page is generated with the htmlToSlate serializer.Default
By default, htmlToSlate incorporates transformation rules based on the example in HTML | Serializing | Slate.
Configuration
Slate JS has a schema-less core. It makes few assumptions about the schema of the data you will be transforming. See Principles | Introduction | Slate
As a result, it is likely that you will need to create your own configuration file that implements your schema.
Starting point
- See packages/html/src/lib/serializers/htmlToSlate/config/payload.ts for an example of how to extend the default configuration; or
- copy packages/html/src/lib/serializers/htmlToSlate/config/default.ts and rewrite it as appropriate.
Payload CMS
If you are using Slate Rich Text in Payload CMS, a dedicated configuration file is available. See htmlToSlate: Payload CMS configuration.
Options
textTags
Define transform functions for HTML formatting elements.
- Default: packages/html/src/lib/serializers/htmlToSlate/config/default.ts.
- Receives
elof typeElement. ImportElementfrom@slate-serializers/html(orslate-serializers).- Combine with utilities from
domutilsto perform further manipulation.
- Combine with utilities from
- Test examples: packages/html/src/lib/tests/htmlToSlate/configuration/textTags.spec.ts.
In the following example, strong and i HTML tags are mapped in the default configuration.
elementTags
Map HTML element tags to Slate JSON nodes.
- Default: packages/html/src/lib/serializers/htmlToSlate/config/default.ts.
- Receives
elof typeElement. ImportElementfrom@slate-serializers/html(orslate-serializers).- Combine with utilities from
domutilsto perform further manipulation.
- Combine with utilities from
- Test examples: packages/html/src/lib/tests/htmlToSlate/configuration/elementTags.spec.ts.
textTags vs elementTags
Use elementTags transform functions for HTML element tags that structure content. e.g. h1, h2, div...etc.
Use textTags transform functions for HTML element tags that define inline meaning, structure or style of content. e.g. strong, abbr, sub...etc.
textTags are combined to represent inline meaning/structure/style whereas elementTags always create new Slate nodes.elementAttributeTransform
Apply attribute transformations to every node.
- Test example: packages/html/src/lib/tests/htmlToSlate/configuration/elementAttributeTransform.spec.ts.
elementTagscan also be used to transform attributes, but these functions are defined per element.elementAttributeTransformaccepts a single function that applies to every element.
htmlUpdaterMap
Manipulate/Transform your HTML before serialization.
A powerful feature that allows you to hook into the DOM object created using htmlparser2 and perform manipulation with utilities such as domutils before Slate nodes are created.
In the following example, the structure of the DOM is changed before Slate nodes are created.
htmlPreProcessString
Perform any operations on the HTML string before serializing to the DOM. This is the first operation to run.
String operations are not ideal, but may be necessary in some cases.
- Default: packages/html/src/lib/serializers/htmlToSlate/config/default.ts.
- In the default config, regular expressions are used to replace all
<pre>HTML elements with<code>. This is helpful becausehtmlparser2will separate out<pre>tags into their own block, whereas<code>tags are kept inline.
filterWhitespaceNodes
Remove any Slate JSON nodes that have no type or content. For example:
These nodes can appear when whitespace handling splits or empties content; enable this option to drop them.
convertBrToLineBreak
When true, convert <br> tags according to brStrategy. Set to false to leave <br> for elementTags (or drop them if unmapped).
Default: true.
- Default: packages/html/src/lib/serializers/htmlToSlate/config/default.ts.
- Test examples: packages/html/src/lib/tests/htmlToSlate/configuration/convertBrToLineBreak.spec.ts.
brStrategy
How <br> tags become Slate text when convertBrToLineBreak is true.
Default: 'block'. Use 'newline' when you want line breaks as \n inside a single block instead of separate blocks.
'block'— empty text ('') outside a block context (often its own block);\ninside one. Top-levelLine 1<br>Line 2becomes three default blocks.'newline'— always\n; merge adjacent plain-text leaves; collapse<br><br>before a following block to a single\n. Top-levelLine 1<br>Line 2becomes one default block withLine 1\nLine 2.
liftWrappedBlocks
HTML often wraps its content in an element that has no mapping in elementTags, such as <div>, <section>, or the <body> of a full HTML document. When such a wrapper is at the top level and contains only block-level HTML elements (<p>, <h1>, <ul>, …), their Slate elements are placed at the top level of the result.
Default: true.
- Default: packages/html/src/lib/serializers/htmlToSlate/config/default.ts.
- Wrappers that contain text or inline elements (for example
<div>Text <a>link</a></div>) become a single Slate element, as before. - Wrappers mapped in
elementTagsare never lifted. - The
<head>of a full HTML document is ignored, so text such as the page<title>is not added to the content. - Test examples: packages/html/src/lib/serializers/htmlToSlate/wrappers.spec.ts.
Set liftWrappedBlocks to false to keep the Slate elements inside a single Slate element with no type.
trimWhiteSpace
Extra whitespace is valid in HTML and will often be reduced to a single space or removed when the HTML is rendered. By default, htmlToSlate will apply such whitespace reduction rules to Slate node values.
Default: true.