slateToHtml
All output.html
file content on this page is generated with serializers.Default
By default, slateToHtml incorporates transformation rules based on the example in Deserializing | 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/dom/src/lib/config/payload.ts for an example of how to extend the default configuration; or
- copy packages/dom/src/lib/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 slateToHtml
: Payload CMS configuration.
Options
markMap
Map Slate JSON properties to HTML formatting element tags.
- Default: packages/dom/src/lib/config/default.ts.
- A Slate JSON node may have multiple attributes.
- Accepts an array of HTML element tag names.
- Test example: packages/html/src/lib/tests/slateToHtml/configuration/markMap.spec.ts.
elementMap
Map Slate JSON type
values to HTML element tags.
- Default: packages/dom/src/lib/config/default.ts.
- Staightforward transform - no attributes are considered.
- Use
elementTransforms
for more control over the returned element. - Test example: packages/html/src/lib/tests/slateToHtml/configuration/elementMap.spec.ts.
markTransforms
Define transform functions for Slate JSON properties.
- Default: packages/dom/src/lib/config/default.ts.
- Overrides corresponding values in
markMap
. - Use
markTransforms
for more control over the returned element. - Test example: packages/html/src/lib/tests/slateToHtml/configuration/markTransforms.spec.ts.
elementTransforms
Map Slate JSON type
values to HTML element tags.
- Default: packages/dom/src/lib/config/default.ts.
- Overrides corresponding values in
elementMap
. - Use
elementTransforms
for more control over the returned element. - Test example: packages/html/src/lib/tests/slateToHtml/configuration/elementTransforms.spec.ts.
elementAttributeTransform
Apply attribute transformations to every node.
- For a comprehensive example transforming HTML CSS attributes, see packages/tests/src/lib/html/snapshots/htmlToSlateToHtml.spec.ts.
elementTransforms
can also be used to transform attributes, but these functions are defined per element.elementAttributeTransform
accepts a single function that applies to every element.
Formatting
Control the way resulting HTML is encoded/formatted.
- Default: packages/dom/src/lib/config/default.ts.
- Test examples: packages/html/src/lib/tests/slateToHtml/configuration/formatting.spec.ts.
encodeEntities
See cheeriojs/dom-serializer - encodeEntities.
- Default:
true
alwaysEncodeBreakingEntities
Encode &, < and > regardless of other option settings.
- Default:
false
alwaysEncodeCodeEntities
Encode entities in <pre> tags regardless of other option settings.
- Default:
true
convertLineBreakToBr
Convert \n line breaks in Slate text nodes to an HTML <br> element.
defaultTag
Render a HTML element for Slate nodes that have no type
.
- The Payload CMS configuration uses a
defaultTag
ofp
. See packages/dom/src/lib/config/payload.ts. - This is consistent with the approach taken by Payload CMS: See payloadcms/payload/blob/master/docs/fields/rich-text.mdx.