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

editorjs-blocks-react-renderer-rm

v1.3.2

Published

EditorJS blocks render to semanticly beautiful html5 tags via React.

Downloads

67

Readme

Editor.js Blocks React Renderer

Renders Editor.js blocks to semantic React HTML5 components. Unnopinated and flexible.

An unnopinated React library that renders flexible HTML5 React components from Editor.js block style data.

It follows semantic practices and without inlining styles. It's flexible enough to allow developers to include their own styles via CSS classes, which can be passed to each renderer via configuration.

This also supports server side rendering.

Usage

Install the package via npm:

npm i -S editorjs-blocks-react-renderer-rm

Import in your component:

import Blocks from 'editorjs-blocks-react-renderer-rm';

Use it with a block style data from Editor.js:

export const Article = () => <Blocks data={dataFromEditor} />;

Render a single block

Blocks are independent and you can import only a set of them and use them diretly:

import { Header } from 'editorjs-react-renderer-rm';

const dataHeader: HeaderBlockData = {
  text: "Heading 2",
  level: 2
}

export const Heading () => <Header data={dataHeader} className="text-xl" />;

Internal blocks

The package ships with the following renderers, but you can include your custom ones:

  • Code
  • Header
  • Paragraph
  • Image
  • SimpleImage
  • Embed
  • List
  • NestedList
  • Table
  • Quote
  • Delimiter
  • Raw (HTML)

Styling and optional configs

This library does not include/force any style nor inlines any styles. Before you ask, we're not supporting inline styles due to Content-Security-Policy requirements.

However, each renderer supports a set of props, including className which can be used to style each block. You just need to pass a config object to Blocks or directly to any renderer like so:

<Blocks data={dataFromEditor} config={{
  code: {
    className: "language-js"
  },
  delimiter: {
    className: "border border-2 w-16 mx-auto"
  },
  embed: {
    className: "border-0"
  },
  header: {
    className: "font-bold"
  },
  image: {
    className: "w-full max-w-screen-md",
    actionsClassNames: {
      stretched: "w-full h-80 object-cover",
      withBorder: "border border-2",
      withBackground: "p-2",
    }
  },
  list: {
    className: "list-inside"
  },
  paragraph: {
    className: "text-base text-opacity-75",
    actionsClassNames: {
      alignment: "text-{alignment}", // This is a substitution placeholder: left or center.
    }
  },
  quote: {
    className: "py-3 px-5 italic font-serif"
  },
  table: {
    className: "table-auto"
  }
}} />

The example above uses TailwindCSS classes, replacing the default ones, which you can find below:

const defaultConfigs = {
  code: {
    className: ""
  },
  delimiter: {
    className: ""
  },
  embed: {
    className: "",
    rel: "noreferer nofollower external", // Generates an <a> if not able to receive an "embed" property
    sandbox: undefined
  },
  header: {
    className: ""
  },
  image: {
    className: "",
    actionsClassNames: {
      stretched: "image-block--stretched",
      withBorder: "image-block--with-border",
      withBackground: "image-block--with-background",
    }
  },
  list: {
    className: ""
  },
  paragraph: {
    className: ""
  },
  quote: {
    className: "",
    actionsClassNames: {
      alignment: "text-align-{alignment}", // This is a substitution placeholder: left or center.
    }
  },
  table: {
    className: ""
  }
}

So, in theory, any CSS framework can work seamlessly with this library as long as you pass the correct properties.

Custom Renderers

You can provide your own custom renderers or replace the default ones by passing a renderers object to the Blocks.

const Checklist: RenderFn<{
  items: string[]
}> = ({
  data, className = ""
}) => {

  return (
    <>
      {data?.items.map((item, i) => (
        <p key={i} className={className}>
          <label>
            <input type="checkbox" /> {HTMLReactParser(item)}
          </label>
        </p>
      ))}
    </>
  )
}

export default () => <Blocks data={dataFromEditor} renderers={{
  checklist: Checklist
}} />

Security optimization

For embed block, you can pass a string of Feature-Policy directives for sandbox to optimize for security. Take into account that services such as YouTube won't work properly if you set those settings.

Inspiration