Spaces:
Sleeping
Sleeping
fix Latex
Browse files- web/src/math-preprocessing.ts +102 -0
web/src/math-preprocessing.ts
CHANGED
|
@@ -15,6 +15,9 @@ export function preprocessMathContent(content: string): string {
|
|
| 15 |
if (!content) return content;
|
| 16 |
|
| 17 |
let processed = content;
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
// Step 1: Normalize delimiters (MUST BE FIRST)
|
| 20 |
// Convert \(...\) to $...$ and \[...\] to $$...$$
|
|
@@ -26,6 +29,9 @@ export function preprocessMathContent(content: string): string {
|
|
| 26 |
|
| 27 |
// Step 1.5: Wrap bare LaTeX commands that appear outside math blocks
|
| 28 |
processed = wrapBareLatexOutsideMath(processed);
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
// Step 3: Clean up delimiter stacking ($$$$, $$$, etc.)
|
| 31 |
while (processed.includes('$$$')) {
|
|
@@ -74,6 +80,85 @@ export function preprocessMathContent(content: string): string {
|
|
| 74 |
return processed;
|
| 75 |
}
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
function wrapBareLatexOutsideMath(content: string): string {
|
| 78 |
if (!content) return content;
|
| 79 |
|
|
@@ -98,6 +183,8 @@ function wrapBareLatexOutsideMath(content: string): string {
|
|
| 98 |
const latexHeavyPattern = /(?:\\[a-zA-Z]+(?:\{[^}]*\}|[\^_]\{?[^\s}]*\}?)*[\s=+\-*/,]*){1,}/g;
|
| 99 |
const inlineLatexToken = /\\[a-zA-Z]+(?:\{[^}]*\})?(?:\([^\s$]*?\))?(?:\[[^\s$]*?\])?(?:_\{[^}]*\}|_\w+)?(?:\^\{[^}]*\}|\^\w+)?/g;
|
| 100 |
const latexFunctionPattern = /\\[a-zA-Z]+(?:\{[^}]*\})?\s*\([^\n]*?\)/g;
|
|
|
|
|
|
|
| 101 |
|
| 102 |
return segments
|
| 103 |
.map(seg => {
|
|
@@ -117,6 +204,13 @@ function wrapBareLatexOutsideMath(content: string): string {
|
|
| 117 |
return `$${value.trim()}$`;
|
| 118 |
});
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
// Wrap command + parenthesis groups (e.g., \text{Var}(X^2))
|
| 121 |
cleaned = cleaned.replace(latexFunctionPattern, (value) => {
|
| 122 |
const trimmed = value.trim();
|
|
@@ -124,6 +218,14 @@ function wrapBareLatexOutsideMath(content: string): string {
|
|
| 124 |
return `$${trimmed}$`;
|
| 125 |
});
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
// Wrap individual LaTeX tokens (e.g., \text{Var}(X^2), \lambda) that remain
|
| 128 |
cleaned = cleaned.replace(inlineLatexToken, (value, offset, full) => {
|
| 129 |
const before = full[offset - 1];
|
|
|
|
| 15 |
if (!content) return content;
|
| 16 |
|
| 17 |
let processed = content;
|
| 18 |
+
|
| 19 |
+
// Step 0: Normalize escaped dollar delimiters outside code fences
|
| 20 |
+
processed = normalizeEscapedDollarDelimiters(processed);
|
| 21 |
|
| 22 |
// Step 1: Normalize delimiters (MUST BE FIRST)
|
| 23 |
// Convert \(...\) to $...$ and \[...\] to $$...$$
|
|
|
|
| 29 |
|
| 30 |
// Step 1.5: Wrap bare LaTeX commands that appear outside math blocks
|
| 31 |
processed = wrapBareLatexOutsideMath(processed);
|
| 32 |
+
|
| 33 |
+
// Step 1.6: Wrap common plain-math patterns outside math blocks
|
| 34 |
+
processed = wrapPlainMathOutsideMath(processed);
|
| 35 |
|
| 36 |
// Step 3: Clean up delimiter stacking ($$$$, $$$, etc.)
|
| 37 |
while (processed.includes('$$$')) {
|
|
|
|
| 80 |
return processed;
|
| 81 |
}
|
| 82 |
|
| 83 |
+
function normalizeEscapedDollarDelimiters(content: string): string {
|
| 84 |
+
if (!content) return content;
|
| 85 |
+
|
| 86 |
+
let result = '';
|
| 87 |
+
let i = 0;
|
| 88 |
+
let inCodeFence = false;
|
| 89 |
+
|
| 90 |
+
while (i < content.length) {
|
| 91 |
+
if (content.startsWith('```', i)) {
|
| 92 |
+
inCodeFence = !inCodeFence;
|
| 93 |
+
result += '```';
|
| 94 |
+
i += 3;
|
| 95 |
+
continue;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
if (inCodeFence) {
|
| 99 |
+
result += content[i];
|
| 100 |
+
i += 1;
|
| 101 |
+
continue;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
if (content.startsWith('\\$', i)) {
|
| 105 |
+
result += '$';
|
| 106 |
+
i += 2;
|
| 107 |
+
continue;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
result += content[i];
|
| 111 |
+
i += 1;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
return result;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
function wrapPlainMathOutsideMath(content: string): string {
|
| 118 |
+
if (!content) return content;
|
| 119 |
+
|
| 120 |
+
const segments: Array<{ isMath: boolean; content: string }> = [];
|
| 121 |
+
let lastIndex = 0;
|
| 122 |
+
const mathBlockRegex = /(\$\$[\s\S]*?\$\$|\$[^\$\n]+?\$)/g;
|
| 123 |
+
let match;
|
| 124 |
+
|
| 125 |
+
while ((match = mathBlockRegex.exec(content)) !== null) {
|
| 126 |
+
if (match.index > lastIndex) {
|
| 127 |
+
segments.push({ isMath: false, content: content.substring(lastIndex, match.index) });
|
| 128 |
+
}
|
| 129 |
+
segments.push({ isMath: true, content: match[0] });
|
| 130 |
+
lastIndex = match.index + match[0].length;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
if (lastIndex < content.length) {
|
| 134 |
+
segments.push({ isMath: false, content: content.substring(lastIndex) });
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
const expectationPattern = /\bE\s*\[[^\]\n]+\]/g;
|
| 138 |
+
const variancePattern = /\b(?:Var|Cov|Pr|P)\s*\([^\)\n]+\)/g;
|
| 139 |
+
const powerPattern = /\b[A-Za-z]\s*\^\s*\d+/g;
|
| 140 |
+
const repeatedParenPattern = /\b[A-Za-z]\s*\([^\)\n]+\)(?:\s*\([^\)\n]+\))+/g;
|
| 141 |
+
const singleParenPattern = /\b[A-Za-z]\s*\([^\)\n]+\)/g;
|
| 142 |
+
|
| 143 |
+
const wrap = (value: string): string => `$${value.trim()}$`;
|
| 144 |
+
|
| 145 |
+
return segments
|
| 146 |
+
.map(seg => {
|
| 147 |
+
if (seg.isMath) return seg.content;
|
| 148 |
+
|
| 149 |
+
let cleaned = seg.content;
|
| 150 |
+
|
| 151 |
+
cleaned = cleaned.replace(expectationPattern, wrap);
|
| 152 |
+
cleaned = cleaned.replace(variancePattern, wrap);
|
| 153 |
+
cleaned = cleaned.replace(repeatedParenPattern, wrap);
|
| 154 |
+
cleaned = cleaned.replace(singleParenPattern, wrap);
|
| 155 |
+
cleaned = cleaned.replace(powerPattern, wrap);
|
| 156 |
+
|
| 157 |
+
return cleaned;
|
| 158 |
+
})
|
| 159 |
+
.join('');
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
function wrapBareLatexOutsideMath(content: string): string {
|
| 163 |
if (!content) return content;
|
| 164 |
|
|
|
|
| 183 |
const latexHeavyPattern = /(?:\\[a-zA-Z]+(?:\{[^}]*\}|[\^_]\{?[^\s}]*\}?)*[\s=+\-*/,]*){1,}/g;
|
| 184 |
const inlineLatexToken = /\\[a-zA-Z]+(?:\{[^}]*\})?(?:\([^\s$]*?\))?(?:\[[^\s$]*?\])?(?:_\{[^}]*\}|_\w+)?(?:\^\{[^}]*\}|\^\w+)?/g;
|
| 185 |
const latexFunctionPattern = /\\[a-zA-Z]+(?:\{[^}]*\})?\s*\([^\n]*?\)/g;
|
| 186 |
+
const latexCommandWithArgsPattern = /\\[a-zA-Z]+(?:\{[^}]*\})?\s*(?:\([^\n]*?\)|\[[^\n]*?\])/g;
|
| 187 |
+
const latexStandaloneCommandPattern = /\\[a-zA-Z]+(?:\{[^}]*\})+/g;
|
| 188 |
|
| 189 |
return segments
|
| 190 |
.map(seg => {
|
|
|
|
| 204 |
return `$${value.trim()}$`;
|
| 205 |
});
|
| 206 |
|
| 207 |
+
// Wrap command + parenthesis/bracket groups (e.g., \text{Var} (X^2), \mathbb{E}[X])
|
| 208 |
+
cleaned = cleaned.replace(latexCommandWithArgsPattern, (value) => {
|
| 209 |
+
const trimmed = value.trim();
|
| 210 |
+
if (trimmed.startsWith('$') || trimmed.endsWith('$')) return value;
|
| 211 |
+
return `$${trimmed.replace(/\s+/g, '')}$`;
|
| 212 |
+
});
|
| 213 |
+
|
| 214 |
// Wrap command + parenthesis groups (e.g., \text{Var}(X^2))
|
| 215 |
cleaned = cleaned.replace(latexFunctionPattern, (value) => {
|
| 216 |
const trimmed = value.trim();
|
|
|
|
| 218 |
return `$${trimmed}$`;
|
| 219 |
});
|
| 220 |
|
| 221 |
+
// Wrap standalone LaTeX commands with braces (e.g., \text{Var}, \mathbb{E})
|
| 222 |
+
cleaned = cleaned.replace(latexStandaloneCommandPattern, (value, offset, full) => {
|
| 223 |
+
const before = full[offset - 1];
|
| 224 |
+
const after = full[offset + value.length];
|
| 225 |
+
if (before === '$' || after === '$') return value;
|
| 226 |
+
return `$${value}$`;
|
| 227 |
+
});
|
| 228 |
+
|
| 229 |
// Wrap individual LaTeX tokens (e.g., \text{Var}(X^2), \lambda) that remain
|
| 230 |
cleaned = cleaned.replace(inlineLatexToken, (value, offset, full) => {
|
| 231 |
const before = full[offset - 1];
|