pavittarx / editorjs-html

A javascript library to parse editorjs clean data to html. It is adaptable and usable in all kind of projects.
https://runkit.com/pavittarx/editorjs-html-example
MIT License
328 stars 61 forks source link

html to blocks #34

Closed jadsongmatos closed 2 years ago

jadsongmatos commented 2 years ago

I needed a way to convert html to js editor blocks, one way around this was to add an individual element to the end of the html containing json with blocks.

const cheerio = require('cheerio');
const edjsHTML = require('editorjs-html');
const edjsParser = edjsHTML();

const blockEx = {"time":1643918931382,"blocks":[{"id":"XDpiezNn1n","type":"header","data":{"text":"Editor.js","level":2}},{"id":"gyoo5e5NUG","type":"paragraph","data":{"text":"Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."}},{"id":"qlVhYEvnLJ","type":"header","data":{"text":"Key features","level":3}},{"id":"_10RHNmKt_","type":"list","data":{"style":"unordered","items":["It is a block-styled editor","It returns clean data output in JSON","Designed to be extendable and pluggable with a simple API"]}},{"id":"A1N4nVbAfM","type":"header","data":{"text":"What does it mean «block-styled editor»","level":3}},{"id":"jqorGxMt2-","type":"paragraph","data":{"text":"Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class=\"cdx-marker\">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor's Core."}},{"id":"DYXx6OgqUP","type":"paragraph","data":{"text":"There are dozens of <a href=\"https://github.com/editor-js\">ready-to-use Blocks</a> and the <a href=\"https://editorjs.io/creating-a-block-tool\">simple API</a> for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games."}},{"id":"sKaYVkjhDZ","type":"header","data":{"text":"What does it mean clean data output","level":3}},{"id":"KukjVMkYxN","type":"paragraph","data":{"text":"Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below"}},{"id":"frBgGWJL3h","type":"paragraph","data":{"text":"Given data can be used as you want: render with HTML for <code class=\"inline-code\">Web clients</code>, render natively for <code class=\"inline-code\">mobile apps</code>, create markup for <code class=\"inline-code\">Facebook Instant Articles</code> or <code class=\"inline-code\">Google AMP</code>, generate an <code class=\"inline-code\">audio version</code> and so on."}},{"id":"XVkVUZabJJ","type":"paragraph","data":{"text":"Clean data is useful to sanitize, validate and process on the backend."}},{"id":"fnQPCvWXt5","type":"delimiter","data":{}},{"id":"R5WOM2LU6E","type":"paragraph","data":{"text":"We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏"}},{"id":"sClX9m8AH6","type":"image","data":{"file":{"url":"https://codex.so/public/app/img/external/codex2x.png"},"caption":"","withBorder":false,"stretched":false,"withBackground":false}}],"version":"2.22.2"}

const html = edjsParser.parse(blockEx);

let htmlBlock = html.join("")
htmlBlock = htmlBlock + `<p hidden id="blocks">${JSON.stringify(blockEx)}</p>`
//console.log(htmlBlock);

const $ = cheerio.load(htmlBlock);
console.log(JSON.parse($('#blocks').text()));
pavittarx commented 2 years ago

@jadsongmatos

If you already have the blocks, it would be much easier to maintain a copy of the same. And, use editorjs-html to convert them only when you need to render blocks on a webpage.

However, if you have raw HTML that you need to convert to editorjs styled blocks. You'll first need to parse html by using some DOM library such as jsdom, traverse through the document nodes, and write suitable parsers for converting html elements to different JSON blocks.