Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor: avoid using the <math> and its corresponding stylesheet in formulas, which slows down the text editor

Adorian Doranc3cef8368a58
diff --git a/apps/client/src/services/math.spec.ts b/apps/client/src/services/math.spec.ts
new file mode 100644
index 0000000000..b1e7a96b3e
--- /dev/null
+++ b/apps/client/src/services/math.spec.ts
@@ -0,0 +1,40 @@
+import { describe, expect, it, vi } from "vitest";
+
+import math, { renderMathInElement } from "./math";
+
+// KaTeX checks for standards mode when it loads, and happy-dom has no `document.compatMode`.
+vi.hoisted(() => {
+ Object.defineProperty(document, "compatMode", { value: "CSS1Compat", configurable: true });
+});
+
+describe("math", () => {
+ it("renders formulas as HTML without MathML, labelled with their TeX", () => {
+ const container = document.createElement("div");
+ container.innerHTML = '<p><span class="math-tex">\\(x^2\\)</span> and '
+ + '<span class="math-tex">\\[\\frac{a}{b}\\]</span></p>';
+ const formula = document.createElement("span");
+ formula.className = "math-tex";
+ formula.textContent = "\\(z\\)";
+ const element = document.createElement("span");
+
+ renderMathInElement(container);
+ renderMathInElement(formula);
+ math.render("y_1", element, { displayMode: true });
+
+ for (const root of [ container, formula, element ]) {
+ expect(root.querySelector(".katex-html")).not.toBeNull();
+ expect(root.querySelector("math")).toBeNull();
+ }
+ expect(labels(container)).toEqual([ "img x^2", "img \\frac{a}{b}" ]);
+ expect(labels(formula)).toEqual([ "img z" ]);
+ expect(labels(element)).toEqual([ "img y_1" ]);
+
+ renderMathInElement(container);
+ expect(labels(container)).toEqual([ "img x^2", "img \\frac{a}{b}" ]);
+ });
+});
+
+function labels(root: Element) {
+ return [ ...root.querySelectorAll(".katex") ]
+ .map((formula) => `${formula.getAttribute("role")} ${formula.getAttribute("aria-label")}`);
+}
diff --git a/apps/client/src/services/math.ts b/apps/client/src/services/math.ts
index 3103339c03..eb59c10665 100644
--- a/apps/client/src/services/math.ts
+++ b/apps/client/src/services/math.ts
@@ -1,7 +1,40 @@
-import katex from "katex";
import "katex/contrib/mhchem";
-
import "./katex.scss";
-export { default as renderMathInElement } from "katex/contrib/auto-render";
-export default katex;
+import katex, { type KatexOptions } from "katex";
+import autoRender, { type RenderMathInElementOptions } from "katex/contrib/auto-render";
+
+/**
+ * KaTeX that renders HTML only. A `<math>` element makes Chromium load its MathML stylesheet, and
+ * with the sibling rules in it, Chromium restyles the whole note on every inserted block.
+ */
+const math: typeof katex = { ...katex, render };
+export default math;
+
+/** KaTeX's `renderMathInElement()`, with HTML output and each `span.math-tex` labelled. */
+export function renderMathInElement(container: HTMLElement, options?: RenderMathInElementOptions) {
+ const formulas = [ container, ...container.querySelectorAll<HTMLElement>("span.math-tex") ]
+ .filter((element) => element.matches("span.math-tex") && !element.querySelector(".katex"))
+ .map((element) => ({ element, tex: stripDelimiters(element.textContent ?? "") }));
+ autoRender(container, { ...options, output: "html" });
+ for (const { element, tex } of formulas) {
+ labelFormula(element, tex);
+ }
+}
+
+function render(tex: string, element: HTMLElement, options?: KatexOptions) {
+ katex.render(tex, element, { ...options, output: "html" });
+ labelFormula(element, tex);
+}
+
+/** KaTeX marks its HTML output `aria-hidden`, so the formula is labelled with its TeX source. */
+function labelFormula(element: Element, tex: string) {
+ const formula = element.querySelector(".katex");
+ formula?.setAttribute("role", "img");
+ formula?.setAttribute("aria-label", tex);
+}
+
+/** The TeX of a formula stored as `\(…\)` or `\[…\]`. */
+function stripDelimiters(source: string) {
+ return source.trim().replace(/^\\[([]|\\[)\]]$/g, "").trim();
+}
diff --git a/apps/client/src/widgets/collections/presentation/index.tsx b/apps/client/src/widgets/collections/presentation/index.tsx
index e43e444aa0..21d4ad760e 100644
--- a/apps/client/src/widgets/collections/presentation/index.tsx
+++ b/apps/client/src/widgets/collections/presentation/index.tsx
@@ -8,6 +8,7 @@ import slideBaseStylesheet from "reveal.js/reveal.css?raw";
import { openInCurrentNoteContext } from "../../../components/note_context";
import FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
+import katexStylesheet from "../../../services/katex.scss?inline";
import CollectionProperties from "../../note_bars/CollectionProperties";
import ActionButton from "../../react/ActionButton";
import { useEffectiveReadOnly, useNoteContext, useNoteLabelWithDefault, useTriliumEvent } from "../../react/hooks";
@@ -87,6 +88,7 @@ function usePresentationStylesheets(note: FNote, media: ViewModeMedia) {
let stylesheets = [
slideBaseStylesheet,
themeStylesheet,
+ katexStylesheet,
slideCustomStylesheet
];
if (media === "screen") {
diff --git a/apps/client/src/widgets/collections/presentation/slidejs.css b/apps/client/src/widgets/collections/presentation/slidejs.css
index 4fee508f20..c8969c5341 100644
--- a/apps/client/src/widgets/collections/presentation/slidejs.css
+++ b/apps/client/src/widgets/collections/presentation/slidejs.css
@@ -3,10 +3,6 @@ figure img {
height: auto !important;
}
-span.katex-html {
- display: none !important;
-}
-
p:has(span.text-tiny),
p:has(span.text-small),
p:has(span.text-big),