Skip to content

Build your personal knowledge base with Trilium Notes

perf(katex): ship only the woff2 font faces

Elian Doran0fb390067c87
diff --git a/apps/client/src/services/katex.scss b/apps/client/src/services/katex.scss
new file mode 100644
index 0000000000..ca618df726
--- /dev/null
+++ b/apps/client/src/services/katex.scss
@@ -0,0 +1,8 @@
+// Builds KaTeX's stylesheet with only the woff2 faces. The prebuilt katex.min.css lists
+// woff and ttf sources as well, and the bundler emits a file for every url() it finds.
+@use "katex/src/styles/katex.scss" with (
+ $use-woff: false,
+ $use-ttf: false,
+ // Sass emits url() verbatim, so the path resolves against this file, not the package.
+ $font-folder: "../../../../node_modules/katex/dist/fonts"
+);
diff --git a/apps/client/src/services/math.ts b/apps/client/src/services/math.ts
index 2a5fc45b22..3103339c03 100644
--- a/apps/client/src/services/math.ts
+++ b/apps/client/src/services/math.ts
@@ -1,5 +1,7 @@
import katex from "katex";
import "katex/contrib/mhchem";
-import "katex/dist/katex.min.css";
+
+import "./katex.scss";
+
export { default as renderMathInElement } from "katex/contrib/auto-render";
export default katex;
diff --git a/packages/share-theme/package.json b/packages/share-theme/package.json
index 17df76ddcd..c665fe05b2 100644
--- a/packages/share-theme/package.json
+++ b/packages/share-theme/package.json
@@ -35,6 +35,7 @@
"dotenv": "17.4.2",
"esbuild": "0.28.2",
"highlight.js": "11.12.0",
+ "sass": "1.103.1",
"typescript": "6.0.3"
}
}
diff --git a/packages/share-theme/scripts/build.ts b/packages/share-theme/scripts/build.ts
index 7d4a660c5a..79f982c058 100644
--- a/packages/share-theme/scripts/build.ts
+++ b/packages/share-theme/scripts/build.ts
@@ -4,6 +4,9 @@ import path from "node:path";
import dotenv from "dotenv";
import * as esbuild from "esbuild";
import { writeFileSync } from "node:fs";
+import { createRequire } from "node:module";
+import { pathToFileURL } from "node:url";
+import * as sass from "sass";
// const fileURL = fileURLToPath(import.meta.url);
@@ -46,6 +49,31 @@ for (const mod of modulesRequested) {
if (!entryPoints.length) for (const mod of modules) entryPoints.push(makeEntry(mod));
+// Sass resolves relative paths on its own; bare specifiers such as
+// "katex/src/styles/katex.scss" go through Node.
+const nodeModulesImporter: sass.FileImporter<"sync"> = {
+ findFileUrl(url) {
+ if (url.startsWith(".") || url.startsWith("/")) {
+ return null;
+ }
+
+ return pathToFileURL(createRequire(path.join(rootDir, "package.json")).resolve(url));
+ }
+};
+
+// esbuild has no Sass support of its own. katex.scss needs it to build KaTeX's
+// stylesheet without the woff and ttf faces.
+const sassPlugin: esbuild.Plugin = {
+ name: "sass",
+ setup(build) {
+ build.onLoad({ filter: /\.scss$/ }, (args) => ({
+ contents: sass.compile(args.path, { importers: [nodeModulesImporter] }).css,
+ loader: "css",
+ resolveDir: path.dirname(args.path)
+ }));
+ }
+};
+
async function runBuild(watch: boolean) {
const before = performance.now();
const opts: esbuild.BuildOptions = {
@@ -66,6 +94,7 @@ async function runBuild(watch: boolean) {
".html": "text",
".css": "css"
},
+ plugins: [sassPlugin],
logLevel: "info",
metafile: true,
minify: process.argv.includes("--minify")
diff --git a/packages/share-theme/src/scripts/modules/math.ts b/packages/share-theme/src/scripts/modules/math.ts
index 6a8ad2e393..606aeee47f 100644
--- a/packages/share-theme/src/scripts/modules/math.ts
+++ b/packages/share-theme/src/scripts/modules/math.ts
@@ -1,5 +1,5 @@
import { KATEX_MACROS } from "@triliumnext/commons/src/lib/katex_macros.js";
-import "katex/dist/katex.min.css";
+import "../../styles/katex.scss";
export default async function setupMath() {
const anyMathBlock = document.querySelector("#content .math-tex");
diff --git a/packages/share-theme/src/styles/katex.scss b/packages/share-theme/src/styles/katex.scss
new file mode 100644
index 0000000000..ca618df726
--- /dev/null
+++ b/packages/share-theme/src/styles/katex.scss
@@ -0,0 +1,8 @@
+// Builds KaTeX's stylesheet with only the woff2 faces. The prebuilt katex.min.css lists
+// woff and ttf sources as well, and the bundler emits a file for every url() it finds.
+@use "katex/src/styles/katex.scss" with (
+ $use-woff: false,
+ $use-ttf: false,
+ // Sass emits url() verbatim, so the path resolves against this file, not the package.
+ $font-folder: "../../../../node_modules/katex/dist/fonts"
+);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 05df83e63f..1f7d08865f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1353,6 +1353,9 @@ importers:
highlight.js:
specifier: 11.12.0
version: 11.12.0
+ sass:
+ specifier: 1.103.1
+ version: 1.103.1
typescript:
specifier: 6.0.3
version: 6.0.3