chore: use CDN to download mermaid js (#651)

* chore: download js assets during build time

* chore: remove CDN asset download task and update Mermaid script fingerprinting
This commit is contained in:
Xin 2025-03-30 21:20:50 +01:00 committed by GitHub
parent b700825943
commit 8021437f77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 34 additions and 2253 deletions

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@
enable = true enable = true
disableIDs = true disableIDs = true
[[build.cachebusters]] [[build.cachebusters]]
source = 'assets/watching/hugo_stats\.json' source = 'assets/notwatching/hugo_stats\.json'
target = 'styles\.css' target = 'styles\.css'
[[build.cachebusters]] [[build.cachebusters]]
source = '(postcss|tailwind)\.config\.mjs' source = '(postcss|tailwind)\.config\.mjs'
@ -23,4 +23,5 @@
target = "assets" target = "assets"
[[module.mounts]] [[module.mounts]]
source = "hugo_stats.json" source = "hugo_stats.json"
target = "assets/watching/hugo_stats.json" target = "assets/notwatching/hugo_stats.json"
disableWatch = true

View File

@ -60,20 +60,20 @@
</script> </script>
<!-- KaTeX--> <!-- KaTeX-->
{{ $noop := .WordCount }} {{ $noop := .WordCount -}}
{{ if .Page.Store.Get "hasMath" }} {{ if .Page.Store.Get "hasMath" -}}
{{ $katex_css_url := printf "https://cdn.jsdelivr.net/npm/katex@latest/dist/katex%s.css" (cond hugo.IsProduction ".min" "") -}} <!-- TODO: make url configurable -->
{{ with try (resources.GetRemote $katex_css_url) -}} {{ $katexCssUrl := printf "https://cdn.jsdelivr.net/npm/katex@latest/dist/katex%s.css" (cond hugo.IsProduction ".min" "") -}}
{{ with try (resources.GetRemote $katexCssUrl) -}}
{{ with .Err -}} {{ with .Err -}}
{{ errorf "Could not retrieve KaTeX css file from CDN. Reason: %s." . -}} {{ errorf "Could not retrieve KaTeX css file from %s. Reason: %s." $katexCssUrl . -}}
{{ else with.Value -}} {{ else with.Value -}}
{{ with resources.Copy (printf "css/katex%s.css" (cond hugo.IsProduction ".min" "")) . }} {{ with resources.Copy (printf "css/katex%s.css" (cond hugo.IsProduction ".min" "")) . -}}
{{ $secureCSS := . | resources.Fingerprint "sha512" -}} <link rel="stylesheet" href="{{- .RelPermalink -}}" integrity="{{- . | fingerprint -}}" crossorigin="anonymous" />
<link rel="stylesheet" href="{{- .RelPermalink -}}" integrity="{{- $secureCSS.Data.Integrity -}}" crossorigin="anonymous"> {{ end -}}
{{ end -}} {{ end -}}
{{ end -}} {{ end -}}
{{ end -}} {{ end -}}
{{ end }}
{{ partial "custom/head-end.html" . }} {{ partial "custom/head-end.html" . -}}
</head> </head>

View File

@ -1,11 +1,22 @@
{{/* Mermaid */}} {{/* Mermaid */}}
{{- $mermaidJS := resources.Get "lib/mermaid/mermaid.min.js" | fingerprint -}} {{ $mermaidJsUrl := printf "https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid%s.js" (cond hugo.IsProduction ".min" "") -}}
<script defer src="{{ $mermaidJS.RelPermalink }}" integrity="{{ $mermaidJS.Data.Integrity }}"></script> {{ with try (resources.GetRemote $mermaidJsUrl) -}}
{{ with .Err -}}
{{ errorf "Could not retrieve Mermaid js file from %s. Reason: %s." $mermaidJsUrl . -}}
{{ else with.Value -}}
{{ with resources.Copy (printf "js/mermaid.min.js") . -}}
{{ $mermaidJs := . | fingerprint -}}
<script defer src="{{ $mermaidJs.RelPermalink }}" integrity="{{ $mermaidJs.Data.Integrity }}" crossorigin="anonymous"></script>
{{ end -}}
{{ end -}}
{{ end -}}
<script> <script>
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
// Store original mermaid code for each diagram // Store original mermaid code for each diagram
document.querySelectorAll(".mermaid").forEach(el => { document.querySelectorAll(".mermaid").forEach((el) => {
el.dataset.original = el.innerHTML; el.dataset.original = el.innerHTML;
}); });
@ -17,7 +28,7 @@
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(() => { timeout = setTimeout(() => {
const theme = document.documentElement.classList.contains("dark") ? "dark" : "default"; const theme = document.documentElement.classList.contains("dark") ? "dark" : "default";
document.querySelectorAll(".mermaid").forEach(el => { document.querySelectorAll(".mermaid").forEach((el) => {
// Reset to original content, preserving HTML // Reset to original content, preserving HTML
el.innerHTML = el.dataset.original; el.innerHTML = el.dataset.original;
el.removeAttribute("data-processed"); el.removeAttribute("data-processed");
@ -27,7 +38,7 @@
}, 150); }, 150);
}).observe(document.documentElement, { }).observe(document.documentElement, {
attributes: true, attributes: true,
attributeFilter: ["class"] attributeFilter: ["class"],
}); });
}); });
</script> </script>

View File

@ -16,9 +16,3 @@ tasks:
desc: Start development server for Hextra theme desc: Start development server for Hextra theme
cmds: cmds:
- npm run dev:theme - npm run dev:theme
libs:
desc: Download libs from CDN
cmds:
- curl -o assets/lib/flexsearch/flexsearch.bundle.min.js https://cdn.jsdelivr.net/npm/flexsearch@0.7.31/dist/flexsearch.bundle.min.js
- curl -o assets/lib/mermaid/mermaid.min.js https://cdn.jsdelivr.net/npm/mermaid@11.3.0/dist/mermaid.min.js