https://github.com/dohomi/storyblok-rich-text-react-renderer-ts
A React renderer for Storyblok rich text content based on TS
https://github.com/dohomi/storyblok-rich-text-react-renderer-ts
react rich-text storyblok typescript
Last synced: 6 months ago
JSON representation
A React renderer for Storyblok rich text content based on TS
- Host: GitHub
- URL: https://github.com/dohomi/storyblok-rich-text-react-renderer-ts
- Owner: dohomi
- License: mit
- Created: 2020-12-14T06:56:18.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-07-07T03:20:55.000Z (over 3 years ago)
- Last Synced: 2025-03-30T23:32:45.698Z (6 months ago)
- Topics: react, rich-text, storyblok, typescript
- Language: TypeScript
- Homepage:
- Size: 288 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Storyblok Rich Text Renderer for React - TypeScript
[](https://www.npmjs.com/package/storyblok-rich-text-react-renderer-ts)
[](https://github.com/claus/storyblok-rich-text-react-renderer/blob/master/LICENSE)This project is a TypeScript fork from [storyblok-rich-text-react-renderer](https://github.com/claus/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
the `dangerouslySetInnerHTML` property:```js
import StoryblokClient from 'storyblok-js-client-ts';const Storyblok = new StoryblokClient({accessToken: 'YOUR_TOKEN'});
function RichText ({document}) {
;
const html = Storyblok.richTextResolver.render(document);
return
}
```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.:1. Embedded Storyblok components
2. Links that you might want to pass through your app's routerInstead 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
```js
import {render} from 'storyblok-rich-text-react-renderer-ts';function RichText ({document}) {
// document is the rich text object you receive from Storyblok,
// in the form { type: "doc", content: [ ... ] }
return{render(document)};
}
```## Advanced usage
To map rich text elements to custom React components, resolvers can be passed via the optional second argument of
the `render` function:```js
render(document, {
markResolvers: {...}, // inline elements
nodeResolvers: {...}, // block elements
blokResolvers: {...} // embedded components
});
```Sensible [default resolvers](#defaults) 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](#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:
- MARK_BOLD — `(children) => { ... }`
- MARK_ITALIC — `(children) => { ... }`
- MARK_STRIKE — `(children) => { ... }`
- MARK_UNDERLINE — `(children) => { ... }`
- MARK_CODE — `(children) => { ... }`
- MARK_STYLED — `(children, { class }) => { ... }`
- MARK_LINK — `(children, { href, target, linktype }) => { ... }`#### Example: Map bold elements to ``
```ts
import { render } from 'storyblok-rich-text-react-renderer-ts';render(document, {
markResolvers: {
bold: (children) => {children}
}
});
```#### Example: Map link elements to Next.js' `` component
```js
import Link from 'next/link';
import { render } from 'storyblok-rich-text-react-renderer-ts';render(document, {
markResolvers: {
link: (children, props) => {
const {href, target, linktype} = props;
if (linktype === 'email') {
// Email links: add `mailto:` scheme and map to
return {children};
}
if (href.match(/^(https?:)?\/\//)) {
// External links: map to
return {children};
}
// Internal links: map to
return
{children}
;
}
}
});
```### Node 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:
- NODE_HEADING — `(children, { level }) => { ... }`
- NODE_CODEBLOCK — `(children, { class }) => { ... }`
- NODE_IMAGE — `(children, { src, alt, title }) => { ... }`
- NODE_PARAGRAPH — `(children) => { ... }`
- NODE_QUOTE — `(children) => { ... }`
- NODE_OL — `(children) => { ... }`
- NODE_UL — `(children) => { ... }`
- NODE_LI — `(children) => { ... }`
- NODE_HR — `() => { ... }`
- NODE_BR — `() => { ... }`#### Example: Map image elements to custom React components
```js
import MyImage from 'components/MyImage';
import { render } from 'storyblok-rich-text-react-renderer-ts';render(document, {
nodeResolvers: {
image: (children, props) =>
}
});
```### 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) => { ... }`, 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
```js
import MyComponent from 'components/MyComponent';
import { render } from 'storyblok-rich-text-react-renderer-ts';render(document, {
blokResolvers: {
['my_component']: (props) =>
}
});
```### Default blok resolver
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:
```js
import { render } from 'storyblok-rich-text-react-renderer-ts';render(document, {
defaultBlokResolver: (name, props) => (
Missing blok resolver for blok type "{name}".
{JSON.stringify(props, undefined, 2)}
)
});
```### 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:
```js
import { render } from 'storyblok-rich-text-react-renderer-ts';render(document, {
defaultStringResolver: (str) => ({str}
)
});
```## Defaults
Default mark resolvers:
- MARK_BOLD — ` ... `
- MARK_ITALIC — ` ... `
- MARK_STRIKE — ` ... `
- MARK_UNDERLINE — ` ... `
- MARK_CODE — `...
`
- MARK_STYLED — ` ... `
- MARK_LINK — ` ... `Default node resolvers:
- NODE_HEADING — `
...
` to `...
`
- NODE_CODEBLOCK — ``...
- NODE_IMAGE — ``
- NODE_PARAGRAPH — `...
`
- NODE_QUOTE — `...`
- NODE_OL — `...
`
- NODE_UL — `
- ...
- NODE_LI — `
- NODE_HR — `
`
- NODE_BR — `
`
## Changelog
- 1.0.0 — Initial release
- 1.1.0 — Add MARK_STYLED mark resolver
- 1.2.0 — Add defaultBlockResolver
- 2.0.0 — Rename defaultBlockResolver (typo) to defaultBlokResolver (⚠️ Breaking change ⚠️)
- 2.1.0 — Allow React 17 as peer
- 3.0.0 - Initial release of TypeScript version of this renderer
- 3.0.1 - Improved typings
- 3.0.2 - Bugfix: change render options to optional
- 3.1.0 - Add defaultStringResolver, Allow block elements as children of inline elements (in particular linked images)
- 3.1.1 - Fixed build script and update dependencies
- 3.2.0 - Add React 18 support. Improve typings. Add anchor to link attributes.
### Credits
All credits go to [@claus](https://github.com/claus) who initialized the project in [JavaScript](https://github.com/claus/storyblok-rich-text-react-renderer)