md 标题中包含标签导致渲染错误

This commit is contained in:
黄心宇 2024-05-16 10:03:29 +08:00
parent e58478bfdc
commit a31d0e06dc
1 changed files with 11 additions and 1 deletions

View File

@ -150,6 +150,16 @@ renderer.code = function (code, infostring, escaped) {
}
function cleanString(str) {
// 移除整个HTML标签及其内容
let cleanedStr = str.replace(/<[^>]*>[^<]*<\/[^>]*>/g, '');
// 移除剩余的HTML标签
cleanedStr = cleanedStr.replace(/<[^>]*>/g, '');
// 移除特殊字符
cleanedStr = cleanedStr.replace(/[.,/#!$%^&*;:{}=\-_`~():,。¥;「」|?》《~·【】‘、!]/g, '');
return cleanedStr;
}
renderer.heading = function (text, level, raw) {
let anchor = this.options.headerPrefix + raw.toLowerCase().replace(/[^\w\\u4e00-\\u9fa5]]+/g, '-');
toc.push({
@ -157,7 +167,7 @@ renderer.heading = function (text, level, raw) {
level: level,
text: text
})
let id = anchor.replace(/[.,/#!$%^&*;:{}=\-_`~():,。¥;「」|?》《~·【】‘、!]/g,"");
let id = cleanString(anchor);
return '<h' + level + ' id="' + id + '" class="markdown_anchors"><a name="#'+id+'" class="anchors"><i class="iconfont icon-lianjieicon font-14"></i></a>' + text + '</h' + level + '>'
}
marked.setOptions({