Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for mermaid diagrams #244

Merged
merged 3 commits into from Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions content/notes/config.md
Expand Up @@ -57,6 +57,9 @@ enableRecentNotes: false
enableGitHubEdit: true
GitHubLink: https://github.com/jackyzha0/quartz/tree/hugo/content

# whether to render mermaid diagrams
enableMermaid: true

# whether to use Operand to power semantic search
# IMPORTANT: replace this API key with your own if you plan on using
# Operand search!
Expand Down
1 change: 1 addition & 0 deletions data/config.yaml
Expand Up @@ -11,6 +11,7 @@ enableFooter: true
enableContextualBacklinks: true
enableRecentNotes: false
enableGitHubEdit: true
enableMermaid: true
GitHubLink: https://github.com/jackyzha0/quartz/tree/hugo/content
search:
enableSemanticSearch: false
Expand Down
4 changes: 4 additions & 0 deletions layouts/_default/_markup/render-codeblock-mermaid.html
@@ -0,0 +1,4 @@
<div class="mermaid">
{{- .Inner | safeHTML }}
</div>
{{ .Page.Store.Set "hasMermaid" true }}
15 changes: 15 additions & 0 deletions layouts/partials/head.html
Expand Up @@ -50,6 +50,8 @@
<script src="{{$s.Permalink}}"></script>
{{end}}
{{partial "katex.html" .}}

{{partial "mermaid.html" .}}

<script src="https://unpkg.com/@floating-ui/core@0.7.3"></script>
<script src="https://unpkg.com/@floating-ui/dom@0.5.4"></script>
Expand Down Expand Up @@ -145,6 +147,19 @@

}
{{end}}

{{if $data.enableMermaid | default $.Site.Data.config.enableMermaid}}
var els = document.getElementsByClassName("mermaid");
if (els.length > 0) {
import('https://unpkg.com/mermaid@9/dist/mermaid.esm.min.mjs').then(
(obj) => {
// init forces mermaid to render mermaid markdown without waiting
// for DOMContentLoaded event
obj.default.init();
}
)
}
{{end}}
}

const init = (doc = document) => {
Expand Down
8 changes: 8 additions & 0 deletions layouts/partials/mermaid.html
@@ -0,0 +1,8 @@
{{if $.Site.Data.config.enableMermaid}}
{{ if .Page.Store.Get "hasMermaid" }}
<script type="module">
import mermaid from 'https://unpkg.com/mermaid@9/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });
</script>
{{ end }}
{{ end }}