Spaces:
Sleeping
Sleeping
further improved latex inline rendering
Browse files- web/src/App.tsx +39 -30
web/src/App.tsx
CHANGED
|
@@ -118,7 +118,7 @@ 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);
|
|
@@ -132,58 +132,67 @@ function preprocessMathContent(content: string): string {
|
|
| 132 |
return `___INLINE_CODE_${inlineCode.length - 1}___`;
|
| 133 |
});
|
| 134 |
|
| 135 |
-
// Step 3: Protect
|
| 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
|
| 143 |
-
//
|
| 144 |
-
processed = processed.replace(/\\begin\{(equation|align|cases|matrix|pmatrix|bmatrix|array)\}[\s\S]*?\\end\{\1\}/g, (match) => {
|
| 145 |
return `$$${match}$$`;
|
| 146 |
});
|
| 147 |
|
| 148 |
-
//
|
| 149 |
-
// Pattern: backslash followed by LaTeX command with optional arguments and content
|
| 150 |
processed = processed.replace(/\\(?:frac|tfrac|dfrac|cfrac)\{[^}]*\}\{[^}]*\}/g, (match) => `$${match}$`);
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
});
|
| 158 |
|
| 159 |
-
//
|
| 160 |
-
processed = processed.replace(/(
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
}
|
| 165 |
return match;
|
| 166 |
});
|
| 167 |
|
| 168 |
-
// Wrap
|
| 169 |
-
processed = processed.replace(/\
|
| 170 |
|
| 171 |
-
//
|
| 172 |
-
|
| 173 |
-
processed = processed.replace(/\
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
// Step
|
| 176 |
-
|
| 177 |
-
processed = processed.replace(/(?<!\\)\$(\d+(?:\.\d{2})?)\$/g, '\\$$1');
|
| 178 |
|
| 179 |
-
// Step
|
| 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
|
| 185 |
-
|
| 186 |
-
processed = processed.replace(/\$\
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
return processed;
|
| 189 |
}
|
|
|
|
| 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);
|
|
|
|
| 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 |
}
|