fraudTech commited on
Commit
e05d051
·
1 Parent(s): 3e0876f

further fix latex rendering

Browse files
Files changed (1) hide show
  1. web/src/App.tsx +31 -41
web/src/App.tsx CHANGED
@@ -118,81 +118,71 @@ function preprocessMathContent(content: string): string {
118
 
119
  let processed = content;
120
 
121
- // Step 1: Protect code blocks from processing
 
 
 
122
  const codeBlocks: string[] = [];
123
  processed = processed.replace(/```[\s\S]*?```/g, (match) => {
124
  codeBlocks.push(match);
125
  return `___CODE_BLOCK_${codeBlocks.length - 1}___`;
126
  });
127
 
128
- // Step 2: Protect inline code spans
129
  const inlineCode: string[] = [];
130
  processed = processed.replace(/`[^`]+`/g, (match) => {
131
  inlineCode.push(match);
132
  return `___INLINE_CODE_${inlineCode.length - 1}___`;
133
  });
134
 
135
- // Step 3: Protect already-wrapped math ($$...$$ and $...$)
 
 
 
 
 
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 LaTeX environments (equation, cases, matrix, etc.)
143
- processed = processed.replace(/\\begin\{(equation|align|cases|matrix|pmatrix|bmatrix|array|split)\}[\s\S]*?\\end\{\1\}/g, (match) => {
144
  return `$$${match}$$`;
145
  });
146
 
147
- // Step 5: Wrap standalone fractions
148
- processed = processed.replace(/\\(?:frac|tfrac|dfrac|cfrac)\{[^}]*\}\{[^}]*\}/g, (match) => `$${match}$`);
149
-
150
- // Step 6: Wrap LaTeX symbols and commands
151
- // Greek letters, symbols, operators
152
- processed = processed.replace(/\\(?:alpha|beta|gamma|delta|epsilon|theta|lambda|mu|pi|sigma|tau|phi|psi|omega|Delta|Gamma|Lambda|Omega|Phi|Psi|Sigma|Theta|infty|partial|nabla|cdot|times|div|pm|mp|leq|geq|neq|approx|equiv|sum|prod|int|oint|lim|to|rightarrow|leftarrow|Rightarrow|Leftarrow|forall|exists|in|subset|subseteq|cup|cap|emptyset|mathbb|mathbf|mathrm|text)\b/g, (match) => `$${match}$`);
153
 
154
- // Step 7: Wrap expressions with superscripts/subscripts that include braces
155
- processed = processed.replace(/([a-zA-Z0-9])\^?\{[^}]+\}/g, (match) => {
156
- // Don't wrap if already in a LaTeX command
157
- return `$${match}$`;
158
  });
159
 
160
- // Step 8: Wrap simple superscripts and subscripts
161
- processed = processed.replace(/\b([a-zA-Z])(\^|_)(\{[^}]+\}|[a-zA-Z0-9])/g, (match) => `$${match}$`);
162
 
163
- // Step 9: Wrap parentheses containing LaTeX
164
- processed = processed.replace(/\([^()]*\\[a-zA-Z]+[^()]*\)/g, (match) => {
165
- // Check if this looks like a math expression
166
- if (match.includes('\\')) {
167
- return `$${match}$`;
168
- }
169
- return match;
170
- });
171
 
172
- // Step 10: Wrap bracket notation with LaTeX
173
- processed = processed.replace(/\[[^\]]*\\[a-zA-Z]+[^\]]*\]/g, (match) => `$${match}$`);
174
 
175
- // Step 11: Wrap sequences of LaTeX commands with operators
176
- // Matches patterns like: \frac{d}{dx}(x^2) + \frac{d}{dx}(1)
177
- processed = processed.replace(/(?<!\\)(?:^|[\s(])((?:\\[a-zA-Z]+(?:\{[^}]*\})*|[a-zA-Z0-9]+[\^_](?:\{[^}]*\}|[a-zA-Z0-9]))(?:\s*[\+\-\*/=]\s*(?:\\[a-zA-Z]+(?:\{[^}]*\})*|[a-zA-Z0-9]+[\^_](?:\{[^}]*\}|[a-zA-Z0-9])))+)/g, (match, latex) => {
178
- return match.replace(latex, `$${latex}$`);
179
- });
180
-
181
- // Step 12: Clean up currency (protect $5.00 from becoming math)
182
  processed = processed.replace(/\$(\d+(?:\.\d{2})?)\$/g, '\\$$1');
183
 
 
 
 
 
184
  // Step 13: Restore protected content
185
  processed = processed.replace(/___EXISTING_MATH_(\d+)___/g, (_, index) => existingMath[parseInt(index)]);
186
  processed = processed.replace(/___CODE_BLOCK_(\d+)___/g, (_, index) => codeBlocks[parseInt(index)]);
187
  processed = processed.replace(/___INLINE_CODE_(\d+)___/g, (_, index) => inlineCode[parseInt(index)]);
188
 
189
- // Step 14: Clean up double-wrapping issues
190
- // Remove duplicate dollar signs (but preserve $$...$$ for display math)
191
- processed = processed.replace(/\$\s*\$\s*\$/g, '$');
192
- processed = processed.replace(/\$\$\$+/g, '$$');
193
-
194
- // Fix nested dollar signs like $...$...$
195
- processed = processed.replace(/\$([^$]*)\$\s*\$([^$]*)\$/g, '$$$1 $2$$');
196
 
197
  return processed;
198
  }
 
118
 
119
  let processed = content;
120
 
121
+ // Debug log to see what we're processing
122
+ console.log('Original content:', content.substring(0, 200));
123
+
124
+ // Step 1: Protect code blocks
125
  const codeBlocks: string[] = [];
126
  processed = processed.replace(/```[\s\S]*?```/g, (match) => {
127
  codeBlocks.push(match);
128
  return `___CODE_BLOCK_${codeBlocks.length - 1}___`;
129
  });
130
 
131
+ // Step 2: Protect inline code
132
  const inlineCode: string[] = [];
133
  processed = processed.replace(/`[^`]+`/g, (match) => {
134
  inlineCode.push(match);
135
  return `___INLINE_CODE_${inlineCode.length - 1}___`;
136
  });
137
 
138
+ // Step 3: Fix malformed delimiter patterns FIRST
139
+ // $[ ... ]$ -> $$...$$ (display math)
140
+ processed = processed.replace(/\$\[\s*/g, '$$');
141
+ processed = processed.replace(/\s*\]\$/g, '$$');
142
+
143
+ // Step 4: Protect properly formed math delimiters
144
  const existingMath: string[] = [];
145
  processed = processed.replace(/\$\$[\s\S]+?\$\$|\$[^\$\n]+?\$/g, (match) => {
146
  existingMath.push(match);
147
  return `___EXISTING_MATH_${existingMath.length - 1}___`;
148
  });
149
 
150
+ // Step 5: Wrap LaTeX environments
151
+ processed = processed.replace(/\\begin\{(equation|align|cases|matrix|pmatrix|bmatrix|array|split|gather)\}[\s\S]*?\\end\{\1\}/g, (match) => {
152
  return `$$${match}$$`;
153
  });
154
 
155
+ // Step 6: Wrap complete fractions (both args present)
156
+ processed = processed.replace(/\\(?:frac|tfrac|dfrac|cfrac)\s*\{[^}]*\}\s*\{[^}]*\}/g, (match) => `$${match}$`);
 
 
 
 
157
 
