editor.js icon indicating copy to clipboard operation
editor.js copied to clipboard

Is there any library convert html to block of editorjs, or convert block to html

Open qqqays opened this issue 6 years ago • 14 comments

i want to convert html to block (json structure of editorjs )

qqqays avatar Jul 14 '20 08:07 qqqays

Maybe I am wrong, but I think if you start editorjs with html as initial payload it will convert for blocks for you. Block to html you can find samples of code online.

hiitsgabe avatar Jul 24 '20 17:07 hiitsgabe

@gabrielmoterani Thank you for your reply. I have gotten some html pages with python spider from public website. Now I want to parse them to blocks of editorjs in backend, and then save these blocks to database. I have found a way to convert blocks to html。

qqqays avatar Jul 27 '20 03:07 qqqays

@qqqays Yeah, I need to convert HTML for EditorJs blocks too... What I am doing is using a nodejs lib to convert HTML to json and then iterating each item of this json, converting to blocks. But I dont think this is such a good ideia. HTML is very nested... I think Editorjs has something inside that do this conversion, since you can paste html in Editor and he will convert to Editorjs Blocks.

It would be better if EditorJs provide us that api.

If anyone knows a better way to do that, please share with us :)

hiitsgabe avatar Jul 27 '20 18:07 hiitsgabe

@gabrielmoterani Hello, bro! How is the work of parsing html into editor.js blocks? I did this with jsdom that ran in nodejs,I created a entity of editorjs in jsdom, then share this entity in jsdom and nodejs env. The entity as a factory converting html to blocks, then return the blocks.

qqqays avatar Aug 04 '20 10:08 qqqays

Well I am still struggling to make a good solution for my application. @qqqays can u share more details of your implementation? Can u share the code that converts jsdom to blocks?

I tried to use a lib that named Hymalaya that converts html to JSON, but HTML object became so nested, thats impossible to iterate over it.

Does anyone nows how Editorjs convert pasted content to blocks?

hiitsgabe avatar Sep 16 '20 13:09 hiitsgabe

@gabrielmoterani I put the project to https://github.com/qqqays/server4parseEditorJs. You can try it ; )

qqqays avatar Dec 09 '20 10:12 qqqays

How did you do it? Please show me

proflinksgh avatar May 24 '22 06:05 proflinksgh

@proflinksgh I was able to take a clue from @qqqays's library and I used the following code block to convert an HTML string to EditorJS blocks:

const editor = new EditorJS({
  ...
  onReady: async () => {
    if (dataIsHtml) {
      await editor.blocks.renderFromHTML(data);
    }
  },
  ...
});

Alchez avatar Jul 15 '22 11:07 Alchez

@Alchez Hello, I came across your reply as I am facing the same issue, did you change anything else except adding above code to the editorjs instance? Where can you get the outputted html string from?Also, if possible can I have a look at the code file?

sushant-78 avatar Aug 04 '22 08:08 sushant-78

Hey @sushant-78, unfortunately the code is private on Github so I can't share it.

We were receiving an HTML string from our backend server that we wanted to output as EditorJS blocks, that was our source. By just adding that onReady function, we were able to convert the string to blocks.

Hope that helps.

Alchez avatar Aug 04 '22 09:08 Alchez

@Alchez Thanks, for quick reply, didn't need to try it out, worked out a solution,needed to migrate data, so wrote patch script for it

sushant-tce avatar Aug 05 '22 10:08 sushant-tce

@qqqays You are the savior. The solution works well with [email protected] but not the latest. Yet, you are the legend

<script src="https://cdn.jsdelivr.net/npm/@editorjs/[email protected]/dist/editor.min.js"></script>

ranjancse26 avatar Nov 18 '22 14:11 ranjancse26

hi team, is there any defined solution r=to render html to editorjs. i have tried above suggestion but i failed.

GhostEarz avatar Jan 24 '25 13:01 GhostEarz

But it give [object HTMLPreElement] for pre tag, how to solve it? Thanks.

cactis avatar Jan 26 '25 04:01 cactis