mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
Merge branch 'master' into develop
* master: docs: Fix changelog docs: v10 breaking changes Remove `null` from diagrams before render fix docs diagram
This commit is contained in:
commit
0df8c149f9
85
CHANGELOG.md
85
CHANGELOG.md
@ -1,6 +1,87 @@
|
|||||||
# Change Log
|
# Changelog
|
||||||
|
|
||||||
// TODO: Populate changelog
|
## [10.0.0](https://github.com/mermaid-js/mermaid/releases/tag/v10.0.0)
|
||||||
|
|
||||||
|
### mermaid.render is async and doesn't accept callbacks
|
||||||
|
|
||||||
|
```js
|
||||||
|
// < v10
|
||||||
|
mermaid.render('id', 'graph TD;\nA-->B', (svg, bindFunctions) => {
|
||||||
|
element.innerHTML = svg;
|
||||||
|
if (bindFunctions) {
|
||||||
|
bindFunctions(element);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Shorter syntax
|
||||||
|
if (bindFunctions) {
|
||||||
|
bindFunctions(element);
|
||||||
|
}
|
||||||
|
// can be replaced with the `?.` shorthand
|
||||||
|
bindFunctions?.(element);
|
||||||
|
|
||||||
|
// >= v10 with async/await
|
||||||
|
const { svg, bindFunctions } = await mermaid.render('id', 'graph TD;\nA-->B');
|
||||||
|
element.innerHTML = svg;
|
||||||
|
bindFunctions?.(element);
|
||||||
|
|
||||||
|
// >= v10 with promise.then
|
||||||
|
mermaid.render('id', 'graph TD;A-->B').then(({ svg, bindFunctions }) => {
|
||||||
|
element.innerHTML = svg;
|
||||||
|
bindFunctions?.(element);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### mermaid.parse is async and ParseError is removed
|
||||||
|
|
||||||
|
```js
|
||||||
|
// < v10
|
||||||
|
mermaid.parse(text, parseError);
|
||||||
|
|
||||||
|
//>= v10
|
||||||
|
await mermaid.parse(text).catch(parseError);
|
||||||
|
// or
|
||||||
|
try {
|
||||||
|
await mermaid.parse(text);
|
||||||
|
} catch (err) {
|
||||||
|
parseError(err);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Init deprecated and InitThrowsErrors removed
|
||||||
|
|
||||||
|
The config passed to `init` was not being used eariler.
|
||||||
|
It will now be used.
|
||||||
|
The `init` function is deprecated and will be removed in the next major release.
|
||||||
|
init currently works as a wrapper to `initialize` and `run`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
// < v10
|
||||||
|
mermaid.init(config, selector, cb);
|
||||||
|
|
||||||
|
//>= v10
|
||||||
|
mermaid.initialize(config);
|
||||||
|
mermaid.run({
|
||||||
|
querySelector: selector,
|
||||||
|
postRenderCallback: cb,
|
||||||
|
suppressErrors: true,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
// < v10
|
||||||
|
mermaid.initThrowsErrors(config, selector, cb);
|
||||||
|
|
||||||
|
//>= v10
|
||||||
|
mermaid.initialize(config);
|
||||||
|
mermaid.run({
|
||||||
|
querySelector: selector,
|
||||||
|
postRenderCallback: cb,
|
||||||
|
suppressErrors: false,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
// TODO: Populate changelog pre v10
|
||||||
|
|
||||||
- Config has a lot of changes
|
- Config has a lot of changes
|
||||||
- globalReset resets to `defaultConfig` instead of current config. Use `reset` instead.
|
- globalReset resets to `defaultConfig` instead of current config. Use `reset` instead.
|
||||||
|
@ -1,58 +0,0 @@
|
|||||||
# A collection of updates that change the behavior
|
|
||||||
|
|
||||||
## Async
|
|
||||||
|
|
||||||
`parse`, `render` are now async.
|
|
||||||
|
|
||||||
## Lazy loading and asynchronisity
|
|
||||||
|
|
||||||
- Invalid dates are rendered as syntax error instead of returning best guess or the current date
|
|
||||||
|
|
||||||
## ParseError is removed
|
|
||||||
|
|
||||||
```js
|
|
||||||
//< v10.0.0
|
|
||||||
mermaid.parse(text, parseError);
|
|
||||||
|
|
||||||
//>= v10.0.0
|
|
||||||
await mermaid.parse(text).catch(parseError);
|
|
||||||
// or
|
|
||||||
try {
|
|
||||||
await mermaid.parse(text);
|
|
||||||
} catch (err) {
|
|
||||||
parseError(err);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Init deprecated and InitThrowsErrors removed
|
|
||||||
|
|
||||||
The config passed to `init` was not being used eariler.
|
|
||||||
It will now be used.
|
|
||||||
The `init` function is deprecated and will be removed in the next major release.
|
|
||||||
init currently works as a wrapper to `initialize` and `run`.
|
|
||||||
|
|
||||||
```js
|
|
||||||
//< v10.0.0
|
|
||||||
mermaid.init(config, selector, cb);
|
|
||||||
|
|
||||||
//>= v10.0.0
|
|
||||||
mermaid.initialize(config);
|
|
||||||
mermaid.run({
|
|
||||||
querySelector: selector,
|
|
||||||
postRenderCallback: cb,
|
|
||||||
suppressErrors: true,
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
```js
|
|
||||||
//< v10.0.0
|
|
||||||
mermaid.initThrowsErrors(config, selector, cb);
|
|
||||||
|
|
||||||
//>= v10.0.0
|
|
||||||
mermaid.initialize(config);
|
|
||||||
mermaid.run({
|
|
||||||
querySelector: selector,
|
|
||||||
postRenderCallback: cb,
|
|
||||||
suppressErrors: false,
|
|
||||||
});
|
|
||||||
```
|
|
@ -46,7 +46,7 @@ const contentLoaded = async function () {
|
|||||||
|
|
||||||
await mermaid2.registerExternalDiagrams([externalExample]);
|
await mermaid2.registerExternalDiagrams([externalExample]);
|
||||||
mermaid2.initialize(graphObj.mermaid);
|
mermaid2.initialize(graphObj.mermaid);
|
||||||
await mermaid2.init();
|
await mermaid2.run();
|
||||||
markRendered();
|
markRendered();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -20,21 +20,24 @@ Please note that you can switch versions through the dropdown box at the top rig
|
|||||||
|
|
||||||
For the majority of users, Using the [Live Editor](https://mermaid.live/) would be sufficient, however you may also opt to deploy mermaid as a dependency or using the [Mermaid API](./setup/README.md).
|
For the majority of users, Using the [Live Editor](https://mermaid.live/) would be sufficient, however you may also opt to deploy mermaid as a dependency or using the [Mermaid API](./setup/README.md).
|
||||||
|
|
||||||
We have compiled some Video [Tutorials](./Tutorials.md) on how to use the mermaid Live Editor.
|
We have compiled some Video [Tutorials](./Tutorials.md) on how to use the Mermaid Live Editor.
|
||||||
|
|
||||||
### Installing and Hosting Mermaid on a Webpage
|
### Installing and Hosting Mermaid on a Webpage
|
||||||
|
|
||||||
**Using the npm package:**
|
**Using the npm package:**
|
||||||
|
|
||||||
1. You will need to install `node v16`, which would have npm.
|
Requirements:
|
||||||
|
|
||||||
2. Download `yarn` using npm.
|
- Node >= 16
|
||||||
|
|
||||||
3. Enter the following command: `yarn add mermaid`.
|
```bash
|
||||||
|
# NPM
|
||||||
4. At this point, you can add mermaid as a dev dependency using this command: `yarn add --dev mermaid`.
|
npm install mermaid
|
||||||
|
# Yarn
|
||||||
5. Alternatively, you can also deploy mermaid using the script tag in an HTML file with mermaid diagram descriptions as is shown in the example below.
|
yarn add mermaid
|
||||||
|
# PNPM
|
||||||
|
pnpm add mermaid
|
||||||
|
```
|
||||||
|
|
||||||
**Hosting mermaid on a web page:**
|
**Hosting mermaid on a web page:**
|
||||||
|
|
||||||
@ -42,7 +45,9 @@ We have compiled some Video [Tutorials](./Tutorials.md) on how to use the mermai
|
|||||||
|
|
||||||
The easiest way to integrate mermaid on a web page requires two elements:
|
The easiest way to integrate mermaid on a web page requires two elements:
|
||||||
|
|
||||||
- A graph definition, inside `<pre>` tags labeled `class=mermaid`. Example:
|
- A graph definition, inside `<pre>` tags labeled `class=mermaid`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<pre class="mermaid">
|
<pre class="mermaid">
|
||||||
@ -53,14 +58,13 @@ The easiest way to integrate mermaid on a web page requires two elements:
|
|||||||
</pre>
|
</pre>
|
||||||
```
|
```
|
||||||
|
|
||||||
- Inclusion of the mermaid address in the html page body using a `script` tag as an ESM import, and the `mermaidAPI` call.
|
- The mermaid js script. Added using a `script` tag as an ESM import.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
|
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
|
||||||
mermaid.initialize({ startOnLoad: true });
|
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -71,9 +75,6 @@ Example:
|
|||||||
```html
|
```html
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
</head>
|
|
||||||
<body>
|
<body>
|
||||||
<pre class="mermaid">
|
<pre class="mermaid">
|
||||||
graph LR
|
graph LR
|
||||||
@ -83,7 +84,6 @@ Example:
|
|||||||
</pre>
|
</pre>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
|
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
|
||||||
mermaid.initialize({ startOnLoad: true });
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@ -95,11 +95,12 @@ An id attribute is also added to mermaid tags without one.
|
|||||||
|
|
||||||
Mermaid can load multiple diagrams, in the same page.
|
Mermaid can load multiple diagrams, in the same page.
|
||||||
|
|
||||||
> Try it out, save this code as HTML and load it using any browser.(Except Internet Explorer, please don't use Internet Explorer.)
|
> Try it out, save this code as HTML and load it using any browser.
|
||||||
|
> (Except Internet Explorer, please don't use Internet Explorer.)
|
||||||
|
|
||||||
## Enabling Click Event and Tags in Nodes
|
## Enabling Click Event and Tags in Nodes
|
||||||
|
|
||||||
A `securityLevel` configuration has to first be cleared, `securityLevel` sets the level of trust for the parsed diagrams and limits click functionality. This was introduce in version 8.2 as a security improvement, aimed at preventing malicious use.
|
A `securityLevel` configuration has to first be cleared. `securityLevel` sets the level of trust for the parsed diagrams and limits click functionality. This was introduce in version 8.2 as a security improvement, aimed at preventing malicious use.
|
||||||
|
|
||||||
**It is the site owner's responsibility to discriminate between trustworthy and untrustworthy user-bases and we encourage the use of discretion.**
|
**It is the site owner's responsibility to discriminate between trustworthy and untrustworthy user-bases and we encourage the use of discretion.**
|
||||||
|
|
||||||
@ -107,7 +108,7 @@ A `securityLevel` configuration has to first be cleared, `securityLevel` sets th
|
|||||||
|
|
||||||
| Parameter | Description | Type | Required | Values |
|
| Parameter | Description | Type | Required | Values |
|
||||||
| ------------- | --------------------------------- | ------ | -------- | ------------------------------------------ |
|
| ------------- | --------------------------------- | ------ | -------- | ------------------------------------------ |
|
||||||
| securityLevel | Level of trust for parsed diagram | String | Required | 'sandbox', 'strict', 'loose', 'antiscript' |
|
| securityLevel | Level of trust for parsed diagram | String | Optional | 'sandbox', 'strict', 'loose', 'antiscript' |
|
||||||
|
|
||||||
Values:
|
Values:
|
||||||
|
|
||||||
@ -122,26 +123,17 @@ Values:
|
|||||||
|
|
||||||
**If you are taking responsibility for the diagram source security you can set the `securityLevel` to a value of your choosing . This allows clicks and tags are allowed.**
|
**If you are taking responsibility for the diagram source security you can set the `securityLevel` to a value of your choosing . This allows clicks and tags are allowed.**
|
||||||
|
|
||||||
**To change `securityLevel`, you have to call `mermaidAPI.initialize`:**
|
**To change `securityLevel`, you have to call `mermaid.initialize`:**
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
mermaidAPI.initialize({
|
mermaid.initialize({
|
||||||
securityLevel: 'loose',
|
securityLevel: 'loose',
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Labels out of bounds
|
### Labels out of bounds
|
||||||
|
|
||||||
If you use dynamically loaded fonts that are loaded through CSS, such as Google fonts, mermaid should wait for the
|
If you use dynamically loaded fonts that are loaded through CSS, such as fonts, mermaid should wait for the whole page to load (dom + assets, particularly the fonts file).
|
||||||
whole page to load (dom + assets, particularly the fonts file).
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
$(document).load(function () {
|
|
||||||
mermaid.initialize();
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
or
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
@ -154,12 +146,54 @@ Not doing so will most likely result in mermaid rendering graphs that have label
|
|||||||
If your page has other fonts in its body those might be used instead of the mermaid font. Specifying the font in your styling is a workaround for this.
|
If your page has other fonts in its body those might be used instead of the mermaid font. Specifying the font in your styling is a workaround for this.
|
||||||
|
|
||||||
```css
|
```css
|
||||||
div.mermaid {
|
pre.mermaid {
|
||||||
font-family: 'trebuchet ms', verdana, arial;
|
font-family: 'trebuchet ms', verdana, arial;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Calling `mermaid.init`
|
### Using `mermaid.run`
|
||||||
|
|
||||||
|
mermaid.run was added in v10 and is the preferred way of handling more complex integration.
|
||||||
|
By default, `mermaid.run` will be called when the document is ready, rendering all elements with `class="mermaid"`.
|
||||||
|
|
||||||
|
You can customize that behavior by calling `await mermaid.run(<config>)`.
|
||||||
|
|
||||||
|
`mermaid.initialize({startOnLoad: false})` will prevent `mermaid.run` from being called automatically after load.
|
||||||
|
|
||||||
|
Render all elements with querySelector ".someOtherClass"
|
||||||
|
|
||||||
|
```js
|
||||||
|
mermaid.initialize({ startOnLoad: false });
|
||||||
|
await mermaid.run({
|
||||||
|
querySelector: '.someOtherClass',
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Render all elements passed as an array
|
||||||
|
|
||||||
|
```js
|
||||||
|
mermaid.initialize({ startOnLoad: false });
|
||||||
|
await mermaid.run({
|
||||||
|
nodes: [document.getElementById('someId'), document.getElementById('anotherId')],
|
||||||
|
});
|
||||||
|
await mermaid.run({
|
||||||
|
nodes: document.querySelectorAll('.yetAnotherClass'),
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Render all `.mermaid` elements while suppressing any error
|
||||||
|
|
||||||
|
```js
|
||||||
|
mermaid.initialize({ startOnLoad: false });
|
||||||
|
await mermaid.run({
|
||||||
|
suppressErrors: true,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Calling `mermaid.init` - Deprecated
|
||||||
|
|
||||||
|
> **Warning**
|
||||||
|
> mermaid.init is deprecated in v10 and will be removed in v11. Please use mermaid.run instead.
|
||||||
|
|
||||||
By default, `mermaid.init` will be called when the document is ready, finding all elements with
|
By default, `mermaid.init` will be called when the document is ready, finding all elements with
|
||||||
`class="mermaid"`. If you are adding content after mermaid is loaded, or otherwise need
|
`class="mermaid"`. If you are adding content after mermaid is loaded, or otherwise need
|
||||||
@ -192,25 +226,24 @@ mermaid fully supports webpack. Here is a [working demo](https://github.com/merm
|
|||||||
|
|
||||||
## API usage
|
## API usage
|
||||||
|
|
||||||
The main idea of the API is to be able to call a render function with the graph definition as a string. The render function
|
The main idea of the API is to be able to call a render function with the graph definition as a string. The render function will render the graph and call a callback with the resulting SVG code. With this approach it is up to the site creator to fetch the graph definition from the site (perhaps from a textarea), render it and place the graph somewhere in the site.
|
||||||
will render the graph and call a callback with the resulting SVG code. With this approach it is up to the site creator to
|
|
||||||
fetch the graph definition from the site (perhaps from a textarea), render it and place the graph somewhere in the site.
|
|
||||||
|
|
||||||
The example below show an outline of how this could be used. The example just logs the resulting SVG to the JavaScript console.
|
The example below show an outline of how this could be used. The example just logs the resulting SVG to the JavaScript console.
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import mermaid from './mermaid.mjs';
|
import mermaid from './mermaid.esm.mjs';
|
||||||
mermaid.mermaidAPI.initialize({ startOnLoad: false });
|
mermaid.initialize({ startOnLoad: false });
|
||||||
$(async function () {
|
|
||||||
// Example of using the API var
|
// Example of using the render function
|
||||||
|
const drawDiagram = async function () {
|
||||||
element = document.querySelector('#graphDiv');
|
element = document.querySelector('#graphDiv');
|
||||||
const insertSvg = function (svgCode, bindFunctions) {
|
|
||||||
element.innerHTML = svgCode;
|
|
||||||
};
|
|
||||||
const graphDefinition = 'graph TB\na-->b';
|
const graphDefinition = 'graph TB\na-->b';
|
||||||
const graph = await mermaid.mermaidAPI.render('graphDiv', graphDefinition, insertSvg);
|
const { svg } = await mermaid.render('graphDiv', graphDefinition);
|
||||||
});
|
element.innerHTML = svg;
|
||||||
|
};
|
||||||
|
|
||||||
|
await drawDiagram();
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -223,17 +256,17 @@ The example code below is an extract of what mermaid does when using the API. Th
|
|||||||
bind events to an SVG when using the API for rendering.
|
bind events to an SVG when using the API for rendering.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const insertSvg = function (svgCode, bindFunctions) {
|
// Example of using the bindFunctions
|
||||||
element.innerHTML = svgCode;
|
const drawDiagram = async function () {
|
||||||
if (typeof callback !== 'undefined') {
|
element = document.querySelector('#graphDiv');
|
||||||
callback(id);
|
const graphDefinition = 'graph TB\na-->b';
|
||||||
}
|
const { svg, bindFunctions } = await mermaid.render('graphDiv', graphDefinition);
|
||||||
|
element.innerHTML = svg;
|
||||||
|
// This can also be written as `bindFunctions?.(element);` using the `?` shorthand.
|
||||||
|
if (bindFunctions) {
|
||||||
bindFunctions(element);
|
bindFunctions(element);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const id = 'theGraph';
|
|
||||||
|
|
||||||
mermaidAPI.render(id, txt, insertSvg, element);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. The graph is generated using the render call.
|
1. The graph is generated using the render call.
|
||||||
|
@ -103,7 +103,7 @@ When writing the .html file, we give two instructions inside the html code to th
|
|||||||
|
|
||||||
a. The mermaid code for the diagram we want to create.
|
a. The mermaid code for the diagram we want to create.
|
||||||
|
|
||||||
b. The importing of mermaid library through the `mermaid.esm.js` or `mermaid.esm.min.mjs` and the `mermaid.initialize()` call, which dictates the appearance of diagrams and also starts the rendering process .
|
b. The importing of mermaid library through the `mermaid.esm.mjs` or `mermaid.esm.min.mjs` and the `mermaid.initialize()` call, which dictates the appearance of diagrams and also starts the rendering process .
|
||||||
|
|
||||||
**a. The embedded mermaid diagram definition inside a `<pre class="mermaid">`:**
|
**a. The embedded mermaid diagram definition inside a `<pre class="mermaid">`:**
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ b. The importing of mermaid library through the `mermaid.esm.js` or `mermaid.esm
|
|||||||
```
|
```
|
||||||
|
|
||||||
**Notes**:
|
**Notes**:
|
||||||
Rendering in Mermaid is initialized by `mermaid.initialize()` call. You can place `mermaid.initialize()` inside `mermaid.esm.min.mjs` for brevity. However, doing the opposite lets you control when it starts looking for `<div>`tags inside the web page with `mermaid.initialize()`. This is useful when you think that not all `<div>` tags may have loaded on the execution of `mermaid.esm.min.mjs` file.
|
Rendering in Mermaid is initialized by `mermaid.initialize()` call. However, doing the opposite lets you control when it starts looking for `<pre>` tags inside the web page with `mermaid.initialize()`. This is useful when you think that not all `<pre>` tags may have loaded on the execution of `mermaid.esm.min.mjs` file.
|
||||||
|
|
||||||
`startOnLoad` is one of the parameters that can be defined by `mermaid.initialize()`
|
`startOnLoad` is one of the parameters that can be defined by `mermaid.initialize()`
|
||||||
|
|
||||||
@ -143,10 +143,6 @@ Rendering in Mermaid is initialized by `mermaid.initialize()` call. You can plac
|
|||||||
| ----------- | --------------------------------- | ------- | ----------- |
|
| ----------- | --------------------------------- | ------- | ----------- |
|
||||||
| startOnLoad | Toggle for Rendering upon loading | Boolean | true, false |
|
| startOnLoad | Toggle for Rendering upon loading | Boolean | true, false |
|
||||||
|
|
||||||
### Adding external diagrams to mermaid
|
|
||||||
|
|
||||||
Please refer to the [Mindmap](../syntax/mindmap.md?id=integrating-with-your-librarywebsite) section for more information.
|
|
||||||
|
|
||||||
### Working Examples
|
### Working Examples
|
||||||
|
|
||||||
**Here is a full working example of the mermaidAPI being called through the CDN:**
|
**Here is a full working example of the mermaidAPI being called through the CDN:**
|
||||||
|
@ -17,7 +17,7 @@ const props = defineProps({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const svg = ref(null);
|
const svg = ref('');
|
||||||
let mut = null;
|
let mut = null;
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
|
@ -2,6 +2,6 @@ import mermaid, { type MermaidConfig } from 'mermaid';
|
|||||||
|
|
||||||
export const render = async (id: string, code: string, config: MermaidConfig): Promise<string> => {
|
export const render = async (id: string, code: string, config: MermaidConfig): Promise<string> => {
|
||||||
mermaid.initialize(config);
|
mermaid.initialize(config);
|
||||||
const svg = await mermaid.render(id, code);
|
const { svg } = await mermaid.render(id, code);
|
||||||
return svg;
|
return svg;
|
||||||
};
|
};
|
||||||
|
@ -14,21 +14,24 @@ Please note that you can switch versions through the dropdown box at the top rig
|
|||||||
|
|
||||||
For the majority of users, Using the [Live Editor](https://mermaid.live/) would be sufficient, however you may also opt to deploy mermaid as a dependency or using the [Mermaid API](./setup/README.md).
|
For the majority of users, Using the [Live Editor](https://mermaid.live/) would be sufficient, however you may also opt to deploy mermaid as a dependency or using the [Mermaid API](./setup/README.md).
|
||||||
|
|
||||||
We have compiled some Video [Tutorials](./Tutorials.md) on how to use the mermaid Live Editor.
|
We have compiled some Video [Tutorials](./Tutorials.md) on how to use the Mermaid Live Editor.
|
||||||
|
|
||||||
### Installing and Hosting Mermaid on a Webpage
|
### Installing and Hosting Mermaid on a Webpage
|
||||||
|
|
||||||
**Using the npm package:**
|
**Using the npm package:**
|
||||||
|
|
||||||
1. You will need to install `node v16`, which would have npm.
|
Requirements:
|
||||||
|
|
||||||
2. Download `yarn` using npm.
|
- Node >= 16
|
||||||
|
|
||||||
3. Enter the following command: `yarn add mermaid`.
|
```bash
|
||||||
|
# NPM
|
||||||
4. At this point, you can add mermaid as a dev dependency using this command: `yarn add --dev mermaid`.
|
npm install mermaid
|
||||||
|
# Yarn
|
||||||
5. Alternatively, you can also deploy mermaid using the script tag in an HTML file with mermaid diagram descriptions as is shown in the example below.
|
yarn add mermaid
|
||||||
|
# PNPM
|
||||||
|
pnpm add mermaid
|
||||||
|
```
|
||||||
|
|
||||||
**Hosting mermaid on a web page:**
|
**Hosting mermaid on a web page:**
|
||||||
|
|
||||||
@ -36,7 +39,9 @@ We have compiled some Video [Tutorials](./Tutorials.md) on how to use the mermai
|
|||||||
|
|
||||||
The easiest way to integrate mermaid on a web page requires two elements:
|
The easiest way to integrate mermaid on a web page requires two elements:
|
||||||
|
|
||||||
- A graph definition, inside `<pre>` tags labeled `class=mermaid`. Example:
|
- A graph definition, inside `<pre>` tags labeled `class=mermaid`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<pre class="mermaid">
|
<pre class="mermaid">
|
||||||
@ -47,14 +52,13 @@ The easiest way to integrate mermaid on a web page requires two elements:
|
|||||||
</pre>
|
</pre>
|
||||||
```
|
```
|
||||||
|
|
||||||
- Inclusion of the mermaid address in the html page body using a `script` tag as an ESM import, and the `mermaidAPI` call.
|
- The mermaid js script. Added using a `script` tag as an ESM import.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import mermaid from '<CDN_URL>/mermaid@<MERMAID_VERSION>/dist/mermaid.esm.min.mjs';
|
import mermaid from '<CDN_URL>/mermaid@<MERMAID_VERSION>/dist/mermaid.esm.min.mjs';
|
||||||
mermaid.initialize({ startOnLoad: true });
|
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -65,9 +69,6 @@ Example:
|
|||||||
```html
|
```html
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
</head>
|
|
||||||
<body>
|
<body>
|
||||||
<pre class="mermaid">
|
<pre class="mermaid">
|
||||||
graph LR
|
graph LR
|
||||||
@ -77,7 +78,6 @@ Example:
|
|||||||
</pre>
|
</pre>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import mermaid from '<CDN_URL>/mermaid@<MERMAID_VERSION>/dist/mermaid.esm.min.mjs';
|
import mermaid from '<CDN_URL>/mermaid@<MERMAID_VERSION>/dist/mermaid.esm.min.mjs';
|
||||||
mermaid.initialize({ startOnLoad: true });
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@ -89,11 +89,12 @@ An id attribute is also added to mermaid tags without one.
|
|||||||
|
|
||||||
Mermaid can load multiple diagrams, in the same page.
|
Mermaid can load multiple diagrams, in the same page.
|
||||||
|
|
||||||
> Try it out, save this code as HTML and load it using any browser.(Except Internet Explorer, please don't use Internet Explorer.)
|
> Try it out, save this code as HTML and load it using any browser.
|
||||||
|
> (Except Internet Explorer, please don't use Internet Explorer.)
|
||||||
|
|
||||||
## Enabling Click Event and Tags in Nodes
|
## Enabling Click Event and Tags in Nodes
|
||||||
|
|
||||||
A `securityLevel` configuration has to first be cleared, `securityLevel` sets the level of trust for the parsed diagrams and limits click functionality. This was introduce in version 8.2 as a security improvement, aimed at preventing malicious use.
|
A `securityLevel` configuration has to first be cleared. `securityLevel` sets the level of trust for the parsed diagrams and limits click functionality. This was introduce in version 8.2 as a security improvement, aimed at preventing malicious use.
|
||||||
|
|
||||||
**It is the site owner's responsibility to discriminate between trustworthy and untrustworthy user-bases and we encourage the use of discretion.**
|
**It is the site owner's responsibility to discriminate between trustworthy and untrustworthy user-bases and we encourage the use of discretion.**
|
||||||
|
|
||||||
@ -101,7 +102,7 @@ A `securityLevel` configuration has to first be cleared, `securityLevel` sets th
|
|||||||
|
|
||||||
| Parameter | Description | Type | Required | Values |
|
| Parameter | Description | Type | Required | Values |
|
||||||
| ------------- | --------------------------------- | ------ | -------- | ------------------------------------------ |
|
| ------------- | --------------------------------- | ------ | -------- | ------------------------------------------ |
|
||||||
| securityLevel | Level of trust for parsed diagram | String | Required | 'sandbox', 'strict', 'loose', 'antiscript' |
|
| securityLevel | Level of trust for parsed diagram | String | Optional | 'sandbox', 'strict', 'loose', 'antiscript' |
|
||||||
|
|
||||||
Values:
|
Values:
|
||||||
|
|
||||||
@ -117,26 +118,17 @@ This changes the default behaviour of mermaid so that after upgrade to 8.2, unle
|
|||||||
|
|
||||||
**If you are taking responsibility for the diagram source security you can set the `securityLevel` to a value of your choosing . This allows clicks and tags are allowed.**
|
**If you are taking responsibility for the diagram source security you can set the `securityLevel` to a value of your choosing . This allows clicks and tags are allowed.**
|
||||||
|
|
||||||
**To change `securityLevel`, you have to call `mermaidAPI.initialize`:**
|
**To change `securityLevel`, you have to call `mermaid.initialize`:**
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
mermaidAPI.initialize({
|
mermaid.initialize({
|
||||||
securityLevel: 'loose',
|
securityLevel: 'loose',
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### Labels out of bounds
|
### Labels out of bounds
|
||||||
|
|
||||||
If you use dynamically loaded fonts that are loaded through CSS, such as Google fonts, mermaid should wait for the
|
If you use dynamically loaded fonts that are loaded through CSS, such as fonts, mermaid should wait for the whole page to load (dom + assets, particularly the fonts file).
|
||||||
whole page to load (dom + assets, particularly the fonts file).
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
$(document).load(function () {
|
|
||||||
mermaid.initialize();
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
or
|
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
@ -149,12 +141,55 @@ Not doing so will most likely result in mermaid rendering graphs that have label
|
|||||||
If your page has other fonts in its body those might be used instead of the mermaid font. Specifying the font in your styling is a workaround for this.
|
If your page has other fonts in its body those might be used instead of the mermaid font. Specifying the font in your styling is a workaround for this.
|
||||||
|
|
||||||
```css
|
```css
|
||||||
div.mermaid {
|
pre.mermaid {
|
||||||
font-family: 'trebuchet ms', verdana, arial;
|
font-family: 'trebuchet ms', verdana, arial;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Calling `mermaid.init`
|
### Using `mermaid.run`
|
||||||
|
|
||||||
|
mermaid.run was added in v10 and is the preferred way of handling more complex integration.
|
||||||
|
By default, `mermaid.run` will be called when the document is ready, rendering all elements with `class="mermaid"`.
|
||||||
|
|
||||||
|
You can customize that behavior by calling `await mermaid.run(<config>)`.
|
||||||
|
|
||||||
|
`mermaid.initialize({startOnLoad: false})` will prevent `mermaid.run` from being called automatically after load.
|
||||||
|
|
||||||
|
Render all elements with querySelector ".someOtherClass"
|
||||||
|
|
||||||
|
```js
|
||||||
|
mermaid.initialize({ startOnLoad: false });
|
||||||
|
await mermaid.run({
|
||||||
|
querySelector: '.someOtherClass',
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Render all elements passed as an array
|
||||||
|
|
||||||
|
```js
|
||||||
|
mermaid.initialize({ startOnLoad: false });
|
||||||
|
await mermaid.run({
|
||||||
|
nodes: [document.getElementById('someId'), document.getElementById('anotherId')],
|
||||||
|
});
|
||||||
|
await mermaid.run({
|
||||||
|
nodes: document.querySelectorAll('.yetAnotherClass'),
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Render all `.mermaid` elements while suppressing any error
|
||||||
|
|
||||||
|
```js
|
||||||
|
mermaid.initialize({ startOnLoad: false });
|
||||||
|
await mermaid.run({
|
||||||
|
suppressErrors: true,
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Calling `mermaid.init` - Deprecated
|
||||||
|
|
||||||
|
```warning
|
||||||
|
mermaid.init is deprecated in v10 and will be removed in v11. Please use mermaid.run instead.
|
||||||
|
```
|
||||||
|
|
||||||
By default, `mermaid.init` will be called when the document is ready, finding all elements with
|
By default, `mermaid.init` will be called when the document is ready, finding all elements with
|
||||||
`class="mermaid"`. If you are adding content after mermaid is loaded, or otherwise need
|
`class="mermaid"`. If you are adding content after mermaid is loaded, or otherwise need
|
||||||
@ -188,25 +223,24 @@ mermaid fully supports webpack. Here is a [working demo](https://github.com/merm
|
|||||||
|
|
||||||
## API usage
|
## API usage
|
||||||
|
|
||||||
The main idea of the API is to be able to call a render function with the graph definition as a string. The render function
|
The main idea of the API is to be able to call a render function with the graph definition as a string. The render function will render the graph and call a callback with the resulting SVG code. With this approach it is up to the site creator to fetch the graph definition from the site (perhaps from a textarea), render it and place the graph somewhere in the site.
|
||||||
will render the graph and call a callback with the resulting SVG code. With this approach it is up to the site creator to
|
|
||||||
fetch the graph definition from the site (perhaps from a textarea), render it and place the graph somewhere in the site.
|
|
||||||
|
|
||||||
The example below show an outline of how this could be used. The example just logs the resulting SVG to the JavaScript console.
|
The example below show an outline of how this could be used. The example just logs the resulting SVG to the JavaScript console.
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import mermaid from './mermaid.mjs';
|
import mermaid from './mermaid.esm.mjs';
|
||||||
mermaid.mermaidAPI.initialize({ startOnLoad: false });
|
mermaid.initialize({ startOnLoad: false });
|
||||||
$(async function () {
|
|
||||||
// Example of using the API var
|
// Example of using the render function
|
||||||
|
const drawDiagram = async function () {
|
||||||
element = document.querySelector('#graphDiv');
|
element = document.querySelector('#graphDiv');
|
||||||
const insertSvg = function (svgCode, bindFunctions) {
|
|
||||||
element.innerHTML = svgCode;
|
|
||||||
};
|
|
||||||
const graphDefinition = 'graph TB\na-->b';
|
const graphDefinition = 'graph TB\na-->b';
|
||||||
const graph = await mermaid.mermaidAPI.render('graphDiv', graphDefinition, insertSvg);
|
const { svg } = await mermaid.render('graphDiv', graphDefinition);
|
||||||
});
|
element.innerHTML = svg;
|
||||||
|
};
|
||||||
|
|
||||||
|
await drawDiagram();
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -219,17 +253,17 @@ The example code below is an extract of what mermaid does when using the API. Th
|
|||||||
bind events to an SVG when using the API for rendering.
|
bind events to an SVG when using the API for rendering.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const insertSvg = function (svgCode, bindFunctions) {
|
// Example of using the bindFunctions
|
||||||
element.innerHTML = svgCode;
|
const drawDiagram = async function () {
|
||||||
if (typeof callback !== 'undefined') {
|
element = document.querySelector('#graphDiv');
|
||||||
callback(id);
|
const graphDefinition = 'graph TB\na-->b';
|
||||||
}
|
const { svg, bindFunctions } = await mermaid.render('graphDiv', graphDefinition);
|
||||||
|
element.innerHTML = svg;
|
||||||
|
// This can also be written as `bindFunctions?.(element);` using the `?` shorthand.
|
||||||
|
if (bindFunctions) {
|
||||||
bindFunctions(element);
|
bindFunctions(element);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const id = 'theGraph';
|
|
||||||
|
|
||||||
mermaidAPI.render(id, txt, insertSvg, element);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. The graph is generated using the render call.
|
1. The graph is generated using the render call.
|
||||||
|
@ -86,7 +86,7 @@ When writing the .html file, we give two instructions inside the html code to th
|
|||||||
|
|
||||||
a. The mermaid code for the diagram we want to create.
|
a. The mermaid code for the diagram we want to create.
|
||||||
|
|
||||||
b. The importing of mermaid library through the `mermaid.esm.js` or `mermaid.esm.min.mjs` and the `mermaid.initialize()` call, which dictates the appearance of diagrams and also starts the rendering process .
|
b. The importing of mermaid library through the `mermaid.esm.mjs` or `mermaid.esm.min.mjs` and the `mermaid.initialize()` call, which dictates the appearance of diagrams and also starts the rendering process .
|
||||||
|
|
||||||
**a. The embedded mermaid diagram definition inside a `<pre class="mermaid">`:**
|
**a. The embedded mermaid diagram definition inside a `<pre class="mermaid">`:**
|
||||||
|
|
||||||
@ -118,7 +118,7 @@ b. The importing of mermaid library through the `mermaid.esm.js` or `mermaid.esm
|
|||||||
```
|
```
|
||||||
|
|
||||||
**Notes**:
|
**Notes**:
|
||||||
Rendering in Mermaid is initialized by `mermaid.initialize()` call. You can place `mermaid.initialize()` inside `mermaid.esm.min.mjs` for brevity. However, doing the opposite lets you control when it starts looking for `<div>`tags inside the web page with `mermaid.initialize()`. This is useful when you think that not all `<div>` tags may have loaded on the execution of `mermaid.esm.min.mjs` file.
|
Rendering in Mermaid is initialized by `mermaid.initialize()` call. However, doing the opposite lets you control when it starts looking for `<pre>` tags inside the web page with `mermaid.initialize()`. This is useful when you think that not all `<pre>` tags may have loaded on the execution of `mermaid.esm.min.mjs` file.
|
||||||
|
|
||||||
`startOnLoad` is one of the parameters that can be defined by `mermaid.initialize()`
|
`startOnLoad` is one of the parameters that can be defined by `mermaid.initialize()`
|
||||||
|
|
||||||
@ -126,10 +126,6 @@ Rendering in Mermaid is initialized by `mermaid.initialize()` call. You can plac
|
|||||||
| ----------- | --------------------------------- | ------- | ----------- |
|
| ----------- | --------------------------------- | ------- | ----------- |
|
||||||
| startOnLoad | Toggle for Rendering upon loading | Boolean | true, false |
|
| startOnLoad | Toggle for Rendering upon loading | Boolean | true, false |
|
||||||
|
|
||||||
### Adding external diagrams to mermaid
|
|
||||||
|
|
||||||
Please refer to the [Mindmap](../syntax/mindmap.md?id=integrating-with-your-librarywebsite) section for more information.
|
|
||||||
|
|
||||||
### Working Examples
|
### Working Examples
|
||||||
|
|
||||||
**Here is a full working example of the mermaidAPI being called through the CDN:**
|
**Here is a full working example of the mermaidAPI being called through the CDN:**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user