158
+ // Step 7: Wrap incomplete fractions like \frac{d} or \frac{d}dx by fixing them
159
+ // This handles cases where spacing breaks the pattern
160
+ processed = processed.replace(/\\frac\s*\{([^}]*)\}\s*([a-zA-Z]+)/g, (match, num, den) => {
161
+ return `$\\frac{${num}}{${den}}$`;
162
  });
163
 
164
+ // Step 8: Wrap standalone LaTeX commands
165
+ processed = processed.replace(/\\(alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|kappa|lambda|mu|nu|xi|pi|rho|sigma|tau|upsilon|phi|chi|psi|omega|Gamma|Delta|Theta|Lambda|Xi|Pi|Sigma|Upsilon|Phi|Psi|Omega|infty|partial|nabla|cdot|times|div|pm|mp|leq|geq|neq|approx|equiv|sum|prod|int|oint|lim|sqrt|sin|cos|tan|ln|log|exp|to|rightarrow|leftarrow|Rightarrow|Leftarrow|forall|exists|mathbb|mathbf|mathrm|text)\b/g, (match) => `$${match}$`);
166
 
167
+ // Step 9: Wrap superscripts and subscripts
168
+ processed = processed.replace(/([a-zA-Z0-9]+)([\^_])(\{[^}]+\}|[a-zA-Z0-9]+)/g, (match) => `$${match}$`);
 
 
 
 
 
 
169
 
170
+ // Step 10: Wrap parentheses/brackets with backslash commands
171
+ processed = processed.replace(/[\(\[]([^()\[\]]*\\[a-zA-Z]+[^()\[\]]*)[\)\]]/g, (match) => `$${match}$`);
172
 
173
+ // Step 11: Clean up currency
 
 
 
 
 
 
174
  processed = processed.replace(/\$(\d+(?:\.\d{2})?)\$/g, '\\$$1');
175
 
176
+ // Step 12: Merge adjacent math delimiters
177
+ processed = processed.replace(/\$\s*\$/g, ' ');
178
+ processed = processed.replace(/\$([^$]+)\$\s*\$([^$]+)\$/g, '$$$1 $2$$');
179
+
180
  // Step 13: Restore protected content
181
  processed = processed.replace(/___EXISTING_MATH_(\d+)___/g, (_, index) => existingMath[parseInt(index)]);
182
  processed = processed.replace(/___CODE_BLOCK_(\d+)___/g, (_, index) => codeBlocks[parseInt(index)]);
183
  processed = processed.replace(/___INLINE_CODE_(\d+)___/g, (_, index) => inlineCode[parseInt(index)]);
184
 
185
+ console.log('Processed content:', processed.substring(0, 200));
 
 
 
 
 
 
186
 
187
  return processed;
188
  }