fraudTech commited on
Commit
3a78ee1
·
1 Parent(s): 20d0c91

fix LaTex confusing with currency

Browse files
Files changed (1) hide show
  1. 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 ($...$) that contain LaTeX commands or are clearly math
129
- // Match $ followed by LaTeX commands like \frac, \sqrt, \times, or math-like content
130
- processed = processed.replace(/\$([^$\n]*(?:\\[a-zA-Z]+|[a-zA-Z_{}^]+)[^$\n]*)\$/g, (match) => {
131
- mathBlocks.push(match);
132
- return `__MATH_BLOCK_${mathBlocks.length - 1}__`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  });
134
 
135
  // Step 2: Now escape standalone dollar signs (currency)
136
- // Match $ followed by digits and optional commas/periods (like $100 or $1,234.56)
137
- processed = processed.replace(/\$(\d[\d,]*\.?\d*)/g, '\\$$1');
 
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) => {