| 1 | import katex from 'katex' |
| 2 | |
| 3 | export interface FormulaRenderResult { |
| 4 | html: string |
| 5 | error: string | null |
| 6 | } |
| 7 | |
| 8 | export function renderFormulaToHtml(latex: string, displayMode: boolean): FormulaRenderResult { |
| 9 | const source = latex.trim() |
| 10 | if (!source) return { html: '', error: null } |
| 11 | try { |
| 12 | return { |
| 13 | html: katex.renderToString(source, { |
| 14 | displayMode, |
| 15 | throwOnError: true, |
| 16 | strict: false, |
| 17 | trust: false, |
| 18 | output: 'htmlAndMathml' |
| 19 | }), |
| 20 | error: null |
| 21 | } |
| 22 | } catch (error) { |
| 23 | return { |
| 24 | html: '', |
| 25 | error: error instanceof Error ? error.message : 'Invalid formula' |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 |