File size: 10,318 Bytes
bb4c532
c6261d7
3fb001d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a64ef1f
 
 
 
 
3fb001d
 
 
a64ef1f
 
3fb001d
 
 
 
 
 
 
 
 
a64ef1f
 
3fb001d
a64ef1f
 
 
 
 
 
 
 
3fb001d
a64ef1f
 
 
 
 
 
 
 
 
 
 
3fb001d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c532
3fb001d
 
 
 
 
bb4c532
3fb001d
bb4c532
3fb001d
 
 
 
 
 
 
 
 
 
 
 
bb4c532
 
 
c6261d7
bb4c532
 
 
 
 
 
 
 
 
 
 
 
 
3c241e3
 
bb4c532
3c241e3
 
 
 
c6261d7
 
 
 
 
 
 
 
 
 
 
3c241e3
 
bb4c532
3c241e3
 
 
 
be53625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c241e3
 
 
 
bb4c532
 
 
 
3c241e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6261d7
 
a64ef1f
bb4c532
be53625
 
bb4c532
a64ef1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb4c532
a64ef1f
 
bb4c532
c6261d7
 
 
a64ef1f
bb4c532
c6261d7
 
a64ef1f
 
 
 
 
c6261d7
a64ef1f
 
c6261d7
 
 
a64ef1f
 
3c241e3
a64ef1f
c6261d7
 
 
a64ef1f
 
 
 
 
 
 
 
 
 
 
 
c6261d7
 
 
 
 
 
 
a64ef1f
 
 
 
 
 
 
c6261d7
 
 
 
3fb001d
 
a64ef1f
 
 
 
 
 
 
3fb001d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a16832
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { Fragment, memo, useRef, useEffect } from 'react'

// ── Table detection ──────────────────────────────────────────────
// Require 3+ pipes (2+ columns) so |x| (absolute value) never matches
const TABLE_LINE = /^\|.+\|.+\|$/
const SEPARATOR_ROW = /^\|[-| :]+\|$/
// Multiline anchors: only match whole lines starting/ending with |, not mid-line |x|
const BLOCK_SPLIT = /((?:^\|.+\|.+\|[ \t]*$\n?)+)/gm

function isTableBlock(text) {
  const lines = text.trim().split('\n').map(l => l.trim())
  // A real table MUST have a separator row (e.g. |---|---|)
  const hasSeparator = lines.some(l => SEPARATOR_ROW.test(l))
  if (!hasSeparator) return false
  // And at least one data row with 2+ columns
  return lines.some(l => TABLE_LINE.test(l) && !SEPARATOR_ROW.test(l))
}

function isEscapedPipeAt(str, i) {
  return str[i] === '\\' && i + 1 < str.length && str[i + 1] === '|'
}

function tokenizeRowCells(inner) {
  const cells = []
  let current = ''
  for (let i = 0; i < inner.length; i++) {
    if (isEscapedPipeAt(inner, i)) {
      current += '|'
      i++
    } else if (inner[i] === '|') {
      cells.push(current.trim())
      current = ''
    } else {
      current += inner[i]
    }
  }
  cells.push(current.trim())
  return cells
}

function mergeExcessCells(cells, expectedCols) {
  const extraCount = cells.length - expectedCols
  const merged = []
  for (let i = 0; i < cells.length; i++) {
    if (i > 0 && i <= extraCount) {
      merged[merged.length - 1] += ' | ' + cells[i]
    } else {
      merged.push(cells[i])
    }
  }
  return merged
}

/** Split a row on unescaped | characters, respecting \| as a literal pipe. */
function splitTableCells(row, expectedCols) {
  const inner = row.slice(1, -1) // strip leading/trailing |
  const cells = tokenizeRowCells(inner)

  if (expectedCols && cells.length > expectedCols) {
    return mergeExcessCells(cells, expectedCols)
  }

  return cells
}

function parseTable(text) {
  const allRows = text.trim().split('\n').map(l => l.trim()).filter(l => TABLE_LINE.test(l))

  // Detect column count from the separator row (e.g. |---|---|)
  const sepRow = allRows.find(r => SEPARATOR_ROW.test(r))
  const expectedCols = sepRow ? sepRow.slice(1, -1).split('|').length : null

  const dataRows = allRows.filter(l => !SEPARATOR_ROW.test(l))

  return (
    <table className="math-table">
      <tbody>
        {dataRows.map((row, i) => {
          const cells = splitTableCells(row, expectedCols)
          const Tag = i === 0 ? 'th' : 'td'
          return (
            <tr key={i} className={i % 2 === 0 ? 'math-table-row-alt' : ''}>
              {cells.map((cell, j) => (
                <Tag key={j} className="math-table-cell">
                  {cell}
                </Tag>
              ))}
            </tr>
          )
        })}
      </tbody>
    </table>
  )
}

