mermaid/docs/n00b-gettingStarted.md

213 lines
9.1 KiB
Markdown
Raw Normal View History

# A basic mermaid User-Guide for Beginners
2020-07-21 16:29:41 -07:00
**Edit this Page** [![N|Solid](./img/GitHub-Mark-32px.png)](https://github.com/mermaid-js/mermaid/blob/develop/docs/n00b-gettingStarted.md)
2020-07-15 21:30:23 -07:00
Creating diagrams and charts using mermaid code is simple.
2020-07-21 16:29:41 -07:00
The code is turned into a diagram in the web page with the use of a mermaid renderer.
2020-07-21 16:29:41 -07:00
The mermaid renderer is a piece of javascript that parses mermaid definitions, when called.
2020-07-22 21:28:02 -07:00
This then renders a diagram based on that code in SVG, alternatively it
2020-07-22 21:28:02 -07:00
Most web browsers, such as Firefox, Chrome and Safari, can render mermaid, Internet Explorer however cannot.
2019-11-07 23:37:46 +01:00
2020-07-22 21:28:02 -07:00
## For beginners, there are four relatively easy ways you can use mermaid:
1. Using the mermaid [live editor](https://mermaid-js.github.io/mermaid-live-editor/) For some popular video tutorials on the live editor go to [Overview](./n00b-overview.md).
2020-07-21 16:29:41 -07:00
2. Using one of the many [mermaid plugins](https://mermaid-js.github.io/mermaid/#/integrations).
2020-07-22 21:28:02 -07:00
3. Calling mermaid renderer with HTML,click here for more in depth information on [Usage](./usage.md).
4. Installing mermaid with npm and deploying it using a relative link in the `<script>` tag.
.
# Following either of these examples, you can get started with creating your own diagrams using mermaid code.
2020-02-22 22:16:00 -08:00
## 1. The mermaid live editor:
2020-02-22 22:16:00 -08:00
A great way to get started with mermaid is to visit [The mermaid live editor](https://mermaidjs.github.io/mermaid-live-editor).
In the `Code` section one can write or edit raw mermaid code, and instantly `Preview` the rendered result on the panel beside it.
2020-02-22 22:16:26 -08:00
2020-07-22 21:28:02 -07:00
**This is a great way to learn how to define a mermaid diagram.**
For some popular video tutorials on the live editor go to [Overview](./n00b-overview.md).
2020-07-22 21:28:02 -07:00
![Flowchart](./img/n00b-liveEditor.png)
2020-02-22 23:10:08 -08:00
2020-07-22 21:28:02 -07:00
**Notes:**
2020-07-22 21:28:02 -07:00
You can also click "Copy Markdown" to copy the markdown code for the diagram, that can then be pasted directly into your documentation.
2020-02-22 22:16:00 -08:00
2020-02-22 23:10:08 -08:00
![Flowchart](./img/liveEditorOptions.png)
2020-07-18 20:12:08 -07:00
The `Mermaid configuration` is for controlling mermaid behaviour. An easy introduction to mermaid configuration is found in the [Advanced usage](n00b-advanced.md) section. A complete configuration reference cataloguing default values is found on the [mermaidAPI](https://mermaid-js.github.io/mermaid/#/Setup) page.
2020-02-22 23:11:51 -08:00
## 2. Using mermaid plugins:
2020-07-18 20:12:08 -07:00
Thanks to the growing popularity of mermaid, many plugins already exist which incorporate a mermaid renderer. An extensive list can be found [here](./integrations.md).
2020-02-26 23:07:11 +01:00
2020-02-27 01:05:23 -08:00
One example in the list is the [Atlassian Confluence mermaid plugin](https://marketplace.atlassian.com/apps/1214124/mermaid-plugin-for-confluence?hosting=server&tab=overview)
When the mermaid plugin is installed on a Confluence server, one can insert a mermaid object into any Confluence page.
# Here is a step by step process for using the mermaid-Confluence plugin:
---
- In a Confluence page, Add Other macros.
![Flowchart](./img/n00b-Confluence1.png)
---
- Search for mermaid.
![Flowchart](./img/n00b-Confluence2.png)
---
2019-11-07 23:37:46 +01:00
- The mermaid object appears. Paste your mermaid code into it.
![Flowchart](./img/n00b-Confluence3.png)
---
2019-11-07 23:37:46 +01:00
- Save the page and the diagram appears.
![Flowchart](./img/n00b-Confluence4.png)
---
2020-07-22 21:28:02 -07:00
## There are two ways of hosting mermaid on a webpage. They will be discussed in 3 and 4, respectively.
2020-02-22 23:11:51 -08:00
## 3. mermaid using any web server (or just a browser):
2020-07-21 16:29:41 -07:00
**This is covered in detail in the [Usage section](https://mermaid-js.github.io/mermaid/#/usage)**
This method can be used with any common web server. Apache, IIS, nginx, node express [...], you pick your favourite.
We do not need to install anything on the server, apart from a program (like Notepad++) that can generate an html file, which is then deployed by a web browser (such as Firefox, Chrome, Safari, but not Internet Explorer).
2020-07-22 21:28:02 -07:00
So if you want to really simplify things when testing this out, don't use a web server at all but just create an HTML file locally and drag it into your browser window. The browser will do the work of rendering the mermaid diagrams according to the descriptions you've given!
2020-07-21 16:29:41 -07:00
### Note that all this is written in the html `<body>` section of the web page.
When writing the html file, we give the web browser three instructions inside the html code:
2020-02-27 01:07:25 -08:00
a. A reference for fetching the online mermaid renderer, which is written in Javascript.
2020-02-27 01:06:38 -08:00
b. The mermaid code for the diagram we want to create.
2020-02-27 01:06:38 -08:00
c. The `mermaid.initialize()` command to start the rendering process.
2020-07-22 21:28:02 -07:00
## This is what needs to go into the html file (and all of them are important):
2019-11-08 01:45:29 +01:00
2020-07-21 16:29:41 -07:00
### a. The reference to the mermaid renderer has to be contained in a `<script src>` tag like so:
```
<body>
2020-07-22 21:28:02 -07:00
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
</body>
```
2020-07-22 21:28:02 -07:00
### b. The embedded mermaid diagram definition is similarly placed inside a `<div>` tag:
```
<body>
Here is a mermaid diagram:
<div class="mermaid">
graph TD
A[Client] --> B[Load Balancer]
B --> C[Server01]
B --> D[Server02]
</div>
</body>
```
2020-07-22 21:28:02 -07:00
(take note that every mermaid chart/graph/diagram definition, has to have separate `<div>` tags.)
2020-07-21 16:29:41 -07:00
### c. When initializing mermaid using `mermaid.initialize()`, mermaid takes all the `<div class="mermaid">` tags it can find in the html body and starts to render them one by one. This is done like so:
```
<body>
<script>mermaid.initialize({startOnLoad:true});</script>
</body>
```
2020-07-21 16:29:41 -07:00
### *Finally*
### If the three steps mentioned are followed you will end up with something like this:
2020-07-22 21:28:02 -07:00
### Note that
2020-07-15 21:30:23 -07:00
```
<html>
<body>
2020-07-22 21:28:02 -07:00
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>mermaid.initialize({startOnLoad:true});</script>
Here is one mermaid diagram:
<div class="mermaid">
graph TD
A[Client] --> B[Load Balancer]
2019-11-28 12:34:26 +01:00
B --> C[Server1]
B --> D[Server2]
</div>
2019-11-28 12:34:26 +01:00
And here is another:
<div class="mermaid">
graph TD
2019-11-28 12:34:26 +01:00
A[Client] -->|tcp_123| B(Load Balancer)
B -->|tcp_456| C[Server1]
B -->|tcp_456| D[Server2]
</div>
</body>
</html>
```
2020-07-22 21:28:02 -07:00
### Save this to an html file and open it with a browser.
---
2020-07-22 21:28:02 -07:00
## 4. Calling mermaid from a relative link.
This method is similar to 3, if only a little more involved. The difference may be very subtle even, insignificant to a majority of people, but it offers its own advantages.
1. install node v10 or 12, which would have npm
2. download yarn using npm.
npm install -g yarn
3. After yarn installs, enter the following command:
yarn add mermaid
4. After downloading mermaid, you can then open the mermaid youve downloaded and go to the `dist` folder.
5. Find the `mermaid.min.js` file, press the shift key and right click on and select copy as path from the options.
6. Paste it within the `script` tag as the `src`.
```
<script src=[Paste the mermaid.min.js file address here]></script>
<script>mermaid.initialize({startOnLoad:true});</script>
```
7. It should look something like this
```
<script src="C:\Users\myPC\mermaid\dist\mermaid.js"></script>
<script>mermaid.initialize({startOnLoad:true});</script>
```
8. Add the graph and diagram definitions as you would in number 3, be mindful of the `div` tags.
9. Save, load/edit your HTML file to your liking.
**Note** placing the HTML file on the same folder the mermaid filed you've downloaded is a good practice and allows you to shorten the address.
**As seen here:**
```
<script src=".\mermaid\dist\mermaid.min.js"></script>
```
2019-11-08 01:25:41 +01:00
**Three additional comments from Knut Sveidqvist, creator of mermaid:**
2019-11-08 10:35:38 +01:00
- In early versions of mermaid, the `<script src>` tag was invoked in the `<head>` part of the web page. Nowdays we can place it directly in `<body>` as seen above. However, older parts of the documentation frequently reflects the previous way which still works.
2019-11-08 10:36:41 +01:00
- We initialize the mermaid rendering with `mermaid.initialize()` directly in the html code. In principle this could be done through placing `mermaid.initialize()` inside of `mermaid.min.js`. We would then eliminate the need for this explicit line in the html. However, there are use cases where we do want to separate the two steps. Sometimes we want full control over when we start looking for `<div>`tags inside the web page with `mermaid.initialize()`, for example when we think that all `<div>` tags may not have been loaded by the time `mermaid.min.js` runs.
2020-07-22 21:28:02 -07:00
- In the third method, `mermaid.min.js` is called using an absolute path. Even worse, the example includes the mermaid version number which of course will change as time goes by. However, the example makes it easy to understand what is going on - even though it is perhaps doomed in a way we do not want in a production environment. When going from testing mermaid out to getting serious with it, I would suggest one of the following approaches for calling `mermaid.min.js`:
2019-11-28 13:13:26 +01:00
1. If you do not enter a specific version, you automatically get the latest one.
2. If you really need a specific version, hard code it (this is rare but it happens).
2019-11-28 13:14:29 +01:00
3. If you need to know the current mermaid version, replace a mermaid code block with the word `info` and the version will be returned like [this](https://mermaid-js.github.io/mermaid-live-editor/#/edit/eyJjb2RlIjoiaW5mb1xuXG4iLCJtZXJtYWlkIjp7InRoZW1lIjoiZGVmYXVsdCJ9fQ==)