Skip to content

Commit 9789556

Browse files
committed
layouts: render ```mermaid code blocks as diagrams
Adds a Hugo codeblock render hook that turns ```mermaid fences into <pre class="mermaid"> containers, plus a lazily-loaded mermaid bundle that only ships on pages that actually contain a diagram. Mirrors the existing YouTube embed pattern (Store flag + conditional script tag in baseof) so the main scripts.js bundle stays unchanged.
1 parent a0a179c commit 9789556

6 files changed

Lines changed: 990 additions & 2 deletions

File tree

assets/js/mermaid.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import mermaid from 'mermaid'
2+
3+
const isDark = () => document.documentElement.classList.contains('dark')
4+
5+
mermaid.initialize({
6+
startOnLoad: false,
7+
securityLevel: 'strict',
8+
theme: isDark() ? 'dark' : 'default',
9+
})
10+
11+
const render = () => {
12+
mermaid.run({ querySelector: 'pre.mermaid' })
13+
}
14+
15+
if (document.readyState === 'loading') {
16+
document.addEventListener('DOMContentLoaded', render)
17+
} else {
18+
render()
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{{- .Page.Store.Set "mermaid" true -}}
2+
<pre
3+
class="mermaid not-prose my-4 flex justify-center bg-transparent"
4+
data-pagefind-ignore
5+
>{{- .Inner -}}</pre>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{ $mermaid := resources.Get "js/mermaid.js"
2+
| js.Build (dict "minify" true "targetPath" "mermaid.js")
3+
}}
4+
<script defer src="{{ $mermaid.Permalink }}"></script>

layouts/baseof.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,9 @@
9595
{{ with .Store.Get "youtube" }}
9696
{{ partialCached "youtube-script.html" "-" "-" }}
9797
{{ end }}
98+
{{/* Load mermaid if the page contains a mermaid code block */}}
99+
{{ with .Store.Get "mermaid" }}
100+
{{ partialCached "mermaid-script.html" "-" "-" }}
101+
{{ end }}
98102
</body>
99103
</html>

0 commit comments

Comments
 (0)