Using Mermaid in Hugo
Create a shortcode for Mermaid
Create a
layouts/shortcodes
directory in your theme directory.Inside
layouts/shortcodes
, create a file namedmermaid.html
. The filename is the same as the shortcode name we are going to use. Paste the following content into the file:<div class="mermaid"> {{.Inner}} </div>
Load MermaidJS on posts that requires it. Find somewhere in your theme that’s suitable for adding a
<scritp>
and add the following snippet:{{ if (.Params.mermaid) }} <!-- MermaidJS support --> <script async src="https://unpkg.com/mermaid@8.2.3/dist/mermaid.min.js"></script> {{ end }}
Use the mermaid shortcode to create a graph
In a post you want to use Mermaid, you need to turn the
mermaid
option on to load the required script. For example, the configuration for this post looks like:+++ title = "Using Mermaid in Hugo" date = 2020-08-09T20:47:40+08:00 mermaid = true +++
Use the short code. For example:
{{< mermaid >}} sequenceDiagram participant Alice participant Bob Alice->>John: Hello John, how are you? loop Healthcheck John->John: Fight against hypochondria end Note right of John: Rational thoughts <br/>prevail... John-->Alice: Great! John->Bob: How about you? Bob-->John: Jolly good! {{< /mermaid >}}
generates the following graph:
sequenceDiagram
participant Alice
participant Bob
Alice->>John: Hello John, how are you?
loop Healthcheck
John->John: Fight against hypochondria
end
Note right of John: Rational thoughts
prevail... John-->Alice: Great! John->Bob: How about you? Bob-->John: Jolly good!
prevail... John-->Alice: Great! John->Bob: How about you? Bob-->John: Jolly good!