![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
storyblok-rich-text-react-renderer
Advanced tools
Renders Storyblok rich text content to React elements.
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:
import StoryblokClient from 'storyblok-js-client';
const Storyblok = new StoryblokClient({ accessToken: 'YOUR_TOKEN' });
function RichText({ document }) {
const html = Storyblok.richTextResolver.render(document);
return <div dangerouslySetInnerHTML={{ __html: html }} />;
}
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
outputs
React elements, and provides options to map any Stoyblok rich text
element to custom React components.
npm install storyblok-rich-text-react-renderer
import { render } from 'storyblok-rich-text-react-renderer';
function RichText({ document }) {
// document is the rich text object you receive from Storyblok,
// in the form { type: "doc", content: [ ... ] }
return <div>{render(document)}</div>;
}
To map rich text elements to custom React components, resolvers can be passed
via the optional second argument of the render
function:
render(document, {
markResolvers: { ... }, // inline elements
nodeResolvers: { ... }, // block elements
blokResolvers: { ... } // embedded components
});
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 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 }) => { ... }
<strong>
import { render, MARK_BOLD } from 'storyblok-rich-text-react-renderer';
render(document, {
markResolvers: {
[MARK_BOLD]: (children) => <strong>{children}</strong>
}
});
<Link>
componentimport Link from 'next/link';
import { render, MARK_LINK } from 'storyblok-rich-text-react-renderer';
render(document, {
markResolvers: {
[MARK_LINK]: (children, props) => {
const { href, target, linktype } = props;
if (linktype === 'email') {
// Email links: add `mailto:` scheme and map to <a>
return <a href={`mailto:${href}`}>{children}</a>;
}
if (href.match(/^(https?:)?\/\//)) {
// External links: map to <a>
return <a href={href} target={target}>{children}</a>;
}
// Internal links: map to <Link>
return <Link href={href}><a>{children}</a></Link>;
}
}
});
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) => { ... }
() => { ... }
() => { ... }
import MyImage from 'components/MyImage';
import { render, NODE_IMAGE } from 'storyblok-rich-text-react-renderer';
render(document, {
nodeResolvers: {
[NODE_IMAGE]: (children, props) => <MyImage {...props} />
}
});
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.
import MyComponent from 'components/MyComponent';
import { render } from 'storyblok-rich-text-react-renderer';
render(document, {
blokResolvers: {
['my_component']: (props) => <MyComponent {...props} />
}
});
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.
import { render } from 'storyblok-rich-text-react-renderer';
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 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 />
FAQs
A React renderer for Storyblok rich text content
The npm package storyblok-rich-text-react-renderer receives a total of 23,898 weekly downloads. As such, storyblok-rich-text-react-renderer popularity was classified as popular.
We found that storyblok-rich-text-react-renderer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.