From 1915d7b1954b4ba95311e14387eb943f1459fd64 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Mon, 25 Aug 2025 02:44:13 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AE=20fix:=20Properly=20Escape=20Curre?= =?UTF-8?q?ncy=20and=20Prevent=20Code=20Block=20LaTeX=20Bugs=20(#9248)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(latex): prevent LaTeX conversion when closing $ is preceded by backtick When text contained patterns like "$lookup namespace" followed by "`$lookup`", the regex would match from the first $ to the backtick's $, treating the entire span as a LaTeX expression. This caused programming constructs to be incorrectly converted to double dollars. - Added negative lookbehind (? \\$0\\}$$ for positive prices'; expect(preprocessLaTeX(content)).toBe(expected); }); + + test('does not convert when closing dollar is preceded by backtick', () => { + const content = 'The error "invalid $lookup namespace" occurs when using `$lookup` operator'; + const expected = 'The error "invalid $lookup namespace" occurs when using `$lookup` operator'; + expect(preprocessLaTeX(content)).toBe(expected); + }); + + test('handles mixed backtick and non-backtick cases', () => { + const content = 'Use $x + y$ in math but `$lookup` in code'; + const expected = 'Use $$x + y$$ in math but `$lookup` in code'; + expect(preprocessLaTeX(content)).toBe(expected); + }); + + test('escapes currency amounts without commas', () => { + const content = + 'The total amount invested is $1157.90 (existing amount) + $500 (new investment) = $1657.90.'; + const expected = + 'The total amount invested is \\$1157.90 (existing amount) + \\$500 (new investment) = \\$1657.90.'; + expect(preprocessLaTeX(content)).toBe(expected); + }); + + test('handles large currency amounts', () => { + const content = 'You can win $1000000 or even $9999999.99!'; + const expected = 'You can win \\$1000000 or even \\$9999999.99!'; + expect(preprocessLaTeX(content)).toBe(expected); + }); + + test('escapes currency with many decimal places', () => { + const content = 'Bitcoin: $0.00001234, Gas: $3.999, Rate: $1.234567890'; + const expected = 'Bitcoin: \\$0.00001234, Gas: \\$3.999, Rate: \\$1.234567890'; + expect(preprocessLaTeX(content)).toBe(expected); + }); }); diff --git a/client/src/utils/latex.ts b/client/src/utils/latex.ts index b004d798f..11438245c 100644 --- a/client/src/utils/latex.ts +++ b/client/src/utils/latex.ts @@ -3,9 +3,8 @@ const MHCHEM_CE_REGEX = /\$\\ce\{/g; const MHCHEM_PU_REGEX = /\$\\pu\{/g; const MHCHEM_CE_ESCAPED_REGEX = /\$\\\\ce\{[^}]*\}\$/g; const MHCHEM_PU_ESCAPED_REGEX = /\$\\\\pu\{[^}]*\}\$/g; -const CURRENCY_REGEX = - /(?