From 0bfbace9aa38ec93e99b6d48d14e3f75acaa8428 Mon Sep 17 00:00:00 2001 From: Hwang In Tak Date: Tue, 24 Sep 2024 22:00:01 +0900 Subject: [PATCH] fix: Simplify regex --- src/lib/utils/index.ts | 18 ------------------ src/lib/utils/marked/katex-extension.ts | 6 ++++-- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index bb553c57e..cea7f6e64 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -8,23 +8,6 @@ import { TTS_RESPONSE_SPLIT } from '$lib/types'; // Helper functions ////////////////////////// -const convertLatexToSingleLine = (content) => { - // Patterns to match multiline LaTeX blocks - const patterns = [ - /(\$\$\s[\s\S]*?\s\$\$)/g, // Match $$ ... $$ - /(\\\[[\s\S]*?\\\])/g, // Match \[ ... \] - /(\\begin\{[a-z]+\}[\s\S]*?\\end\{[a-z]+\})/g // Match \begin{...} ... \end{...} - ]; - - patterns.forEach((pattern) => { - content = content.replace(pattern, (match) => { - return match.replace(/\s*\n\s*/g, ' ').trim(); - }); - }); - - return content; -}; - export const replaceTokens = (content, char, user) => { const charToken = /{{char}}/gi; const userToken = /{{user}}/gi; @@ -68,7 +51,6 @@ export const sanitizeResponseContent = (content: string) => { }; export const processResponseContent = (content: string) => { - content = convertLatexToSingleLine(content); return content.trim(); }; diff --git a/src/lib/utils/marked/katex-extension.ts b/src/lib/utils/marked/katex-extension.ts index 755519566..371c4c932 100644 --- a/src/lib/utils/marked/katex-extension.ts +++ b/src/lib/utils/marked/katex-extension.ts @@ -35,11 +35,11 @@ function generateRegexRules(delimiters) { if (!display) { inlinePatterns.push( - `${escapedLeft}((?:\\\\.|[^\\\\\\n])*?(?:\\\\.|[^\\\\\\n${escapedRight}]))${escapedRight}` + `${escapedLeft}((?:\\\\[^]|[^\\\\])+?)${escapedRight}` ); } else { blockPatterns.push( - `${escapedLeft}((?:\\\\.|[^\\\\\\n])*?(?:\\\\.|[^\\\\\\n${escapedRight}]))${escapedRight}` + `${escapedLeft}((?:\\\\[^]|[^\\\\])+?)${escapedRight}` ); } }); @@ -109,6 +109,8 @@ function katexTokenizer(src, tokens, displayMode: boolean) { const match = src.match(ruleReg); + console.log("searching:", src); + if (match) { const text = match .slice(2)