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
331 stars 61 forks source link

Uncaught TypeError: Cannot read property 'map' of undefined #4

Closed chb1828 closed 4 years ago

chb1828 commented 4 years ago

how to solve this problem in html?

contents = [{"data":{"text":"내용<br>"},"type":"paragraph"},{"data":{"text":"헤더1","level":2},"type":"header"},{"data":{"style":"ordered","items":["리스트1","리스트2"]},"type":"list"}]
//contents is string
const edjsParser = edjsHTML();
let html = edjsParser.parse(contents);
pavittarx commented 4 years ago

The reason you have encountered this error, is because the parser is not able to initialise with data you provided. The possible solutions are

Solution 1: Pass all of the editorjs data as it comes. Do not modify it.

Solution 2: Pass it as blocks variable inside an object.

blocks = [{"data":{"text":"내용<br>"},"type":"paragraph"},{"data":{"text":"헤더1","level":2},"type":"header"},{"data":{"style":"ordered","items":["리스트1","리스트2"]},"type":"list"}]
//contents is string
const edjsParser = edjsHTML();
let html = edjsParser.parse({blocks});

Solution 3: Pass it as blocks key of the object.

contents = [{"data":{"text":"내용<br>"},"type":"paragraph"},{"data":{"text":"헤더1","level":2},"type":"header"},{"data":{"style":"ordered","items":["리스트1","리스트2"]},"type":"list"}]
//contents is string
const edjsParser = edjsHTML();
let html = edjsParser.parse({blocks: contents});

Solution 4: Use the edjsParser.parseBlock() to parse one block at a time.

const block = {"data":{"text":"내용<br>"},"type":"paragraph"};
const html = edjsParser.parseBlock(block);

editorjs keeps all its data under the blocks property. So, you must pass it under the same name. You can use any of the provided approaches above. If you encounter any other issues, let me know.

chb1828 commented 4 years ago

Thanks to your answer I solved it.