From 29169f8de199560c2d24a3ae2fb4b8839884666d Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sun, 3 Nov 2024 23:43:09 +0530 Subject: [PATCH] feat: Add support for LaTeX environments Support `equation` and `align` environments for LaTeX. This change enables us to handle more complex LaTeX expressions, including those within environments. By replacing the delimiters for these environments with the appropriate MathJax equivalents, we ensure consistent rendering of mathematical equations within the application. --- src/utils/latex.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/utils/latex.ts b/src/utils/latex.ts index c1e55eb..77f98ff 100644 --- a/src/utils/latex.ts +++ b/src/utils/latex.ts @@ -1,14 +1,19 @@ export const preprocessLaTeX = (content: string) => { - // Replace block-level LaTeX delimiters \[ \] with $$ $$ - - const blockProcessedContent = content.replace( - /\\\[(.*?)\\\]/gs, - (_, equation) => `$$${equation}$$` - ) - // Replace inline LaTeX delimiters \( \) with $ $ - const inlineProcessedContent = blockProcessedContent.replace( - /\\\((.*?)\\\)/gs, - (_, equation) => `$${equation}$` - ) - return inlineProcessedContent + let processedContent = content.replace( + /\\\[([\s\S]*?)\\\]/g, + (_, equation) => `$$${equation}$$` + ); + processedContent = processedContent.replace( + /\\\(([\s\S]*?)\\\)/g, + (_, equation) => `$${equation}$` + ); + processedContent = processedContent.replace( + /\\begin\{equation\}([\s\S]*?)\\end\{equation\}/g, + (_, equation) => `$$${equation}$$` + ); + processedContent = processedContent.replace( + /\\begin\{align\}([\s\S]*?)\\end\{align\}/g, + (_, equation) => `$$\\begin{aligned}${equation}\\end{aligned}$$` + ); + return processedContent; } \ No newline at end of file