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 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
el
of typeElement
as an argument.- Combine with utilities from
domutils
to 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
el
of typeElement
as an argument.- Combine with utilities from
domutils
to 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.
elementTags
can also be used to transform attributes, but these functions are defined per element.elementAttributeTransform
accepts 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 manipultion with utilities such as domutils
.
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 becausehtmlparser2
will 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 may appear after processing whitespace.
convertBrToLineBreak
Convert <br>
HTML element tags to Slate nodes with empty content or \n
as appropriate.
Default: true
.
- Default: packages/html/src/lib/serializers/htmlToSlate/config/default.ts.
- Test examples: packages/html/src/lib/tests/htmlToSlate/configuration/convertBrToLineBreak.spec.ts.
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
.
- Default: packages/html/src/lib/serializers/htmlToSlate/config/default.ts.
- Test examples: packages/html/src/lib/tests/htmlToSlate/configuration/whitespace.spec.ts.
- See rationale in processing whitespace.