fraudTech commited on
Commit
a519a97
·
1 Parent(s): a6ba9f6

fix LaTex

Browse files
Files changed (1) hide show
  1. web/src/App.tsx +48 -3
web/src/App.tsx CHANGED
@@ -132,14 +132,59 @@ function preprocessMathContent(content: string): string {
132
  return `___INLINE_CODE_${inlineCode.length - 1}___`;
133
  });
134
 
135
- // Step 3: Protect currency amounts from being parsed as math
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  // Replace $number with \$number (unless already escaped)
137
- processed = processed.replace(/(?<!\\)\$(\d+(?:\.\d{2})?)/g, '\\$$1');
138
 
139
- // Step 4: Restore code blocks and inline code
 
140
  processed = processed.replace(/___CODE_BLOCK_(\d+)___/g, (_, index) => codeBlocks[parseInt(index)]);
141
  processed = processed.replace(/___INLINE_CODE_(\d+)___/g, (_, index) => inlineCode[parseInt(index)]);
142
 
 
 
 
 
143
  return processed;
144
  }
145
 
 
132
  return `___INLINE_CODE_${inlineCode.length - 1}___`;
133
  });
134
 
135
+ // Step 3: Protect existing math delimiters (already wrapped in $ or $$)
136
+ const existingMath: string[] = [];
137
+ processed = processed.replace(/\$\$[\s\S]+?\$\$|\$[^\$\n]+?\$/g, (match) => {
138
+ existingMath.push(match);
139
+ return `___EXISTING_MATH_${existingMath.length - 1}___`;
140
+ });
141
+
142
+ // Step 4: Auto-wrap common LaTeX patterns that aren't already in math mode
143
+ // Wrap display math environments
144
+ processed = processed.replace(/\\begin\{(equation|align|cases|matrix|pmatrix|bmatrix|array)\}[\s\S]*?\\end\{\1\}/g, (match) => {
145
+ return `$$${match}$$`;
146
+ });
147
+
148
+ // Wrap inline LaTeX commands - match common patterns
149
+ // Pattern: backslash followed by LaTeX command with optional arguments and content
150
+ processed = processed.replace(/\\(?:frac|tfrac|dfrac|cfrac)\{[^}]*\}\{[^}]*\}/g, (match) => `$${match}$`);
151
+ processed = processed.replace(/\\(?:sqrt|sum|prod|int|lim|sin|cos|tan|log|ln|exp)\b[^$\n]*/g, (match) => {
152
+ // Only wrap if it looks like a complete expression (ends with space, punctuation, or end of string)
153
+ if (/[\s,.);\n]$/.test(match) || match.length < 50) {
154
+ return `$${match.trim()}$`;
155
+ }
156
+ return match;
157
+ });
158
+
159
+ // Wrap sequences with multiple LaTeX commands (like \frac{d}{dx}(x^2))
160
+ processed = processed.replace(/(?:^|[^\$\\])(\\\w+(?:\{[^}]*\}|\([^)]*\))*(?:\s*[+\-*/=]\s*\\\w+(?:\{[^}]*\}|\([^)]*\))*)+)/g, (match, latex) => {
161
+ // Check if it's part of a larger math expression
162
+ if (latex && latex.length > 3) {
163
+ return match.replace(latex, `$${latex}$`);
164
+ }
165
+ return match;
166
+ });
167
+
168
+ // Wrap parenthetical expressions with LaTeX
169
+ processed = processed.replace(/\(\\[a-zA-Z]+[^)]*\)/g, (match) => `$${match}$`);
170
+
171
+ // Wrap simple patterns like x^2, x_i, etc when they appear to be math
172
+ processed = processed.replace(/\b([a-zA-Z])\^(\{[^}]+\}|\d+)/g, (match) => `$${match}$`);
173
+ processed = processed.replace(/\b([a-zA-Z])_(\{[^}]+\}|\d+)/g, (match) => `$${match}$`);
174
+
175
+ // Step 5: Protect currency amounts from being parsed as math
176
  // Replace $number with \$number (unless already escaped)
177
+ processed = processed.replace(/(?<!\\)\$(\d+(?:\.\d{2})?)\$/g, '\\$$1');
178
 
179
+ // Step 6: Restore protected content
180
+ processed = processed.replace(/___EXISTING_MATH_(\d+)___/g, (_, index) => existingMath[parseInt(index)]);
181
  processed = processed.replace(/___CODE_BLOCK_(\d+)___/g, (_, index) => codeBlocks[parseInt(index)]);
182
  processed = processed.replace(/___INLINE_CODE_(\d+)___/g, (_, index) => inlineCode[parseInt(index)]);
183
 
184
+ // Step 7: Clean up double-wrapped math ($$...$$)
185
+ processed = processed.replace(/\$\$\$\$/g, '$$$$');
186
+ processed = processed.replace(/\$\$([^$]+)\$\$/g, '$$$$$$');
187
+
188
  return processed;
189
  }
190