Просмотр исходного кода

fix: 修复Markdown渲染中图片和链接被escapeHtml转义的bug

Note Manager 2 недель назад
Родитель
Сommit
f464ce2b75
1 измененных файлов с 17 добавлено и 9 удалено
  1. 17 9
      web/index.html

+ 17 - 9
web/index.html

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