npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webiny/react-rich-text-renderer

v5.41.1

Published

[![](https://img.shields.io/npm/dw/@webiny/react-rich-text-renderer.svg)](https://www.npmjs.com/package/@webiny/react-rich-text-renderer) [![](https://img.shields.io/npm/v/@webiny/react-rich-text-renderer.svg)](https://www.npmjs.com/package/@webiny/react-

Downloads

2,269

Readme

@webiny/react-rich-text-renderer

code style: prettier PRs Welcome

A React component to render data coming from Webiny Headless CMS and Webiny Form Builder.

About

Webiny uses https://editorjs.io/ as a go to Rich Text Editor, with some additional plugins. To speed up the rendering of data for developers, we created this component with default renderers for block types that are used in Webiny by default.

Install

npm install --save @webiny/react-rich-text-renderer

Or if you prefer yarn:

yarn add @webiny/react-rich-text-renderer

Usage

Fetch your data from Headless CMS, then pass it to the component like this:

import {RichTextRenderer} from "@webiny/react-rich-text-renderer";

// Load content from Headless CMS (here we show what your content might look like).
const content = [
    {
        type: "paragraph",
        data: {
            text: "A well written paragraph of text can bring so much joy!",
            textAlign: "left",
            className: ""
        }
    }
];

// Mount the component.
<RichTextRenderer data={content}/>;

Adding custom renderers

You can override the default renderers or add new renderers for your custom block types like this:

import {RichTextRenderer, RichTextBlockRenderer} from "@webiny/react-rich-text-renderer";

const customRenderers: Record<string, RichTextBlockRenderer> = {
    // Override the default renderer for "delimiter" block
    delimiter: block => {
        return <div data-type={block.type} className={"my-custom-delimiter"}/>;
    },
    // Add a renderer for "youtube" block.
    youtube: block => {
        return (
            <iframe
                width="560"
                height="315"
                src={block.data.url}
                title={block.data.title}
                frameborder="0"
                allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
                allowfullscreen
            ></iframe>
        );
    }
};

const content = [
    // This block will use the default renderer.
    {
        type: "paragraph",
        data: {
            text: "A well written paragraph of text can bring so much joy!",
            textAlign: "left",
            className: ""
        }
    },
    // This block will use the custom "delimiter" renderer.
    {
        type: "delimiter"
    },
    // This block will use the new "youtube" renderer.
    {
        type: "youtube",
        data: {
            url: "https://www.youtube.com/embed/gOGJKHXntiU",
            title: "Webiny Overview"
        }
    }
];

// Mount the component.
<RichTextRenderer data={content} renderers={customRenderers}/>;

Sanitization configuration

We are using sanitize-html package for content sanitization.

Use configureSanitization function to set your global sanitization preference.

To provide sanitize configuration to specific component, use sanitizationConfig prop.

Please check sanitize-html configuration options on their GitHub page.

import {
    RichTextRenderer,
    configureSanitization,
} from "@webiny/react-rich-text-renderer";

const globalSanitizaionConfig = {
    allowedTags: ["b", "i", "em", "strong", "a"],
    allowedAttributes: {
        a: ["href"],
    },
    allowedIframeHostnames: ["www.youtube.com"],
};

// This is global configuration.
configureSanitization(globalSanitizaionConfig);

/*
* Set sanitization configuration options for specific component. 
* Note: Provided configuration will override your global configuration options.
* */
const sanitizationConfig = {
    // change the configuration only for this option.
    allowedIframeHostnames: ["www.webiny.com"],
};

<RichTextRenderer sanitizationConfig={sanitizationConfig}/>;