SPB Git forge

spb/uqo-imm1033

Public
5commits 1branches 0releases
30.2 MBsize
maindefault branch
1 h agolast push
JavaScript 68% CSS 32%
1.9 KB · 61 lines javascript
Raw Blame History
1// render_math.mjs — rend en lot une liste de formules avec KaTeX.2// stdin : JSON [{mode:"inline"|"display", tex:"..."}] → stdout : JSON [{html, error}]3import katex from "katex";45const macros = {6  "\\up": "^{\\text{#1}}",7  "\\ier": "^{\\text{er}}",8  "\\iere": "^{\\text{re}}",9  "\\ieme": "^{\\text{e}}",10  "\\textonehalf": "\\tfrac{1}{2}",11  "\\textsuperscript": "^{\\text{#1}}",12  "\\oe": "\\text{œ}",13  "\\uqoalert": "\\textcolor{B22A20}{\\mathbf{#1}}",14  "\\uqotitle": "\\textcolor{003E7E}{\\mathbf{#1}}",15  "\\uqogreen": "\\textcolor{107C4E}{\\mathbf{#1}}",16  "\\uqohighlight": "\\textcolor{9E7D1C}{\\mathbf{#1}}",17  "\\emph": "\\textit{#1}",18  "\\textdegree": "^{\\circ}",19  "\\pgfmathprintnumber": "#1",20  "\\euro": "\\text{€}",21  "\\Ratio": "\\text{Ratio}",22  "\\RNE": "\\text{RNE}",23  "\\TGA": "\\text{TGA}",24};2526let input = "";27process.stdin.setEncoding("utf8");28process.stdin.on("data", (d) => (input += d));29process.stdin.on("end", () => {30  const items = JSON.parse(input);31  const out = items.map(({ mode, tex }) => {32    try {33      const html = katex.renderToString(tex, {34        displayMode: mode === "display",35        throwOnError: true,36        strict: "ignore",37        trust: false,38        macros: { ...macros },39        output: "htmlAndMathml",40        fleqn: false,41      });42      return { html, error: null };43    } catch (e) {44      // seconde tentative tolérante45      try {46        const html = katex.renderToString(tex, {47          displayMode: mode === "display",48          throwOnError: false,49          strict: "ignore",50          macros: { ...macros },51          output: "htmlAndMathml",52        });53        return { html, error: String(e.message || e) };54      } catch (e2) {55        return { html: `<code class="math-error">${tex.replace(/</g, "&lt;")}</code>`, error: String(e2.message || e2) };56      }57    }58  });59  process.stdout.write(JSON.stringify(out));60});61