mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-23 17:29:54 +02:00
Merge pull request #5118 from mermaid-js/sidv/diagramType
feat: Add diagramType to RenderResult and ParseResult
This commit is contained in:
@@ -14,6 +14,9 @@
|
|||||||
|
|
||||||
• `Optional` **suppressErrors**: `boolean`
|
• `Optional` **suppressErrors**: `boolean`
|
||||||
|
|
||||||
|
If `true`, parse will return `false` instead of throwing error when the diagram is invalid.
|
||||||
|
The `parseError` function will not be called.
|
||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:60](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L60)
|
[mermaidAPI.ts:64](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L64)
|
||||||
|
21
docs/config/setup/interfaces/mermaidAPI.ParseResult.md
Normal file
21
docs/config/setup/interfaces/mermaidAPI.ParseResult.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
> **Warning**
|
||||||
|
>
|
||||||
|
> ## THIS IS AN AUTOGENERATED FILE. DO NOT EDIT.
|
||||||
|
>
|
||||||
|
> ## Please edit the corresponding file in [/packages/mermaid/src/docs/config/setup/interfaces/mermaidAPI.ParseResult.md](../../../../packages/mermaid/src/docs/config/setup/interfaces/mermaidAPI.ParseResult.md).
|
||||||
|
|
||||||
|
# Interface: ParseResult
|
||||||
|
|
||||||
|
[mermaidAPI](../modules/mermaidAPI.md).ParseResult
|
||||||
|
|
||||||
|
## Properties
|
||||||
|
|
||||||
|
### diagramType
|
||||||
|
|
||||||
|
• **diagramType**: `string`
|
||||||
|
|
||||||
|
The diagram type, e.g. 'flowchart', 'sequence', etc.
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
[mermaidAPI.ts:71](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L71)
|
@@ -39,7 +39,19 @@ bindFunctions?.(div); // To call bindFunctions only if it's present.
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:80](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L80)
|
[mermaidAPI.ts:94](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L94)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### diagramType
|
||||||
|
|
||||||
|
• **diagramType**: `string`
|
||||||
|
|
||||||
|
The diagram type, e.g. 'flowchart', 'sequence', etc.
|
||||||
|
|
||||||
|
#### Defined in
|
||||||
|
|
||||||
|
[mermaidAPI.ts:84](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L84)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -51,4 +63,4 @@ The svg code for the rendered graph.
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:70](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L70)
|
[mermaidAPI.ts:80](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L80)
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
## Interfaces
|
## Interfaces
|
||||||
|
|
||||||
- [ParseOptions](../interfaces/mermaidAPI.ParseOptions.md)
|
- [ParseOptions](../interfaces/mermaidAPI.ParseOptions.md)
|
||||||
|
- [ParseResult](../interfaces/mermaidAPI.ParseResult.md)
|
||||||
- [RenderResult](../interfaces/mermaidAPI.RenderResult.md)
|
- [RenderResult](../interfaces/mermaidAPI.RenderResult.md)
|
||||||
|
|
||||||
## References
|
## References
|
||||||
@@ -25,13 +26,13 @@ Renames and re-exports [mermaidAPI](mermaidAPI.md#mermaidapi)
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:64](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L64)
|
[mermaidAPI.ts:74](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L74)
|
||||||
|
|
||||||
## Variables
|
## Variables
|
||||||
|
|
||||||
### mermaidAPI
|
### mermaidAPI
|
||||||
|
|
||||||
• `Const` **mermaidAPI**: `Readonly`<{ `defaultConfig`: `MermaidConfig` = configApi.defaultConfig; `getConfig`: () => `MermaidConfig` = configApi.getConfig; `getDiagramFromText`: (`text`: `string`, `metadata`: `Pick`<`DiagramMetadata`, `"title"`>) => `Promise`<`Diagram`> ; `getSiteConfig`: () => `MermaidConfig` = configApi.getSiteConfig; `globalReset`: () => `void` ; `initialize`: (`options`: `MermaidConfig`) => `void` ; `parse`: (`text`: `string`, `parseOptions?`: [`ParseOptions`](../interfaces/mermaidAPI.ParseOptions.md)) => `Promise`<`boolean`> ; `render`: (`id`: `string`, `text`: `string`, `svgContainingElement?`: `Element`) => `Promise`<[`RenderResult`](../interfaces/mermaidAPI.RenderResult.md)> ; `reset`: () => `void` ; `setConfig`: (`conf`: `MermaidConfig`) => `MermaidConfig` = configApi.setConfig; `updateSiteConfig`: (`conf`: `MermaidConfig`) => `MermaidConfig` = configApi.updateSiteConfig }>
|
• `Const` **mermaidAPI**: `Readonly`<{ `defaultConfig`: `MermaidConfig` = configApi.defaultConfig; `getConfig`: () => `MermaidConfig` = configApi.getConfig; `getDiagramFromText`: (`text`: `string`, `metadata`: `Pick`<`DiagramMetadata`, `"title"`>) => `Promise`<`Diagram`> ; `getSiteConfig`: () => `MermaidConfig` = configApi.getSiteConfig; `globalReset`: () => `void` ; `initialize`: (`options`: `MermaidConfig`) => `void` ; `parse`: (`text`: `string`, `parseOptions`: [`ParseOptions`](../interfaces/mermaidAPI.ParseOptions.md) & { `suppressErrors`: `true` }) => `Promise`<[`ParseResult`](../interfaces/mermaidAPI.ParseResult.md) | `false`>(`text`: `string`, `parseOptions?`: [`ParseOptions`](../interfaces/mermaidAPI.ParseOptions.md)) => `Promise`<[`ParseResult`](../interfaces/mermaidAPI.ParseResult.md)> ; `render`: (`id`: `string`, `text`: `string`, `svgContainingElement?`: `Element`) => `Promise`<[`RenderResult`](../interfaces/mermaidAPI.RenderResult.md)> ; `reset`: () => `void` ; `setConfig`: (`conf`: `MermaidConfig`) => `MermaidConfig` = configApi.setConfig; `updateSiteConfig`: (`conf`: `MermaidConfig`) => `MermaidConfig` = configApi.updateSiteConfig }>
|
||||||
|
|
||||||
## mermaidAPI configuration defaults
|
## mermaidAPI configuration defaults
|
||||||
|
|
||||||
@@ -96,7 +97,7 @@ mermaid.initialize(config);
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:608](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L608)
|
[mermaidAPI.ts:623](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L623)
|
||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
|
|
||||||
@@ -127,7 +128,7 @@ Return the last node appended
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:263](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L263)
|
[mermaidAPI.ts:277](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L277)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -153,7 +154,7 @@ the cleaned up svgCode
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:209](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L209)
|
[mermaidAPI.ts:223](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L223)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -178,7 +179,7 @@ the string with all the user styles
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:139](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L139)
|
[mermaidAPI.ts:153](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L153)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -201,7 +202,7 @@ the string with all the user styles
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:186](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L186)
|
[mermaidAPI.ts:200](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L200)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -228,7 +229,7 @@ with an enclosing block that has each of the cssClasses followed by !important;
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:124](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L124)
|
[mermaidAPI.ts:138](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L138)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -254,7 +255,7 @@ Put the svgCode into an iFrame. Return the iFrame code
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:240](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L240)
|
[mermaidAPI.ts:254](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L254)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -279,4 +280,4 @@ Remove any existing elements from the given document
|
|||||||
|
|
||||||
#### Defined in
|
#### Defined in
|
||||||
|
|
||||||
[mermaidAPI.ts:313](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L313)
|
[mermaidAPI.ts:327](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L327)
|
||||||
|
@@ -331,15 +331,17 @@ module.exports = (options) ->
|
|||||||
|
|
||||||
## Advanced usage
|
## Advanced usage
|
||||||
|
|
||||||
**Syntax validation without rendering (Work in Progress)**
|
### Syntax validation without rendering
|
||||||
|
|
||||||
The **mermaid.parse(txt)** function validates graph definitions without rendering a graph. **[This function is still a work in progress](https://github.com/mermaid-js/mermaid/issues/1066), find alternatives below.**
|
The `mermaid.parse(text, parseOptions)` function validates graph definitions without rendering a graph.
|
||||||
|
|
||||||
The function **mermaid.parse(txt)**, takes a text string as an argument and returns true if the definition follows mermaid's syntax and
|
The function `mermaid.parse(text, parseOptions)`, takes a text string as an argument and returns `{ diagramType: string }` if the definition follows mermaid's syntax.
|
||||||
false if it does not. The parseError function will be called when the parse function returns false.
|
|
||||||
|
|
||||||
When the parser encounters invalid syntax the **mermaid.parseError** function is called. It is possible to override this
|
If the definition is invalid, the function returns `false` if `parseOptions.suppressErrors` is set to `true`. Otherwise, it throws an error.
|
||||||
function in order to handle the error in an application-specific way.
|
|
||||||
|
The parseError function will be called when the parse function throws an error. It will not be called if `parseOptions.suppressErrors` is set to `true`.
|
||||||
|
|
||||||
|
It is possible to override this function in order to handle the error in an application-specific way.
|
||||||
|
|
||||||
The code-example below in meta code illustrates how this could work:
|
The code-example below in meta code illustrates how this could work:
|
||||||
|
|
||||||
@@ -359,26 +361,10 @@ const textFieldUpdated = async function () {
|
|||||||
bindEventHandler('change', 'code', textFieldUpdated);
|
bindEventHandler('change', 'code', textFieldUpdated);
|
||||||
```
|
```
|
||||||
|
|
||||||
**Alternative to mermaid.parse():**
|
|
||||||
One effective and more future-proof method of validating your graph definitions, is to paste and render them via the [Mermaid Live Editor](https://mermaid.live/). This will ensure that your code is compliant with the syntax of Mermaid's most recent version.
|
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
Mermaid takes a number of options which lets you tweak the rendering of the diagrams. Currently there are three ways of
|
You can pass the required configuration to the `mermaid.initialize` call. This is the preferred way of configuring mermaid.
|
||||||
setting the options in mermaid.
|
The list of configuration objects are described [in the mermaidAPI documentation](./setup/README.md).
|
||||||
|
|
||||||
1. Instantiation of the configuration using the initialize call
|
|
||||||
2. _Using the global mermaid object_ - **Deprecated**
|
|
||||||
3. _using the global mermaid_config object_ - **Deprecated**
|
|
||||||
4. Instantiation of the configuration using the **mermaid.init** call- **Deprecated**
|
|
||||||
|
|
||||||
The list above has two ways too many of doing this. Three are deprecated and will eventually be removed. The list of
|
|
||||||
configuration objects are described [in the mermaidAPI documentation](./setup/README.md).
|
|
||||||
|
|
||||||
## Using the `mermaidAPI.initialize`/`mermaid.initialize` call
|
|
||||||
|
|
||||||
The future proof way of setting the configuration is by using the initialization call to mermaid or mermaidAPI depending
|
|
||||||
on what kind of integration you use.
|
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script type="module">
|
<script type="module">
|
||||||
@@ -407,32 +393,3 @@ mermaid.startOnLoad = true;
|
|||||||
|
|
||||||
> **Warning**
|
> **Warning**
|
||||||
> This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
> This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
||||||
|
|
||||||
## Using the mermaid_config
|
|
||||||
|
|
||||||
It is possible to set some configuration via the mermaid object. The two parameters that are supported using this
|
|
||||||
approach are:
|
|
||||||
|
|
||||||
- mermaid_config.startOnLoad
|
|
||||||
- mermaid_config.htmlLabels
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
mermaid_config.startOnLoad = true;
|
|
||||||
```
|
|
||||||
|
|
||||||
> **Warning**
|
|
||||||
> This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
|
||||||
|
|
||||||
## Using the mermaid.init call
|
|
||||||
|
|
||||||
To set some configuration via the mermaid object. The two parameters that are supported using this approach are:
|
|
||||||
|
|
||||||
- mermaid_config.startOnLoad
|
|
||||||
- mermaid_config.htmlLabels
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
mermaid_config.startOnLoad = true;
|
|
||||||
```
|
|
||||||
|
|
||||||
> **Warning**
|
|
||||||
> This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
|
||||||
|
@@ -328,15 +328,17 @@ module.exports = (options) ->
|
|||||||
|
|
||||||
## Advanced usage
|
## Advanced usage
|
||||||
|
|
||||||
**Syntax validation without rendering (Work in Progress)**
|
### Syntax validation without rendering
|
||||||
|
|
||||||
The **mermaid.parse(txt)** function validates graph definitions without rendering a graph. **[This function is still a work in progress](https://github.com/mermaid-js/mermaid/issues/1066), find alternatives below.**
|
The `mermaid.parse(text, parseOptions)` function validates graph definitions without rendering a graph.
|
||||||
|
|
||||||
The function **mermaid.parse(txt)**, takes a text string as an argument and returns true if the definition follows mermaid's syntax and
|
The function `mermaid.parse(text, parseOptions)`, takes a text string as an argument and returns `{ diagramType: string }` if the definition follows mermaid's syntax.
|
||||||
false if it does not. The parseError function will be called when the parse function returns false.
|
|
||||||
|
|
||||||
When the parser encounters invalid syntax the **mermaid.parseError** function is called. It is possible to override this
|
If the definition is invalid, the function returns `false` if `parseOptions.suppressErrors` is set to `true`. Otherwise, it throws an error.
|
||||||
function in order to handle the error in an application-specific way.
|
|
||||||
|
The parseError function will be called when the parse function throws an error. It will not be called if `parseOptions.suppressErrors` is set to `true`.
|
||||||
|
|
||||||
|
It is possible to override this function in order to handle the error in an application-specific way.
|
||||||
|
|
||||||
The code-example below in meta code illustrates how this could work:
|
The code-example below in meta code illustrates how this could work:
|
||||||
|
|
||||||
@@ -356,26 +358,10 @@ const textFieldUpdated = async function () {
|
|||||||
bindEventHandler('change', 'code', textFieldUpdated);
|
bindEventHandler('change', 'code', textFieldUpdated);
|
||||||
```
|
```
|
||||||
|
|
||||||
**Alternative to mermaid.parse():**
|
|
||||||
One effective and more future-proof method of validating your graph definitions, is to paste and render them via the [Mermaid Live Editor](https://mermaid.live/). This will ensure that your code is compliant with the syntax of Mermaid's most recent version.
|
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
Mermaid takes a number of options which lets you tweak the rendering of the diagrams. Currently there are three ways of
|
You can pass the required configuration to the `mermaid.initialize` call. This is the preferred way of configuring mermaid.
|
||||||
setting the options in mermaid.
|
The list of configuration objects are described [in the mermaidAPI documentation](./setup/README.md).
|
||||||
|
|
||||||
1. Instantiation of the configuration using the initialize call
|
|
||||||
2. _Using the global mermaid object_ - **Deprecated**
|
|
||||||
3. _using the global mermaid_config object_ - **Deprecated**
|
|
||||||
4. Instantiation of the configuration using the **mermaid.init** call- **Deprecated**
|
|
||||||
|
|
||||||
The list above has two ways too many of doing this. Three are deprecated and will eventually be removed. The list of
|
|
||||||
configuration objects are described [in the mermaidAPI documentation](./setup/README.md).
|
|
||||||
|
|
||||||
## Using the `mermaidAPI.initialize`/`mermaid.initialize` call
|
|
||||||
|
|
||||||
The future proof way of setting the configuration is by using the initialization call to mermaid or mermaidAPI depending
|
|
||||||
on what kind of integration you use.
|
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script type="module">
|
<script type="module">
|
||||||
@@ -406,34 +392,3 @@ mermaid.startOnLoad = true;
|
|||||||
```warning
|
```warning
|
||||||
This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
||||||
```
|
```
|
||||||
|
|
||||||
## Using the mermaid_config
|
|
||||||
|
|
||||||
It is possible to set some configuration via the mermaid object. The two parameters that are supported using this
|
|
||||||
approach are:
|
|
||||||
|
|
||||||
- mermaid_config.startOnLoad
|
|
||||||
- mermaid_config.htmlLabels
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
mermaid_config.startOnLoad = true;
|
|
||||||
```
|
|
||||||
|
|
||||||
```warning
|
|
||||||
This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
|
||||||
```
|
|
||||||
|
|
||||||
## Using the mermaid.init call
|
|
||||||
|
|
||||||
To set some configuration via the mermaid object. The two parameters that are supported using this approach are:
|
|
||||||
|
|
||||||
- mermaid_config.startOnLoad
|
|
||||||
- mermaid_config.htmlLabels
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
mermaid_config.startOnLoad = true;
|
|
||||||
```
|
|
||||||
|
|
||||||
```warning
|
|
||||||
This way of setting the configuration is deprecated. Instead the preferred way is to use the initialize method. This functionality is only kept for backwards compatibility.
|
|
||||||
```
|
|
||||||
|
@@ -6,7 +6,7 @@ import { dedent } from 'ts-dedent';
|
|||||||
import type { MermaidConfig } from './config.type.js';
|
import type { MermaidConfig } from './config.type.js';
|
||||||
import { log } from './logger.js';
|
import { log } from './logger.js';
|
||||||
import utils from './utils.js';
|
import utils from './utils.js';
|
||||||
import type { ParseOptions, RenderResult } from './mermaidAPI.js';
|
import type { ParseOptions, ParseResult, RenderResult } from './mermaidAPI.js';
|
||||||
import { mermaidAPI } from './mermaidAPI.js';
|
import { mermaidAPI } from './mermaidAPI.js';
|
||||||
import { registerLazyLoadedDiagrams, detectType } from './diagram-api/detectType.js';
|
import { registerLazyLoadedDiagrams, detectType } from './diagram-api/detectType.js';
|
||||||
import { loadRegisteredDiagrams } from './diagram-api/loadDiagram.js';
|
import { loadRegisteredDiagrams } from './diagram-api/loadDiagram.js';
|
||||||
@@ -24,6 +24,7 @@ export type {
|
|||||||
ParseErrorFunction,
|
ParseErrorFunction,
|
||||||
RenderResult,
|
RenderResult,
|
||||||
ParseOptions,
|
ParseOptions,
|
||||||
|
ParseResult,
|
||||||
UnknownDiagramError,
|
UnknownDiagramError,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -313,11 +314,23 @@ const executeQueue = async () => {
|
|||||||
/**
|
/**
|
||||||
* Parse the text and validate the syntax.
|
* Parse the text and validate the syntax.
|
||||||
* @param text - The mermaid diagram definition.
|
* @param text - The mermaid diagram definition.
|
||||||
* @param parseOptions - Options for parsing.
|
* @param parseOptions - Options for parsing. @see {@link ParseOptions}
|
||||||
* @returns true if the diagram is valid, false otherwise if parseOptions.suppressErrors is true.
|
* @returns If valid, {@link ParseResult} otherwise `false` if parseOptions.suppressErrors is `true`.
|
||||||
* @throws Error if the diagram is invalid and parseOptions.suppressErrors is false.
|
* @throws Error if the diagram is invalid and parseOptions.suppressErrors is false or not set.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```js
|
||||||
|
* console.log(await mermaid.parse('flowchart \n a --> b'));
|
||||||
|
* // { diagramType: 'flowchart-v2' }
|
||||||
|
* console.log(await mermaid.parse('wrong \n a --> b', { suppressErrors: true }));
|
||||||
|
* // false
|
||||||
|
* console.log(await mermaid.parse('wrong \n a --> b', { suppressErrors: false }));
|
||||||
|
* // throws Error
|
||||||
|
* console.log(await mermaid.parse('wrong \n a --> b'));
|
||||||
|
* // throws Error
|
||||||
|
* ```
|
||||||
*/
|
*/
|
||||||
const parse = async (text: string, parseOptions?: ParseOptions): Promise<boolean | void> => {
|
const parse: typeof mermaidAPI.parse = async (text, parseOptions) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// This promise will resolve when the render call is done.
|
// This promise will resolve when the render call is done.
|
||||||
// It will be queued first and will be executed when it is first in line
|
// It will be queued first and will be executed when it is first in line
|
||||||
@@ -366,7 +379,7 @@ const parse = async (text: string, parseOptions?: ParseOptions): Promise<boolean
|
|||||||
* element will be removed when rendering is completed.
|
* element will be removed when rendering is completed.
|
||||||
* @returns Returns the SVG Definition and BindFunctions.
|
* @returns Returns the SVG Definition and BindFunctions.
|
||||||
*/
|
*/
|
||||||
const render = (id: string, text: string, container?: Element): Promise<RenderResult> => {
|
const render: typeof mermaidAPI.render = (id, text, container) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// This promise will resolve when the mermaidAPI.render call is done.
|
// This promise will resolve when the mermaidAPI.render call is done.
|
||||||
// It will be queued first and will be executed when it is first in line
|
// It will be queued first and will be executed when it is first in line
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
import { vi } from 'vitest';
|
import { vi, it, expect, describe, beforeEach } from 'vitest';
|
||||||
|
|
||||||
// -------------------------------------
|
// -------------------------------------
|
||||||
// Mocks and mocking
|
// Mocks and mocking
|
||||||
@@ -685,14 +685,21 @@ describe('mermaidAPI', () => {
|
|||||||
).resolves.toBe(false);
|
).resolves.toBe(false);
|
||||||
});
|
});
|
||||||
it('resolves for valid definition', async () => {
|
it('resolves for valid definition', async () => {
|
||||||
await expect(
|
await expect(mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).resolves
|
||||||
mermaidAPI.parse('graph TD;A--x|text including URL space|B;')
|
.toMatchInlineSnapshot(`
|
||||||
).resolves.toBeTruthy();
|
{
|
||||||
|
"diagramType": "flowchart-v2",
|
||||||
|
}
|
||||||
|
`);
|
||||||
});
|
});
|
||||||
it('returns true for valid definition with silent option', async () => {
|
it('returns true for valid definition with silent option', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
mermaidAPI.parse('graph TD;A--x|text including URL space|B;', { suppressErrors: true })
|
mermaidAPI.parse('graph TD;A--x|text including URL space|B;', { suppressErrors: true })
|
||||||
).resolves.toBe(true);
|
).resolves.toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"diagramType": "flowchart-v2",
|
||||||
|
}
|
||||||
|
`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -734,7 +741,8 @@ describe('mermaidAPI', () => {
|
|||||||
it('should set aria-roledscription to the diagram type AND should call addSVGa11yTitleDescription', async () => {
|
it('should set aria-roledscription to the diagram type AND should call addSVGa11yTitleDescription', async () => {
|
||||||
const a11yDiagramInfo_spy = vi.spyOn(accessibility, 'setA11yDiagramInfo');
|
const a11yDiagramInfo_spy = vi.spyOn(accessibility, 'setA11yDiagramInfo');
|
||||||
const a11yTitleDesc_spy = vi.spyOn(accessibility, 'addSVGa11yTitleDescription');
|
const a11yTitleDesc_spy = vi.spyOn(accessibility, 'addSVGa11yTitleDescription');
|
||||||
await mermaidAPI.render(id, diagramText);
|
const result = await mermaidAPI.render(id, diagramText);
|
||||||
|
expect(result.diagramType).toBe(expectedDiagramType);
|
||||||
expect(a11yDiagramInfo_spy).toHaveBeenCalledWith(
|
expect(a11yDiagramInfo_spy).toHaveBeenCalledWith(
|
||||||
expect.anything(),
|
expect.anything(),
|
||||||
expectedDiagramType
|
expectedDiagramType
|
||||||
|
@@ -57,9 +57,19 @@ const DOMPURIFY_TAGS = ['foreignobject'];
|
|||||||
const DOMPURIFY_ATTR = ['dominant-baseline'];
|
const DOMPURIFY_ATTR = ['dominant-baseline'];
|
||||||
|
|
||||||
export interface ParseOptions {
|
export interface ParseOptions {
|
||||||
|
/**
|
||||||
|
* If `true`, parse will return `false` instead of throwing error when the diagram is invalid.
|
||||||
|
* The `parseError` function will not be called.
|
||||||
|
*/
|
||||||
suppressErrors?: boolean;
|
suppressErrors?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ParseResult {
|
||||||
|
/**
|
||||||
|
* The diagram type, e.g. 'flowchart', 'sequence', etc.
|
||||||
|
*/
|
||||||
|
diagramType: string;
|
||||||
|
}
|
||||||
// This makes it clear that we're working with a d3 selected element of some kind, even though it's hard to specify the exact type.
|
// This makes it clear that we're working with a d3 selected element of some kind, even though it's hard to specify the exact type.
|
||||||
export type D3Element = any;
|
export type D3Element = any;
|
||||||
|
|
||||||
@@ -68,6 +78,10 @@ export interface RenderResult {
|
|||||||
* The svg code for the rendered graph.
|
* The svg code for the rendered graph.
|
||||||
*/
|
*/
|
||||||
svg: string;
|
svg: string;
|
||||||
|
/**
|
||||||
|
* The diagram type, e.g. 'flowchart', 'sequence', etc.
|
||||||
|
*/
|
||||||
|
diagramType: string;
|
||||||
/**
|
/**
|
||||||
* Bind function to be called after the svg has been inserted into the DOM.
|
* Bind function to be called after the svg has been inserted into the DOM.
|
||||||
* This is necessary for adding event listeners to the elements in the svg.
|
* This is necessary for adding event listeners to the elements in the svg.
|
||||||
@@ -90,29 +104,29 @@ function processAndSetConfigs(text: string) {
|
|||||||
/**
|
/**
|
||||||
* Parse the text and validate the syntax.
|
* Parse the text and validate the syntax.
|
||||||
* @param text - The mermaid diagram definition.
|
* @param text - The mermaid diagram definition.
|
||||||
* @param parseOptions - Options for parsing.
|
* @param parseOptions - Options for parsing. @see {@link ParseOptions}
|
||||||
* @returns true if the diagram is valid, false otherwise if parseOptions.suppressErrors is true.
|
* @returns An object with the `diagramType` set to type of the diagram if valid. Otherwise `false` if parseOptions.suppressErrors is `true`.
|
||||||
* @throws Error if the diagram is invalid and parseOptions.suppressErrors is false.
|
* @throws Error if the diagram is invalid and parseOptions.suppressErrors is false or not set.
|
||||||
*/
|
*/
|
||||||
|
async function parse(
|
||||||
async function parse(text: string, parseOptions?: ParseOptions): Promise<boolean> {
|
text: string,
|
||||||
|
parseOptions: ParseOptions & { suppressErrors: true }
|
||||||
|
): Promise<ParseResult | false>;
|
||||||
|
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult>;
|
||||||
|
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult | false> {
|
||||||
addDiagrams();
|
addDiagrams();
|
||||||
|
|
||||||
text = processAndSetConfigs(text).code;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await getDiagramFromText(text);
|
const { code } = processAndSetConfigs(text);
|
||||||
|
const diagram = await getDiagramFromText(code);
|
||||||
|
return { diagramType: diagram.type };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (parseOptions?.suppressErrors) {
|
if (parseOptions?.suppressErrors) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// append !important; to each cssClass followed by a final !important, all enclosed in { }
|
|
||||||
//
|
|
||||||
/**
|
/**
|
||||||
* Create a CSS style that starts with the given class name, then the element,
|
* Create a CSS style that starts with the given class name, then the element,
|
||||||
* with an enclosing block that has each of the cssClasses followed by !important;
|
* with an enclosing block that has each of the cssClasses followed by !important;
|
||||||
@@ -483,6 +497,7 @@ const render = async function (
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
diagramType,
|
||||||
svg: svgCode,
|
svg: svgCode,
|
||||||
bindFunctions: diag.db.bindFunctions,
|
bindFunctions: diag.db.bindFunctions,
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user