Chat settings > Render math (off by default). When it is on, \( \), \[ \], $x$ and $$ $$ in an answer are drawn as formulas by KaTeX 0.19.0, which is vendored in serve/web/vendor/katex (script, style, 20 woff2 fonts, MIT licence and a PROVENANCE.json with the npm integrity and the sha256 of every file, so the page needs no network). Nothing of KaTeX loads until an answer holds a formula. The shared core (math.js, from #2972) keeps the text of every message exactly as the model wrote it: formulas are swapped for placeholders before the Markdown runs and drawn over them afterwards, so history, copying, the Markdown export and the requests never see the drawing. Code, link targets, prices ($5 and $10) and a formula still being streamed stay text. A formula that fails, is over 16 KiB or times out stays as its source text. Safety: KaTeX runs with trust off (no \href, \url, \includegraphics, \htmlClass: they are drawn as their own name), maxExpand 1000, maxSize 20 and a fresh macro table per formula; it builds DOM nodes itself, so no HTML string from a model reaches the page. A formula already drawn is placed again at once while an answer streams, so it does not flicker back to source. serve/test_web_math.js runs the real vendored KaTeX against hostile formulas; serve/test_web_math.py checks the vendored files against PROVENANCE.json and that nothing outside this server is addressed. serve/web/vendor is -text in .gitattributes so the hashes hold on a Windows checkout. Ported by hand from PRs #2972 (core) and #2974 (KaTeX) by CC-David-CC. Co-authored-by: David <336302908+CC-David-CC@users.noreply.github.com>
35 lines
2.0 KiB
JavaScript
35 lines
2.0 KiB
JavaScript
// serve/web/math-adapter.js - the KaTeX renderer for math.js (PR #2974 by CC-David-CC). KaTeX 0.19.0 is vendored in
|
|
// serve/web/vendor/katex (MIT, see PROVENANCE.json there): no network, no CDN. Nothing is loaded until "Render math" is
|
|
// switched on and an answer holds a formula.
|
|
//
|
|
// What a formula may do: nothing but draw. `trust: false` turns off \href, \url, \includegraphics and \htmlClass-style
|
|
// commands, so a formula cannot make a link, load a picture or add an attribute; the macros are not shared between
|
|
// formulas; \def loops are cut by maxExpand and a huge \rule or \kern by maxSize. KaTeX builds DOM nodes itself (no
|
|
// innerHTML), the result is cloned into the page by math.js, and an error returns the source text instead.
|
|
"use strict";
|
|
(function (root) {
|
|
const OPTIONS = {output: "htmlAndMathml", trust: false, throwOnError: true, strict: "ignore", maxExpand: 1000, maxSize: 20};
|
|
const adapter = {
|
|
OPTIONS,
|
|
async initialize() {
|
|
if (root.katex && document.querySelector("link[data-katex-ready]")) return;
|
|
const css = document.createElement("link"); css.rel = "stylesheet";
|
|
css.href = "web/vendor/katex/dist/katex.min.css";
|
|
const loaded = new Promise((resolve, reject) => { css.onload = resolve; css.onerror = reject; });
|
|
document.head.append(css);
|
|
await Promise.all([loaded, root.katex ? Promise.resolve() : root.StrataMath.script("web/vendor/katex/dist/katex.min.js")]);
|
|
css.dataset.katexReady = "1";
|
|
},
|
|
async render(source, {display, signal}) {
|
|
if (signal.aborted) return {ok: false, reason: "unavailable"};
|
|
const element = document.createElement("span");
|
|
try {
|
|
root.katex.render(source, element, {...OPTIONS, displayMode: display, macros: {}});
|
|
return {ok: true, element: element.firstElementChild};
|
|
} catch (_) { return {ok: false, reason: "invalid"}; }
|
|
},
|
|
};
|
|
root.StrataMathAdapter = adapter;
|
|
if (typeof module !== "undefined" && module.exports) module.exports = adapter;
|
|
})(typeof globalThis !== "undefined" ? globalThis : this);
|