mirror of
https://github.com/tomfran/typo.git
synced 2025-04-28 14:39:56 +03:00
make mermaid themes configurable
This commit is contained in:
parent
31ba2ee311
commit
f5f04dba37
3 changed files with 43 additions and 5 deletions
|
@ -73,13 +73,17 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{ if .Store.Get "hasMermaid" }}
|
{{ if .Store.Get "hasMermaid" }}
|
||||||
<script type="module">
|
{{ $mermaidDarkTheme := default "dark" (or .Params.mermaidDarkTheme .Site.Params.mermaidDarkTheme) }}
|
||||||
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
|
{{ $mermaidTheme := default "default" (or .Params.mermaidTheme .Site.Params.mermaidTheme) }}
|
||||||
mermaid.initialize({ startOnLoad: true });
|
<script defer
|
||||||
|
type="module"
|
||||||
|
id="mermaid_script"
|
||||||
|
data-light-theme="{{ $mermaidTheme }}"
|
||||||
|
data-dark-theme="{{ $mermaidDarkTheme }}"
|
||||||
|
src='{{ "js/mermaid.js" | relURL }}'>
|
||||||
</script>
|
</script>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
|
||||||
{{/* Next prev controls */}}
|
{{/* Next prev controls */}}
|
||||||
|
|
||||||
{{ if not (.Param "hidePagination") }}
|
{{ if not (.Param "hidePagination") }}
|
||||||
|
|
28
static/js/mermaid.js
Normal file
28
static/js/mermaid.js
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
|
||||||
|
|
||||||
|
const this_js_script = document.getElementById('mermaid_script');
|
||||||
|
const light_theme = this_js_script.getAttribute('data-light-theme');
|
||||||
|
const dark_theme = this_js_script.getAttribute('data-dark-theme');
|
||||||
|
|
||||||
|
function runmermaid() {
|
||||||
|
const theme = (document.body.classList.contains('dark') ? dark_theme : light_theme)
|
||||||
|
mermaid.initialize({ startOnLoad: false, theme: theme});
|
||||||
|
const items = document.querySelectorAll('.mermaid');
|
||||||
|
let counter = 0;
|
||||||
|
for (const item of items) {
|
||||||
|
const id = counter++;
|
||||||
|
if(item.originalCode === undefined) {
|
||||||
|
item.originalCode = item.textContent.trim();
|
||||||
|
}
|
||||||
|
mermaid.render("mermaid"+id, item.originalCode).then((val) => {
|
||||||
|
item.innerHTML = val.svg
|
||||||
|
}, (err) => {
|
||||||
|
console.log(err);
|
||||||
|
// Workaround: move incorrectly placed error messages into their diagram
|
||||||
|
item.innerHTML = "";
|
||||||
|
item.appendChild(document.getElementById("mermaid" + id));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.addEventListener('DOMContentLoaded', runmermaid);
|
||||||
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', runmermaid);
|
|
@ -89,4 +89,10 @@ You can easily generate favicons using [this website](https://realfavicongenerat
|
||||||
|
|
||||||
## Mermaid Diagrams
|
## Mermaid Diagrams
|
||||||
|
|
||||||
Mermaid diagrams are supported by theme, just follow [this reference](https://gohugo.io/content-management/diagrams/#mermaid-diagrams) to use them.
|
Mermaid diagrams are supported, just follow [this reference](https://gohugo.io/content-management/diagrams/#mermaid-diagrams) to use them. You can set Mermaid's light- and dark themes in your config; they switch with your blog's light/dark state. These are the defaults:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[params]
|
||||||
|
mermaidTheme = "default"
|
||||||
|
mermaidDarkTheme = "dark"
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue