// render_math.mjs — rend en lot une liste de formules avec KaTeX. // stdin : JSON [{mode:"inline"|"display", tex:"..."}] → stdout : JSON [{html, error}] import katex from "katex"; const macros = { "\\up": "^{\\text{#1}}", "\\ier": "^{\\text{er}}", "\\iere": "^{\\text{re}}", "\\ieme": "^{\\text{e}}", "\\textonehalf": "\\tfrac{1}{2}", "\\textsuperscript": "^{\\text{#1}}", "\\oe": "\\text{œ}", "\\uqoalert": "\\textcolor{B22A20}{\\mathbf{#1}}", "\\uqotitle": "\\textcolor{003E7E}{\\mathbf{#1}}", "\\uqogreen": "\\textcolor{107C4E}{\\mathbf{#1}}", "\\uqohighlight": "\\textcolor{9E7D1C}{\\mathbf{#1}}", "\\emph": "\\textit{#1}", "\\textdegree": "^{\\circ}", "\\pgfmathprintnumber": "#1", "\\euro": "\\text{€}", "\\Ratio": "\\text{Ratio}", "\\RNE": "\\text{RNE}", "\\TGA": "\\text{TGA}", }; let input = ""; process.stdin.setEncoding("utf8"); process.stdin.on("data", (d) => (input += d)); process.stdin.on("end", () => { const items = JSON.parse(input); const out = items.map(({ mode, tex }) => { try { const html = katex.renderToString(tex, { displayMode: mode === "display", throwOnError: true, strict: "ignore", trust: false, macros: { ...macros }, output: "htmlAndMathml", fleqn: false, }); return { html, error: null }; } catch (e) { // seconde tentative tolérante try { const html = katex.renderToString(tex, { displayMode: mode === "display", throwOnError: false, strict: "ignore", macros: { ...macros }, output: "htmlAndMathml", }); return { html, error: String(e.message || e) }; } catch (e2) { return { html: `${tex.replace(/`, error: String(e2.message || e2) }; } } }); process.stdout.write(JSON.stringify(out)); });