SlateToReact
The <SlateToReact> configuration is a flat object: the same keys as slateToHtml (markMap, elementMap, formatting options, and so on) plus elementTransforms for React output (each function returns a React node, for example JSX or React.createElement).
Default
By default, SlateToReact incorporates transformation rules based on the example in Deserializing | Serializing | Slate.
Output
Heading 1
Paragraph 1
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/react/src/lib/config/payload.tsx for an example of how to extend the default configuration; or
- copy packages/react/src/lib/config/default.tsx (source) and rewrite it as appropriate.
Payload CMS
If you are using Slate Rich Text in Payload CMS, a dedicated configuration file is available. See <SlateToReact>: Payload CMS configuration.
Options
markMap
Map Slate JSON properties to HTML formatting element tags.
- Default: packages/react/src/lib/config/default.tsx.
- A Slate JSON node may have multiple attributes.
- Accepts an array of HTML element tag names.
- Configure these on
config.markMap. - See
markMap|slateToHtml. The same keys apply as inslateToHtml. - Test example: packages/html/src/lib/tests/slateToHtml/configuration/markMap.spec.ts.
Output
Subscript and bold text
elementMap
Map Slate JSON type values to HTML element tags.
- Default: packages/react/src/lib/config/default.tsx.
- Straightforward transform - no attributes are considered.
- Use
elementTransformsfor more control over the returned element. - Configure mapping on
config.elementMap. - Test example: packages/html/src/lib/tests/slateToHtml/configuration/elementMap.spec.ts.
Output
Heading 1
Heading 1 (identified with our custom type)
markTransforms
Define transform functions for Slate JSON properties.
- Default: packages/react/src/lib/config/default.tsx.
- Overrides corresponding values in
markMap. - Use
markTransformsfor more control over the returned element. - Test example: packages/html/src/lib/tests/slateToHtml/configuration/markTransforms.spec.ts.
Custom DOM markTransforms apply to slateToHtml. The <SlateToReact> implementation routes text marks through the DOM pipeline using markMap only; use slateToHtml if you need the full markTransforms behavior shown below.
elementTransforms
For React, define transforms on config.elementTransforms. Each function returns a React node (for example JSX or React.createElement).
- Default: packages/react/src/lib/config/default.tsx.
- Overrides corresponding values in
elementMap. - Test example: packages/html/src/lib/tests/slateToHtml/configuration/elementTransforms.spec.ts (HTML serializer; the same shapes apply on the DOM side).
Output
Paragraph
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.
elementTransformscan also be used to transform attributes, but these functions are defined per element.elementAttributeTransformaccepts a single function that applies to every element.- Set this on
config.elementAttributeTransform.
Formatting
Control the way resulting markup is encoded/formatted.
- Default: packages/react/src/lib/config/default.tsx.
- These options live on
configat the top level (same keys asslateToHtml). - 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
defaultTagofp. See packages/react/src/lib/config/payload.tsx. - This is consistent with the approach taken by Payload CMS: See payloadcms/payload/blob/master/docs/fields/rich-text.mdx.