'use client' import type { InlineMathDelimiter } from '@admin/utils/editor/inline-math-delimiter' import { findUnescapedCharacter } from '@admin/utils/editor/find-unescaped-character' import { findUnescapedSequence } from '@admin/utils/editor/find-unescaped-sequence' import { isValidDollarInlineMath } from '@admin/utils/editor/is-valid-dollar-inline-math' interface InlineMathMatch { raw: string text: string delimiter: InlineMathDelimiter } export function matchInlineMath(src: string): InlineMathMatch | null { if (src.startsWith('\\(')) { const closingIndex = findUnescapedSequence(src, '\\)', 2) if (closingIndex === -1) return null return { raw: src.slice(0, closingIndex + 2), text: src.slice(2, closingIndex), delimiter: '\\(' } } if (!src.startsWith('$') || src.startsWith('$$')) return null if (src[1] && /\s/u.test(src[1])) return null let closingIndex = 1 while (closingIndex < src.length) { closingIndex = findUnescapedCharacter(src, '$', closingIndex) if (closingIndex === -1) return null const text = src.slice(1, closingIndex) if (isValidDollarInlineMath(src, text, closingIndex)) { return { raw: src.slice(0, closingIndex + 1), text, delimiter: '$' } } closingIndex++ } return null }