fraudTech commited on
Commit
9fd39af
·
1 Parent(s): ff567ff

Standalone inline math lines now render as boxed display equations

Browse files
Files changed (1) hide show
  1. web/src/math-preprocessing.ts +98 -14
web/src/math-preprocessing.ts CHANGED
@@ -24,15 +24,6 @@ export function preprocessMathContent(content: string): string {
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('$$$')) {
38
  processed = processed.replace(/\$\$\$\$+/g, '$$');
@@ -45,11 +36,6 @@ export function preprocessMathContent(content: string): string {
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();
@@ -59,11 +45,25 @@ export function preprocessMathContent(content: string): string {
59
  return `$$\n${trimmed}\n$$\n`;
60
  });
61
 
 
 
 
 
 
 
 
 
 
 
 
62
  // Step 6.5: Ensure $$ delimiters are on their own lines
63
  // Insert newline before $$ if it shares a line with other text
64
  processed = processed.replace(/([^\n])\$\$/g, '$1\n$$');
65
  // Insert newline after $$ if it shares a line with other text
66
  processed = processed.replace(/\$\$([^\n])/g, '$$\n$1');
 
 
 
67
 
68
  // Step 7: Clean up multiple newlines
69
  processed = processed.replace(/\n{3,}/g, '\n\n');
@@ -71,6 +71,90 @@ export function preprocessMathContent(content: string): string {
71
  return processed;
72
  }
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  /**
75
  * Extract math expressions from content for validation/preview
76
  */
 
24
  .replace(/\\\[/g, '\n$$')
25
  .replace(/\\\]/g, '$$\n');
26
 
 
 
 
 
 
 
 
 
 
27
  // Step 3: Clean up delimiter stacking ($$$$, $$$, etc.)
28
  while (processed.includes('$$$')) {
29
  processed = processed.replace(/\$\$\$\$+/g, '$$');
 
36
  processed = processed.replace(/\\left(?![(\[{|])/g, '');
37
  processed = processed.replace(/\\right(?![)\]}|])/g, '');
38
 
 
 
 
 
 
39
  // Step 6: Ensure display math has proper newlines
40
  processed = processed.replace(/\$\$([\s\S]*?)\$\$/g, (_match, math) => {
41
  const trimmed = math.trim();
 
45
  return `$$\n${trimmed}\n$$\n`;
46
  });
47
 
48
+ // Step 6.2: Promote standalone inline math lines to display blocks
49
+ processed = processed
50
+ .split('\n')
51
+ .map(line => {
52
+ const trimmed = line.trim();
53
+ const standaloneInline = /^\$([^$\n]+)\$$/.exec(trimmed);
54
+ if (!standaloneInline) return line;
55
+ return `$$\n${standaloneInline[1].trim()}\n$$`;
56
+ })
57
+ .join('\n');
58
+
59
  // Step 6.5: Ensure $$ delimiters are on their own lines
60
  // Insert newline before $$ if it shares a line with other text
61
  processed = processed.replace(/([^\n])\$\$/g, '$1\n$$');
62
  // Insert newline after $$ if it shares a line with other text
63
  processed = processed.replace(/\$\$([^\n])/g, '$$\n$1');
64
+
65
+ // Step 7: Escape unmatched $ delimiters to avoid KaTeX parsing large spans
66
+ processed = escapeUnmatchedDollarDelimiters(processed);
67
 
68
  // Step 7: Clean up multiple newlines
69
  processed = processed.replace(/\n{3,}/g, '\n\n');
 
71
  return processed;
72
  }
73
 
74
+ function escapeUnmatchedDollarDelimiters(content: string): string {
75
+ let result = '';
76
+ let i = 0;
77
+ let inCodeFence = false;
78
+ let inDisplayMath = false;
79
+
80
+ const hasClosingDisplay = (startIndex: number): boolean => {
81
+ return content.indexOf('$$', startIndex) !== -1;
82
+ };
83
+
84
+ const findInlineClosing = (startIndex: number): boolean => {
85
+ for (let j = startIndex; j < content.length; j += 1) {
86
+ const char = content[j];
87
+ if (char === '\n') return false;
88
+ if (char === '\\') {
89
+ j += 1;
90
+ continue;
91
+ }
92
+ if (char === '$') {
93
+ if (content[j + 1] === '$') return false;
94
+ return true;
95
+ }
96
+ }
97
+ return false;
98
+ };
99
+
100
+ while (i < content.length) {
101
+ if (content.startsWith('```', i)) {
102
+ inCodeFence = !inCodeFence;
103
+ result += '```';
104
+ i += 3;
105
+ continue;
106
+ }
107
+
108
+ if (inCodeFence) {
109
+ result += content[i];
110
+ i += 1;
111
+ continue;
112
+ }
113
+
114
+ if (content.startsWith('\\$', i)) {
115
+ result += '\\$';
116
+ i += 2;
117
+ continue;
118
+ }
119
+
120
+ if (content.startsWith('$$', i)) {
121
+ if (inDisplayMath) {
122
+ inDisplayMath = false;
123
+ result += '$$';
124
+ i += 2;
125
+ continue;
126
+ }
127
+
128
+ if (hasClosingDisplay(i + 2)) {
129
+ inDisplayMath = true;
130
+ result += '$$';
131
+ i += 2;
132
+ continue;
133
+ }
134
+
135
+ result += '\\$\\$';
136
+ i += 2;
137
+ continue;
138
+ }
139
+
140
+ if (content[i] === '$' && !inDisplayMath) {
141
+ if (findInlineClosing(i + 1)) {
142
+ result += '$';
143
+ i += 1;
144
+ continue;
145
+ }
146
+ result += '\\$';
147
+ i += 1;
148
+ continue;
149
+ }
150
+
151
+ result += content[i];
152
+ i += 1;
153
+ }
154
+
155
+ return result;
156
+ }
157
+
158
  /**
159
  * Extract math expressions from content for validation/preview
160
  */