fraudTech commited on
Commit
2aff861
·
1 Parent(s): 9f248c5

revert LaTex rendering

Browse files
Files changed (2) hide show
  1. web/src/App.tsx +3 -14
  2. web/src/math-preprocessing.ts +15 -273
web/src/App.tsx CHANGED
@@ -791,7 +791,7 @@ function App() {
791
  [remarkMath, { singleDollarTextMath: true }],
792
  remarkGfm
793
  ]}
794
- rehypePlugins={[[rehypeKatex, { throwOnError: false, strict: false }]]}
795
  urlTransform={allowDataImageUrl}
796
  components={{
797
  code({ className, children, ...props }: any) {
@@ -829,7 +829,7 @@ function App() {
829
  [remarkMath, { singleDollarTextMath: true }],
830
  remarkGfm
831
  ]}
832
- rehypePlugins={[[rehypeKatex, { throwOnError: false, strict: false }]]}
833
  urlTransform={allowDataImageUrl}
834
  components={{
835
  code({ className, children, ...props }: any) {
@@ -904,7 +904,7 @@ function App() {
904
  [remarkMath, { singleDollarTextMath: true }],
905
  remarkGfm
906
  ]}
907
- rehypePlugins={[[rehypeKatex, { throwOnError: false, strict: false }]]}
908
  urlTransform={allowDataImageUrl}
909
  components={{
910
  code({ className, children, ...props }: any) {
@@ -1030,7 +1030,6 @@ function App() {
1030
  Matrices
1031
  </button>
1032
  </div>
1033
-
1034
  <div className="symbol-content">
1035
  {activeSymbolCategory === 'greek' && (
1036
  <div className="symbol-category">
@@ -1060,7 +1059,6 @@ function App() {
1060
  </div>
1061
  </div>
1062
  )}
1063
-
1064
  {activeSymbolCategory === 'operators' && (
1065
  <div className="symbol-category">
1066
  <div className="symbol-row">
@@ -1081,7 +1079,6 @@ function App() {
1081
  </div>
1082
  </div>
1083
  )}
1084
-
1085
  {activeSymbolCategory === 'relations' && (
1086
  <div className="symbol-category">
1087
  <div className="symbol-row">
@@ -1099,7 +1096,6 @@ function App() {
1099
  </div>
1100
  </div>
1101
  )}
1102
-
1103
  {activeSymbolCategory === 'sets' && (
1104
  <div className="symbol-category">
1105
  <div className="symbol-row">
@@ -1116,7 +1112,6 @@ function App() {
1116
  </div>
1117
  </div>
1118
  )}
1119
-
1120
  {activeSymbolCategory === 'logic' && (
1121
  <div className="symbol-category">
1122
  <div className="symbol-row">
@@ -1133,7 +1128,6 @@ function App() {
1133
  </div>
1134
  </div>
1135
  )}
1136
-
1137
  {activeSymbolCategory === 'arrows' && (
1138
  <div className="symbol-category">
1139
  <div className="symbol-row">
@@ -1150,7 +1144,6 @@ function App() {
1150
  </div>
1151
  </div>
1152
  )}
1153
-
1154
  {activeSymbolCategory === 'special' && (
1155
  <div className="symbol-category">
1156
  <div className="symbol-row">
@@ -1173,7 +1166,6 @@ function App() {
1173
  </div>
1174
  </div>
1175
  )}
1176
-
1177
  {activeSymbolCategory === 'calculus' && (
1178
  <div className="symbol-category">
1179
  <div className="symbol-row">
@@ -1188,7 +1180,6 @@ function App() {
1188
  </div>
1189
  </div>
1190
  )}
1191
-
1192
  {activeSymbolCategory === 'linear' && (
1193
  <div className="symbol-category">
1194
  <div className="symbol-row">
@@ -1203,7 +1194,6 @@ function App() {
1203
  </div>
1204
  </div>
1205
  )}
1206
-
1207
  {activeSymbolCategory === 'equations' && (
1208
  <div className="symbol-category">
1209
  <div className="symbol-row">
@@ -1221,7 +1211,6 @@ function App() {
1221
  </div>
1222
  </div>
1223
  )}
1224
-
1225
  {activeSymbolCategory === 'matrices' && (
1226
  <div className="symbol-category">
1227
  <div className="symbol-row">
 
791
  [remarkMath, { singleDollarTextMath: true }],
792
  remarkGfm
793
  ]}
794
+ rehypePlugins={[rehypeKatex]}
795
  urlTransform={allowDataImageUrl}
796
  components={{
797
  code({ className, children, ...props }: any) {
 
829
  [remarkMath, { singleDollarTextMath: true }],
830
  remarkGfm
831
  ]}
832
+ rehypePlugins={[rehypeKatex]}
833
  urlTransform={allowDataImageUrl}
834
  components={{
835
  code({ className, children, ...props }: any) {
 
904
  [remarkMath, { singleDollarTextMath: true }],
905
  remarkGfm
906
  ]}
907
+ rehypePlugins={[rehypeKatex]}
908
  urlTransform={allowDataImageUrl}
909
  components={{
910
  code({ className, children, ...props }: any) {
 
1030
  Matrices
1031
  </button>
1032
  </div>
 
1033
  <div className="symbol-content">
1034
  {activeSymbolCategory === 'greek' && (
1035
  <div className="symbol-category">
 
1059
  </div>
1060
  </div>
1061
  )}
 
1062
  {activeSymbolCategory === 'operators' && (
1063
  <div className="symbol-category">
1064
  <div className="symbol-row">
 
1079
  </div>
1080
  </div>
1081
  )}
 
1082
  {activeSymbolCategory === 'relations' && (
1083
  <div className="symbol-category">
1084
  <div className="symbol-row">
 
1096
  </div>
1097
  </div>
1098
  )}
 
1099
  {activeSymbolCategory === 'sets' && (
1100
  <div className="symbol-category">
1101
  <div className="symbol-row">
 
1112
  </div>
1113
  </div>
1114
  )}
 
1115
  {activeSymbolCategory === 'logic' && (
1116
  <div className="symbol-category">
1117
  <div className="symbol-row">
 
1128
  </div>
1129
  </div>
1130
  )}
 
1131
  {activeSymbolCategory === 'arrows' && (
1132
  <div className="symbol-category">
1133
  <div className="symbol-row">
 
1144
  </div>
1145
  </div>
1146
  )}
 
1147
  {activeSymbolCategory === 'special' && (
1148
  <div className="symbol-category">
1149
  <div className="symbol-row">
 
1166
  </div>
1167
  </div>
1168
  )}
 
1169
  {activeSymbolCategory === 'calculus' && (
1170
  <div className="symbol-category">
1171
  <div className="symbol-row">
 
1180
  </div>
1181
  </div>
1182
  )}
 
1183
  {activeSymbolCategory === 'linear' && (
1184
  <div className="symbol-category">
1185
  <div className="symbol-row">
 
1194
  </div>
1195
  </div>
1196
  )}
 
1197
  {activeSymbolCategory === 'equations' && (
1198
  <div className="symbol-category">
1199
  <div className="symbol-row">
 
1211
  </div>
1212
  </div>
1213
  )}
 
1214
  {activeSymbolCategory === 'matrices' && (
1215
  <div className="symbol-category">
1216
  <div className="symbol-row">
web/src/math-preprocessing.ts CHANGED
@@ -15,9 +15,6 @@ export function preprocessMathContent(content: string): string {
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 $$...$$
@@ -26,12 +23,15 @@ export function preprocessMathContent(content: string): string {
26
  .replace(/\\\)/g, '$')
27
  .replace(/\\\[/g, '\n$$')
28
  .replace(/\\\]/g, '$$\n');
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('$$$')) {
@@ -45,34 +45,19 @@ export function preprocessMathContent(content: string): string {
45
  processed = processed.replace(/\\left(?![(\[{|])/g, '');
46
  processed = processed.replace(/\\right(?![)\]}|])/g, '');
47
 
 
 
 
 
 
48
  // Step 6: Ensure display math has proper newlines
49
  processed = processed.replace(/\$\$([\s\S]*?)\$\$/g, (_match, math) => {
50
  const trimmed = math.trim();
51
  if (trimmed.includes('\n')) {
52
  return `$$\n${trimmed}\n$$\n`;
53
  }
54
- return `$$\n${trimmed}\n$$\n`;
55
  });
56
-
57
- // Step 6.2: Promote standalone inline math lines to display blocks
58
- processed = processed
59
- .split('\n')
60
- .map(line => {
61
- const trimmed = line.trim();
62
- const standaloneInline = /^\$([^$\n]+)\$$/.exec(trimmed);
63
- if (!standaloneInline) return line;
64
- return `$$\n${standaloneInline[1].trim()}\n$$`;
65
- })
66
- .join('\n');
67
-
68
- // Step 6.5: Ensure $$ delimiters are on their own lines
69
- // Insert newline before $$ if it shares a line with other text
70
- processed = processed.replace(/([^\n])\$\$/g, '$1\n$$');
71
- // Insert newline after $$ if it shares a line with other text
72
- processed = processed.replace(/\$\$([^\n])/g, '$$\n$1');
73
-
74
- // Step 7: Escape unmatched $ delimiters to avoid KaTeX parsing large spans
75
- processed = escapeUnmatchedDollarDelimiters(processed);
76
 
77
  // Step 7: Clean up multiple newlines
78
  processed = processed.replace(/\n{3,}/g, '\n\n');
@@ -80,249 +65,6 @@ export function preprocessMathContent(content: string): string {
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
-
165
- // Split into existing math blocks vs non-math segments
166
- const segments: Array<{ isMath: boolean; content: string }> = [];
167
- let lastIndex = 0;
168
- const mathBlockRegex = /(\$\$[\s\S]*?\$\$|\$[^\$\n]+?\$)/g;
169
- let match;
170
-
171
- while ((match = mathBlockRegex.exec(content)) !== null) {
172
- if (match.index > lastIndex) {
173
- segments.push({ isMath: false, content: content.substring(lastIndex, match.index) });
174
- }
175
- segments.push({ isMath: true, content: match[0] });
176
- lastIndex = match.index + match[0].length;
177
- }
178
-
179
- if (lastIndex < content.length) {
180
- segments.push({ isMath: false, content: content.substring(lastIndex) });
181
- }
182
-
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 => {
191
- if (seg.isMath) return seg.content;
192
-
193
- // Remove LaTeX line-breaks and stray single backslashes at end of line in plain text
194
- let cleaned = seg.content
195
- .replace(/\\\s*\n/g, '\n')
196
- .replace(/(^|[^\\])\\\s*$/gm, '$1')
197
- .replace(/\\\./g, '.');
198
-
199
- // Wrap contiguous LaTeX command sequences
200
- cleaned = cleaned.replace(latexHeavyPattern, (value) => {
201
- const commandCount = (value.match(/\\[a-zA-Z]+/g) || []).length;
202
- if (commandCount === 0) return value;
203
- if (value.trim().startsWith('$') || value.trim().endsWith('$')) return value;
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();
217
- if (trimmed.startsWith('$') || trimmed.endsWith('$')) return value;
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];
232
- const after = full[offset + value.length];
233
- if (before === '$' || after === '$') return value;
234
- return `$${value}$`;
235
- });
236
-
237
- return cleaned;
238
- })
239
- .join('');
240
- }
241
-
242
- function escapeUnmatchedDollarDelimiters(content: string): string {
243
- let result = '';
244
- let i = 0;
245
- let inCodeFence = false;
246
- let inDisplayMath = false;
247
-
248
- const hasClosingDisplay = (startIndex: number): boolean => {
249
- return content.indexOf('$$', startIndex) !== -1;
250
- };
251
-
252
- const findInlineClosing = (startIndex: number): boolean => {
253
- for (let j = startIndex; j < content.length; j += 1) {
254
- const char = content[j];
255
- if (char === '\n') return false;
256
- if (char === '\\') {
257
- j += 1;
258
- continue;
259
- }
260
- if (char === '$') {
261
- if (content[j + 1] === '$') return false;
262
- return true;
263
- }
264
- }
265
- return false;
266
- };
267
-
268
- while (i < content.length) {
269
- if (content.startsWith('```', i)) {
270
- inCodeFence = !inCodeFence;
271
- result += '```';
272
- i += 3;
273
- continue;
274
- }
275
-
276
- if (inCodeFence) {
277
- result += content[i];
278
- i += 1;
279
- continue;
280
- }
281
-
282
- if (content.startsWith('\\$', i)) {
283
- result += '\\$';
284
- i += 2;
285
- continue;
286
- }
287
-
288
- if (content.startsWith('$$', i)) {
289
- if (inDisplayMath) {
290
- inDisplayMath = false;
291
- result += '$$';
292
- i += 2;
293
- continue;
294
- }
295
-
296
- if (hasClosingDisplay(i + 2)) {
297
- inDisplayMath = true;
298
- result += '$$';
299
- i += 2;
300
- continue;
301
- }
302
-
303
- result += '\\$\\$';
304
- i += 2;
305
- continue;
306
- }
307
-
308
- if (content[i] === '$' && !inDisplayMath) {
309
- if (findInlineClosing(i + 1)) {
310
- result += '$';
311
- i += 1;
312
- continue;
313
- }
314
- result += '\\$';
315
- i += 1;
316
- continue;
317
- }
318
-
319
- result += content[i];
320
- i += 1;
321
- }
322
-
323
- return result;
324
- }
325
-
326
  /**
327
  * Extract math expressions from content for validation/preview
328
  */
 
15
  if (!content) return content;
16
 
17
  let processed = content;
 
 
 
18
 
19
  // Step 1: Normalize delimiters (MUST BE FIRST)
20
  // Convert \(...\) to $...$ and \[...\] to $$...$$
 
23
  .replace(/\\\)/g, '$')
24
  .replace(/\\\[/g, '\n$$')
25
  .replace(/\\\]/g, '$$\n');
26
+
27
+ // Step 2: Remove nested inline math delimiters inside display math
28
+ // Pattern: $$...$ ... $...$$ → $$... ... ...$$
29
+ // Must handle both single-line and multi-line display math
30
+ processed = processed.replace(/\$\$([\s\S]*?)\$\$/g, (_match, content) => {
31
+ // Remove ALL $ signs inside display math (they're always wrong)
32
+ const cleaned = content.replace(/\$/g, '');
33
+ return `$$${cleaned}$$`;
34
+ });
35
 
36
  // Step 3: Clean up delimiter stacking ($$$$, $$$, etc.)
37
  while (processed.includes('$$$')) {
 
45
  processed = processed.replace(/\\left(?![(\[{|])/g, '');
46
  processed = processed.replace(/\\right(?![)\]}|])/g, '');
47
 
48
+ // Step 5: Fix list markers contaminating delimiters
49
+ processed = processed.replace(/(\d+)\$\$/g, '$1. $$');
50
+ processed = processed.replace(/\$\$(\d+)\$/g, '$$ $1.');
51
+ processed = processed.replace(/\$(\d+)\$/g, '$ $1. $');
52
+
53
  // Step 6: Ensure display math has proper newlines
54
  processed = processed.replace(/\$\$([\s\S]*?)\$\$/g, (_match, math) => {
55
  const trimmed = math.trim();
56
  if (trimmed.includes('\n')) {
57
  return `$$\n${trimmed}\n$$\n`;
58
  }
59
+ return `$$${trimmed}$$\n`;
60
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  // Step 7: Clean up multiple newlines
63
  processed = processed.replace(/\n{3,}/g, '\n\n');
 
65
  return processed;
66
  }
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  /**
69
  * Extract math expressions from content for validation/preview
70
  */