Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🧮 fix: Properly Escape Currency and Prevent Code Block LaTeX Bugs (#9248)

Danny Avila1915d7b1954b
diff --git a/client/src/utils/latex.spec.ts b/client/src/utils/latex.spec.ts
index 6b1e685dfe..d223edbd8e 100644
--- a/client/src/utils/latex.spec.ts
+++ b/client/src/utils/latex.spec.ts
@@ -207,4 +207,36 @@ y$ which spans lines`;
const expected = 'Set $$\\{x | x > \\$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 b004d798fb..11438245c1 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 =
- /(?<![\\$])\$(?!\$)(?=\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?(?:\s|$|[^a-zA-Z\d]))/g;
-const SINGLE_DOLLAR_REGEX = /(?<!\\)\$(?!\$)((?:[^$\n]|\\[$])+?)(?<!\\)\$(?!\$)/g;
+const CURRENCY_REGEX = /(?<![\\$])\$(?!\$)(?=\d+(?:,\d{3})*(?:\.\d+)?(?:\s|$|[^a-zA-Z\d]))/g;
+const SINGLE_DOLLAR_REGEX = /(?<!\\)\$(?!\$)((?:[^$\n]|\\[$])+?)(?<!\\)(?<!`)\$(?!\$)/g;
/**
* Escapes mhchem package notation in LaTeX by converting single dollar delimiters to double dollars