fraudTech commited on
Commit
8858730
·
1 Parent(s): e5f49d0

fix: LaTeX rendering for currency dollar signs inside math spans

Browse files
Files changed (1) hide show
  1. src/App.js +44 -1
src/App.js CHANGED
@@ -70,6 +70,48 @@ const LOGIC_SAYINGS = [
70
  // ---------------------------------------------------------------------------
71
  // HELPERS
72
  // ---------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  function normalizeLatex(text) {
74
  if (!text) return "";
75
  let n = text;
@@ -85,6 +127,7 @@ function normalizeLatex(text) {
85
  n = n.replace(/\\end\{gather\*?\}/g, "\\end{gathered}");
86
  n = n.replace(/\\begin\{eqnarray\*?\}/g, "\\begin{aligned}");
87
  n = n.replace(/\\end\{eqnarray\*?\}/g, "\\end{aligned}");
 
88
  n = n.replace(/\$\$([^$]+?)\$\$/gs, (match, inner) => {
89
  const t = inner.trim();
90
  const hasAlign = /[^\\]&/.test(t) || t.startsWith("&");
@@ -156,7 +199,7 @@ function Message({ message }) {
156
  <ReactMarkdown
157
  className="markdown-body"
158
  remarkPlugins={[remarkMath, remarkGfm]}
159
- rehypePlugins={[[rehypeKatex, { strict: "ignore", throwOnError: false, trust: true, errorColor: "#e2e8f0" }]]}
160
  skipHtml={false}
161
  breaks
162
  >
 
70
  // ---------------------------------------------------------------------------
71
  // HELPERS
72
  // ---------------------------------------------------------------------------
73
+ function fixCurrencyInMath(text) {
74
+ let result = '';
75
+ let i = 0;
76
+ const len = text.length;
77
+ while (i < len) {
78
+ // Display math: $$...$$
79
+ if (text[i] === '$' && i + 1 < len && text[i + 1] === '$' &&
80
+ (i === 0 || text[i - 1] !== '\\')) {
81
+ let j = i + 2, close = -1;
82
+ while (j < len - 1) {
83
+ if (text[j] === '$' && text[j + 1] === '$' && text[j - 1] !== '\\') { close = j; break; }
84
+ j++;
85
+ }
86
+ if (close !== -1) {
87
+ let c = text.slice(i + 2, close).replace(/\\\$/g, '\\dollar{}');
88
+ result += '$$' + c + '$$';
89
+ i = close + 2;
90
+ continue;
91
+ }
92
+ }
93
+ // Inline math: $...$
94
+ if (text[i] === '$' && (i === 0 || text[i - 1] !== '\\') &&
95
+ (i + 1 >= len || text[i + 1] !== '$')) {
96
+ let j = i + 1, close = -1;
97
+ while (j < len) {
98
+ if (text[j] === '$' && text[j - 1] !== '\\' &&
99
+ (j + 1 >= len || text[j + 1] !== '$')) { close = j; break; }
100
+ j++;
101
+ }
102
+ if (close !== -1) {
103
+ let c = text.slice(i + 1, close).replace(/\\\$/g, '\\dollar{}');
104
+ result += '$' + c + '$';
105
+ i = close + 1;
106
+ continue;
107
+ }
108
+ }
109
+ result += text[i];
110
+ i++;
111
+ }
112
+ return result;
113
+ }
114
+
115
  function normalizeLatex(text) {
116
  if (!text) return "";
117
  let n = text;
 
127
  n = n.replace(/\\end\{gather\*?\}/g, "\\end{gathered}");
128
  n = n.replace(/\\begin\{eqnarray\*?\}/g, "\\begin{aligned}");
129
  n = n.replace(/\\end\{eqnarray\*?\}/g, "\\end{aligned}");
130
+ n = fixCurrencyInMath(n);
131
  n = n.replace(/\$\$([^$]+?)\$\$/gs, (match, inner) => {
132
  const t = inner.trim();
133
  const hasAlign = /[^\\]&/.test(t) || t.startsWith("&");
 
199
  <ReactMarkdown
200
  className="markdown-body"
201
  remarkPlugins={[remarkMath, remarkGfm]}
202
+ rehypePlugins={[[rehypeKatex, { strict: "ignore", throwOnError: false, trust: true, errorColor: "#e2e8f0", macros: { "\\dollar": "\\$" } }]]}
203
  skipHtml={false}
204
  breaks
205
  >