Renders Storyblok rich text content to React elements.
Motivation
Storyblok provides a renderer for its rich text field type via their
storyblok-js-client package. This renderer outputs HTML markup, which can be used in React via
the dangerouslySetInnerHTML property:
Apart from being a bit awkward (dangerouslySetInnerHTML is, as the name implies, dangerous), this is problematic
because it is not possible to map rich text elements to React components, e.g.:
Embedded Storyblok components
Links that you might want to pass through your app's router
Instead of HTML markup, storyblok-rich-text-react-renderer-ts outputs React elements, and provides options to map any
Stoyblok rich text element to custom React components.
Installation
npm install storyblok-rich-text-react-renderer-ts
Usage
import{render}from'storyblok-rich-text-react-renderer-ts';functionRichText({document}){// document is the rich text object you receive from Storyblok,// in the form { type: "doc", content: [ ... ] }return<div>{render(document)}</div>;}
Advanced usage
To map rich text elements to custom React components, resolvers can be passed via the optional second argument of
the render function:
Sensible default resolvers for marks and nodes are provided out of the box. You only have to provide custom
ones if you want to override the default behavior.
If you use embedded Storyblok components, you have to provide
blok resolvers to map them to your React components though, otherwise they are ignored.
Mark resolvers
Mark resolvers are used to map inline elements.
Use the markResolvers option to add mark resolvers.
Supported element types and their resolver function signatures are:
Blok resolvers are used to map embedded Storyblok components.
Use the blokResolvers option to add blok resolvers. Keys are the Storyblok component's "technical" name. The function
signature is always (props) => { ... }, where props is an object that contains all the component's fields, as well
as its _uid and _editable values.
Example: Map blok elements to custom React components
Use the defaultBlokResolver option to add a default blok resolver. The function signature
is (name, props) => { ... }, where name is the Storyblok component's "technical" name and props is an object that
contains all the component's fields, as well as its _uid and _editable values.
Example:
import{render}from'storyblok-rich-text-react-renderer-ts';render(document,{defaultBlokResolver: (name,props)=>(<div><code>Missing blok resolver for blok type "{name}".</code><pre><code>{JSON.stringify(props,undefined,2)}</code></pre></div>)});
Default string resolver
Storyblok might return a simple string instead of a document object for rich text fields with trivial content. By default, the render function returns this string as-is. Use the defaultStringResolver option to customize this behavior. The function signature is (str) => { ... }.
dohomi/storyblok-rich-text-react-renderer-ts
Storyblok Rich Text Renderer for React - TypeScript
This project is a TypeScript fork from storyblok-rich-text-react-renderer.
Renders Storyblok rich text content to React elements.
Motivation
Storyblok provides a renderer for its rich text field type via their
storyblok-js-client
package. This renderer outputs HTML markup, which can be used in React via thedangerouslySetInnerHTML
property:Apart from being a bit awkward (
dangerouslySetInnerHTML
is, as the name implies, dangerous), this is problematic because it is not possible to map rich text elements to React components, e.g.:Instead of HTML markup,
storyblok-rich-text-react-renderer-ts
outputs React elements, and provides options to map any Stoyblok rich text element to custom React components.Installation
Usage
Advanced usage
To map rich text elements to custom React components, resolvers can be passed via the optional second argument of the
render
function:Sensible default resolvers for marks and nodes are provided out of the box. You only have to provide custom ones if you want to override the default behavior.
If you use embedded Storyblok components, you have to provide blok resolvers to map them to your React components though, otherwise they are ignored.
Mark resolvers
Mark resolvers are used to map inline elements.
Use the
markResolvers
option to add mark resolvers.Supported element types and their resolver function signatures are:
(children) => { ... }
(children) => { ... }
(children) => { ... }
(children) => { ... }
(children) => { ... }
(children, { class }) => { ... }
(children, { href, target, linktype }) => { ... }
Example: Map bold elements to
<strong>
Example: Map link elements to Next.js'
<Link>
componentNode resolvers
Node resolvers are used to map block elements.
Use the
nodeResolvers
option to add node resolvers.Supported element types and their resolver function signatures are:
(children, { level }) => { ... }
(children, { class }) => { ... }
(children, { src, alt, title }) => { ... }
(children) => { ... }
(children) => { ... }
(children) => { ... }
(children) => { ... }
(children) => { ... }
() => { ... }
() => { ... }
Example: Map image elements to custom React components
Blok resolvers
Blok resolvers are used to map embedded Storyblok components.
Use the
blokResolvers
option to add blok resolvers. Keys are the Storyblok component's "technical" name. The function signature is always(props) => { ... }
, whereprops
is an object that contains all the component's fields, as well as its_uid
and_editable
values.Example: Map blok elements to custom React components
Default blok resolver
Use the
defaultBlokResolver
option to add a default blok resolver. The function signature is(name, props) => { ... }
, wherename
is the Storyblok component's "technical" name andprops
is an object that contains all the component's fields, as well as its_uid
and_editable
values.Example:
Default string resolver
Storyblok might return a simple string instead of a document object for rich text fields with trivial content. By default, the render function returns this string as-is. Use the
defaultStringResolver
option to customize this behavior. The function signature is(str) => { ... }
.Example:
Defaults
Default mark resolvers:
<b> ... </b>
<i> ... </i>
<s> ... </s>
<u> ... </u>
<code> ... </code>
<span className> ... </span>
<a href target> ... </a>
Default node resolvers:
<h1> ... </h1>
to<h6> ... </h6>
<pre><code className> ... </code></pre>
<img src alt title />
<p> ... </p>
<blockquote> ... </blockquote>
<ol> ... </ol>
<ul> ... </ul>
<li> ... </li>
<hr />
<br />
Changelog
Credits
All credits go to @claus who initialized the project in JavaScript