fix: 修复Markdown渲染中图片和链接被escapeHtml转义的bug
This commit is contained in:
+17
-9
@@ -818,9 +818,7 @@
|
|||||||
return placeholder;
|
return placeholder;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 在转义之前处理图片(必须先于链接处理)
|
// 用占位符保护图片,避免被后续处理影响
|
||||||
// 图片:  链接: [text](url)
|
|
||||||
// 先用占位符保护图片,再处理链接
|
|
||||||
const imgPlaceholders = [];
|
const imgPlaceholders = [];
|
||||||
text = text.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, function(match, alt, src) {
|
text = text.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, function(match, alt, src) {
|
||||||
const placeholder = '__IMG_' + imgPlaceholders.length + '__';
|
const placeholder = '__IMG_' + imgPlaceholders.length + '__';
|
||||||
@@ -828,17 +826,27 @@
|
|||||||
return placeholder;
|
return placeholder;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 处理链接
|
// 用占位符保护链接
|
||||||
text = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank">$1</a>');
|
const linkPlaceholders = [];
|
||||||
|
text = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, function(match, text_, url) {
|
||||||
// 恢复图片
|
const placeholder = '__LINK_' + linkPlaceholders.length + '__';
|
||||||
imgPlaceholders.forEach(function(img, i) {
|
linkPlaceholders.push('<a href="' + url + '" target="_blank">' + text_ + '</a>');
|
||||||
text = text.replace('__IMG_' + i + '__', img);
|
return placeholder;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 转义 HTML(处理代码块内的内容和其他文本)
|
// 转义 HTML(处理代码块内的内容和其他文本)
|
||||||
text = escapeHtml(text);
|
text = escapeHtml(text);
|
||||||
|
|
||||||
|
// 恢复链接(在转义之后)
|
||||||
|
linkPlaceholders.forEach(function(link, i) {
|
||||||
|
text = text.replace('__LINK_' + i + '__', link);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 恢复图片(在转义之后)
|
||||||
|
imgPlaceholders.forEach(function(img, i) {
|
||||||
|
text = text.replace('__IMG_' + i + '__', img);
|
||||||
|
});
|
||||||
|
|
||||||
// 处理行内代码
|
// 处理行内代码
|
||||||
text = text.replace(/`([^`]+)`/g, '<code>$1</code>');
|
text = text.replace(/`([^`]+)`/g, '<code>$1</code>');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user