Hugo Mermaid shortcode
A shortcode which allows to easily integrate Mermaid diagrams in your Hugo content. Mermaid is a JavaScript library which can draw various diagrams (flowchart, sequence diagram, class diagram, state diagram, …), it is well documented and there are plenty examples available.
This article was written for Hugo 0.74
.
The mermaid library is used to draw diagrams. At the time of writing
8.5.2
.
Mermaid will generate the diagrams on first page load BUT diagrams cannot not be embedded in a
display: none;
element (will cause errors).
The diagrams are generated on the fly client side, this might cause page jumps. Alternatively one can generate the mermaid images once and include them as svg files which saves compute time on the client in exchange for more bandwidth. If you want to pre-generate diagrams you can also use diagrams.net instead (online/desktop and free).
Usage
There are no options to speak of, place the mermaid syntax in the shortcode an off you go.
option | effect | optional | default |
---|---|---|---|
class | adds additional CSS class(es) | yes |
Example
{{< mermaid >}}
graph TD
A[Client] -->|tcp_123| B(Load Balancer)
B -->|tcp_456| C[Server1]
B -->|tcp_456| D[Server2]
{{< /mermaid >}}
graph TD A[Client] -->|tcp_123| B(Load Balancer) B -->|tcp_456| C[Server1] B -->|tcp_456| D[Server2]
Code
Adding mermaid support is very easy, just load the Javascript and add an element with the mermaid
class.
Because mermaid is fairly heavy it is only included if the shortcode is used at least once on a page. This is done by setting the scratch variable.
|
|
CDN are useful to save yourself bandwidth, however if they go down1 your site will break. In that case you would want to redirect to your own hosted copy instead so your site remains online.
This little javascript snippet will do just that, you have to wire it to the onerror event (see example above). Add a copy of the file to your static folder in Hugo.
|
|
It has happend that CDN’s go down for various reasons. Misconfigured network or general server issues. ↩︎
- Permalink: //oostens.me/posts/hugo-mermaid-shortcode/
- License: The text and content is licensed under CC BY-NC-SA 4.0. All source code I wrote on this page is licensed under The Unlicense; do as you please, I'm not liable nor provide warranty.