From de182ddec24a37ae37481f080684b98701b631b9 Mon Sep 17 00:00:00 2001 From: tth37 Date: Tue, 6 May 2025 18:38:42 +0800 Subject: [PATCH 1/2] fix(katex): Allow Chinese characters adjacent to math delimiters --- src/lib/utils/marked/katex-extension.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/lib/utils/marked/katex-extension.ts b/src/lib/utils/marked/katex-extension.ts index f679a6b22..b648440d8 100644 --- a/src/lib/utils/marked/katex-extension.ts +++ b/src/lib/utils/marked/katex-extension.ts @@ -10,6 +10,9 @@ const DELIMITER_LIST = [ { left: '\\begin{equation}', right: '\\end{equation}', display: true } ]; +// Defines characters that are allowed to immediately precede or follow a math delimiter. +const ALLOWED_SURROUNDING_CHARS = '\\s?。,!-\\/:-@\\[-`{-~\\p{Script=Han}'; + // const DELIMITER_LIST = [ // { left: '$$', right: '$$', display: false }, // { left: '$', right: '$', display: false }, @@ -44,10 +47,13 @@ function generateRegexRules(delimiters) { // Math formulas can end in special characters const inlineRule = new RegExp( - `^(${inlinePatterns.join('|')})(?=[\\s?。,!-\/:-@[-\`{-~]|$)`, + `^(${inlinePatterns.join('|')})(?=[${ALLOWED_SURROUNDING_CHARS}]|$)`, + 'u' + ); + const blockRule = new RegExp( + `^(${blockPatterns.join('|')})(?=[${ALLOWED_SURROUNDING_CHARS}]|$)`, 'u' ); - const blockRule = new RegExp(`^(${blockPatterns.join('|')})(?=[\\s?。,!-\/:-@[-\`{-~]|$)`, 'u'); return { inlineRule, blockRule }; } @@ -91,7 +97,9 @@ function katexStart(src, displayMode: boolean) { // Check if the delimiter is preceded by a special character. // If it does, then it's potentially a math formula. - const f = index === 0 || indexSrc.charAt(index - 1).match(/[\s?。,!-\/:-@[-`{-~]/); + const f = + index === 0 || + indexSrc.charAt(index - 1).match(new RegExp(`[${ALLOWED_SURROUNDING_CHARS}]`, 'u')); if (f) { const possibleKatex = indexSrc.substring(index); From 8e14372dd830e7d3291b32fcefed15a07d2865f5 Mon Sep 17 00:00:00 2001 From: tth37 Date: Tue, 6 May 2025 21:26:09 +0800 Subject: [PATCH 2/2] enh: Support more languages --- src/lib/utils/marked/katex-extension.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/utils/marked/katex-extension.ts b/src/lib/utils/marked/katex-extension.ts index b648440d8..02a52b651 100644 --- a/src/lib/utils/marked/katex-extension.ts +++ b/src/lib/utils/marked/katex-extension.ts @@ -11,7 +11,8 @@ const DELIMITER_LIST = [ ]; // Defines characters that are allowed to immediately precede or follow a math delimiter. -const ALLOWED_SURROUNDING_CHARS = '\\s?。,!-\\/:-@\\[-`{-~\\p{Script=Han}'; +const ALLOWED_SURROUNDING_CHARS = + '\\s?。,、;!-\\/:-@\\[-`{-~\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}\\p{Script=Hangul}'; // const DELIMITER_LIST = [ // { left: '$$', right: '$$', display: false },