Explorar o código

Merge branch 'solution-大括号问题' into dev

过卫栋 hai 4 semanas
pai
achega
5ee2e5be4f
Modificáronse 1 ficheiros con 13 adicións e 7 borrados
  1. 13 7
      scripts/katex-render.mjs

+ 13 - 7
scripts/katex-render.mjs

@@ -114,17 +114,17 @@ function processDelimiter(html, left, right, displayMode) {
     const rightEscaped = escapeRegex(right);
 
     // 构建正则表达式
-    // 【关键修复】排除包含 HTML 标签的内容(不匹配 < 或 >)
+    // 【修复】允许数学符号 < 和 >,只排除明确的 HTML 标签(如 <span>, </div>)
     let pattern;
     if (left === '$' && right === '$') {
-        // 单个 $...$:不匹配 $$,不匹配包含 < > 的内容
-        pattern = new RegExp(`(?<!\\$)\\$(?!\\$)([^$<>]+?)(?<!\\$)\\$(?!\\$)`, 'g');
+        // 单个 $...$:不匹配 $$,允许 < > 但不匹配换行
+        pattern = new RegExp(`(?<!\\$)\\$(?!\\$)([^$\\n]+?)(?<!\\$)\\$(?!\\$)`, 'g');
     } else if (left === '$$' && right === '$$') {
-        // $$...$$:不匹配包含 < > 的内容
-        pattern = new RegExp(`\\$\\$([^<>]*?)\\$\\$`, 'g');
+        // $$...$$:允许多行和 < >
+        pattern = new RegExp(`\\$\\$([\\s\\S]*?)\\$\\$`, 'g');
     } else {
-        // \(...\) 和 \[...\]:不匹配包含 < > 的内容
-        pattern = new RegExp(`${leftEscaped}([^<>]*?)${rightEscaped}`, 'g');
+        // \(...\) 和 \[...\]:允许 < >
+        pattern = new RegExp(`${leftEscaped}([\\s\\S]*?)${rightEscaped}`, 'g');
     }
 
     return html.replace(pattern, (match, latex) => {
@@ -137,6 +137,12 @@ function processDelimiter(html, left, right, displayMode) {
                 return match;
             }
 
+            // 【新增】跳过包含 HTML 标签的内容(如 <span>, </div>, <br>)
+            // 但允许数学符号 < 和 >(如 a > 0, x < y)
+            if (/<\/?[a-zA-Z][^>]*>/.test(cleanLatex)) {
+                return match;
+            }
+
             // 【修复】处理公式内的换行符:将 \n 替换成空格,避免破坏公式
             // 使用负向前瞻 (?![a-zA-Z]) 避免误伤 LaTeX 命令如 \neq, \ne, \newline, \nu 等
             cleanLatex = cleanLatex.replace(/\\n(?![a-zA-Z])/g, ' ').replace(/\n/g, ' ');