Editor.js HTML Parser
editorjs-html is a robust, lightweight, and highly customizable utility designed to transform Editor.js's clean JSON output into standard HTML. The current stable version is 4.0.5, which represents a complete rewrite focusing on improved modularity, readability, and modern dependency usage. This library offers flexible integration across various JavaScript environments including plain JavaScript/TypeScript, React, Angular, and Vue. It supports all common Editor.js blocks out-of-the-box and provides a clear mechanism for extending its capabilities to parse custom Editor.js blocks. A key differentiator is its emphasis on strict parsing options and type safety, leveraging Editor.js's own type definitions for development ease. While a specific release cadence isn't explicitly stated, the rapid iteration to v4 suggests active maintenance and responsiveness to the Editor.js ecosystem.
Common errors
-
TypeError: edjsParser.parseStrict is not a function
cause Attempting to use the `parseStrict` method, which was removed in v4.fixUse the `parse` method with the `strict: true` option during parser initialization: `const edjsParser = edjsHTML({}, { strict: true }); const html = edjsParser.parse(data);` -
ReferenceError: edjsHTML is not defined (in browser, from CDN)
cause The global `edjsHTML` variable is available if using the browser-specific CDN build. If you're using the universal build or the npm package, it's typically an ESM or CJS module.fixIf using a browser-only CDN build, ensure the script is loaded before your code. If using the npm package, `import edjsHTML from 'editorjs-html'` (ESM) or `const edjsHTML = require('editorjs-html')` (CJS). -
Error: Missing parser function for block type 'myCustomBlock'
cause In strict mode, the parser encountered a block type ('myCustomBlock') for which no corresponding custom parser function was provided.fixProvide a parser function for 'myCustomBlock' when initializing `edjsHTML`. Example: `const plugins = { myCustomBlock: (block) => `<div class="custom">${block.data.text}</div>` }; const edjsParser = edjsHTML(plugins, { strict: true });` -
TypeError: Cannot read properties of undefined (reading 'blocks')
cause The `parse` method was called with `undefined` or a non-object value, or the object passed did not have a `blocks` property, which is expected for Editor.js data.fixEnsure the argument passed to `edjsParser.parse()` is a valid Editor.js data object with a `blocks` array, e.g., `{ blocks: [...] }`.
Warnings
- breaking Version 4.0.0 introduced a complete rewrite of the library. While API changes are stated as 'minimal', users upgrading from v3 or earlier should review their code, especially around strict mode configuration and custom block parsers.
- breaking The `parseStrict()` and `validate()` functions have been removed in version 4.0.0. Strict mode functionality is now controlled via options during parser initialization.
- gotcha When a parser function for a specific block type is not available, the default behavior (non-strict mode) is to log a console error. The block will not be parsed into HTML.
- gotcha In strict mode (`strict: true`), if a parser function for an encountered block type is missing, the `parse()` method will throw an `Error` instance instead of returning HTML. This requires explicit error handling.
- gotcha The `edjsHTML()` function is a factory that returns a new parser instance. Reusing the same parser instance across different configurations (e.g., changing strict mode or adding new custom parsers) is not supported without re-instantiating.
Install
-
npm install editorjs-html -
yarn add editorjs-html -
pnpm add editorjs-html
Imports
- edjsHTML
import { edjsHTML } from 'editorjs-html';import edjsHTML from 'editorjs-html';
- edjsHTML
const { edjsHTML } = require('editorjs-html');const edjsHTML = require('editorjs-html'); - ParserOptions
import type { ParserOptions } from 'editorjs-html';
Quickstart
import edjsHTML from 'editorjs-html';
// Example Editor.js data
const editorjsData = {
blocks: [
{
id: "qweasd123",
type: "header",
data: {
text: "Hello Editor.js HTML!",
level: 2
}
},
{
id: "zxcvbnm456",
type: "paragraph",
data: {
text: "This is an example of parsing Editor.js clean data into HTML using editorjs-html."
}
},
{
id: "lkjhgfd789",
type: "list",
data: {
style: "unordered",
items: [
"First item",
"Second item",
"Third item"
]
}
}
]
};
const edjsParser = edjsHTML();
const htmlOutput = edjsParser.parse(editorjsData);
console.log(htmlOutput.join('\n'));
// Expected output: <h2>Hello Editor.js HTML!</h2><p>This is an example of parsing Editor.js clean data into HTML using editorjs-html.</p><ul><li>First item</li><li>Second item</li><li>Third item</li></ul>