Spaces:
Sleeping
Sleeping
fix LaTex confusing with currency
Browse files- web/src/App.tsx +22 -7
web/src/App.tsx
CHANGED
|
@@ -125,16 +125,31 @@ function preprocessMath(content: string): string {
|
|
| 125 |
return `__MATH_BLOCK_${mathBlocks.length - 1}__`;
|
| 126 |
});
|
| 127 |
|
| 128 |
-
// Protect inline math blocks ($...$)
|
| 129 |
-
// Match $ followed by
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
});
|
| 134 |
|
| 135 |
// Step 2: Now escape standalone dollar signs (currency)
|
| 136 |
-
// Match $ followed by digits
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
// Step 3: Restore protected math blocks
|
| 140 |
mathBlocks.forEach((block, index) => {
|
|
|
|
| 125 |
return `__MATH_BLOCK_${mathBlocks.length - 1}__`;
|
| 126 |
});
|
| 127 |
|
| 128 |
+
// Protect ALL inline math blocks ($...$) - be more aggressive
|
| 129 |
+
// Match $ followed by ANY content until the next $ on the same line or nearby
|
| 130 |
+
// This includes simple cases like $1$, $0$, $x$, etc.
|
| 131 |
+
processed = processed.replace(/\$([^$\n]+)\$/g, (match, inner) => {
|
| 132 |
+
// Only treat as math if:
|
| 133 |
+
// 1. Contains LaTeX commands (\frac, \sqrt, etc.)
|
| 134 |
+
// 2. Contains letters (variables like x, y, n)
|
| 135 |
+
// 3. Is a single digit or simple number (like $1$, $0$, $2x$)
|
| 136 |
+
// 4. Contains math operators (^, _, {, })
|
| 137 |
+
|
| 138 |
+
const isMath = /[a-zA-Z\\^_{}]/.test(inner) ||
|
| 139 |
+
/^\d+$/.test(inner.trim()) ||
|
| 140 |
+
inner.includes('+') || inner.includes('-') || inner.includes('*') || inner.includes('/');
|
| 141 |
+
|
| 142 |
+
if (isMath) {
|
| 143 |
+
mathBlocks.push(match);
|
| 144 |
+
return `__MATH_BLOCK_${mathBlocks.length - 1}__`;
|
| 145 |
+
}
|
| 146 |
+
return match; // Leave non-math dollar signs alone
|
| 147 |
});
|
| 148 |
|
| 149 |
// Step 2: Now escape standalone dollar signs (currency)
|
| 150 |
+
// Match $ followed by digits with commas/periods (like $100, $1,234.56)
|
| 151 |
+
// But NOT single digits which are likely math
|
| 152 |
+
processed = processed.replace(/\$(\d{2,}[\d,]*\.?\d*|\d+,\d+\.?\d*)/g, '\\$$1');
|
| 153 |
|
| 154 |
// Step 3: Restore protected math blocks
|
| 155 |
mathBlocks.forEach((block, index) => {
|