// ── Math patterns ────────────────────────────────────────────────
const MATH_FUNCTIONS_STR = "sqrt|frac|sum|int|log|ln|sin|cos|tan|sec|csc|cot|arcsin|arccos|arctan|sinh|cosh|tanh|exp|lim|max|min|det";
const GREEK_LETTERS_STR = "alpha|beta|gamma|theta|Theta|pi|infty|Omega";

const MATH_PATTERN = /(\$\$[\s\S]+?\$\$|\$[^$]+\$|\\\([\s\S]+?\\\)|\\\[[\s\S]+?\\\])/g
const LEGACY_MATH_PATTERN = new RegExp(
  `(\\\\(?:${MATH_FUNCTIONS_STR}|${GREEK_LETTERS_STR})(?:[\\^_]\\s*(?:\\{[^{}]*\\}|[A-Za-z0-9]+))*(?:\\s*(?:\\{[^{}]*\\}|\\([^()]*\\)|[A-Za-z0-9]+))*|` +
  `\\\\?(?:Theta|Omega)\\s*\\([^()]+\\)|` +
  `\\bO\\s*\\([^()]+\\)|` +
  `\\b(?:${MATH_FUNCTIONS_STR})\\s*(?:[\\^_]\\s*(?:\\{[^{}]*\\}|[A-Za-z0-9]+))*\\s*\\([^()]+\\)|` +
  `\\b[A-Za-z0-9]+\\s*\\^\\s*(?:\\{[^{}]+\\}|\\([^()]+\\)|[A-Za-z0-9]+)(?:\\s*[-+]\\s*\\d+)?|` +
  `\\b(?:[A-Za-z]\\s+)?(?:log|ln)(?:\\s*\\^\\s*(?:\\{[^{}]+\\}|\\([^()]+\\)|[A-Za-z0-9]+))?\\s+[A-Za-z]\\b)`,
  "g"
);
const RAW_LATEX_HINT = new RegExp(
  `\\\\(${MATH_FUNCTIONS_STR}|${GREEK_LETTERS_STR})|\\\\?(Theta|Omega)\\s*\\(|\\bO\\s*\\(|[\\^_]\\{?[\\w\\d]+|\\b(?:[A-Za-z]\\s+)?(?:log|ln)(?:\\s*\\^\\s*[\\w\\d]+)?\\s+[A-Za-z]\\b`
);

const MATH_WORDS = new Set([
  'O', 'o', 'n', 'h', 'm', 'k', 'x', 'y', 'z',
  'log', 'ln', 'sin', 'cos', 'tan', 'sec', 'csc', 'cot', 'arcsin', 'arccos', 'arctan', 'sinh', 'cosh', 'tanh', 'exp', 'lim', 'max', 'min', 'det',
  'sqrt', 'frac', 'sum', 'int',
  'Theta', 'theta', 'Omega',
  'alpha', 'beta', 'gamma', 'pi', 'infty',
])

function normalizeMath(math) {
  return math
    .replace(/^\\\(/, '')
    .replace(/\\\)$/, '')
    .replace(/^\\\[/, '')
    .replace(/\\\]$/, '')
    .replace(/^\$\$/, '')
    .replace(/\$\$$/, '')
    .replace(/^\$/, '')
    .replace(/\$$/, '')
    .replace(/(^|[^\\])\blog~/g, '$1\\log ')
    .replace(/\^\(([^()]+)\)/g, '^{$1}')
    .replace(/(^|[^\\])\b(log|ln|sin|cos|tan|sec|csc|cot|arcsin|arccos|arctan|sinh|cosh|tanh|exp|lim|max|min|det)\b/g, '$1\\$2')
    .replace(/(^|[^\\])\bTheta\b/g, '$1\\Theta')
    .replace(/(^|[^\\])\bOmega\b/g, '$1\\Omega')
}

function mathAriaLabel(math) {
  const label = normalizeMath(math)
    .replace(/\\frac/g, ' fraction ')
    .replace(/\\sqrt/g, ' square root ')
    .replace(/\\sum/g, ' summation ')
    .replace(/\\int/g, ' integral ')
    .replace(/\\log/g, ' log ')
    .replace(/\\ln/g, ' natural log ')
    .replace(/\\sin/g, ' sine ')
    .replace(/\\cos/g, ' cosine ')
    .replace(/\\tan/g, ' tangent ')
    .replace(/\\Theta/g, ' Theta ')
    .replace(/\\Omega/g, ' Omega ')
    .replace(/\\alpha/g, ' alpha ')
    .replace(/\\beta/g, ' beta ')
    .replace(/\\gamma/g, ' gamma ')
    .replace(/\\theta/g, ' theta ')
    .replace(/\\pi/g, ' pi ')
    .replace(/\\infty/g, ' infinity ')
    .replace(/\^/g, ' to the power of ')
    .replace(/_/g, ' subscript ')
    .replace(/\\/g, ' ')
    .replace(/[{}]/g, ' ')
    .replace(/\s+/g, ' ')
    .trim()

  return `Math expression: ${label}`
}

