Skip to content

Commit

Permalink
feat: support mathjax (HEIGE-PCloud#1267)
Browse files Browse the repository at this point in the history
* feat: add mathjax dependency

* feat: support mathjax

* feat(docs): update docs
  • Loading branch information
HEIGE-PCloud authored and Jatin794 committed Dec 19, 2024
1 parent 23bb365 commit 54b3a80
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 21 deletions.
12 changes: 12 additions & 0 deletions assets/js/lib/mathjax.js
Original file line number Diff line number Diff line change
@@ -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
}
};
2 changes: 2 additions & 0 deletions content/posts/theme-documentation-basics/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions content/posts/theme-documentation-basics/index.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
57 changes: 36 additions & 21 deletions themes/DoIt/layouts/partials/assets.html
Original file line number Diff line number Diff line change
Expand Up @@ -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) -}}
Expand Down

0 comments on commit 54b3a80

Please sign in to comment.