From 9f70e8a87bc7dfabc3c9062256082c6daa8f53a7 Mon Sep 17 00:00:00 2001
From: Xin <5097752+imfing@users.noreply.github.com>
Date: Thu, 14 Aug 2025 00:00:03 +0800
Subject: [PATCH] refactor(mermaid): enhance Mermaid JS loading logic for
flexibility (#745)
* refactor(mermaid): enhance Mermaid JS loading logic for flexibility
- Updated the Mermaid JS loader to support configurable remote or local asset loading based on site parameters.
- Improved error handling for asset retrieval and added comments for clarity on behavior based on different configurations.
- Ensured that the default behavior falls back to a CDN if no base is provided, enhancing usability and maintainability.
* chore: clean up comments
* fix: comment typo
---
layouts/_partials/scripts/mermaid.html | 59 ++++++++++++++++++++------
1 file changed, 47 insertions(+), 12 deletions(-)
diff --git a/layouts/_partials/scripts/mermaid.html b/layouts/_partials/scripts/mermaid.html
index 3e4fcbf..b4f957b 100644
--- a/layouts/_partials/scripts/mermaid.html
+++ b/layouts/_partials/scripts/mermaid.html
@@ -1,17 +1,52 @@
-{{/* Mermaid */}}
+{{- /* Mermaid */ -}}
-{{ $mermaidJsUrl := printf "https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid%s.js" (cond hugo.IsProduction ".min" "") -}}
-{{ 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 -}}
-
- {{ end -}}
- {{ end -}}
-{{ end -}}
+{{- $mermaidBase := "" -}}
+{{- $useDefaultCdn := true -}}
+{{- with site.Params.mermaid.base -}}
+ {{- $mermaidBase = . -}}
+ {{- $useDefaultCdn = false -}}
+{{- end -}}
+{{- $mermaidJsAsset := "" -}}
+{{- with site.Params.mermaid.js -}}
+ {{- $mermaidJsAsset = . -}}
+{{- end -}}
+
+{{- /* If only js is set without base, use local asset loading */ -}}
+{{- if and $useDefaultCdn (ne $mermaidJsAsset "") -}}
+ {{- $useDefaultCdn = false -}}
+{{- end -}}
+
+{{- /* Set default CDN base if needed */ -}}
+{{- if $useDefaultCdn -}}
+ {{- $mermaidBase = "https://cdn.jsdelivr.net/npm/mermaid@latest/dist" -}}
+{{- end -}}
+
+{{- $isRemoteBase := or (strings.HasPrefix $mermaidBase "http://") (strings.HasPrefix $mermaidBase "https://") -}}
+{{- $minSuffix := cond hugo.IsProduction ".min" "" -}}
+
+{{- /* JS retrieval: get raw JS from either local asset or remote, then process */ -}}
+{{- if $isRemoteBase -}}
+ {{- $jsPath := cond (ne $mermaidJsAsset "") $mermaidJsAsset (printf "mermaid%s.js" $minSuffix) -}}
+ {{- $mermaidJsUrl := printf "%s/%s" $mermaidBase $jsPath -}}
+ {{- 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%s.js" $minSuffix) . -}}
+ {{- $mermaidJs := . | fingerprint -}}
+
+ {{- end -}}
+ {{- end -}}
+ {{- end -}}
+{{- else if $mermaidJsAsset -}}
+ {{- with resources.Get $mermaidJsAsset -}}
+ {{- $mermaidJs := . | fingerprint -}}
+
+ {{- else -}}
+ {{- errorf "Mermaid js asset not found at %q" $mermaidJsAsset -}}
+ {{- end -}}
+{{- end -}}