mermaid/docs/config/directives.md

333 lines
9.6 KiB
Markdown
Raw Normal View History

2022-10-18 04:58:51 +02:00
> **Warning**
>
> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT.
>
2022-10-28 21:16:25 +02:00
> ## Please edit the corresponding file in [/packages/mermaid/src/docs/config/directives.md](../../packages/mermaid/src/docs/config/directives.md).
2022-09-03 09:35:47 +02:00
2022-09-03 05:43:37 +02:00
# Directives
2023-08-21 11:32:05 +02:00
> **Warning**
2023-10-04 11:01:57 +02:00
> Directives are deprecated from v10.5.0. Please use the `config` key in frontmatter to pass configuration. See [Configuration](./configuration.md) for more details.
2023-08-21 11:32:05 +02:00
2022-09-03 05:43:37 +02:00
## Directives
Directives give a diagram author the capability to alter the appearance of a diagram before rendering by changing the applied configuration.
2022-09-03 05:43:37 +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 configuration. The beauty of directives is that you can use them to alter configuration settings for a specific diagram, i.e. at an individual level.
2022-09-03 05:43:37 +02:00
While directives allow you to change most of the default configuration settings, there are some that are not available, for security reasons. Also, you have the _option to define the set of configurations_ that you wish to allow diagram authors to override with directives.
2022-09-03 05:43:37 +02:00
## Types of Directives options
Mermaid basically supports two types of configuration options to be overridden by directives.
2022-09-05 16:18:38 +02:00
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:
2022-09-03 05:43:37 +02:00
- theme
- fontFamily
- logLevel
- securityLevel
- startOnLoad
- secure
2022-09-03 05:43:37 +02:00
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 alters whether the actors are mirrored or not. So this config is available only for the `SequenceDiagram` type.
2022-09-03 05:43:37 +02:00
**NOTE:** Not all configuration options are listed here. To get hold of all the configuration options, please refer to the [defaultConfig.ts](https://github.com/mermaid-js/mermaid/blob/develop/packages/mermaid/src/defaultConfig.ts) in the source code.
2022-09-03 05:43:37 +02:00
2022-12-15 22:11:07 +01:00
> **Note**
> We plan to publish a complete list of top-level configurations & diagram-specific configurations with their possible values in the docs soon.
2022-09-03 05:43:37 +02:00
## Declaring directives
Now that we have defined the types of configurations that are available, we can learn how to declare directives.
A directive always starts and ends with `%%` signs with directive text in between, like `%% {directive_text} %%`.
2022-09-03 05:43:37 +02:00
2022-09-03 09:35:47 +02:00
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-09-03 05:43:37 +02:00
The following code snippet shows the structure of a directive:
2022-09-03 05:43:37 +02:00
2022-09-05 16:18:38 +02:00
%%{
init: {
"theme": "dark",
"fontFamily": "monospace",
"logLevel": "info",
"flowchart": {
"htmlLabels": true,
"curve": "linear"
},
"sequence": {
"mirrorActors": true
}
}
}%%
2022-09-03 05:43:37 +02:00
You can also define the directives in a single line, like this:
%%{init: { **insert configuration options here** } }%%
2022-09-03 05:43:37 +02:00
For example, the following code snippet:
2022-09-05 16:18:38 +02:00
%%{init: { "sequence": { "mirrorActors":false }}}%%
2022-09-03 05:43:37 +02:00
**Notes:**
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-09-03 05:43:37 +02:00
Valid Key Value pairs can be found in config.
Example with a simple graph:
```mermaid-example
%%{init: { 'logLevel': 'debug', 'theme': 'dark' } }%%
graph LR
A-->B
```
```mermaid
%%{init: { 'logLevel': 'debug', 'theme': 'dark' } }%%
graph LR
A-->B
```
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.
Note: You can use 'init' or 'initialize' as both are acceptable as init directives. Also note that `%%init%%` and `%%initialize%%` directives will be grouped together after they are parsed.
2022-09-03 05:43:37 +02:00
2022-10-15 05:50:11 +02:00
```mermaid-example
%%{init: { 'logLevel': 'debug', 'theme': 'forest' } }%%
%%{initialize: { 'logLevel': 'fatal', "theme":'dark', 'startOnLoad': true } }%%
...
```
```mermaid
2022-09-03 05:43:37 +02:00
%%{init: { 'logLevel': 'debug', 'theme': 'forest' } }%%
%%{initialize: { 'logLevel': 'fatal', "theme":'dark', 'startOnLoad': true } }%%
...
```
For example, parsing the above generates a single `%%init%%` JSON object below, combining the two directives and carrying over the last value given for `loglevel`:
2022-09-03 05:43:37 +02:00
2022-10-15 05:50:11 +02:00
```json
2022-09-03 05:43:37 +02:00
{
2022-10-15 05:50:11 +02:00
"logLevel": "fatal",
"theme": "dark",
"startOnLoad": true
2022-09-03 05:43:37 +02:00
}
```
This will then be sent to `mermaid.initialize(...)` for rendering.
## Directive Examples
Now that the concept of directives has been explained, let us see some more examples of directive usage:
2022-09-03 05:43:37 +02:00
### Changing theme via directive
2022-09-03 05:43:37 +02:00
The following code snippet changes `theme` to `forest`:
2022-09-03 05:43:37 +02:00
`%%{init: { "theme": "forest" } }%%`
Possible theme values are: `default`, `base`, `dark`, `forest` and `neutral`.
2022-09-03 05:43:37 +02:00
Default Value is `default`.
Example:
```mermaid-example
%%{init: { "theme": "forest" } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
```mermaid
%%{init: { "theme": "forest" } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
### Changing fontFamily via directive
The following code snippet changes fontFamily to Trebuchet MS, Verdana, Arial, Sans-Serif:
2022-09-03 05:43:37 +02:00
`%%{init: { "fontFamily": "Trebuchet MS, Verdana, Arial, Sans-Serif" } }%%`
Example:
```mermaid-example
%%{init: { "fontFamily": "Trebuchet MS, Verdana, Arial, Sans-Serif" } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
```mermaid
%%{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
The following code snippet changes `logLevel` to `2`:
2022-09-03 05:43:37 +02:00
`%%{init: { "logLevel": 2 } }%%`
Possible `logLevel` values are:
2022-09-03 05:43:37 +02:00
2022-09-03 10:00:16 +02:00
- `1` for _debug_,
- `2` for _info_
- `3` for _warn_
- `4` for _error_
- `5` for _only fatal errors_
2022-09-03 05:43:37 +02:00
Default Value is `5`.
Example:
```mermaid-example
%%{init: { "logLevel": 2 } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
```mermaid
%%{init: { "logLevel": 2 } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
### Changing flowchart config via directive
Some common flowchart configurations are:
2022-09-03 10:00:16 +02:00
- _htmlLabels_: true/false
- _curve_: linear/curve
- _diagramPadding_: number
- _useMaxWidth_: number
2022-09-03 05:43:37 +02:00
For a complete list of flowchart configurations, see [defaultConfig.ts](https://github.com/mermaid-js/mermaid/blob/develop/packages/mermaid/src/defaultConfig.ts) in the source code.
_Soon we plan to publish a complete list of all diagram-specific configurations updated in the docs._
2022-09-03 05:43:37 +02:00
The following code snippet changes flowchart config:
`%%{init: { "flowchart": { "htmlLabels": true, "curve": "linear" } } }%%`
Here we are overriding only the flowchart config, and not the general config, setting `htmlLabels` to `true` and `curve` to `linear`.
2022-09-03 05:43:37 +02:00
```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 16:18:38 +02:00
```mermaid
%%{init: { "flowchart": { "htmlLabels": true, "curve": "linear" } } }%%
graph TD
A(Forest) --> B[/Another/]
A --> C[End]
subgraph section
B
C
end
```
2022-09-03 05:43:37 +02:00
### Changing Sequence diagram config via directive
Some common sequence diagram configurations are:
2022-09-03 05:43:37 +02:00
2022-09-03 10:00:16 +02:00
- _width_: number
- _height_: number
- _messageAlign_: left, center, right
- _mirrorActors_: boolean
- _useMaxWidth_: boolean
- _rightAngles_: boolean
- _showSequenceNumbers_: boolean
- _wrap_: boolean
2022-09-03 09:35:47 +02:00
For a complete list of sequence diagram configurations, see [defaultConfig.ts](https://github.com/mermaid-js/mermaid/blob/develop/packages/mermaid/src/defaultConfig.ts) in the source code.
_Soon we plan to publish a complete list of all diagram-specific configurations updated in the docs._
2022-09-03 05:43:37 +02:00
So, `wrap` by default has a value of `false` for sequence diagrams.
Let us see an example:
```mermaid-example
sequenceDiagram
Alice->Bob: Hello Bob, how are you?
Bob->Alice: Fine, how did your mother like the book I suggested? And did you catch the new book about alien invasion?
2022-09-03 05:43:37 +02:00
Alice->Bob: Good.
Bob->Alice: Cool
```
```mermaid
sequenceDiagram
Alice->Bob: Hello Bob, how are you?
Bob->Alice: Fine, how did your mother like the book I suggested? And did you catch the new book about alien invasion?
2022-09-03 05:43:37 +02:00
Alice->Bob: Good.
Bob->Alice: Cool
```
Now let us enable wrap for sequence diagrams.
The following code snippet changes sequence diagram config for `wrap` to `true`:
`%%{init: { "sequence": { "wrap": true} } }%%`
By applying that snippet to the diagram above, `wrap` will be enabled:
2022-09-03 05:43:37 +02:00
```mermaid-example
%%{init: { "sequence": { "wrap": true, "width":300 } } }%%
sequenceDiagram
Alice->Bob: Hello Bob, how are you?
Bob->Alice: Fine, how did your mother like the book I suggested? And did you catch the new book about alien invasion?
2022-09-03 05:43:37 +02:00
Alice->Bob: Good.
Bob->Alice: Cool
```
2022-09-05 16:18:38 +02:00
```mermaid
%%{init: { "sequence": { "wrap": true, "width":300 } } }%%
sequenceDiagram
Alice->Bob: Hello Bob, how are you?
Bob->Alice: Fine, how did your mother like the book I suggested? And did you catch the new book about alien invasion?
2022-09-05 16:18:38 +02:00
Alice->Bob: Good.
Bob->Alice: Cool
```