简体   繁体   English

markdown-it替代方案,它创建dom元素而不是呈现HTML文本

[英]markdown-it alternative that creates dom elements instead of rendering HTML text

I'm currently using markdown-it for my web-extensions to display translatable tutorials & changelogs. 我目前正在将markdown-it用于我的网络扩展程序,以显示可翻译的教程和变更日志。 But Mozilla has safety checks in place telling me that the use of innerHTML is unsafe. 但是Mozilla进行了安全检查,告诉我使用innerHTML是不安全的。 So I'm wondering if there is a JavaScript (ideally TypeScript) markdown library that creates dom-elements rather than rendering HTML text? 所以我想知道是否有一个JavaScript(理想的TypeScript)markdown库可以创建dom元素而不是呈现HTML文本?

I found a workaround for the issue that works without using eval: 我找到了一种无需使用eval即可解决的问题的解决方法:

// This was the old code:
container.innerHTML = md.render(value);

// This is the new code:
const domParser = new DOMParser();
const doc = domParser.parseFromString(md.render(value), 'text/html');
removeAllChildren(container);
for(let i=0; i<doc.body.childNodes.length; i++)
    container.appendChild(doc.body.childNodes[i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM