💄 Support render math formula by mathjax & katex framework. close #27

This commit is contained in:
凡梦星尘
2022-09-11 11:14:54 +08:00
parent dc66bc1a05
commit c38a439171
11 changed files with 390 additions and 109 deletions

View File

@@ -40,7 +40,7 @@
</div>
</footer>
{{- partialCached "scripts.html" . }}
{{- partial "scripts.html" . }}
</body>
</html>

View File

@@ -0,0 +1,16 @@
{{/* Use to get resources path with vendor */}}
{{ $fmt := "%s/%s@%s/%s" }}
{{ $npm := .res.name }}
{{ with .res.alias }}
{{ $npm = . }}
{{ end }}
{{ $file := .res.file }}
{{ if eq .vendor "cdnjs" }}
{{ $file = replaceRE `(dist|lib|source\/js)\/` "" .res.file }}
{{ $fmt = "%s/%s/%s/%s" }}
{{ end }}
{{ $pluginRes := printf $fmt .router $npm .res.version $file }}
{{ return $pluginRes }}

View File

@@ -1,80 +1,39 @@
<!-- Plugin scripts files -->
{{- $jsRes := .Site.Data.resources.js }}
{{ partialCached "scripts/global.html" . }}
{{- $vendor := .Site.Params.vendors.plugins }}
{{- $router := .Scratch.Get "router" }}
{{ $jsFmt := "%s/%s@%s/%s" }}
{{- range $js := $jsRes }}
{{- $npm := $js.name }}
{{- $file := $js.file }}
{{- if eq $vendor "cdnjs" }}
{{- with $js.alias }}
{{- $npm = . }}
{{- end }}
{{- $file = replaceRE `(dist|lib|source\/js)\/` "" $js.file }}
{{- $jsFmt = "%s/%s/%s/%s" }}
{{- end }}
{{- $pluginJS := printf $jsFmt $router $npm $js.version $file }}
<script type="text/javascript" src="{{ $pluginJS }}" defer></script>
{{- end }}
<script class="next-config" data-name="main" type="application/json">{{- .Scratch.Get "config" -}}</script>
<!-- Load math render scripts -->
{{ $math := .Params.math | default .Site.Params.math }}
{{ if $math }}
{{- partialCached "scripts/plugins.html" (dict "vendor" $vendor "router" $router "res" .Site.Data.resources.plugins "index" $math) }}
{{ if eq $math "katex" }}
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
renderMathInElement(document.body, {
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '$', right: '$', display: false},
{left: '\\(', right: '\\)', display: false},
{left: '\\[', right: '\\]', display: true}
],
throwOnError : false
});
});
</script>
{{ end }}
{{ if eq $math "mathjax" }}
<script type="text/javascript">
window.MathJax = {
tex: {
inlineMath: [["$", "$"]],
}
};
</script>
{{ end }}
{{ end }}
{{- $config := resources.Get "js/config.js" }}
{{- $motion := resources.Get "js/motion.js" }}
{{- $boot := resources.Get "js/next-boot.js" }}
{{- $utils := resources.Get "js/utils.js" }}
{{- $nextjs := (slice $config $utils $boot ) }}
{{- if .Site.Params.motion.enable }}
{{ $motionjs := resources.Get "js/motion.js" }}
{{ $nextjs = $nextjs | append $motionjs }}
{{- end }}
{{- if or (eq .Site.Params.shceme "Muse") (eq .Site.Params.shceme "Mist") }}
{{ $musejs := resources.Get "js/schemes/muse.js" }}
{{ $nextjs = $nextjs | append $musejs }}
{{- end }}
{{- if .Site.Params.bookmark.enable }}
{{- $bookmarkjs := resources.Get "js/bookmark.js" }}
{{- $nextjs = $nextjs | append $bookmarkjs }}
{{- end }}
{{- if .Site.Params.pjax }}
{{- $pjaxjs := resources.Get "js/pjax.js" }}
{{- $nextjs = $nextjs | append $pjaxjs }}
{{- end }}
{{- if isset .Site.Params "addthisid" }}
{{- $addthisjs := resources.Get "js/third-party/share/addthis.js" }}
{{- $nextjs = $nextjs | append $addthisjs }}
{{- end }}
{{- if isset .Site.Params "waline" }}
{{- $walinejs := resources.Get "js/third-party/comments/waline.js" }}
{{- $nextjs = $nextjs | append $walinejs }}
{{- end }}
{{- if isset .Site.Params "giscus" }}
{{- $giscusjs := resources.Get "js/third-party/comments/giscus.js" }}
{{- $nextjs = $nextjs | append $giscusjs }}
{{- end }}
{{- if isset .Site.Params "livere" }}
{{- $liverejs := resources.Get "js/third-party/comments/livere.js" }}
{{- $nextjs = $nextjs | append $liverejs }}
{{- end }}
{{- if isset .Site.Params "artalk" }}
{{- $artalkjs := resources.Get "js/third-party/comments/artalk.js" }}
{{- $nextjs = $nextjs | append $artalkjs }}
{{- end }}
{{- if isset .Site.Params "utterances" }}
{{- $utterancesjs := resources.Get "js/third-party/comments/utterances.js" }}
{{- $nextjs = $nextjs | append $utterancesjs }}
{{- end }}
{{- if .Site.Params.localSearch.enable }}
{{- $search := resources.Get "js/third-party/search/local.js" }}
{{- $nextjs = $nextjs | append $search }}
{{ end }}
{{- if .Site.Params.algoliaSearch.enable }}
{{- $search := resources.Get "js/third-party/search/algolia.js" }}
{{- $nextjs = $nextjs | append $search }}
{{ end }}
{{- $nextjs = $nextjs | resources.Concat "js/main.js"}}
{{ if hugo.IsProduction }}
{{- $nextjs = $nextjs | minify | fingerprint }}
{{ end }}
<script type="text/javascript" src="{{ $nextjs.RelPermalink }}" defer></script>

