From 54b3a808ccfb9156c1e8bff460a7357c64b9963f Mon Sep 17 00:00:00 2001 From: PCloud Date: Sat, 11 May 2024 19:20:07 +0100 Subject: [PATCH] feat: support mathjax (#1267) * feat: add mathjax dependency * feat: support mathjax * feat(docs): update docs --- assets/js/lib/mathjax.js | 12 ++++ .../theme-documentation-basics/index.en.md | 2 + .../theme-documentation-basics/index.zh-cn.md | 2 + exampleSite/config/_default/params.toml | 3 + themes/DoIt/layouts/partials/assets.html | 57 ++++++++++++------- 5 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 assets/js/lib/mathjax.js diff --git a/assets/js/lib/mathjax.js b/assets/js/lib/mathjax.js new file mode 100644 index 000000000..bab585cca --- /dev/null +++ b/assets/js/lib/mathjax.js @@ -0,0 +1,12 @@ +const delimiters = window.config?.math?.delimiters +if (!delimiters) { + return; +} +const displayMath = delimiters.filter(d => d.display).map(d => [d.left, d.right]) +const inlineMath = delimiters.filter(d => !d.display).map(d => [d.left, d.right]) +window.MathJax = { + tex: { + displayMath, + inlineMath + } +}; diff --git a/content/posts/theme-documentation-basics/index.en.md b/content/posts/theme-documentation-basics/index.en.md index 50303a821..f456c61b6 100644 --- a/content/posts/theme-documentation-basics/index.en.md +++ b/content/posts/theme-documentation-basics/index.en.md @@ -509,6 +509,8 @@ Please open the code block below to view the complete sample configuration {{< f copyTex = true # KaTeX extension mhchem mhchem = true + # Use MathJax instead of KaTeX + mathjax = false # {{< version 0.2.0 >}} Code config [params.page.code] # the maximum number of lines of displayed code by default diff --git a/content/posts/theme-documentation-basics/index.zh-cn.md b/content/posts/theme-documentation-basics/index.zh-cn.md index fe3ec6fde..4603f18cc 100644 --- a/content/posts/theme-documentation-basics/index.zh-cn.md +++ b/content/posts/theme-documentation-basics/index.zh-cn.md @@ -521,6 +521,8 @@ optimizeImages = true copyTex = true # KaTeX 插件 mhchem mhchem = true + # 使用 MathJax 替代 KaTeX + mathjax = false # {{< version 0.2.0 >}} {{< link "https://docs.mapbox.com/mapbox-gl-js" "Mapbox GL JS" >}} 配置 [params.page.mapbox] # Mapbox GL JS 的 access token diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml index 3955e1599..d03e70ae1 100644 --- a/exampleSite/config/_default/params.toml +++ b/exampleSite/config/_default/params.toml @@ -253,6 +253,9 @@ srcsetLargeResizeMethod = "800x webp Lanczos q75" # KaTeX extension mhchem # KaTeX 插件 mhchem mhchem = true + # Use MathJax instead of KaTeX + # 使用 MathJax 替代 KaTeX + mathjax = false # Mapbox GL JS config (Mapbox GL JS https://docs.mapbox.com/mapbox-gl-js) # Mapbox GL JS 配置 (Mapbox GL JS https://docs.mapbox.com/mapbox-gl-js) [page.mapbox] diff --git a/themes/DoIt/layouts/partials/assets.html b/themes/DoIt/layouts/partials/assets.html index 6f756bd3a..8276a39d1 100644 --- a/themes/DoIt/layouts/partials/assets.html +++ b/themes/DoIt/layouts/partials/assets.html @@ -84,30 +84,45 @@ {{- $math = dict "enable" false -}} {{- end -}} {{- if $math.enable -}} - {{- $source := $cdn.katexCSS | default "lib/katex/katex.min.css" -}} - {{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/style.html" -}} - {{- $source := $cdn.katexJS | default "lib/katex/katex.min.js" -}} - {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} - {{- $source := $cdn.katexAutoRenderJS | default "lib/katex/auto-render.min.js" -}} - {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} - {{- if $math.copyTex -}} - {{- $source := $cdn.katexCopyTexCSS | default "lib/katex/copy-tex.min.css" -}} - {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/style.html" -}} - {{- $source := $cdn.katexCopyTexJS | default "lib/katex/copy-tex.min.js" -}} + {{- if $math.mathjax -}} + {{- $source := "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" -}} {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} - {{- end -}} - {{- if $math.mhchem -}} - {{- $source := $cdn.katexMhchemJS | default "lib/katex/mhchem.min.js" -}} + {{- $options := dict -}} + {{- $options = dict "targetPath" "js/mathjax.min.js" | merge $options -}} + {{- $options = dict "minify" true | merge $options -}} + {{- $js := resources.Get "js/lib/mathjax.js" | js.Build $options -}} + {{- $_ := $js.RelPermalink -}} + {{- dict "Link" $js.RelPermalink "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + + {{- else -}} + + {{- $source := $cdn.katexCSS | default "lib/katex/katex.min.css" -}} + {{- dict "Source" $source "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/style.html" -}} + {{- $source := $cdn.katexJS | default "lib/katex/katex.min.js" -}} {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + {{- $source := $cdn.katexAutoRenderJS | default "lib/katex/auto-render.min.js" -}} + {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + {{- if $math.copyTex -}} + {{- $source := $cdn.katexCopyTexCSS | default "lib/katex/copy-tex.min.css" -}} + {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/style.html" -}} + {{- $source := $cdn.katexCopyTexJS | default "lib/katex/copy-tex.min.js" -}} + {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + {{- end -}} + {{- if $math.mhchem -}} + {{- $source := $cdn.katexMhchemJS | default "lib/katex/mhchem.min.js" -}} + {{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + {{- end -}} + {{- $shims := dict "katex/dist/contrib/auto-render" "js/shims/katex.js" -}} + {{- $options := dict -}} + {{- $options = dict "targetPath" "js/katex.min.js" | merge $options -}} + {{- $options = dict "minify" true | merge $options -}} + {{- $options = dict "shims" $shims | merge $options -}} + {{- $js := resources.Get "js/lib/katex.js" | js.Build $options -}} + {{- $_ := $js.RelPermalink -}} + {{- dict "Link" $js.RelPermalink "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + {{- end -}} - {{- $shims := dict "katex/dist/contrib/auto-render" "js/shims/katex.js" -}} - {{- $options := dict -}} - {{- $options = dict "targetPath" "js/katex.min.js" | merge $options -}} - {{- $options = dict "minify" true | merge $options -}} - {{- $options = dict "shims" $shims | merge $options -}} - {{- $js := resources.Get "js/lib/katex.js" | js.Build $options -}} - {{- $_ := $js.RelPermalink -}} - {{- dict "Link" $js.RelPermalink "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} + {{- $blockDelimiters := slice (dict "left" "$$" "right" "$$" "display" true) (dict "left" "\\[" "right" "\\]" "display" true) -}} {{- if and $math.blockLeftDelimiter $math.blockRightDelimiter -}} {{- $blockDelimiters = slice (dict "left" $math.blockLeftDelimiter "right" $math.blockRightDelimiter "display" true) -}}