html 是否可以将HTLM转换为EditorJS json?

nzkunb0c  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(99)

我想把HTML字符串显示给editorJS,你有什么想法吗?
<h1>The video src attribute</h1><video width="320" height="240" controls src="movie.ogg">Yourbrowser does not support the video tag.</video><p><b>Note:</b> The .ogg fileformat is not supported in old IE and Safari.</p>开始
变成了这样

"time" : 1616822242088,
    "blocks" : [ 
        {
            "type" : "image",
            "data" : {
                "file" : {
                    "url" : "https://codex.so/public/app/img/external/codex2x.png"
                },
                "caption" : "",
                "withBorder" : false,
                "stretched" : false,
                "withBackground" : false
            }
        }
    ],
    "version" : "2.19.3"
}```
lrpiutwd

lrpiutwd1#

您可以使用与创建此JSON对象块相同的editorsjs。EditorJs库有一个函数-renderFromHtml(),用于将HTML块转换回JSON对象块。

editor = new EditorJS({
     autofocus: true,
     tools: <editorjs_tools>,
     data: HTML
});

editor.blocks.renderFromHTML(req.body.html);
     window.editor.save().then(data => {
     console.log(data); // json block data as expected
     res.end(JSON.stringify(data))
})

相关问题