Skip to content

Commit

Permalink
add mermaid integration
Browse files Browse the repository at this point in the history
  • Loading branch information
HidegonSan committed Nov 26, 2023
1 parent 2fdce2e commit 20c6bad
Show file tree
Hide file tree
Showing 9 changed files with 1,717 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ katex:
cd ./../../ && \
rm -rf ./tmp

mermaid:
curl https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js -o ./assets/js/mermaid.min.js

update: submodule goatcounter instantpage katex

debug-make-posts:
Expand Down
7 changes: 7 additions & 0 deletions assets/css/mermaid/mermaid_tweak.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.language-mermaid + button {
display: none !important;
}

.language-mermaid, .mermaid {
text-align: center;
}
1,646 changes: 1,646 additions & 0 deletions assets/js/mermaid.min.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions layouts/_default/_markup/render-codeblock-mermaid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


{{- $result := transform.HighlightCodeBlock . -}}

<div>
{{- $result.Wrapped | safeHTML -}}
</div>

{{- .Page.Store.Set "hasMermaid" true -}}
10 changes: 8 additions & 2 deletions layouts/partials/extend_head.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{{- partialCached "head/goatcounter.html" . -}}

{{- partial "head/katex.html" . -}}

{{- partial "head/seo.html" . -}}

{{/* {{- partialCached "head/anti_adblock.html" . -}} */}}
Expand All @@ -17,3 +15,11 @@
{{- partial "head/css/spoiler_css.html" . -}}

{{- partial "head/css/color_css.html" . -}}

{{- partial "head/css/mermaid_css.html" . -}}

{{/* libraries */}}

{{- partial "head/katex.html" . -}}

{{- partial "head/mermaid.html" . -}}
9 changes: 9 additions & 0 deletions layouts/partials/head/css/mermaid_css.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{/* mermaid form css */}}

{{- if or (.Page.Store.Get "hasMermaid") (.HasShortcode "mermaid") -}}
<style>
{{- with resources.Get "css/mermaid/mermaid_tweak.css" | toCSS | minify -}}
{{- .Content | safeCSS -}}
{{- end -}}
</style>
{{- end -}}
29 changes: 29 additions & 0 deletions layouts/partials/head/mermaid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{- if or (.Page.Store.Get "hasMermaid") (.HasShortcode "mermaid") -}}

{{- $mermaid_license_body := "/*\nSPDX-License-Identifier: MIT\nLicensed under the MIT license.\nhttps://github.com/mermaid-js/mermaid/blob/develop/LICENSE\nhttps://opensource.org/license/mit/\n*/\n" -}}
{{- $mermaid_license_js := $mermaid_license_body | resources.FromString "js/mermaid_license.js" -}}

{{- with (slice $mermaid_license_js (resources.Get "js/mermaid.min.js")) | resources.Concat "assets/js/mermaid.min.js" | fingerprint -}}
<script
defer
crossorigin="anonymous"
type="module"
src="{{- .RelPermalink -}}"
integrity="{{- .Data.Integrity -}}"
></script>
{{- end -}}

<script>
document.addEventListener("DOMContentLoaded", async function() {
mermaid.initialize({ startOnLoad: false })
await mermaid.run({
querySelector: ".language-mermaid, .mermaid",
suppressErrors: true,
});
document.querySelectorAll(".language-mermaid").forEach(function(e) {
e.style.cssText += "background: none !important;"
})
})
</script>

{{- end -}}
6 changes: 3 additions & 3 deletions layouts/shortcodes/katex.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{- if eq (.Get 0) "display" -}}
<p class="katex_sc" data-katex="\displaystyle {{ trim .Inner "\n\r" -}}">{{- trim .Inner "\n\r" -}}</p>
<p class="katex_sc" data-katex="\displaystyle {{ trim .Inner "\n\r" | safeHTMLAttr -}}">{{- trim .Inner "\n\r" | safeHTML -}}</p>
{{- else if eq (.Get 0) "inline" -}}
<span class="katex_sc" data-katex="{{- trim .Inner "\n\r" -}}">{{- trim .Inner "\n\r" -}}</span>
<span class="katex_sc" data-katex="{{- trim .Inner "\n\r" | safeHTMLAttr -}}">{{- trim .Inner "\n\r" | safeHTML -}}</span>
{{- else -}}
<p class="katex_sc" data-katex="\displaystyle {{ trim .Inner "\n\r" -}}">{{- trim .Inner "\n\r" -}}</p>
<p class="katex_sc" data-katex="\displaystyle {{ trim .Inner "\n\r" | safeHTMLAttr -}}">{{- trim .Inner "\n\r" | safeHTML -}}</p>
{{- end -}}
3 changes: 3 additions & 0 deletions layouts/shortcodes/mermaid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p class="mermaid">
{{ .Inner | safeHTML }}
</p>

0 comments on commit 20c6bad

Please sign in to comment.