function isStandaloneMath(text) {
  const trimmed = text.trim()
  if (!RAW_LATEX_HINT.test(trimmed)) return false

  // Strip LaTeX commands (e.g., \to, \infty, \text) so their names don't trigger the plain-text word check
  const textWithoutCommands = trimmed.replace(/\\[A-Za-z]+/g, '')
  
  const words = textWithoutCommands.match(/[A-Za-z]+/g) || []
  if (words.some(word => word.length > 1 && !MATH_WORDS.has(word))) {
    return false
  }

  return true
}

function splitLegacyMath(text) {
  const parts = []
  let lastIndex = 0

  for (const match of text.matchAll(LEGACY_MATH_PATTERN)) {
    const value = match[0]
    const index = match.index ?? 0

    if (index > lastIndex) {
      parts.push({ type: 'text', value: text.slice(lastIndex, index) })
    }

    parts.push({ type: 'math', value })
    lastIndex = index + value.length
  }

  if (lastIndex < text.length) {
    parts.push({ type: 'text', value: text.slice(lastIndex) })
  }

  return parts
}

function useKatexRender(math, displayMode) {
  const containerRef = useRef(null)
  const ariaLabel = mathAriaLabel(math)

  useEffect(() => {
    if (!containerRef.current) return
    Promise.all([import('katex'), import('katex/dist/katex.min.css')])
      .then(([katexModule]) => {
        if (containerRef.current) {
          katexModule.default.render(normalizeMath(math), containerRef.current, {
            displayMode,
            throwOnError: false,
            errorColor: '#cc0000',
          })
        }
      })
      .catch(err => {
        console.error("KaTeX load/render error:", err, "Original:", math)
        if (containerRef.current) containerRef.current.innerText = math
      })
  }, [math, displayMode])

  return { containerRef, ariaLabel }
}

function SafeInlineMath({ math }) {
  const { containerRef, ariaLabel } = useKatexRender(math, false)
  return <span ref={containerRef} role="math" aria-label={ariaLabel} className="inline-math" />
}

function SafeBlockMath({ math }) {
  const { containerRef, ariaLabel } = useKatexRender(math, true)
  return <div ref={containerRef} role="math" aria-label={ariaLabel} className="block-math my-4 flex justify-center" />
}

function getMathPartKind(part) {
  if (part.startsWith('$$') || part.startsWith('\\[')) return 'block'
  if (part.startsWith('$') || part.startsWith('\\(')) return 'inline'
  return 'text'
}

function renderLegacyMathText(text, keyPrefix) {
  if (isStandaloneMath(text)) {
    return <SafeInlineMath math={text} />
  }

  const legacyParts = splitLegacyMath(text)
  const hasLegacyMath = legacyParts.some(part => part.type === 'math')

  if (!hasLegacyMath) {
    return <>{text}</>
  }

  return (
    <>
      {legacyParts.map((part, index) =>
        part.type === 'math'
          ? <SafeInlineMath key={`${keyPrefix}l${index}`} math={part.value} />
          : <Fragment key={`${keyPrefix}l${index}`}>{part.value}</Fragment>
      )}
    </>
  )
}

function renderExplicitMathParts(text, keyPrefix) {
  const parts = text.split(MATH_PATTERN)

  return (
    <>
      {parts.map((part, index) => {
        if (!part) return null

        switch (getMathPartKind(part)) {
          case 'block':
            return <SafeBlockMath key={`${keyPrefix}m${index}`} math={part} />
          case 'inline':
            return <SafeInlineMath key={`${keyPrefix}m${index}`} math={part} />
          default:
            return <Fragment key={`${keyPrefix}m${index}`}>{part}</Fragment>
        }
      })}
    </>
  )
}

function renderMathSegment(text, keyPrefix = '') {
  const hasExplicitMath = text.search(MATH_PATTERN) !== -1
  return hasExplicitMath
    ? renderExplicitMathParts(text, keyPrefix)
    : renderLegacyMathText(text, keyPrefix)
}

const MathText = memo(function MathText({ children }) {
  const text = String(children ?? '')

  if (!text.trim()) return null

  // Split on table blocks first, then parse math in non-table segments
  const segments = text.split(BLOCK_SPLIT)

  const hasTable = segments.some(seg => isTableBlock(seg))

  if (!hasTable) {
    return renderMathSegment(text)
  }

  return (
    <>
      {segments.map((seg, i) =>
        isTableBlock(seg)
          ? <Fragment key={i}>{parseTable(seg)}</Fragment>
          : <Fragment key={i}>{renderMathSegment(seg, `s${i}`)}</Fragment>
      )}
    </>
  )
})

export default MathText;