SPB Git

Repository Public

latex.ts 1.2 KB 23 lines typescript

1import katex from "katex";2import { stripMathDelims } from "./mathtext";34/** The only KaTeX configuration used anywhere: no \href/\url/\includegraphics (trust: false), bounded macro expansion. */5export const KATEX_OPTIONS = { trust: false, strict: "ignore", maxExpand: 1000, output: "htmlAndMathml" } as const;67/**8 * LaTeX → KaTeX HTML (with MathML for assistive technology), or "" when KaTeX rejects the formula — the caller then9 * shows a readable text form instead of a red source dump. Outer $…$ / \\(…\\) delimiters are tolerated.10 */11export function latexToHtml(latex: string, displayMode: boolean): string {12  try { return katex.renderToString(stripMathDelims(latex), { ...KATEX_OPTIONS, displayMode, throwOnError: true }); }13  catch { return ""; }14}1516/** Syntax check used by the quality report. Pure. */17export function checkLatex(latex: string): { ok: true } | { ok: false; error: string } {18  latex = stripMathDelims(latex);19  if (latex.trim() === "") return { ok: false, error: "empty formula" };20  try { katex.renderToString(latex, { ...KATEX_OPTIONS, displayMode: true, throwOnError: true }); return { ok: true }; }21  catch (e) { return { ok: false, error: e instanceof Error ? e.message.replace(/^KaTeX parse error:\s*/, "") : String(e) }; }22}23
Files main
Menu

Theme