From 9f607e5a8478d9b6dfd9c9ff84f885d13716e2c5 Mon Sep 17 00:00:00 2001 From: stradichenko Date: Sun, 26 Apr 2026 15:01:31 +0200 Subject: [PATCH] =?UTF-8?q?Unit=2011:=20math=20engine=20=E2=80=94=20drop?= =?UTF-8?q?=20MathJax,=20keep=20KaTeX?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Delete layouts/partials/math.html (MathJax loader). - Remove the inline math-reprocess script from baseof.html (was the last artifact of the dual KaTeX+MathJax setup). - Add assets/js/katex-init.js: imports KaTeX modules, runs renderMathInElement on DOMContentLoaded, exposes window.reprocessMath for dynamic content. - In head.html (gated on .Params.math or site.Params.math), include KaTeX CSS + JS with SRI integrity. - Document the math: true page param in README. --- README.md | 16 +++++++++++++ assets/js/katex-init.js | 43 +++++++++++++++++++++++++++++++++ layouts/_default/baseof.html | 6 +---- layouts/partials/head.html | 33 +++++++++----------------- layouts/partials/math.html | 46 ------------------------------------ 5 files changed, 71 insertions(+), 73 deletions(-) create mode 100644 assets/js/katex-init.js delete mode 100644 layouts/partials/math.html diff --git a/README.md b/README.md index 401a230..04de6fe 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,22 @@ Described at [documentation](https://github.com/stradichenko/PKB-theme/blob/main ### [Color Theme Customization](https://stradichenko.github.io/PKB-theme/docs/color-theme-customization) +### Math support (KaTeX) + +Math rendering is powered by [KaTeX](https://katex.org/). To enable math on a page, add `math = true` to its front matter: + +```toml ++++ +title = "My math post" +math = true ++++ +``` + +Then write inline math with `$...$` or `\(...\)` and display math with `$$...$$` or `\[...\]`. Chemical equations (`mhchem`) are also supported. To enable math site-wide, set `math = true` under `[params]` in your site config; individual pages can still override with `math = false`. + +A global `window.reprocessMath()` hook is exposed for re-rendering math after dynamic DOM updates. + + ## FAQ ### Hugo's Theme Configuration Inheritance (Lookup Order) diff --git a/assets/js/katex-init.js b/assets/js/katex-init.js new file mode 100644 index 0000000..0c9c813 --- /dev/null +++ b/assets/js/katex-init.js @@ -0,0 +1,43 @@ +// KaTeX initialization +// Configures auto-render and exposes window.reprocessMath for dynamic content. +(function () { + "use strict"; + + var KATEX_OPTIONS = { + delimiters: [ + { left: "$$", right: "$$", display: true }, + { left: "$", right: "$", display: false }, + { left: "\\(", right: "\\)", display: false }, + { left: "\\[", right: "\\]", display: true } + ], + throwOnError: false, + errorColor: "#cc0000", + strict: false, + trust: true + }; + + function renderAll(root) { + if (typeof window.renderMathInElement !== "function") { + return; + } + try { + window.renderMathInElement(root || document.body, KATEX_OPTIONS); + } catch (err) { + // KaTeX auto-render handles per-formula errors via errorColor; this is a safety net. + if (window.console && console.warn) { + console.warn("KaTeX render failed:", err && err.message); + } + } + } + + // Expose a global re-render hook for dynamic content (e.g. theme toggles, AJAX inserts). + window.reprocessMath = function (root) { + renderAll(root); + }; + + if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", function () { renderAll(); }); + } else { + renderAll(); + } +})(); diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index c905a0c..8f449bc 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -61,11 +61,7 @@ {{ partial "head.html" . }} {{ block "head" . }}{{ end }} - - {{ if .Param "math" }} - {{ partialCached "math.html" . }} - {{ end }} - + {{ $mainCSS := resources.Get "css/main.css" }} {{ if $mainCSS }} diff --git a/layouts/partials/head.html b/layouts/partials/head.html index a2d894a..4fdc1ac 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -17,13 +17,14 @@ {{ partial "head/styles.html" . }} - {{/* Math Support - Load early for better rendering */}} - {{- if .Params.math -}} + {{/* Math Support - Preload KaTeX assets when enabled (page param falls back to site default) */}} + {{- $math := .Param "math" -}} + {{- if $math -}} - - + + @@ -58,7 +59,7 @@ {{/* Math rendering scripts - Load after DOM is ready */}} - {{- if .Params.math -}} + {{- if $math -}} @@ -68,22 +69,10 @@ - - + + {{ with resources.Get "js/katex-init.js" }} + {{ $katexInit := . | js.Build | minify | fingerprint }} + + {{ end }} {{- end -}} \ No newline at end of file diff --git a/layouts/partials/math.html b/layouts/partials/math.html deleted file mode 100644 index d6dbed3..0000000 --- a/layouts/partials/math.html +++ /dev/null @@ -1,46 +0,0 @@ - -