This is my code for converting editorjs json to html tag.
// store posts
const blogs = [{
"time": 1641871834196,
"blocks": [{
"id": "nwATYHAhq-",
"type": "paragraph",
"data": {
"text": "I wonder "
}
}],
"version": "2.22.2"
},
{
"time": 1641871849493,
"blocks": [{
"id": "nwATYHAhq-",
"type": "paragraph",
"data": {
"text": "what is wrong"
}
}, {
"id": "qx4OhycRav",
"type": "paragraph",
"data": {
"text": "with this silly js?"
}
}],
"version": "2.22.2"
}
]
// retrieve posts
for (var i = 0; i < blogs.length; i++) {
var getBlocks = blogs[i]['blocks'];
//console.log('retrieving');
//console.log(getBlocks);
for (var y = 0; y < getBlocks.length; y++) {
console.log('printing text.......');
console.log(getBlocks[y]);
var msg = convertDataToHtml(getBlocks);
console.log(msg);
}
var addblog = document.createElement('div');
var textmsg = document.createTextNode(msg);
addblog.appendChild(textmsg);
addblog.style.cssText = "border: 1px solid";
document.getElementById('allblogs').appendChild(addblog);
msg = '';
}
function convertDataToHtml(blocks) {
var convertedHtml = "";
blocks.map(block => {
switch (block.type) {
case "header":
convertedHtml += `<h${block.data.level}>${block.data.text}</h${block.data.level}>`;
break;
case "embded":
convertedHtml += `<div><iframe width="560" height="315" src="${block.data.embed}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></div>`;
break;
case "paragraph":
convertedHtml += `<p>${block.data.text}</p>`;
break;
case "delimiter":
convertedHtml += "<hr />";
break;
case "image":
convertedHtml += `<img class="img-fluid" src="${block.data.file.url}" title="${block.data.caption}" /><br /><em>${block.data.caption}</em>`;
break;
case "list":
convertedHtml += "<ul>";
block.data.items.forEach(function(li) {
convertedHtml += `<li>${li}</li>`;
});
convertedHtml += "</ul>";
break;
default:
console.log("Unknown block type", block.type);
break;
}
});
return convertedHtml;
}
Then I have no idea how to convert html tag to styled html.
var parser = new DOMParser();
var msg = parser.parseFromString(msg, 'text/html');
I have tried this, but it comes [object HTMLDocument]