2022-03-08 13:37:17 -05:00
---
title: Diagrams
2022-11-17 10:14:29 -05:00
description: Use fenced code blocks and markdown render hooks to display diagrams.
2022-03-08 13:37:17 -05:00
categories: [content management]
keywords: [diagrams,drawing]
menu:
docs:
2022-11-17 10:14:29 -05:00
parent: content-management
weight: 50
weight: 50
2023-12-04 09:14:18 -05:00
toc: true
2022-03-08 13:37:17 -05:00
---
2023-12-04 09:14:18 -05:00
{{< new-in 0 . 93 . 0 > }}
2022-03-08 13:37:17 -05:00
2023-12-04 09:14:18 -05:00
## GoAT diagrams (ASCII)
2022-03-08 13:37:17 -05:00
2022-11-17 10:14:29 -05:00
Hugo supports [GoAT ](https://github.com/bep/goat ) natively. This means that this code block:
2022-03-08 13:37:17 -05:00
2022-11-17 10:14:29 -05:00
````txt
2022-03-08 13:37:17 -05:00
```goat
. . . .--- 1 .-- 1 / 1
/ \ | | .---+ .-+ +
/ \ .---+---. .--+--. | '--- 2 | '-- 2 / \ 2
+ + | | | | ---+ ---+ +
/ \ / \ .-+-. .-+-. .+. .+. | .--- 3 | .-- 3 \ / 3
/ \ / \ | | | | | | | | '---+ '-+ +
1 2 3 4 1 2 3 4 1 2 3 4 '--- 4 '-- 4 \ 4
```
````
Will be rendered as:
```goat
. . . .--- 1 .-- 1 / 1
/ \ | | .---+ .-+ +
/ \ .---+---. .--+--. | '--- 2 | '-- 2 / \ 2
+ + | | | | ---+ ---+ +
/ \ / \ .-+-. .-+-. .+. .+. | .--- 3 | .-- 3 \ / 3
/ \ / \ | | | | | | | | '---+ '-+ +
1 2 3 4 1 2 3 4 1 2 3 4 '--- 4 '-- 4 \ 4
```
2023-07-29 05:15:54 -04:00
## Mermaid diagrams
2022-03-08 13:37:17 -05:00
2022-11-17 10:14:29 -05:00
Hugo currently does not provide default templates for Mermaid diagrams. But you can easily add your own. One way to do it would be to create `layouts/_default/_markup/render-codeblock-mermaid.html` :
2022-03-08 13:37:17 -05:00
```go-html-template
2023-07-29 05:15:54 -04:00
< pre class = "mermaid" >
2022-03-08 13:37:17 -05:00
{{- .Inner | safeHTML }}
2023-07-29 05:15:54 -04:00
< / pre >
2022-03-08 13:37:17 -05:00
{{ .Page.Store.Set "hasMermaid" true }}
```
2022-11-17 10:14:29 -05:00
And then include this snippet at the bottom of the content template (**Note**: below `.Content` as the render hook is not processed until `.Content` is executed):
2022-03-08 13:37:17 -05:00
```go-html-template
{{ if .Page.Store.Get "hasMermaid" }}
2023-05-22 10:43:12 -04:00
< script type = "module" >
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
2022-03-08 13:37:17 -05:00
mermaid.initialize({ startOnLoad: true });
< / script >
{{ end }}
```
With that you can use the `mermaid` language in Markdown code blocks:
2023-07-29 05:15:54 -04:00
````text
2022-03-08 13:37:17 -05:00
```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!
```
2023-05-22 10:43:12 -04:00
````
2022-03-08 13:37:17 -05:00
2023-07-29 05:15:54 -04:00
## Goat ASCII diagram examples
2022-03-08 13:37:17 -05:00
### Graphics
```goat
.
0 3 P * Eye / ^ /
*-------* +y \ +) \ / Reflection
1 /| 2 /| ^ \ \ \ v
*-------* | | v0 \ v3 --------*--------
| |4 | |7 | *----\-----*
| *-----|-* +-----> +x / v X \ .-.< -------- o
|/ |/ / / o \ | / | Refraction / \
*-------* v / \ +-' / \
5 6 +z v1 *------------------* v2 | o-----o
v
```
### Complex
```goat
+-------------------+ ^ .---.
| A Box |__.--.__ __ .--> | .-. | |
| | '--' v | * |< --- | |
+-------------------+ '-' | |
Round *---(-. |
.-----------------. .-------. .----------. .-------. | | |
| Mixed Rounded | | | / Diagonals \ | | | | | |
| & Square Corners | '--. .--' / \ |---+---| '-)-' .--------.
'--+------------+-' .--. | '-------+--------' | | | | / Search /
| | | | '---. | '-------' | '-+------'
|< ---------- > | | | | v Interior | ^
' < --- ' ' ---- ' . ----------- . --- . . --- v |
.------------------. Diag line | .-------. +---. \ / . |
| if (a > b) +---. .--->| | | | | Curved line \ / / \ |
| obj->fcn() | \ / | '-------' |< -- ' + / \ |
'------------------' '--' '--+--------' .--. .--. | .-. +Done?+-'
.---+-----. | ^ |\ | | /| .--+ | | \ /
| | | Join \|/ | | Curved | \| |/ | | \ | \ /
| | +----> o --o-- '-' Vertical '--' '--' '-- '--' + .---.
< -- + --- + ----- ' | /| \ | | 3 |
v not:line 'quotes' .-' '---'
.-. .---+--------. / A || B *bold* | ^
| | | Not a dot | < --- + --- < -- A dash--is not a line v |
'-' '---------+--' / Nor/is this. ---
```
### Process
```goat
.
.---------. / \
| START | / \ .-+-------+-. ___________
'----+----' .-------. A / \ B | |COMPLEX| | / \ .-.
| | END |< ----- + CHOICE + ----- > | | | +--->+ PREPARATION +--->| X |
v '-------' \ / | |PROCESS| | \___________/ '-'
.---------. \ / '-+---+---+-'
/ INPUT / \ /
'-----+---' '
| ^
v |
.-----------. .-----+-----. .-.
| PROCESS +---------------->| PROCESS |< ------ + X |
'-----------' '-----------' '-'
```
### File tree
2022-11-17 10:14:29 -05:00
Created from < https: / / arthursonzogni . com / Diagon / # Tree >
2022-03-08 13:37:17 -05:00
```goat { width=300 color="orange" }
───Linux─┬─Android
├─Debian─┬─Ubuntu─┬─Lubuntu
│ │ ├─Kubuntu
│ │ ├─Xubuntu
│ │ └─Xubuntu
│ └─Mint
├─Centos
└─Fedora
```
2023-07-29 05:15:54 -04:00
### Sequence diagram
2022-03-08 13:37:17 -05:00
2022-11-17 10:14:29 -05:00
< https: / / arthursonzogni . com / Diagon / # Sequence >
2022-03-08 13:37:17 -05:00
```goat { class="w-40" }
┌─────┐ ┌───┐
│Alice│ │Bob│
└──┬──┘ └─┬─┘
│ │
│ Hello Bob! │
│───────────>│
│ │
│Hello Alice!│
│< ───────────│
┌──┴──┐ ┌─┴─┐
│Alice│ │Bob│
└─────┘ └───┘
```
### Flowchart
2022-11-17 10:14:29 -05:00
< https: / / arthursonzogni . com / Diagon / # Flowchart >
2022-03-08 13:37:17 -05:00
```goat
_________________
╱ ╲ ┌─────┐
╱ DO YOU UNDERSTAND ╲____________________________________________________│GOOD!│
╲ FLOW CHARTS? ╱ yes └──┬──┘
╲_________________╱ │
│no │
_________ ▽_________ ______________________ │
╱ ╲ ╱ ╲ ┌────┐ │
╱ OKAY, YOU SEE THE ╲________________╱ ... AND YOU CAN SEE ╲___│GOOD│ │
╲ LINE LABELED 'YES'? ╱ yes ╲ THE ONES LABELED 'NO'? ╱ yes└──┬─┘ │
╲___________________╱ ╲______________________╱ │ │
│no │no │ │
________ ▽_________ _________ ▽__________ │ │
╱ ╲ ┌───────────┐ ╱ ╲ │ │
╱ BUT YOU SEE THE ╲___│WAIT, WHAT?│ ╱ BUT YOU JUST ╲___ │ │
╲ ONES LABELED 'NO'? ╱ yes└───────────┘ ╲ FOLLOWED THEM TWICE? ╱ yes│ │ │
╲__________________╱ ╲____________________╱ │ │ │
│no │no │ │ │
┌───▽───┐ │ │ │ │
│LISTEN.│ └───────┬───────┘ │ │
└───┬───┘ ┌──────▽─────┐ │ │
┌─────▽────┐ │(THAT WASN'T│ │ │
│I HATE YOU│ │A QUESTION) │ │ │
└──────────┘ └──────┬─────┘ │ │
┌────▽───┐ │ │
│SCREW IT│ │ │
└────┬───┘ │ │
└─────┬─────┘ │
│ │
└─────┬─────┘
┌───────▽──────┐
│LET'S GO DRING│
└───────┬──────┘
┌─────────▽─────────┐
│HEY, I SHOULD TRY │
│INSTALLING FREEBSD!│
└───────────────────┘
```
### Table
2022-11-17 10:14:29 -05:00
< https: / / arthursonzogni . com / Diagon / # Table >
2022-03-08 13:37:17 -05:00
```goat { class="w-80 dark-blue" }
┌────────────────────────────────────────────────┐
│ │
├────────────────────────────────────────────────┤
│SYNTAX = { PRODUCTION } . │
├────────────────────────────────────────────────┤
│PRODUCTION = IDENTIFIER "=" EXPRESSION "." . │
├────────────────────────────────────────────────┤
│EXPRESSION = TERM { "|" TERM } . │
├────────────────────────────────────────────────┤
│TERM = FACTOR { FACTOR } . │
├────────────────────────────────────────────────┤
│FACTOR = IDENTIFIER │
├────────────────────────────────────────────────┤
│ | LITERAL │
├────────────────────────────────────────────────┤
│ | "[" EXPRESSION "]" │
├────────────────────────────────────────────────┤
│ | "(" EXPRESSION ")" │
├────────────────────────────────────────────────┤
│ | "{" EXPRESSION "}" . │
├────────────────────────────────────────────────┤
│IDENTIFIER = letter { letter } . │
├────────────────────────────────────────────────┤
│LITERAL = """" character { character } """" .│
└────────────────────────────────────────────────┘
```