Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(frontend): render LaTeX math in chat via remark-math + KaTeX

Haozhe Wu585b8f69cda4
diff --git a/frontend/package-lock.json b/frontend/package-lock.json
index 392a4822d..b5cd5a685 100644
--- a/frontend/package-lock.json
+++ b/frontend/package-lock.json
@@ -15,6 +15,7 @@
"highlight.js": "^11.11.1",
"i18next": "^26.3.6",
"i18next-browser-languagedetector": "^8.2.1",
+ "katex": "^0.18.1",
"lucide-react": "^1.24.0",
"react": "^19.0.0",
"react-dom": "^19.2.7",
@@ -22,7 +23,9 @@
"react-markdown": "^9.0.0",
"react-router-dom": "^7.18.1",
"rehype-highlight": "^7.0.0",
+ "rehype-katex": "^7.0.1",
"remark-gfm": "^4.0.1",
+ "remark-math": "^6.0.0",
"sonner": "^2.0.7",
"tailwind-merge": "^3.6.0",
"zustand": "^5.0.14"
@@ -1707,6 +1710,12 @@
"@types/unist": "*"
}
},
+ "node_modules/@types/katex": {
+ "version": "0.16.8",
+ "resolved": "https://registry.npmjs.org/@types/katex/-/katex-0.16.8.tgz",
+ "integrity": "sha512-trgaNyfU+Xh2Tc+ABIb44a5AYUpicB3uwirOioeOkNPPbmgRNtcWyDeeFRzjPZENO9Vq8gvVqfhaaXWLlevVwg==",
+ "license": "MIT"
+ },
"node_modules/@types/mdast": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-4.0.4.tgz",
@@ -2728,6 +2737,99 @@
"node": ">= 0.4"
}
},
+ "node_modules/hast-util-from-dom": {
+ "version": "5.0.1",
+ "resolved": "https://registry.npmjs.org/hast-util-from-dom/-/hast-util-from-dom-5.0.1.tgz",
+ "integrity": "sha512-N+LqofjR2zuzTjCPzyDUdSshy4Ma6li7p/c3pA78uTwzFgENbgbUrm2ugwsOdcjI1muO+o6Dgzp9p8WHtn/39Q==",
+ "license": "ISC",
+ "dependencies": {
+ "@types/hast": "^3.0.0",
+ "hastscript": "^9.0.0",
+ "web-namespaces": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/hast-util-from-html": {
+ "version": "2.0.3",
+ "resolved": "https://registry.npmjs.org/hast-util-from-html/-/hast-util-from-html-2.0.3.tgz",
+ "integrity": "sha512-CUSRHXyKjzHov8yKsQjGOElXy/3EKpyX56ELnkHH34vDVw1N1XSQ1ZcAvTyAPtGqLTuKP/uxM+aLkSPqF/EtMw==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/hast": "^3.0.0",
+ "devlop": "^1.1.0",
+ "hast-util-from-parse5": "^8.0.0",
+ "parse5": "^7.0.0",
+ "vfile": "^6.0.0",
+ "vfile-message": "^4.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/hast-util-from-html-isomorphic": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/hast-util-from-html-isomorphic/-/hast-util-from-html-isomorphic-2.0.0.tgz",
+ "integrity": "sha512-zJfpXq44yff2hmE0XmwEOzdWin5xwH+QIhMLOScpX91e/NSGPsAzNCvLQDIEPyO2TXi+lBmU6hjLIhV8MwP2kw==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/hast": "^3.0.0",
+ "hast-util-from-dom": "^5.0.0",
+ "hast-util-from-html": "^2.0.0",
+ "unist-util-remove-position": "^5.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/hast-util-from-html/node_modules/entities": {
+ "version": "6.0.1",
+ "resolved": "https://registry.npmjs.org/entities/-/entities-6.0.1.tgz",
+ "integrity": "sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g==",
+ "license": "BSD-2-Clause",
+ "engines": {
+ "node": ">=0.12"
+ },
+ "funding": {
+ "url": "https://github.com/fb55/entities?sponsor=1"
+ }
+ },
+ "node_modules/hast-util-from-html/node_modules/parse5": {
+ "version": "7.3.0",
+ "resolved": "https://registry.npmjs.org/parse5/-/parse5-7.3.0.tgz",
+ "integrity": "sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw==",
+ "license": "MIT",
+ "dependencies": {
+ "entities": "^6.0.0"
+ },
+ "funding": {
+ "url": "https://github.com/inikulin/parse5?sponsor=1"
+ }
+ },
+ "node_modules/hast-util-from-parse5": {
+ "version": "8.0.3",
+ "resolved": "https://registry.npmjs.org/hast-util-from-parse5/-/hast-util-from-parse5-8.0.3.tgz",
+ "integrity": "sha512-3kxEVkEKt0zvcZ3hCRYI8rqrgwtlIOFMWkbclACvjlDw8Li9S2hk/d51OI0nr/gIpdMHNepwgOKqZ/sy0Clpyg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/hast": "^3.0.0",
+ "@types/unist": "^3.0.0",
+ "devlop": "^1.0.0",
+ "hastscript": "^9.0.0",
+ "property-information": "^7.0.0",
+ "vfile": "^6.0.0",
+ "vfile-location": "^5.0.0",
+ "web-namespaces": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/hast-util-is-element": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/hast-util-is-element/-/hast-util-is-element-3.0.0.tgz",
@@ -2741,6 +2843,19 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/hast-util-parse-selector": {
+ "version": "4.0.0",
+ "resolved": "https://registry.npmjs.org/hast-util-parse-selector/-/hast-util-parse-selector-4.0.0.tgz",
+ "integrity": "sha512-wkQCkSYoOGCRKERFWcxMVMOcYE2K1AaNLU8DXS9arxnLOUEWbOXKXiJUNzEpqZ3JOKpnha3jkFrumEjVliDe7A==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/hast": "^3.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/hast-util-to-jsx-runtime": {
"version": "2.3.6",
"resolved": "https://registry.npmjs.org/hast-util-to-jsx-runtime/-/hast-util-to-jsx-runtime-2.3.6.tgz",
@@ -2797,6 +2912,23 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/hastscript": {
+ "version": "9.0.1",
+ "resolved": "https://registry.npmjs.org/hastscript/-/hastscript-9.0.1.tgz",
+ "integrity": "sha512-g7df9rMFX/SPi34tyGCyUBREQoKkapwdY/T04Qn9TDWfHhAYt4/I0gMVirzK5wEzeUqIjEB+LXC/ypb7Aqno5w==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/hast": "^3.0.0",
+ "comma-separated-tokens": "^2.0.0",
+ "hast-util-parse-selector": "^4.0.0",
+ "property-information": "^7.0.0",
+ "space-separated-tokens": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/highlight.js": {
"version": "11.11.1",
"resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-11.11.1.tgz",
@@ -3156,6 +3288,31 @@
"node": ">=6"
}
},
+ "node_modules/katex": {
+ "version": "0.18.1",
+ "resolved": "https://registry.npmjs.org/katex/-/katex-0.18.1.tgz",
+ "integrity": "sha512-Td8GCYSxDAoMhHOlKmCFMJ/hz5qlAAb71n66Dryw9nfCVfumLo7nhuotbvKom/XPADmrYC3O5QR71EPq4DarJQ==",
+ "funding": [
+ "https://opencollective.com/katex",
+ "https://github.com/sponsors/katex"
+ ],
+ "license": "MIT",
+ "dependencies": {
+ "commander": "^8.3.0"
+ },
+ "bin": {
+ "katex": "cli.js"
+ }
+ },
+ "node_modules/katex/node_modules/commander": {
+ "version": "8.3.0",
+ "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz",
+ "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 12"
+ }
+ },
"node_modules/lilconfig": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-3.1.3.tgz",
@@ -3433,6 +3590,25 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/mdast-util-math": {
+ "version": "3.0.0",
+ "resolved": "https://registry.npmjs.org/mdast-util-math/-/mdast-util-math-3.0.0.tgz",
+ "integrity": "sha512-Tl9GBNeG/AhJnQM221bJR2HPvLOSnLE/T9cJI9tlc6zwQk2nPk/4f0cHkOdEixQPC/j8UtKDdITswvLAy1OZ1w==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/hast": "^3.0.0",
+ "@types/mdast": "^4.0.0",
+ "devlop": "^1.0.0",
+ "longest-streak": "^3.0.0",
+ "mdast-util-from-markdown": "^2.0.0",
+ "mdast-util-to-markdown": "^2.1.0",
+ "unist-util-remove-position": "^5.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/mdast-util-mdx-expression": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/mdast-util-mdx-expression/-/mdast-util-mdx-expression-2.0.1.tgz",
@@ -3769,6 +3945,50 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/micromark-extension-math": {
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/micromark-extension-math/-/micromark-extension-math-3.1.0.tgz",
+ "integrity": "sha512-lvEqd+fHjATVs+2v/8kg9i5Q0AP2k85H0WUOwpIVvUML8BapsMvh1XAogmQjOCsLpoKRCVQqEkQBB3NhVBcsOg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/katex": "^0.16.0",
+ "devlop": "^1.0.0",
+ "katex": "^0.16.0",
+ "micromark-factory-space": "^2.0.0",
+ "micromark-util-character": "^2.0.0",
+ "micromark-util-symbol": "^2.0.0",
+ "micromark-util-types": "^2.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/micromark-extension-math/node_modules/commander": {
+ "version": "8.3.0",
+ "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz",
+ "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 12"
+ }
+ },
+ "node_modules/micromark-extension-math/node_modules/katex": {
+ "version": "0.16.47",
+ "resolved": "https://registry.npmjs.org/katex/-/katex-0.16.47.tgz",
+ "integrity": "sha512-Eeo8Ys1doU1z+x8AZsPpQu+p/QcZBI5PeOo7QGQdy2x2m0MU/hYagBbGOmXwr5KVbEfVuWv9LpnQWeehogurjg==",
+ "funding": [
+ "https://opencollective.com/katex",
+ "https://github.com/sponsors/katex"
+ ],
+ "license": "MIT",
+ "dependencies": {
+ "commander": "^8.3.0"
+ },
+ "bin": {
+ "katex": "cli.js"
+ }
+ },
"node_modules/micromark-factory-destination": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/micromark-factory-destination/-/micromark-factory-destination-2.0.1.tgz",
@@ -4768,6 +4988,50 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/rehype-katex": {
+ "version": "7.0.1",
+ "resolved": "https://registry.npmjs.org/rehype-katex/-/rehype-katex-7.0.1.tgz",
+ "integrity": "sha512-OiM2wrZ/wuhKkigASodFoo8wimG3H12LWQaH8qSPVJn9apWKFSH3YOCtbKpBorTVw/eI7cuT21XBbvwEswbIOA==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/hast": "^3.0.0",
+ "@types/katex": "^0.16.0",
+ "hast-util-from-html-isomorphic": "^2.0.0",
+ "hast-util-to-text": "^4.0.0",
+ "katex": "^0.16.0",
+ "unist-util-visit-parents": "^6.0.0",
+ "vfile": "^6.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
+ "node_modules/rehype-katex/node_modules/commander": {
+ "version": "8.3.0",
+ "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz",
+ "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==",
+ "license": "MIT",
+ "engines": {
+ "node": ">= 12"
+ }
+ },
+ "node_modules/rehype-katex/node_modules/katex": {
+ "version": "0.16.47",
+ "resolved": "https://registry.npmjs.org/katex/-/katex-0.16.47.tgz",
+ "integrity": "sha512-Eeo8Ys1doU1z+x8AZsPpQu+p/QcZBI5PeOo7QGQdy2x2m0MU/hYagBbGOmXwr5KVbEfVuWv9LpnQWeehogurjg==",
+ "funding": [
+ "https://opencollective.com/katex",
+ "https://github.com/sponsors/katex"
+ ],
+ "license": "MIT",
+ "dependencies": {
+ "commander": "^8.3.0"
+ },
+ "bin": {
+ "katex": "cli.js"
+ }
+ },
"node_modules/remark-gfm": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/remark-gfm/-/remark-gfm-4.0.1.tgz",
@@ -4786,6 +5050,22 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/remark-math": {
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/remark-math/-/remark-math-6.0.0.tgz",
+ "integrity": "sha512-MMqgnP74Igy+S3WwnhQ7kqGlEerTETXMvJhrUzDikVZ2/uogJCb+WHUg97hK9/jcfc0dkD73s3LN8zU49cTEtA==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/mdast": "^4.0.0",
+ "mdast-util-math": "^3.0.0",
+ "micromark-extension-math": "^3.0.0",
+ "unified": "^11.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/remark-parse": {
"version": "11.0.0",
"resolved": "https://registry.npmjs.org/remark-parse/-/remark-parse-11.0.0.tgz",
@@ -5467,6 +5747,20 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/unist-util-remove-position": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/unist-util-remove-position/-/unist-util-remove-position-5.0.0.tgz",
+ "integrity": "sha512-Hp5Kh3wLxv0PHj9m2yZhhLt58KzPtEYKQQ4yxfYFEO7EvHwzyDYnduhHnY1mDxoqr7VUwVuHXk9RXKIiYS1N8Q==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/unist": "^3.0.0",
+ "unist-util-visit": "^5.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/unist-util-stringify-position": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz",
@@ -5570,6 +5864,20 @@
"url": "https://opencollective.com/unified"
}
},
+ "node_modules/vfile-location": {
+ "version": "5.0.3",
+ "resolved": "https://registry.npmjs.org/vfile-location/-/vfile-location-5.0.3.tgz",
+ "integrity": "sha512-5yXvWDEgqeiYiBe1lbxYF7UMAIm/IcopxMHrMQDq3nvKcjPKIhZklUKL+AE7J7uApI4kwe2snsK+eI6UTj9EHg==",
+ "license": "MIT",
+ "dependencies": {
+ "@types/unist": "^3.0.0",
+ "vfile": "^6.0.0"
+ },
+ "funding": {
+ "type": "opencollective",
+ "url": "https://opencollective.com/unified"
+ }
+ },
"node_modules/vfile-message": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-4.0.3.tgz",
@@ -5815,6 +6123,16 @@
"node": ">=18"
}
},
+ "node_modules/web-namespaces": {
+ "version": "2.0.1",
+ "resolved": "https://registry.npmjs.org/web-namespaces/-/web-namespaces-2.0.1.tgz",
+ "integrity": "sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ==",
+ "license": "MIT",
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/wooorm"
+ }
+ },
"node_modules/webidl-conversions": {
"version": "8.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-8.0.1.tgz",
diff --git a/frontend/package.json b/frontend/package.json
index a5bdb14ba..452f91054 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -19,6 +19,7 @@
"highlight.js": "^11.11.1",
"i18next": "^26.3.6",
"i18next-browser-languagedetector": "^8.2.1",
+ "katex": "^0.18.1",
"lucide-react": "^1.24.0",
"react": "^19.0.0",
"react-dom": "^19.2.7",
@@ -26,7 +27,9 @@
"react-markdown": "^9.0.0",
"react-router-dom": "^7.18.1",
"rehype-highlight": "^7.0.0",
+ "rehype-katex": "^7.0.1",
"remark-gfm": "^4.0.1",
+ "remark-math": "^6.0.0",
"sonner": "^2.0.7",
"tailwind-merge": "^3.6.0",
"zustand": "^5.0.14"
diff --git a/frontend/src/components/chat/MessageBubble.tsx b/frontend/src/components/chat/MessageBubble.tsx
index f2aad15b2..f97c0723b 100644
--- a/frontend/src/components/chat/MessageBubble.tsx
+++ b/frontend/src/components/chat/MessageBubble.tsx
@@ -1,16 +1,25 @@
import i18n from '@/i18n';
import { memo, useState, useCallback } from "react";
import { User, XCircle, RefreshCw, Copy, Check } from "lucide-react";
-import ReactMarkdown from "react-markdown";
+import ReactMarkdown, { type Options as ReactMarkdownOptions } from "react-markdown";
import remarkGfm from "remark-gfm";
+import remarkMath from "remark-math";
import rehypeHighlight from "rehype-highlight";
+import rehypeKatex from "rehype-katex";
+import "katex/dist/katex.min.css";
import { formatTimestamp } from "@/lib/formatters";
+import { normalizeMathDelimiters } from "@/lib/markdown";
import type { AgentMessage } from "@/types/agent";
import { AgentAvatar } from "./AgentAvatar";
import { RunCompleteCard } from "./RunCompleteCard";
-const remarkPlugins = [remarkGfm];
-const rehypePlugins = [rehypeHighlight];
+// singleDollarTextMath off: dollar amounts ("$150 to $120") must never parse as
+// formulas; LLM \(...\)/\[...\] delimiters are normalized to $$ before render.
+const remarkPlugins: ReactMarkdownOptions["remarkPlugins"] = [
+ remarkGfm,
+ [remarkMath, { singleDollarTextMath: false }],
+];
+const rehypePlugins: ReactMarkdownOptions["rehypePlugins"] = [rehypeHighlight, rehypeKatex];
function CopyButton({ text }: { text: string }) {
const [copied, setCopied] = useState(false);
@@ -71,7 +80,7 @@ export const MessageBubble = memo(function MessageBubble({ msg, onRetry }: Props
<div className="flex-1 min-w-0 relative">
<CopyButton text={msg.content} />
<div className="prose prose-sm dark:prose-invert max-w-none leading-relaxed prose-table:border prose-table:border-border/50 prose-th:bg-muted/30 prose-th:px-3 prose-th:py-1.5 prose-td:px-3 prose-td:py-1.5 prose-th:text-left prose-th:text-xs prose-th:font-medium prose-td:text-xs prose-hr:hidden">
- <ReactMarkdown remarkPlugins={remarkPlugins} rehypePlugins={rehypePlugins}>{msg.content}</ReactMarkdown>
+ <ReactMarkdown remarkPlugins={remarkPlugins} rehypePlugins={rehypePlugins}>{normalizeMathDelimiters(msg.content)}</ReactMarkdown>
</div>
{ts && <span className="text-[9px] text-muted-foreground/30 mt-1 opacity-0 group-hover:opacity-100 transition-opacity">{ts}</span>}
</div>
diff --git a/frontend/src/components/chat/__tests__/MessageBubble.math.test.tsx b/frontend/src/components/chat/__tests__/MessageBubble.math.test.tsx
new file mode 100644
index 000000000..99c637b32
--- /dev/null
+++ b/frontend/src/components/chat/__tests__/MessageBubble.math.test.tsx
@@ -0,0 +1,47 @@
+import { render } from "@testing-library/react";
+import { MessageBubble } from "../MessageBubble";
+import type { AgentMessage } from "@/types/agent";
+
+// Unlike MessageBubble.test.tsx, react-markdown is NOT mocked here: these tests
+// exercise the real remark-math/rehype-katex pipeline end to end.
+vi.mock("../RunCompleteCard", () => ({ RunCompleteCard: () => null }));
+
+function answer(content: string): AgentMessage {
+ return { id: "msg-1", type: "answer", content, timestamp: Date.now() };
+}
+
+describe("MessageBubble LaTeX rendering", () => {
+ it("renders \\(...\\) as KaTeX inline math", () => {
+ const { container } = render(
+ <MessageBubble msg={answer("Sharpe: \\(\\frac{R_p - R_f}{\\sigma_p}\\)")} />,
+ );
+ expect(container.querySelector(".katex")).not.toBeNull();
+ });
+
+ it("renders \\[...\\] as KaTeX display math", () => {
+ const { container } = render(
+ <MessageBubble msg={answer("\\[\\sum_{i=1}^n w_i r_i\\]")} />,
+ );
+ expect(container.querySelector(".katex-display")).not.toBeNull();
+ });
+
+ it("renders $$...$$ as KaTeX math", () => {
+ const { container } = render(<MessageBubble msg={answer("Vol: $$\\sigma^2$$")} />);
+ expect(container.querySelector(".katex")).not.toBeNull();
+ });
+
+ it("does NOT treat dollar amounts as math", () => {
+ const { container } = render(
+ <MessageBubble msg={answer("AAPL fell from $150 to $120 today")} />,
+ );
+ expect(container.querySelector(".katex")).toBeNull();
+ expect(container.textContent).toContain("from $150 to $120");
+ });
+
+ it("does NOT transform LaTeX-like text inside code blocks", () => {
+ const { container } = render(
+ <MessageBubble msg={answer('```python\nre.match(r"\\(x\\)", s)\n```')} />,
+ );
+ expect(container.querySelector(".katex")).toBeNull();
+ });
+});
diff --git a/frontend/src/lib/__tests__/markdown.test.ts b/frontend/src/lib/__tests__/markdown.test.ts
new file mode 100644
index 000000000..abad41528
--- /dev/null
+++ b/frontend/src/lib/__tests__/markdown.test.ts
@@ -0,0 +1,53 @@
+import { normalizeMathDelimiters } from "../markdown";
+
+describe("normalizeMathDelimiters", () => {
+ it("converts \\(...\\) to inline $$ math", () => {
+ expect(normalizeMathDelimiters("Sharpe is \\(\\frac{R_p - R_f}{\\sigma_p}\\) here")).toBe(
+ "Sharpe is $$\\frac{R_p - R_f}{\\sigma_p}$$ here",
+ );
+ });
+
+ it("converts \\[...\\] to a display $$ block", () => {
+ expect(normalizeMathDelimiters("Formula: \\[\\sum_{i=1}^n w_i r_i\\] done")).toBe(
+ "Formula: \n\n$$\n\\sum_{i=1}^n w_i r_i\n$$\n\n done",
+ );
+ });
+
+ it("handles multiline display math", () => {
+ const input = "\\[\na = b\n+ c\n\\]";
+ expect(normalizeMathDelimiters(input)).toBe("\n\n$$\na = b\n+ c\n$$\n\n");
+ });
+
+ it("converts multiple inline formulas independently", () => {
+ expect(normalizeMathDelimiters("\\(a\\) and \\(b\\)")).toBe("$$a$$ and $$b$$");
+ });
+
+ it("leaves dollar amounts untouched", () => {
+ const text = "AAPL fell from $150 to $120, a $30 drop";
+ expect(normalizeMathDelimiters(text)).toBe(text);
+ });
+
+ it("leaves fenced code blocks untouched", () => {
+ const text = "```python\nre.match(r\"\\(x\\)\", s)\n```";
+ expect(normalizeMathDelimiters(text)).toBe(text);
+ });
+
+ it("leaves inline code untouched", () => {
+ const text = "use `\\(escaped\\)` in regex";
+ expect(normalizeMathDelimiters(text)).toBe(text);
+ });
+
+ it("still transforms text around code segments", () => {
+ expect(normalizeMathDelimiters("\\(x\\) `\\(y\\)` \\(z\\)")).toBe("$$x$$ `\\(y\\)` $$z$$");
+ });
+
+ it("leaves an unterminated streaming fence untouched", () => {
+ const text = "done\n```python\npartial = r\"\\(x\\)\"";
+ expect(normalizeMathDelimiters(text)).toBe(text);
+ });
+
+ it("passes through plain text and existing $$ math unchanged", () => {
+ const text = "plain text with $$x^2$$ already delimited";
+ expect(normalizeMathDelimiters(text)).toBe(text);
+ });
+});
diff --git a/frontend/src/lib/markdown.ts b/frontend/src/lib/markdown.ts
new file mode 100644
index 000000000..2bc3d23e9
--- /dev/null
+++ b/frontend/src/lib/markdown.ts
@@ -0,0 +1,23 @@
+/**
+ * LLMs emit LaTeX with `\(...\)` / `\[...\]` delimiters, but remark-math only
+ * parses dollar-delimited math. Single-dollar parsing is disabled in the
+ * renderer (finance text like "from $150 to $120" would become a formula), so
+ * everything is normalized to the `$$` forms remark-math still accepts:
+ * `\(x\)` → inline `$$x$$`, `\[x\]` → display block. Fenced/inline code is
+ * left untouched, including a trailing fence left unclosed mid-stream.
+ */
+
+const CODE_SEGMENT = /(```[\s\S]*?(?:```|$)|~~~[\s\S]*?(?:~~~|$)|`[^`\n]*`)/g;
+
+function normalizeSegment(segment: string): string {
+ return segment
+ .replace(/\\\[([\s\S]+?)\\\]/g, (_match, expr: string) => `\n\n$$\n${expr.trim()}\n$$\n\n`)
+ .replace(/\\\(([\s\S]+?)\\\)/g, (_match, expr: string) => `$$${expr.trim()}$$`);
+}
+
+export function normalizeMathDelimiters(content: string): string {
+ return content
+ .split(CODE_SEGMENT)
+ .map((segment, index) => (index % 2 === 1 ? segment : normalizeSegment(segment)))
+ .join("");
+}