Replies: 4 comments 13 replies
-
Try implementating like it is implemented in this commit |
Beta Was this translation helpful? Give feedback.
-
Here is how I did it: diff --git a/layouts/partials/extend_head.html b/layouts/partials/extend_head.html
new file mode 100644
index 0000000..59db893
--- /dev/null
+++ b/layouts/partials/extend_head.html
@@ -0,0 +1,9 @@
+{{ if .Page.Store.Get "needMermaid" -}}
+<script type="module">
+ import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
+ mermaid.initialize({
+ startOnLoad: true,
+ 'theme': 'base',
+ });
+</script>
+{{ end -}}
diff --git a/layouts/shortcodes/mermaid.html b/layouts/shortcodes/mermaid.html
new file mode 100644
index 0000000..c00edf5
--- /dev/null
+++ b/layouts/shortcodes/mermaid.html
@@ -0,0 +1,2 @@
+{{ .Page.Store.Set "needMermaid" true }}
+<div class="mermaid" align="center">{{ safeHTML .Inner }}</div> Then in the content:
|
Beta Was this translation helpful? Give feedback.
-
I tried following @howardjohn by editing both
<!--katex is working -->
{{ if or .Params.math .Site.Params.math }}
{{ partial "math.html" . }}
{{ end }}
<!--mermaid is NOT working -->
{{ if .Page.Store.Get "needMermaid" -}}
<script type="module">
var theme = 'neutral';
if (localStorage.getItem("pref-theme") == "dark") {
theme = 'dark'
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
theme = 'dark'
}
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
mermaid.initialize({
startOnLoad: true,
'theme': theme,
});
</script>
{{ end -}}
{{ .Page.Store.Set "needMermaid" true }}
<div class="mermaid" align="center">{{ safeHTML .Inner }}</div> when including the shortcode within {{< mermaid >}}
graph LR
a-->b
{{< /mermaid >}} I got no mermaid diagram rendered (but just a center aligned text Any advice is much appreciated. |
Beta Was this translation helpful? Give feedback.
-
Just a tip for anyone searching for toggle theme mode using mermaid, live example here: <div class="mermaid">
{{- .Inner | safeHTML }}
</div>
{{ .Page.Store.Set "hasMermaid" true }}
{{ if .Page.Store.Get "hasMermaid" }}
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>
const elementCode = '.mermaid'
const loadMermaid = function(theme) {
mermaid.initialize({theme})
mermaid.init({theme}, document.querySelectorAll(elementCode))
}
const saveOriginalData = function(){
return new Promise((resolve, reject) => {
try {
var els = document.querySelectorAll(elementCode),
count = els.length;
els.forEach(element => {
element.setAttribute('data-original-code', element.innerHTML)
count--
if(count == 0){
resolve()
}
});
} catch (error) {
reject(error)
}
})
}
const resetProcessed = function(){
return new Promise((resolve, reject) => {
try {
var els = document.querySelectorAll(elementCode),
count = els.length;
els.forEach(element => {
if(element.getAttribute('data-original-code') != null){
element.removeAttribute('data-processed')
element.innerHTML = element.getAttribute('data-original-code')
}
count--
if(count == 0){
resolve()
}
});
} catch (error) {
reject(error)
}
})
}
saveOriginalData()
.catch( console.error )
let statusTheme = localStorage.getItem("pref-theme")
if (statusTheme == 'dark') {
resetProcessed()
.then(loadMermaid('dark'))
.catch(console.error)
}
if (statusTheme == 'light') {
resetProcessed()
.then(loadMermaid('neutral'))
.catch(console.error)
}
document.getElementById("theme-toggle").addEventListener("click",()=>{
resetProcessed()
.then(loadMermaid(mermaid.mermaidAPI.getConfig().theme == 'neutral' ? 'dark' : 'neutral'))
.catch(console.error)
})
</script>
{{ end }} thanks to mermaid-js/mermaid#1945 (comment) |
Beta Was this translation helpful? Give feedback.
-
Hi!
Starting from the file
single.html
, which is underthemes/PaperMod/layouts/_default
, I edited it adding the following Mermaid codeI retrieved that code from the Hugo website, here https://gohugo.io/content-management/diagrams/#mermaid-diagrams
I put the modified file
single.html
in/layouts/_default/
, leaving the originalsingle.html
underthemes/PaperMod/layouts/_default
.Now the files
single.hml
under/layouts/_default/
has the following lines:Unfortunately, Mermaid doesn't work.
Where was I wrong?
Beta Was this translation helpful? Give feedback.
All reactions