All Projects → claus → storyblok-rich-text-react-renderer

claus / storyblok-rich-text-react-renderer

Licence: MIT license
A React renderer for Storyblok rich text content

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to storyblok-rich-text-react-renderer

quasar-tiptap
A modern WYSIWYG rich-text editor built on top of tiptap and Quasar for Vue.js.
Stars: ✭ 254 (+669.7%)
Mutual labels:  rich-text
nuxtwebsite
A simple Nuxt.js setup to create websites with blog feature using Storyblok as CMS and Netlify to deploy it.
Stars: ✭ 29 (-12.12%)
Mutual labels:  storyblok
full-static-nuxt-storyblok
A boilerplate project designed to demonstrate the benefits of using JAMStack and the easy configuration of a project with NuxtJS and Storyblok <3
Stars: ✭ 26 (-21.21%)
Mutual labels:  storyblok
RichEditorView
Android 基于WebView的富文本编辑器 - 仿简书编辑器
Stars: ✭ 77 (+133.33%)
Mutual labels:  rich-text
we-rich
HTML转微信富文本节点, we just need rich, no text.
Stars: ✭ 36 (+9.09%)
Mutual labels:  rich-text
Storyblok-Android-SDK
Storyblok MP SDK available here: https://github.com/mikepenz/storyblok-mp-SDK
Stars: ✭ 13 (-60.61%)
Mutual labels:  storyblok
artibox
Artibox - A complete rich text editor based on Slate framework.
Stars: ✭ 21 (-36.36%)
Mutual labels:  rich-text
storyblok-vue
Vue.js plugin for Storyblok
Stars: ✭ 42 (+27.27%)
Mutual labels:  storyblok
laravel-storyblok
Make Laravel and Storyblok work together beautifully.
Stars: ✭ 45 (+36.36%)
Mutual labels:  storyblok
storyblok-mp-SDK
Storyblok Kotlin Multiplatform SDK (Android, JVM, JS, iOS, ...)
Stars: ✭ 16 (-51.52%)
Mutual labels:  storyblok
super rich text
The easiest way to style custom text snippets in flutter
Stars: ✭ 14 (-57.58%)
Mutual labels:  rich-text
v-editor
📝Write md or rich text easily
Stars: ✭ 22 (-33.33%)
Mutual labels:  rich-text
mobility-actiontext
Translate Rails Action Text rich text with Mobility.
Stars: ✭ 27 (-18.18%)
Mutual labels:  rich-text
draftjs exporter
Library to convert Draft.js ContentState to HTML
Stars: ✭ 77 (+133.33%)
Mutual labels:  rich-text
rich-text
A set of companion packages for GraphCMS's Rich Text Field
Stars: ✭ 62 (+87.88%)
Mutual labels:  rich-text
RSS-to-Telegram-Bot
A Telegram RSS bot that cares about your reading experience
Stars: ✭ 482 (+1360.61%)
Mutual labels:  rich-text
unicode-formatter
Convert portions of text to fancy text using unicode fonts for use on Twitter and other sites that don't support rich text
Stars: ✭ 31 (-6.06%)
Mutual labels:  rich-text
vuejs-boilerplate
Storyblok - JavaScript - VueJS Boilerplate
Stars: ✭ 22 (-33.33%)
Mutual labels:  storyblok
Taro-ParserRichText
适用于 Taro 的小程序富文本组件
Stars: ✭ 32 (-3.03%)
Mutual labels:  rich-text
textbus
Textbus 是一个组件化的、数据驱动的富文本框架,支持在线协同编辑,同时也可以作为一个开箱即用的富文本编辑器,拥有非常好的扩展性和可定制性,是构建复杂富文本的不二之选!
Stars: ✭ 642 (+1845.45%)
Mutual labels:  rich-text

Storyblok Rich Text Renderer for React

npm GitHub

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:

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.:

  1. Embedded Storyblok components
  2. Links that you might want to pass through your app's router

Instead of HTML markup, storyblok-rich-text-react-renderer outputs React elements, and provides options to map any Storyblok rich text element to custom React components.

Installation

npm install storyblok-rich-text-react-renderer

Usage

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>;
}

Advanced usage

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
    defaultBlokResolver: (name, props) => ( ... ),
    defaultStringResolver: (str) => ( ... ),
    textResolver: (text) => ( ... ),
});

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. You can also provide a default blok resolver if you need a catch-all solution.

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 <strong>

import { render, MARK_BOLD } from 'storyblok-rich-text-react-renderer';

render(document, {
    markResolvers: {
        [MARK_BOLD]: (children) => <strong>{children}</strong>
    }
});

Example: Map link elements to Next.js' <Link> component

import 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

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

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

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

import MyComponent from 'components/MyComponent';
import { render } from 'storyblok-rich-text-react-renderer';

render(document, {
    blokResolvers: {
        ['my_component']: (props) => <MyComponent {...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:

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 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:

import { render } from 'storyblok-rich-text-react-renderer';

render(document, {
    defaultStringResolver: (str) => <p>{str}</p>
});

Text resolver

Use the textResolver option to add a resolver for plain text nodes. The function signature is (text) => { ... }.

Example:

import { render } from 'storyblok-rich-text-react-renderer';
import entities from 'entities';

render(document, {
    textResolver: (text) => entities.decodeHTML(text)
});

Defaults

Default mark resolvers:

  • MARK_BOLD — <b> ... </b>
  • MARK_ITALIC — <i> ... </i>
  • MARK_STRIKE — <s> ... </s>
  • MARK_UNDERLINE — <u> ... </u>
  • MARK_CODE — <code> ... </code>
  • MARK_STYLED — <span className> ... </span>
  • MARK_LINK — <a href target> ... </a>

Default node resolvers:

  • NODE_HEADING — <h1> ... </h1> to <h6> ... </h6>
  • NODE_CODEBLOCK — <pre><code className> ... </code></pre>
  • NODE_IMAGE — <img src alt title />
  • NODE_PARAGRAPH — <p> ... </p>
  • NODE_QUOTE — <blockquote> ... </blockquote>
  • NODE_OL — <ol> ... </ol>
  • NODE_UL — <ul> ... </ul>
  • NODE_LI — <li> ... </li>
  • NODE_HR — <hr />
  • NODE_BR — <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
  • 2.1.1 — Allow block elements as children of inline elements (in particular linked images)
  • 2.2.0 — Bugfix: Code was still referring to defaultBlockResolver (see 2.0.0)
  • 2.3.0 — Add defaultStringResolver, allow plain string as input
  • 2.4.0 — Add TypeScript type definitions (index.d.ts)
  • 2.5.0 — Add textResolver
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].