From fdd0ae49c52497694cdb40b66042cff53fd29b69 Mon Sep 17 00:00:00 2001 From: PCloud Date: Tue, 14 May 2024 16:29:45 +0100 Subject: [PATCH] refactor: math --- assets/js/lib/mathjax.js | 12 --- assets/js/lib/mathjax.ts | 11 +++ layouts/partials/assets.html | 146 ++++++++++++++++------------------- 3 files changed, 78 insertions(+), 91 deletions(-) delete mode 100644 assets/js/lib/mathjax.js create mode 100644 assets/js/lib/mathjax.ts diff --git a/assets/js/lib/mathjax.js b/assets/js/lib/mathjax.js deleted file mode 100644 index bab585cca..000000000 --- a/assets/js/lib/mathjax.js +++ /dev/null @@ -1,12 +0,0 @@ -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/assets/js/lib/mathjax.ts b/assets/js/lib/mathjax.ts new file mode 100644 index 000000000..a9fb43655 --- /dev/null +++ b/assets/js/lib/mathjax.ts @@ -0,0 +1,11 @@ +const delimiters = window.config?.math?.delimiters +if (delimiters) { + 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/layouts/partials/assets.html b/layouts/partials/assets.html index a8a4aea34..539bb1edd 100644 --- a/layouts/partials/assets.html +++ b/layouts/partials/assets.html @@ -26,65 +26,6 @@ {{- end -}} {{- end -}} -{{- /* KaTeX */ -}} -{{- $math := $params.math -}} -{{- if eq $math true -}} - {{- $math = .Site.Params.page.math | default dict -}} -{{- else if eq $math false -}} - {{- $math = dict "enable" false -}} -{{- end -}} -{{- if $math.enable -}} - {{- 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" -}} - {{- $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 -}} - - {{- $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) -}} - {{- end -}} - {{- $inlineDelimiters := slice (dict "left" "$" "right" "$" "display" false) (dict "left" "\\(" "right" "\\)" "display" false) -}} - {{- if and $math.inlineLeftDelimiter $math.inlineRightDelimiter -}} - {{- $inlineDelimiters = slice (dict "left" $math.inlineLeftDelimiter "right" $math.inlineRightDelimiter "display" false) -}} - {{- end -}} - {{- $delimiters := $blockDelimiters | append $inlineDelimiters -}} - {{- $config = dict "strict" false "delimiters" $delimiters | dict "math" | merge $config -}} -{{- end -}} - {{- /* mermaid */ -}} {{- if (.Scratch.Get "this").mermaid -}} + {{- dict "cdn" $cdn.tablesort "default" "lib/tablesort/tablesort.min.js" | partial "partials/assets/deferScript.html" -}} {{- $config = dict "sort" true | dict "table" | merge $config -}} {{- end -}} {{- /* twemoji */ -}} {{/* Run only when twemoji is enabled */}} {{- if $params.twemoji -}} - {{- $src := dict "cdn" $cdn.twemojiJS "default" "lib/twemoji/twemoji.min.js" | partial "partials/assets/RelPermalink.html" -}} - + {{- dict "cdn" $cdn.twemojiJS "default" "lib/twemoji/twemoji.min.js" | partial "partials/assets/deferScript.html" -}} {{- $options := dict "shims" (dict "twemoji" "js/shims/twemoji.js") "targetPath" "js/twemoji.min.js" "minify" true -}} {{- $js := resources.Get "js/lib/twemoji.ts" | js.Build $options -}} @@ -259,17 +198,10 @@ {{- /* lightgallery.js */ -}} {{/* Run only when lightgallary is enabled */}} {{- if $params.lightgallery -}} - {{- $href := dict "cdn" $cdn.lightgalleryCSS "default" "lib/lightgallery/lightgallery.min.css" | partial "partials/assets/RelPermalink.html" -}} - - - {{- $src := dict "cdn" $cdn.lightgalleryJS "default" "lib/lightgallery/lightgallery.min.js" | partial "partials/assets/RelPermalink.html" -}} - - - {{- $src := dict "cdn" $cdn.lightgalleryThumbnailJS "default" "lib/lightgallery/lg-thumbnail.min.js" | partial "partials/assets/RelPermalink.html" -}} - - - {{- $src := dict "cdn" $cdn.lightgalleryZoomJS "default" "lib/lightgallery/lg-zoom.min.js" | partial "partials/assets/RelPermalink.html" -}} - + {{- dict "cdn" $cdn.lightgalleryCSS "default" "lib/lightgallery/lightgallery.min.css" | partial "partials/assets/deferStyle.html" -}} + {{- dict "cdn" $cdn.lightgalleryJS "default" "lib/lightgallery/lightgallery.min.js" | partial "partials/assets/deferScript.html" -}} + {{- dict "cdn" $cdn.lightgalleryThumbnailJS "default" "lib/lightgallery/lg-thumbnail.min.js" | partial "partials/assets/deferScript.html" -}} + {{- dict "cdn" $cdn.lightgalleryZoomJS "default" "lib/lightgallery/lg-zoom.min.js" | partial "partials/assets/deferScript.html" -}} {{- $config = dict "selector" ".lightgallery" "speed" 400 "hideBarsDelay" 2000 "thumbnail" true "exThumbImage" "data-thumbnail" "thumbWidth" 80 "thumbContHeight" 80 "actualSize" false | dict "lightGallery" | merge $config -}} {{- end -}} @@ -277,8 +209,7 @@ {{- /* Sharer.js */ -}} {{/* Run only when enabled and on page that's sharable */}} {{- if $params.share.enable | and (.Scratch.Get "this").sharer -}} - {{- $src := dict "cdn" $cdn.sharerJS "default" "lib/sharer/sharer.min.js" | partial "partials/assets/RelPermalink.html" -}} - + {{- $src := dict "cdn" $cdn.sharerJS "default" "lib/sharer/sharer.min.js" | partial "partials/assets/deferScript.html" -}} {{- $config = dict "sharerjs" true | merge $config -}} {{- end -}} @@ -286,15 +217,60 @@ {{- /* TypeIt */ -}} {{- with (.Scratch.Get "this").typeitMap -}} {{- $typeit := $.Site.Params.typeit -}} - {{- $src := dict "cdn" $cdn.typeitJS "default" "lib/typeit/typeit.min.js" | partial "partials/assets/RelPermalink.html" -}} - + {{- dict "cdn" $cdn.typeitJS "default" "lib/typeit/typeit.min.js" | partial "partials/assets/deferScript.html" -}} {{- $config = dict "speed" $typeit.speed "cursorSpeed" $typeit.cursorSpeed "cursorChar" $typeit.cursorChar "duration" $typeit.duration "data" . | dict "typeit" | merge $config -}} {{- end -}} +{{- /* Math (KaTeX or mathjax) */ -}} +{{- $math := $params.math -}} +{{- if eq $math true -}} + {{- $math = .Site.Params.page.math | default dict -}} +{{- else if eq $math false -}} + {{- $math = dict "enable" false -}} +{{- end -}} +{{- if $math.enable -}} + {{- if $math.mathjax -}} + + + {{- $options := dict "targetPath" "js/mathjax.min.js" "minify" true -}} + {{- $js := resources.Get "js/lib/mathjax.ts" | js.Build $options -}} + + {{- else -}} + {{- dict "cdn" $cdn.katexCSS "default" "lib/katex/katex.min.css" | partial "partials/assets/deferStyle.html" -}} + {{- dict "cdn" $cdn.katexJS "default" "lib/katex/katex.min.js" | partial "partials/assets/deferScript.html" -}} + + {{- dict "cdn" $cdn.katexAutoRenderJS "default" "lib/katex/auto-render.min.js" | partial "partials/assets/deferScript.html" -}} + + {{- if $math.copyTex -}} + {{- dict "cdn" $cdn.katexCopyTexCSS "default" "lib/katex/copy-tex.min.css" | partial "partials/assets/deferStyle.html" -}} + {{- dict "cdn" $cdn.katexCopyTexJS "default" "lib/katex/copy-tex.min.js" | partial "partials/assets/deferScript.html" -}} + {{- end -}} + + {{- if $math.mhchem -}} + {{- dict "cdn" $cdn.katexMhchemJS "default" "lib/katex/mhchem.min.js" | partial "partials/assets/deferScript.html" -}} + {{- end -}} + + {{- $options := dict "targetPath" "js/katex.min.js" "minify" true "shims" (dict "katex/dist/contrib/auto-render" "js/shims/katex.js") -}} + {{- $js := resources.Get "js/lib/katex.js" | js.Build $options -}} + + {{- end -}} + + {{- $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) -}} + {{- end -}} + {{- $inlineDelimiters := slice (dict "left" "$" "right" "$" "display" false) (dict "left" "\\(" "right" "\\)" "display" false) -}} + {{- if and $math.inlineLeftDelimiter $math.inlineRightDelimiter -}} + {{- $inlineDelimiters = slice (dict "left" $math.inlineLeftDelimiter "right" $math.inlineRightDelimiter "display" false) -}} + {{- end -}} + {{- $delimiters := $blockDelimiters | append $inlineDelimiters -}} + {{- $config = dict "strict" false "delimiters" $delimiters | dict "math" | merge $config -}} +{{- end -}} + {{/* Instant page */}} {{- if $params.instantpage.enable -}} -{{- $js := resources.Get "/lib/instant.page/instantpage.min.js" -}} + {{- $js := resources.Get "/lib/instant.page/instantpage.min.js" -}} {{- end -}} @@ -321,3 +297,15 @@ {{- $value := resources.Get $src -}} {{- return $value.RelPermalink -}} {{- end -}} + +{{- define "partials/assets/deferScript.html" -}} + {{- $src := .cdn | default .default -}} + {{- $value := resources.Get $src -}} + +{{- end -}} + +{{- define "partials/assets/deferStyle.html" -}} + {{- $src := .cdn | default .default -}} + {{- $value := resources.Get $src -}} + +{{- end -}}