Skip to content

Commit 68cdc75

Browse files
Minor cleanup
Co-authored-by: Per Brolin <[email protected]>
1 parent 38b2cbc commit 68cdc75

File tree

4 files changed

+43
-55
lines changed

4 files changed

+43
-55
lines changed

docs/config/setup/interfaces/mermaidAPI.RenderResult.md

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,6 @@
88

99
[mermaidAPI](../modules/mermaidAPI.md).RenderResult
1010

11-
Function that renders an svg with a graph from a chart definition. Usage example below.
12-
13-
```javascript
14-
mermaidAPI.initialize({
15-
startOnLoad: true,
16-
});
17-
$(function () {
18-
const graphDefinition = 'graph TB\na-->b';
19-
const cb = function (svgGraph) {
20-
console.log(svgGraph);
21-
};
22-
mermaidAPI.render('id1', graphDefinition, cb);
23-
});
24-
```
25-
26-
**`Param`**
27-
28-
The id for the SVG element (the element to be rendered)
29-
30-
**`Param`**
31-
32-
The text for the graph definition
33-
34-
**`Param`**
35-
36-
Callback which is called after rendering is finished with the svg code as in param.
37-
38-
**`Param`**
39-
40-
HTML element where the svg will be inserted. (Is usually element with the .mermaid class)
41-
If no svgContainingElement is provided then the SVG element will be appended to the body.
42-
Selector to element in which a div with the graph temporarily will be
43-
inserted. If one is provided a hidden div will be inserted in the body of the page instead. The
44-
element will be removed when rendering is completed.
45-
4611
## Properties
4712

4813
### bindFunctions
@@ -53,6 +18,15 @@ element will be removed when rendering is completed.
5318

5419
▸ (`element`): `void`
5520

21+
Bind function to be called after the svg has been inserted into the DOM.
22+
This is necessary for adding event listeners to the elements in the svg.
23+
24+
```js
25+
const { svg, bindFunctions } = mermaidAPI.render('id1', 'graph TD;A-->B');
26+
div.innerHTML = svg;
27+
bindFunctions?.(div); // To call bindFunctions only if it's present.
28+
```
29+
5630
##### Parameters
5731
5832
| Name | Type |
@@ -65,14 +39,16 @@ element will be removed when rendering is completed.
6539
6640
#### Defined in
6741
68-
[mermaidAPI.ts:384](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L384)
42+
[mermaidAPI.ts:91](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L91)
6943
7044
---
7145
7246
### svg
7347
7448
• **svg**: `string`
7549
50+
The svg code for the rendered graph.
51+
7652
#### Defined in
7753
78-
[mermaidAPI.ts:383](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L383)
54+
[mermaidAPI.ts:81](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L81)

docs/config/setup/modules/mermaidAPI.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ mermaid.initialize(config);
9595

9696
#### Defined in
9797

98-
[mermaidAPI.ts:668](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L668)
98+
[mermaidAPI.ts:680](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L680)
9999

100100
## Functions
101101

@@ -126,7 +126,7 @@ Return the last node appended
126126

127127
#### Defined in
128128

129-
[mermaidAPI.ts:291](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L291)
129+
[mermaidAPI.ts:308](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L308)
130130

131131
---
132132

@@ -152,7 +152,7 @@ the cleaned up svgCode
152152

153153
#### Defined in
154154

155-
[mermaidAPI.ts:242](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L242)
155+
[mermaidAPI.ts:259](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L259)
156156

157157
---
158158

@@ -178,7 +178,7 @@ the string with all the user styles
178178

179179
#### Defined in
180180

181-
[mermaidAPI.ts:171](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L171)
181+
[mermaidAPI.ts:188](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L188)
182182

183183
---
184184

@@ -201,7 +201,7 @@ the string with all the user styles
201201

202202
#### Defined in
203203

204-
[mermaidAPI.ts:219](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L219)
204+
[mermaidAPI.ts:236](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L236)
205205

206206
---
207207

@@ -228,7 +228,7 @@ with an enclosing block that has each of the cssClasses followed by !important;
228228

229229
#### Defined in
230230

231-
[mermaidAPI.ts:155](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L155)
231+
[mermaidAPI.ts:172](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L172)
232232

233233
---
234234

@@ -248,7 +248,7 @@ with an enclosing block that has each of the cssClasses followed by !important;
248248

249249
#### Defined in
250250

251-
[mermaidAPI.ts:135](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L135)
251+
[mermaidAPI.ts:152](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L152)
252252

253253
---
254254

@@ -268,7 +268,7 @@ with an enclosing block that has each of the cssClasses followed by !important;
268268

269269
#### Defined in
270270

271-
[mermaidAPI.ts:106](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L106)
271+
[mermaidAPI.ts:123](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L123)
272272

273273
---
274274

@@ -294,7 +294,7 @@ Put the svgCode into an iFrame. Return the iFrame code
294294

295295
#### Defined in
296296

297-
[mermaidAPI.ts:270](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L270)
297+
[mermaidAPI.ts:287](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L287)
298298

299299
---
300300

@@ -319,4 +319,4 @@ Remove any existing elements from the given document
319319

320320
#### Defined in
321321

322-
[mermaidAPI.ts:341](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L341)
322+
[mermaidAPI.ts:358](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L358)

packages/mermaid/src/Diagram.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export class Diagram {
6868
}
6969
}
7070

71-
export const getDiagramFromText = async (txt: string): Promise<Diagram> => {
72-
const type = detectType(txt, configApi.getConfig());
71+
export const getDiagramFromText = async (text: string): Promise<Diagram> => {
72+
const type = detectType(text, configApi.getConfig());
7373
try {
7474
// Trying to find the diagram
7575
getDiagram(type);
@@ -83,5 +83,5 @@ export const getDiagramFromText = async (txt: string): Promise<Diagram> => {
8383
const { id, diagram } = await loader();
8484
registerDiagram(id, diagram);
8585
}
86-
return new Diagram(txt);
86+
return new Diagram(text);
8787
};

packages/mermaid/src/mermaidAPI.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ export interface ParseOptions {
7474
// @ts-ignore Could replicate the type definition in d3. This also makes it possible to use the untyped info from the js diagram files.
7575
export type D3Element = any;
7676

77+
export interface RenderResult {
78+
/**
79+
* The svg code for the rendered graph.
80+
*/
81+
svg: string;
82+
/**
83+
* Bind function to be called after the svg has been inserted into the DOM.
84+
* This is necessary for adding event listeners to the elements in the svg.
85+
* ```js
86+
* const { svg, bindFunctions } = mermaidAPI.render('id1', 'graph TD;A-->B');
87+
* div.innerHTML = svg;
88+
* bindFunctions?.(div); // To call bindFunctions only if it's present.
89+
* ```
90+
*/
91+
bindFunctions?: (element: Element) => void;
92+
}
93+
7794
/**
7895
* Parse the text and validate the syntax.
7996
* @param text - The mermaid diagram definition.
@@ -379,11 +396,6 @@ export const removeExistingElements = (
379396
* @returns Returns the rendered element as a string containing the SVG definition.
380397
*/
381398

382-
export interface RenderResult {
383-
svg: string;
384-
bindFunctions?: (element: Element) => void;
385-
}
386-
387399
const render = async function (
388400
id: string,
389401
text: string,

0 commit comments

Comments
 (0)