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 @@ - -