Skip to content

Commit d9959cb

Browse files
committed
fix more latex
1 parent 124df6e commit d9959cb

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
30 Bytes
Binary file not shown.

examples/server/webui/src/components/MarkdownDisplay.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ export function preprocessLaTeX(content: string): string {
135135

136136
// Step 2: Protect existing LaTeX expressions
137137
const latexExpressions: string[] = [];
138+
139+
// Protect block math ($$...$$), \[...\], and \(...\) as before.
138140
content = content.replace(
139141
/(\$\$[\s\S]*?\$\$|\\\[[\s\S]*?\\\]|\\\(.*?\\\))/g,
140142
(match) => {
@@ -143,7 +145,22 @@ export function preprocessLaTeX(content: string): string {
143145
}
144146
);
145147

146-
// Step 3: Escape dollar signs that are likely currency indicators
148+
// Protect inline math ($...$) only if it does NOT match a currency pattern.
149+
// We assume a currency pattern is one where the inner content is purely numeric (with optional decimals).
150+
content = content.replace(/\$([^$]+)\$/g, (match, inner) => {
151+
if (/^\s*\d+(?:\.\d+)?\s*$/.test(inner)) {
152+
// This looks like a currency value (e.g. "$123" or "$12.34"),
153+
// so don't protect it.
154+
return match;
155+
} else {
156+
// Otherwise, treat it as a LaTeX expression.
157+
latexExpressions.push(match);
158+
return `<<LATEX_${latexExpressions.length - 1}>>`;
159+
}
160+
});
161+
162+
// Step 3: Escape dollar signs that are likely currency indicators.
163+
// (Now that inline math is protected, this will only escape dollars not already protected)
147164
content = content.replace(/\$(?=\d)/g, '\\$');
148165

149166
// Step 4: Restore LaTeX expressions

0 commit comments

Comments
 (0)