collab/mermaid/docs/intro/syntax-reference.mdblame
View source
6dd74de1> **Warning**
6dd74de2>
6dd74de3> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT.
6dd74de4>
6dd74de5> ## Please edit the corresponding file in [/packages/mermaid/src/docs/intro/syntax-reference.md](../../packages/mermaid/src/docs/intro/syntax-reference.md).
6dd74de6
6dd74de7# Diagram Syntax
6dd74de8
6dd74de9Mermaid's syntax is used to create diagrams. You'll find that it is not too tricky and can be learned in a day. The next sections dive deep into the syntax of each diagram type.
6dd74de10
6dd74de11Syntax, together with Deployment and Configuration constitute the whole of Mermaid.
6dd74de12
6dd74de13Diagram Examples can be found in the [Mermaid Live Editor](https://mermaid.live), it is also a great practice area.
6dd74de14
6dd74de15## Syntax Structure
6dd74de16
6dd74de17One would notice that all **Diagrams definitions begin** with a declaration of the **diagram type**, followed by the definitions of the diagram and its contents. This declaration notifies the parser which kind of diagram the code is supposed to generate. The only exception to this a [Frontmatter](#frontmatter-for-diagram-code) configuration.
6dd74de18
6dd74de19Line comments can ignore anything on the line after '%% '.
6dd74de20
6dd74de21Unknown words and misspellings will break a diagram, while parameters silently fail.
6dd74de22
6dd74de23**Example** : The code below is for an Entity Relationship Diagram, specified by the `erDiagram` declaration. What follows is the definition of the different `Entities` represented in it.
6dd74de24
6dd74de25```mermaid-example
6dd74de26erDiagram
6dd74de27 CUSTOMER }|..|{ DELIVERY-ADDRESS : has
6dd74de28 CUSTOMER ||--o{ ORDER : places
6dd74de29 CUSTOMER ||--o{ INVOICE : "liable for"
6dd74de30 DELIVERY-ADDRESS ||--o{ ORDER : receives
6dd74de31 INVOICE ||--|{ ORDER : covers
6dd74de32 ORDER ||--|{ ORDER-ITEM : includes
6dd74de33 PRODUCT-CATEGORY ||--|{ PRODUCT : contains
6dd74de34 PRODUCT ||--o{ ORDER-ITEM : "ordered in"
6dd74de35```
6dd74de36
6dd74de37```mermaid
6dd74de38erDiagram
6dd74de39 CUSTOMER }|..|{ DELIVERY-ADDRESS : has
6dd74de40 CUSTOMER ||--o{ ORDER : places
6dd74de41 CUSTOMER ||--o{ INVOICE : "liable for"
6dd74de42 DELIVERY-ADDRESS ||--o{ ORDER : receives
6dd74de43 INVOICE ||--|{ ORDER : covers
6dd74de44 ORDER ||--|{ ORDER-ITEM : includes
6dd74de45 PRODUCT-CATEGORY ||--|{ PRODUCT : contains
6dd74de46 PRODUCT ||--o{ ORDER-ITEM : "ordered in"
6dd74de47```
6dd74de48
6dd74de49The [Getting Started](./getting-started.md) section can also provide some practical examples of mermaid syntax.
6dd74de50
6dd74de51## Diagram Breaking
6dd74de52
6dd74de53One should **beware the use of some words or symbols** that can break diagrams. These words or symbols are few and often only affect specific types of diagrams. The table below will continuously be updated.
6dd74de54
6dd74de55| Diagram Breakers | Reason | Solution |
6dd74de56| ------------------------------------------------------------------------------------ | ----------------------------------------------------------------------- | ------------------------------------------------- |
6dd74de57| **Comments** | | |
6dd74de58| [`%%{``}%%`](https://github.com/mermaid-js/mermaid/issues/1968) | Similar to [Directives](../config/directives.md) confuses the renderer. | In comments using `%%`, avoid using "{}". |
6dd74de59| **Flow-Charts** | | |
6dd74de60| 'end' | The word "End" can cause Flowcharts and Sequence diagrams to break | Wrap them in quotation marks to prevent breakage. |
6dd74de61| [Nodes inside Nodes](../syntax/flowchart.md?id=special-characters-that-break-syntax) | Mermaid gets confused with nested shapes | wrap them in quotation marks to prevent breaking |
6dd74de62
6dd74de63## Mermaid Live Editor
6dd74de64
6dd74de65Now, that you've seen what you should not add to your diagrams, you can play around with them in the [Mermaid Live Editor](https://mermaid.live).
6dd74de66
6dd74de67## Configuration
6dd74de68
6dd74de69Configuration is the third part of Mermaid, after deployment and syntax. It deals with the different ways that Mermaid can be customized across different deployments.
6dd74de70
6dd74de71If you are interested in altering and customizing your Mermaid Diagrams, you will find the methods and values available for [Configuration](../config/setup/README.md) here. It includes themes.
6dd74de72This section will introduce the different methods of configuring the behaviors and appearances of Mermaid Diagrams.
6dd74de73The following are the most commonly used methods, and they are all tied to Mermaid [Deployment](./getting-started.md) methods.
6dd74de74
6dd74de75### Configuration Section in the [Live Editor](https://mermaid.live).
6dd74de76
6dd74de77Here you can edit certain values to change the behavior and appearance of the diagram.
6dd74de78
6dd74de79Each of these techniques are functionally equivalent, but better for different deployments.
6dd74de80
6dd74de81### [The initialize() call](./getting-started.md#_3-calling-the-javascript-api)
6dd74de82
6dd74de83Used when Mermaid is called via an API, or through a `<script>` tag.
6dd74de84
6dd74de85### Frontmatter for diagram code
6dd74de86
6dd74de87Frontmatter is the term for adding YAML metadata at the start of code. This allows for reconfiguration of a diagram before it is rendered. You can pass metadata Frontmatter with your definition by adding `---` to the lines before and after the definition. This 'triple dash' MUST be the only character on the first line.
6dd74de88
6dd74de89Frontmatter uses YAML syntax. It requires any indentation to be consistent and settings are case sensitive. Mermaid will silently ignore misspelling, but badly formed parameters will break the diagram.
6dd74de90
6dd74de91```mermaid-example
6dd74de92---
6dd74de93title: Frontmatter Example
6dd74de94displayMode: compact
6dd74de95config:
6dd74de96 theme: forest
6dd74de97gantt:
6dd74de98 useWidth: 400
6dd74de99 compact: true
6dd74de100---
6dd74de101gantt
6dd74de102 section Waffle
6dd74de103 Iron : 1982, 3y
6dd74de104 House : 1986, 3y
6dd74de105```
6dd74de106
6dd74de107```mermaid
6dd74de108---
6dd74de109title: Frontmatter Example
6dd74de110displayMode: compact
6dd74de111config:
6dd74de112 theme: forest
6dd74de113gantt:
6dd74de114 useWidth: 400
6dd74de115 compact: true
6dd74de116---
6dd74de117gantt
6dd74de118 section Waffle
6dd74de119 Iron : 1982, 3y
6dd74de120 House : 1986, 3y
6dd74de121```
6dd74de122
6dd74de123### [Directives](../config/directives.md)
6dd74de124
6dd74de125Allows for the limited reconfiguration of a diagram just before it is rendered. It can alter the font style, color and other aesthetic aspects of the diagram. You can pass a directive alongside your definition inside `%%{ }%%`. It can be done either above or below your diagram definition.
6dd74de126
6dd74de127### [Theme Manipulation](../config/theming.md)
6dd74de128
6dd74de129An application of using Directives to change [Themes](../config/theming.md). `Theme` is a value within Mermaid's configuration that dictates the color scheme for diagrams.
6dd74de130
6dd74de131### Layout and look
6dd74de132
6dd74de133We've restructured how Mermaid renders diagrams, enabling new features like selecting layout and look. **Currently, this is supported for flowcharts and state diagrams**, with plans to extend support to all diagram types.
6dd74de134
6dd74de135### Selecting Diagram Looks
6dd74de136
6dd74de137Mermaid offers a variety of styles or “looks” for your diagrams, allowing you to tailor the visual appearance to match your specific needs or preferences. Whether you prefer a hand-drawn or classic style, you can easily customize your diagrams.
6dd74de138
6dd74de139**Available Looks:**
6dd74de140
6dd74de141- Hand-Drawn Look: For a more personal, creative touch, the hand-drawn look brings a sketch-like quality to your diagrams. This style is perfect for informal settings or when you want to add a bit of personality to your diagrams.
6dd74de142- Classic Look: If you prefer the traditional Mermaid style, the classic look maintains the original appearance that many users are familiar with. It’s great for consistency across projects or when you want to keep the familiar aesthetic.
6dd74de143
6dd74de144**How to Select a Look:**
6dd74de145
6dd74de146You can select a look by adding the look parameter in the metadata section of your Mermaid diagram code. Here’s an example:
6dd74de147
6dd74de148```mermaid-example
6dd74de149---
6dd74de150config:
6dd74de151 look: handDrawn
6dd74de152 theme: neutral
6dd74de153---
6dd74de154flowchart LR
6dd74de155 A[Start] --> B{Decision}
6dd74de156 B -->|Yes| C[Continue]
6dd74de157 B -->|No| D[Stop]
6dd74de158```
6dd74de159
6dd74de160```mermaid
6dd74de161---
6dd74de162config:
6dd74de163 look: handDrawn
6dd74de164 theme: neutral
6dd74de165---
6dd74de166flowchart LR
6dd74de167 A[Start] --> B{Decision}
6dd74de168 B -->|Yes| C[Continue]
6dd74de169 B -->|No| D[Stop]
6dd74de170```
6dd74de171
6dd74de172#### Selecting Layout Algorithms
6dd74de173
6dd74de174In addition to customizing the look of your diagrams, Mermaid Chart now allows you to choose different layout algorithms to better organize and present your diagrams, especially when dealing with more complex structures. The layout algorithm dictates how nodes and edges are arranged on the page.
6dd74de175
6dd74de176#### Supported Layout Algorithms:
6dd74de177
6dd74de178- Dagre (default): This is the classic layout algorithm that has been used in Mermaid for a long time. It provides a good balance of simplicity and visual clarity, making it ideal for most diagrams.
6dd74de179- ELK: For those who need more sophisticated layout capabilities, especially when working with large or intricate diagrams, the ELK (Eclipse Layout Kernel) layout offers advanced options. It provides a more optimized arrangement, potentially reducing overlapping and improving readability. This is not included out the box but needs to be added when integrating mermaid for sites/applications that want to have elk support.
6dd74de180
6dd74de181#### How to Select a Layout Algorithm:
6dd74de182
6dd74de183You can specify the layout algorithm directly in the metadata section of your Mermaid diagram code. Here’s an example:
6dd74de184
6dd74de185```mermaid-example
6dd74de186---
6dd74de187config:
6dd74de188 layout: elk
6dd74de189 look: handDrawn
6dd74de190 theme: dark
6dd74de191---
6dd74de192flowchart TB
6dd74de193 A[Start] --> B{Decision}
6dd74de194 B -->|Yes| C[Continue]
6dd74de195 B -->|No| D[Stop]
6dd74de196```
6dd74de197
6dd74de198```mermaid
6dd74de199---
6dd74de200config:
6dd74de201 layout: elk
6dd74de202 look: handDrawn
6dd74de203 theme: dark
6dd74de204---
6dd74de205flowchart TB
6dd74de206 A[Start] --> B{Decision}
6dd74de207 B -->|Yes| C[Continue]
6dd74de208 B -->|No| D[Stop]
6dd74de209```
6dd74de210
6dd74de211In this example, the `layout: elk` line configures the diagram to use the ELK layout algorithm, along with the hand drawn look and forest theme.
6dd74de212
6dd74de213#### Customizing ELK Layout:
6dd74de214
6dd74de215When using the ELK layout, you can further refine the diagram’s configuration, such as how nodes are placed and whether parallel edges should be combined:
6dd74de216
6dd74de217- To combine parallel edges, use mergeEdges: true | false.
6dd74de218- To configure node placement, use nodePlacementStrategy with the following options:
6dd74de219 - SIMPLE
6dd74de220 - NETWORK_SIMPLEX
6dd74de221 - LINEAR_SEGMENTS
6dd74de222 - BRANDES_KOEPF (default)
6dd74de223
6dd74de224**Example configuration:**
6dd74de225
6dd74de226```
6dd74de227---
6dd74de228config:
6dd74de229 layout: elk
6dd74de230 elk:
6dd74de231 mergeEdges: true
6dd74de232 nodePlacementStrategy: LINEAR_SEGMENTS
6dd74de233---
6dd74de234flowchart LR
6dd74de235 A[Start] --> B{Choose Path}
6dd74de236 B -->|Option 1| C[Path 1]
6dd74de237 B -->|Option 2| D[Path 2]
6dd74de238
6dd74de239```
6dd74de240
6dd74de241#### Using Dagre Layout with Classic Look:
6dd74de242
6dd74de243Another example:
6dd74de244
6dd74de245```
6dd74de246---
6dd74de247config:
6dd74de248 layout: dagre
6dd74de249 look: classic
6dd74de250 theme: default
6dd74de251---
6dd74de252
6dd74de253flowchart LR
6dd74de254A[Start] --> B{Choose Path}
6dd74de255B -->|Option 1| C[Path 1]
6dd74de256B -->|Option 2| D[Path 2]
6dd74de257
6dd74de258```
6dd74de259
6dd74de260These options give you the flexibility to create diagrams that not only look great but are also arranged to best suit your data’s structure and flow.
6dd74de261
6dd74de262When integrating Mermaid, you can include look and layout configuration with the initialize call. This is also where you add the loading of elk.