From 55e22ebc48ab01c9a31eae279c5d1143166ab9f7 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sun, 3 Nov 2024 23:39:42 +0530 Subject: [PATCH] Fix: Improve LaTeX preprocessing logic The previous LaTeX preprocessing logic had a bug that could lead to incorrect rendering of mathematical equations. This commit refactors the logic to ensure that both block-level and inline equations are properly handled, improving the accuracy of LaTeX rendering in the application. --- src/utils/latex.ts | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/utils/latex.ts b/src/utils/latex.ts index 690e3e8..c1e55eb 100644 --- a/src/utils/latex.ts +++ b/src/utils/latex.ts @@ -1,19 +1,14 @@ export const preprocessLaTeX = (content: string) => { + // Replace block-level LaTeX delimiters \[ \] with $$ $$ - let processedContent = content.replace( - /\\\[(.*?)\\\]/gs, - (_, equation) => `$$${equation}$$` - ) - - processedContent = processedContent.replace( - /\\\((.*?)\\\)/gs, - (_, equation) => `$${equation}$` - ) - - processedContent = processedContent.replace( - /\$(\d)/g, - '\\$$1' - ) - - return processedContent -} + const blockProcessedContent = content.replace( + /\\\[(.*?)\\\]/gs, + (_, equation) => `$$${equation}$$` + ) + // Replace inline LaTeX delimiters \( \) with $ $ + const inlineProcessedContent = blockProcessedContent.replace( + /\\\((.*?)\\\)/gs, + (_, equation) => `$${equation}$` + ) + return inlineProcessedContent +} \ No newline at end of file