|
1 | 1 | # Tips and Tricks |
2 | 2 |
|
3 | 3 |
|
| 4 | +## Using variables and macros in diagrams (MkDocs-Macros) |
| 5 | + |
| 6 | +What if your diagrams contain a repetitive string, like the URL of a website? |
| 7 | + |
| 8 | +Instead of writing: |
| 9 | + |
| 10 | +``` |
| 11 | +graph TD; |
| 12 | + Platform-->Gaming; |
| 13 | + click Gaming "http://127.0.0.1:8000/Gaming/"; |
| 14 | +``` |
| 15 | + |
| 16 | +You might want to use a **variable**: |
| 17 | + |
| 18 | +``` |
| 19 | +graph TD; |
| 20 | + Platform-->Gaming; |
| 21 | + click Gaming "{{ my_website }}/Gaming/"; |
| 22 | +``` |
| 23 | + |
| 24 | +With the variable defined in your project's config file: |
| 25 | + |
| 26 | +```yaml |
| 27 | +extra: |
| 28 | + my_website: http://127.0.0.1:8000 |
| 29 | +``` |
| 30 | +
|
| 31 | +
|
| 32 | +In this way, you will be able to change that value whenenever you need it. |
| 33 | +
|
| 34 | +To do that, you would have to use the [Mkdocs-Macros plugin](https://mkdocs-macros-plugin.readthedocs.io/). |
| 35 | +
|
| 36 | +
|
| 37 | +
|
| 38 | +This requires [installing the plugin](https://mkdocs-macros-plugin.readthedocs.io/en/latest/#standard-installation), and declaring it in the config file. |
| 39 | +
|
| 40 | +!!! Caution |
| 41 | + Variables are **not** part of the Mermaid specification. The Macros plugin simply expands the variables in the |
| 42 | + Markdown page, so that the result is a standard Mermaid diagram. |
| 43 | +
|
| 44 | + The same variables can be used in any part of the page, outside of Mermaid diagrams. |
| 45 | +
|
| 46 | +
|
| 47 | +Declare the plugins in the config file, in that order: |
| 48 | +
|
| 49 | +```yaml |
| 50 | +plugins: |
| 51 | + - search |
| 52 | + - macros |
| 53 | + - mermaid2 |
| 54 | +``` |
| 55 | +
|
| 56 | +!!! Tip "Using macros to generate diagrams" |
| 57 | + If you can program in Python, you could go further than that: you could use a Python module (`main.py`) to define |
| 58 | + [**macros** (functions)](https://mkdocs-macros-plugin.readthedocs.io/en/latest/macros/) |
| 59 | + that produce hyperlinks or pieces of diagrams from data, |
| 60 | + or even complete diagrams from a source file. |
| 61 | + |
4 | 62 |
|
5 | 63 | ## Setting the security level to "loose" |
6 | 64 |
|
|
0 commit comments