繁体   English   中英

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

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

我目前正在将markdown-it用于我的网络扩展程序,以显示可翻译的教程和变更日志。 但是Mozilla进行了安全检查,告诉我使用innerHTML是不安全的。 所以我想知道是否有一个JavaScript(理想的TypeScript)markdown库可以创建dom元素而不是呈现HTML文本?

我找到了一种无需使用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