fraudTech commited on
Commit
40355e6
·
1 Parent(s): 6810a36

Fix preprocessMath to protect Latex

Browse files
Files changed (1) hide show
  1. web/src/App.tsx +30 -5
web/src/App.tsx CHANGED
@@ -111,12 +111,37 @@ const LOGIC_SAYINGS = [
111
  "Logic enforces rationality",
112
  ];
113
 
114
- // Preprocess content to escape literal dollar signs that aren't part of LaTeX
115
  function preprocessMath(content: string): string {
116
- // Escape dollar signs that are clearly currency (followed by digits)
117
- // but leave actual LaTeX math delimiters ($...$) alone
118
- content = content.replace(/\$(\d)/g, '\\$$1');
119
- return content;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  }
121
 
122
  // Eye-health optimized warm syntax highlighting theme
 
111
  "Logic enforces rationality",
112
  ];
113
 
114
+ // Preprocess content to handle LaTeX and currency symbols correctly
115
  function preprocessMath(content: string): string {
116
+ // Strategy: Protect LaTeX math blocks first, then escape remaining currency
117
+
118
+ // Step 1: Extract and protect LaTeX blocks (both inline $ $ and display $$ $$)
119
+ const mathBlocks: string[] = [];
120
+ let processed = content;
121
+
122
+ // Protect display math blocks ($$...$$)
123
+ processed = processed.replace(/\$\$([\s\S]*?)\$\$/g, (match) => {
124
+ mathBlocks.push(match);
125
+ return `__MATH_BLOCK_${mathBlocks.length - 1}__`;
126
+ });
127
+
128
+ // Protect inline math blocks ($...$) that contain LaTeX commands or are clearly math
129
+ // Match $ followed by LaTeX commands like \frac, \sqrt, \times, or math-like content
130
+ processed = processed.replace(/\$([^$\n]*(?:\\[a-zA-Z]+|[a-zA-Z_{}^]+)[^$\n]*)\$/g, (match) => {
131
+ mathBlocks.push(match);
132
+ return `__MATH_BLOCK_${mathBlocks.length - 1}__`;
133
+ });
134
+
135
+ // Step 2: Now escape standalone dollar signs (currency)
136
+ // Match $ followed by digits and optional commas/periods (like $100 or $1,234.56)
137
+ processed = processed.replace(/\$(\d[\d,]*\.?\d*)/g, '\\$$1');
138
+
139
+ // Step 3: Restore protected math blocks
140
+ mathBlocks.forEach((block, index) => {
141
+ processed = processed.replace(`__MATH_BLOCK_${index}__`, block);
142
+ });
143
+
144
+ return processed;
145
  }
146
 
147
  // Eye-health optimized warm syntax highlighting theme