|
| 1 | +export function stripMathDelimiters(raw: string): string { |
| 2 | + let value = raw.trim(); |
| 3 | + let changed = true; |
| 4 | + |
| 5 | + while (changed) { |
| 6 | + changed = false; |
| 7 | + const pairs: Array<[string, string]> = [ |
| 8 | + ["$$", "$$"], |
| 9 | + ["\\[", "\\]"], |
| 10 | + ["\\(", "\\)"], |
| 11 | + ["$", "$"], |
| 12 | + ]; |
| 13 | + |
| 14 | + for (const [open, close] of pairs) { |
| 15 | + if ( |
| 16 | + value.startsWith(open) && |
| 17 | + value.endsWith(close) && |
| 18 | + value.length >= open.length + close.length |
| 19 | + ) { |
| 20 | + value = value.slice(open.length, value.length - close.length).trim(); |
| 21 | + changed = true; |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return value; |
| 27 | +} |
| 28 | + |
| 29 | +function replaceLatexCommandWithGroup( |
| 30 | + value: string, |
| 31 | + command: string, |
| 32 | + replacement: (inner: string) => string, |
| 33 | +): string { |
| 34 | + let result = value; |
| 35 | + let index = result.indexOf(command); |
| 36 | + |
| 37 | + while (index !== -1) { |
| 38 | + const openIndex = result.indexOf("{", index + command.length); |
| 39 | + if (openIndex === -1) break; |
| 40 | + |
| 41 | + let depth = 0; |
| 42 | + let closeIndex = -1; |
| 43 | + for (let cursor = openIndex; cursor < result.length; cursor += 1) { |
| 44 | + if (result[cursor] === "{") depth += 1; |
| 45 | + if (result[cursor] === "}") { |
| 46 | + depth -= 1; |
| 47 | + if (depth === 0) { |
| 48 | + closeIndex = cursor; |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (closeIndex === -1) break; |
| 55 | + |
| 56 | + const inner = result.slice(openIndex + 1, closeIndex); |
| 57 | + result = `${result.slice(0, index)}${replacement(inner)}${result.slice(closeIndex + 1)}`; |
| 58 | + index = result.indexOf(command, index + 1); |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | +} |
| 63 | + |
| 64 | +function replaceLatexFractions(value: string): string { |
| 65 | + let result = value; |
| 66 | + let index = result.indexOf("\\frac"); |
| 67 | + |
| 68 | + while (index !== -1) { |
| 69 | + const numeratorOpen = result.indexOf("{", index + "\\frac".length); |
| 70 | + if (numeratorOpen === -1) break; |
| 71 | + |
| 72 | + const numeratorClose = findMatchingBrace(result, numeratorOpen); |
| 73 | + if (numeratorClose === -1) break; |
| 74 | + |
| 75 | + const denominatorOpen = result.indexOf("{", numeratorClose + 1); |
| 76 | + if (denominatorOpen === -1) break; |
| 77 | + |
| 78 | + const denominatorClose = findMatchingBrace(result, denominatorOpen); |
| 79 | + if (denominatorClose === -1) break; |
| 80 | + |
| 81 | + const numerator = result.slice(numeratorOpen + 1, numeratorClose); |
| 82 | + const denominator = result.slice(denominatorOpen + 1, denominatorClose); |
| 83 | + result = `${result.slice(0, index)}((${numerator})/(${denominator}))${result.slice(denominatorClose + 1)}`; |
| 84 | + index = result.indexOf("\\frac", index + 1); |
| 85 | + } |
| 86 | + |
| 87 | + return result; |
| 88 | +} |
| 89 | + |
| 90 | +function findMatchingBrace(value: string, openIndex: number): number { |
| 91 | + let depth = 0; |
| 92 | + for (let index = openIndex; index < value.length; index += 1) { |
| 93 | + if (value[index] === "{") depth += 1; |
| 94 | + if (value[index] === "}") { |
| 95 | + depth -= 1; |
| 96 | + if (depth === 0) return index; |
| 97 | + } |
| 98 | + } |
| 99 | + return -1; |
| 100 | +} |
| 101 | + |
| 102 | +export function normalizeMathInputForEvaluation(raw: string): string { |
| 103 | + let value = stripMathDelimiters(raw) |
| 104 | + .trim() |
| 105 | + .toLowerCase() |
| 106 | + .replace(/\\left/g, "") |
| 107 | + .replace(/\\right/g, "") |
| 108 | + .replace(/\\,/g, "") |
| 109 | + .replace(/[,]/g, ",") |
| 110 | + .replace(/[−–—]/g, "-") |
| 111 | + .replace(/[×·]/g, "*") |
| 112 | + .replace(/[÷]/g, "/") |
| 113 | + .replace(/\\cdot|\\times/g, "*") |
| 114 | + .replace(/\\div/g, "/") |
| 115 | + .replace(/\\pi|π/g, "pi") |
| 116 | + .replace(/\*\*/g, "^"); |
| 117 | + |
| 118 | + value = replaceLatexFractions(value); |
| 119 | + value = replaceLatexCommandWithGroup(value, "\\sqrt", (inner) => `sqrt(${inner})`); |
| 120 | + value = value.replace(/sqrt\s*\{([^{}]+)\}/g, "sqrt($1)"); |
| 121 | + value = value.replace(/\^\s*\{([^{}]+)\}/g, "^($1)"); |
| 122 | + value = value.replace(/[{}]/g, ""); |
| 123 | + |
| 124 | + return value; |
| 125 | +} |
| 126 | + |
| 127 | +export function mathInputToTex(raw: string): string { |
| 128 | + let value = stripMathDelimiters(raw).trim(); |
| 129 | + |
| 130 | + if (!value) { |
| 131 | + return ""; |
| 132 | + } |
| 133 | + |
| 134 | + value = value |
| 135 | + .replace(/\\left/g, "") |
| 136 | + .replace(/\\right/g, "") |
| 137 | + .replace(/π/g, "\\pi") |
| 138 | + .replace(/\bpi\b/gi, "\\pi") |
| 139 | + .replace(/\*/g, "\\cdot "); |
| 140 | + |
| 141 | + value = value.replace(/(^|[^\\])\bsqrt\s*\(([^()]+)\)/gi, "$1\\sqrt{$2}"); |
| 142 | + value = value.replace(/(^|[^\\])\bsqrt\s*\{([^{}]+)\}/gi, "$1\\sqrt{$2}"); |
| 143 | + value = value.replace(/\^\s*\(([^()]+)\)/g, "^{$1}"); |
| 144 | + value = value.replace(/\^\s*([^\s+\-*/^]+)/g, "^{$1}"); |
| 145 | + |
| 146 | + return value; |
| 147 | +} |
0 commit comments