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

@mobtakr/editorjs-parser

v0.2.1

Published

Editor JS Parser and Renderer for React with server-side SimpleSchema validation

Downloads

199

Readme

Editor.js Parser and Renderer for React.js or Next.js

The package lets you render the content of Editor.js and lets you extend the functionality easily.

Thanks, @klaucode for the enhancements and the additions of new blocks.

Install the package

Run

npm install @mobtakr/editorjs-parser

Editor.js Output

Editor.js is a great block-styled editor. It lets you embed a text editor in your application.

The output of Editor.js is a JSON Object like below:

{
    "time": 1664376861686,
    "blocks": [
        {
            "id": "9xynmGdBTA",
            "type": "paragraph",
            "data": {
                "text": "I am a text generated from Editor.js"
            }
        },
        {
            "id": "IF6QCbnQQz",
            "type": "paragraph",
            "data": {
                "text": "I am a text generated from Editor.js"
            }
        },
        {
            "id": "l4frEHcq2o",
            "type": "list",
            "data": {
                "style": "ordered",
                "items": [
                    "I am item one,",
                    "and I am two",
                    "three ",
                    "four"
                ]
            }
        }
    ],
    "version": "2.23.2"
}

How to use Editor.js parser?

Below is an example of how you can integrate Editor.js Parser into your React.js or Next.js application.

First install the npm package

npm install @mobtakr/editorjs-parser

Then create a component to render the content

import { EditorParser, EditorRenderer } from "@mobtakr/editorjs-parser";
import styles from "./PostContent.module.css";

const PostContent = (props: { content: string }) => {
  const content = JSON.parse(props.content);
  const parser = new EditorParser(content.blocks);

  const parsedBlocks = parser.parse();
  return (
    <>
      <EditorRenderer parsedBlocks={parsedBlocks} styles={styles} />
    </>
  );
};

export default PostContent;

In the example above you first, parse the JSON object then, create an instance of EditorParser and pass it content.blocks.

Now, you can get the parsed blocks by calling the parse method.

Finally, pass a parsedBlocks prop and a styles object to <EditorRenderer /> component.

Styling and the Style Object

Each Editor.js block has a type and an id.

{
    "id": "9xynmGdBTA",
    "type": "paragraph",
    "data": {
        "text": "I am a text generated from Editor.js"
    }
}

The EditorRenderer component expects a style object that contains styles for each type.

For type paragraph you can pass a style object like this

.paragraph {
    font-size: 1rem;
    margin: 10px 0;
}

Besides that, each block has another class which is block. So you can add some shared styles to your blocks.

.block {
  position: relative;
  margin-block: 10px;
}

Supported blocks

Package currently supports following blocks:editorjs/checklist (official) ✅ editorjs/code (official) ✅ editorjs/header (official) ✅ editorjs/image (official) ✅ editorjs/list (official) ✅ editorjs/paragraph (official) ✅ editorjs/quote (official) ✅ editorjs/table (official) ✅ editorjs-youtube-embed (unofficial)

Add support for additional blocks

You can extend the functionality of this package by adding new blocks.

The registerBlock method of the EditorParser class enables you to parse more blocks. It expects two arguments

  1. The type or the block.
  2. A function that takes the block as an argument and returns a React component.

Example

This is a React component that takes a block as an argument and returns a paragraph.

const TextBlock = (props: { block: any }) => {
 const block = props.block;
 const text = block.data?.text;
 return <p>{text}</p>;
};

export default TextBlock;

Now, you can create a function that takes a block as an argument and returns that component. TextBlock

export const paragraphMapFunc = (block: any) => <TextBlock block={block} />;

Finally, you can add this block using the registerBlock method.

const parser = new EditorParser(content.blocks);
parser.registerBlock('paragraph', paragraphMapFunc)