View File

@@ -0,0 +1,68 @@
{{- $jsRes := .Site.Data.resources.js }}
{{- $vendor := .Site.Params.vendors.plugins }}
{{- $router := .Scratch.Get "router" }}
{{- range $jsRes }}
{{ $pluginJS := partial "_funs/get_res.html" (dict "res" . "vendor" $vendor "router" $router) }}
<script type="text/javascript" src="{{ $pluginJS }}" defer></script>
{{- end }}
<script class="next-config" data-name="main" type="application/json">{{- .Scratch.Get "config" -}}</script>
{{- $config := resources.Get "js/config.js" }}
{{- $motion := resources.Get "js/motion.js" }}
{{- $boot := resources.Get "js/next-boot.js" }}
{{- $utils := resources.Get "js/utils.js" }}
{{- $nextjs := (slice $config $utils $boot ) }}
{{- if .Site.Params.motion.enable }}
{{ $motionjs := resources.Get "js/motion.js" }}
{{ $nextjs = $nextjs | append $motionjs }}
{{- end }}
{{- if or (eq .Site.Params.shceme "Muse") (eq .Site.Params.shceme "Mist") }}
{{ $musejs := resources.Get "js/schemes/muse.js" }}
{{ $nextjs = $nextjs | append $musejs }}
{{- end }}
{{- if .Site.Params.bookmark.enable }}
{{- $bookmarkjs := resources.Get "js/bookmark.js" }}
{{- $nextjs = $nextjs | append $bookmarkjs }}
{{- end }}
{{- if .Site.Params.pjax }}
{{- $pjaxjs := resources.Get "js/pjax.js" }}
{{- $nextjs = $nextjs | append $pjaxjs }}
{{- end }}
{{- if isset .Site.Params "addthisid" }}
{{- $addthisjs := resources.Get "js/third-party/share/addthis.js" }}
{{- $nextjs = $nextjs | append $addthisjs }}
{{- end }}
{{- if isset .Site.Params "waline" }}
{{- $walinejs := resources.Get "js/third-party/comments/waline.js" }}
{{- $nextjs = $nextjs | append $walinejs }}
{{- end }}
{{- if isset .Site.Params "giscus" }}
{{- $giscusjs := resources.Get "js/third-party/comments/giscus.js" }}
{{- $nextjs = $nextjs | append $giscusjs }}
{{- end }}
{{- if isset .Site.Params "livere" }}
{{- $liverejs := resources.Get "js/third-party/comments/livere.js" }}
{{- $nextjs = $nextjs | append $liverejs }}
{{- end }}
{{- if isset .Site.Params "artalk" }}
{{- $artalkjs := resources.Get "js/third-party/comments/artalk.js" }}
{{- $nextjs = $nextjs | append $artalkjs }}
{{- end }}
{{- if isset .Site.Params "utterances" }}
{{- $utterancesjs := resources.Get "js/third-party/comments/utterances.js" }}
{{- $nextjs = $nextjs | append $utterancesjs }}
{{- end }}
{{- if .Site.Params.localSearch.enable }}
{{- $search := resources.Get "js/third-party/search/local.js" }}
{{- $nextjs = $nextjs | append $search }}
{{ end }}
{{- if .Site.Params.algoliaSearch.enable }}
{{- $search := resources.Get "js/third-party/search/algolia.js" }}
{{- $nextjs = $nextjs | append $search }}
{{ end }}
{{- $nextjs = $nextjs | resources.Concat "js/main.js"}}
{{ if hugo.IsProduction }}
{{- $nextjs = $nextjs | minify | fingerprint }}
{{ end }}
<script type="text/javascript" src="{{ $nextjs.RelPermalink }}" defer></script>

View File

@@ -0,0 +1,17 @@
{{- $vendor := .vendor }}
{{- $router := .router }}
{{- $res := (index .res .index) }}
{{- $cssRes := $res.css }}
{{ if $cssRes }}
{{ range $cssRes }}
{{ $css := partial "_funs/get_res.html" (dict "res" . "vendor" $vendor "router" $router) }}
<link rel="stylesheet" href="{{ $css }}"/>
{{ end }}
{{ end }}
{{- $jsRes := $res.js }}
{{ range $jsRes }}
{{ $js := partial "_funs/get_res.html" (dict "res" . "vendor" $vendor "router" $router) }}
<script type="text/javascript" src="{{ $js }}"></script>
{{ end }}