2020-10-25 00:30:20 -07:00
# Directives
2020-06-14 18:41:27 +02:00
2020-10-28 00:37:33 -07:00
**Edit this Page** [![N|Solid ](img/GitHub-Mark-32px.png )](https://github.com/mermaid-js/mermaid/blob/develop/docs/directives.md)
2020-08-21 01:01:06 +08:00
## Directives
2022-09-05 01:00:47 +05:30
2022-01-18 23:02:16 +01:00
Directives gives a diagram author the capability to alter the appearance of a diagram before rendering by changing the applied configuration.
2020-11-08 18:58:46 -08:00
2022-05-31 23:18:45 +02:00
The significance of having directives is that you have them available while writing the diagram, and can modify the default global and diagram specific configurations. So, directives are applied on top of the default configurations. The beauty of directives is that you can use them to alter configuration settings for a specific diagram, i.e. at an individual level.
2020-08-21 01:01:06 +08:00
2022-09-05 01:00:47 +05:30
While directives allow you to change most of the default configuration settings, there are some that are not available, that too for security reasons. Also, you do have the _option to define the set of configurations_ that you would allow to be available to the diagram author for overriding with help of directives.
2020-07-17 20:00:24 -07:00
2022-05-10 20:51:55 +02:00
## Types of Directives options
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
Mermaid basically supports two types of configuration options to be overridden by directives.
2020-07-17 20:00:24 -07:00
2022-09-05 01:00:47 +05:30
1. _General/Top Level configurations_ : These are the configurations that are available and applied to all the diagram. **Some of the most important top-level** configurations are:
- theme
- fontFamily
- logLevel
- securityLevel
- startOnLoad
- secure
2020-07-17 20:00:24 -07:00
2022-09-05 01:00:47 +05:30
2. _Diagram specific configurations_ : These are the configurations that are available and applied to a specific diagram. For each diagram there are specific configuration that will alter how that particular diagram looks and behaves.
For example, `mirrorActors` is a configuration that is specific to the `SequenceDiagram` and alter whether the actors are mirrored or not. So this config is available only for the `SequenceDiagram` type.
2022-05-10 20:51:55 +02:00
**NOTE:** These options listed here are not all the configuration options. To get hold of all the configuration options, please refer to the [defaultConfig.js ](https://github.com/mermaid-js/mermaid/blob/develop/src/defaultConfig.js ) in the source code.
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
```
Soon we plan to publish a complete list of top-level configurations & all the diagram specific configurations,
with their possible values in the docs
```
2022-09-05 01:00:47 +05:30
## Declaring directives
2022-05-10 20:51:55 +02:00
Now that we have defined the types of configurations that are available, we can learn how to declare directives.
A directive always starts and end `%%` sign with directive text in between, like `%% {directive_text} %%` .
2022-09-05 01:00:47 +05:30
Here the structure of a directive text is like a nested key-value pair map or a JSON object with root being _init_ . Where all the general configurations are defined in the top level, and all the diagram specific configurations are defined one level deeper with diagram type as key/root for that section.
2022-05-10 20:51:55 +02:00
Following code snippet shows the structure of a directive:
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
```
%%{
init: {
"theme": "dark",
"fontFamily": "monospace",
"logLevel": "info",
"flowchart": {
"htmlLabels": true,
"curve": "linear"
},
"sequence": {
"mirrorActors": true
}
}
}%%
```
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
You can also define the directives in a single line, like this:
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
```
%%{init: { **insert argument here** }}%%
```
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
For example, the following code snippet:
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
```
%%{init: { "sequence": { "mirrorActors":false }}}%%
```
2020-07-17 20:00:24 -07:00
2022-05-10 20:51:55 +02:00
**Notes:**
2020-11-08 18:58:46 -08:00
The json object that is passed as {**argument** } must be valid key value pairs and encased in quotation marks or it will be ignored.
2022-01-18 23:02:16 +01:00
Valid Key Value pairs can be found in config.
2020-08-21 01:01:06 +08:00
2022-05-10 20:51:55 +02:00
Example with a simple graph:
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
```mermaid-example
2020-06-14 18:41:27 +02:00
%%{init: { 'logLevel': 'debug', 'theme': 'dark' } }%%
2022-05-10 20:51:55 +02:00
graph LR
2020-06-14 18:41:27 +02:00
A-->B
```
2022-05-10 20:51:55 +02:00
Here the directive declaration will set the `logLevel` to `debug` and the `theme` to `dark` for a rendered mermaid diagram, changing the appearance of the diagram itself.
2020-06-14 18:41:27 +02:00
2022-05-10 20:51:55 +02:00
Note: You can use 'init' or 'initialize' as both acceptable as init directives. Also note that `%%init%%` and `%%initialize%%` directives will be grouped together after they are parsed. This means:
2020-06-14 18:41:27 +02:00
2022-01-18 23:02:16 +01:00
```mmd2
2020-06-14 18:41:27 +02:00
%%{init: { 'logLevel': 'debug', 'theme': 'forest' } }%%
%%{initialize: { 'logLevel': 'fatal', "theme":'dark', 'startOnLoad': true } }%%
...
```
2022-05-10 20:51:55 +02:00
parsing the above generates a single `%%init%%` JSON object below, combining the two directives and carrying over the last value given for `loglevel` :
2020-06-14 18:41:27 +02:00
2021-10-31 14:45:41 -07:00
```json5
2020-06-14 18:41:27 +02:00
{
logLevel: 'fatal',
theme: 'dark',
2022-09-05 01:00:47 +05:30
startOnLoad: true,
2020-06-14 18:41:27 +02:00
}
```
2020-11-08 18:58:46 -08:00
This will then be sent to `mermaid.initialize(...)` for rendering.
2020-08-21 01:01:06 +08:00
2022-05-10 20:51:55 +02:00
## Directive Examples
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
More directive examples for diagram specific configuration overrides
Now that the concept of directives has been explained, Let us see some more examples for directives usage:
### Changing Theme via directive
2022-09-05 01:00:47 +05:30
The following code snippet changes theme to forest:
2020-06-14 18:41:27 +02:00
2022-09-05 01:00:47 +05:30
`%%{init: { "theme": "forest" } }%%`
2020-06-14 18:41:27 +02:00
2022-05-10 20:51:55 +02:00
Possible themes value are: `default` ,`base` , `dark` , `forest` and `neutral` .
Default Value is `default` .
Example:
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
```mermaid-example
%%{init: { "theme": "forest" } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
2020-06-14 18:41:27 +02:00
2022-01-18 08:00:06 +01:00
```
2022-05-10 20:51:55 +02:00
### Changing fontFamily via directive
2022-09-05 01:00:47 +05:30
The following code snippet changes theme to forest:
2022-05-10 20:51:55 +02:00
2022-09-05 01:00:47 +05:30
`%%{init: { "fontFamily": "Trebuchet MS, Verdana, Arial, Sans-Serif" } }%%`
2022-05-10 20:51:55 +02:00
Example:
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
```mermaid-example
%%{init: { "fontFamily": "Trebuchet MS, Verdana, Arial, Sans-Serif" } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
### Changing logLevel via directive
2022-09-05 01:00:47 +05:30
The following code snippet changes theme to forest:
2022-05-10 20:51:55 +02:00
2022-09-05 01:00:47 +05:30
`%%{init: { "logLevel": 2 } }%%`
2022-05-10 20:51:55 +02:00
Possible logLevel values are:
2022-09-05 01:00:47 +05:30
- `1` for _debug_ ,
- `2` for _info_
- `3` for _warn_
- `4` for _error_
- `5` for _only fatal errors_
2022-05-10 20:51:55 +02:00
Default Value is `5` .
Example:
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
```mermaid-example
%%{init: { "logLevel": 2 } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
### Changing flowchart config via directive
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
Some common flowchart configurations are:
2022-09-05 01:00:47 +05:30
- _htmlLabels_: true/false
- _curve_: linear/curve
- _diagramPadding_: number
- _useMaxWidth_: number
2022-05-10 20:51:55 +02:00
For complete list of flowchart configurations, see [defaultConfig.js ](https://github.com/mermaid-js/mermaid/blob/develop/src/defaultConfig.js ) in the source code.
2022-09-05 01:00:47 +05:30
_Soon we plan to publish a complete list all diagram specific configurations updated in the docs_
2022-05-10 20:51:55 +02:00
The following code snippet changes flowchart config:
2022-09-05 01:00:47 +05:30
`%%{init: { "flowchart": { "htmlLabels": true, "curve": "linear" } } }%%`
2022-05-10 20:51:55 +02:00
Here were are overriding only the flowchart config, and not the general config, where HtmlLabels is set to true and curve is set to linear.
```mermaid-example
%%{init: { "flowchart": { "htmlLabels": true, "curve": "linear" } } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
### Changing Sequence diagram config via directive
2022-09-05 01:00:47 +05:30
2022-05-10 20:51:55 +02:00
Some common sequence configurations are:
2022-09-05 01:00:47 +05:30
- _width_: number
- _height_: number
- _messageAlign_: left, center, right
- _mirrorActors_: boolean
- _useMaxWidth_: boolean
- _rightAngles_: boolean
- _showSequenceNumbers_: boolean
- _wrap_: boolean
For complete list of sequence diagram configurations, see _defaultConfig.js_ in the source code.
_Soon we plan to publish a complete list all diagram specific configurations updated in the docs_
2022-05-10 20:51:55 +02:00
So, `wrap` by default has a value of `false` for sequence diagrams.
Let us see an example:
```mermaid-example
2020-06-14 18:41:27 +02:00
sequenceDiagram
2022-05-10 20:51:55 +02:00
Alice->Bob: Hello Bob, how are you?
2022-05-31 23:18:45 +02:00
Bob->Alice: Fine, How did you mother like the book I suggested? And did you catch with the new book about alien invasion?
2022-05-10 20:51:55 +02:00
Alice->Bob: Good.
Bob->Alice: Cool
2020-06-14 18:41:27 +02:00
```
2022-05-31 23:18:45 +02:00
Now let us enable wrap for sequence diagrams.
2021-01-21 23:29:22 -08:00
2022-05-10 20:51:55 +02:00
The following code snippet changes sequence diagram config for `wrap` to `true` :
2021-01-21 23:29:22 -08:00
2022-09-05 01:00:47 +05:30
`%%{init: { "sequence": { "wrap": true} } }%%`
2021-01-21 23:29:22 -08:00
2022-05-10 20:51:55 +02:00
Using in the diagram above, the wrap will be enabled.
2020-06-14 18:41:27 +02:00
2022-05-10 20:51:55 +02:00
```mermaid-example
%%{init: { "sequence": { "wrap": true, "width":300 } } }%%
sequenceDiagram
Alice->Bob: Hello Bob, how are you?
2022-05-31 23:18:45 +02:00
Bob->Alice: Fine, How did you mother like the book I suggested? And did you catch with the new book about alien invasion?
2022-05-10 20:51:55 +02:00
Alice->Bob: Good.
Bob->Alice: Cool
```