This is a playground to test JavaScript. It runs a completely standard copy of Node.js on a virtual server created just for you. Every one of npm’s 300,000+ packages are pre-installed, so try it out:
// Import editorjs-html library
const edjsHTML = require('editorjs-html');
// Initialise a parser object
const edjsParser = edjsHTML();
// fetch editorjs cleandata, I will add some sample clean data for this tutorial
const cleanData = {
blocks: [{
type: 'header',
data: {
text: 'This is a sample Header',
level: 2
}
}]
}
// parse cleanData to HTML
const html = edjsParser.parse(cleanData);
// display generated HTML, or use it as you'd like.
console.log(html);
// Press Shift+Enter to view the result.
// Parse a single block
const blockHTML = edjsParser.parseBlock(cleanData.blocks[0]);
console.log(blockHTML);
// Defining a custom Block Function
function customParser({data}){
return `<custom> ${data.text} </custom>`
}
// You need to re-initialize edjsParser Instance, or pass it to earlier instance.
// The `edjsParser` defined above will throw an error since parser for `custom` block type is not defined.
const edjsParser2 = edjsHTML({custom: customParser});
const cleanDataWithCustomBlock = {
blocks: [{
type: 'custom',
data: {
text: 'This is a sample Header',
level: 2
}
}]
}
console.log(edjsParser2.parse(cleanDataWithCustomBlock));