Skip to content

Commit 73ab442

Browse files
authored
feat: support maxEdges, maxTextSize and more params in mermaid config and release v1.1.0 🚀 (#68)
* feat: support maxEdges and maxTextSize in mermaid config * fix font size variable * add link * update cl * update fontSize variable in readme * update docs * update docs * change fontSize to string * fix * font size fixes * add max edge and max TextSize: * update cl * fix playground * spread the config with init config so diagram doesn't get impacted * mark options as required in convertor * upgrade to v1.1.0
1 parent 1a77181 commit 73ab442

File tree

9 files changed

+130
-23
lines changed

9 files changed

+130
-23
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
## Unreleased
1+
## v1.1.0 (2024-07-10)
2+
3+
## Library
4+
5+
### Features
6+
7+
- Add support for passing config params `maxEdge`, `maxTextSize` and more params to mermaid by [@ad1992](https://github.com/ad1992) in https://github.com/excalidraw/mermaid-to-excalidraw/pull/68.
8+
9+
You can read about it [here](https://github.com/excalidraw/mermaid-to-excalidraw/blob/f2c4908acd1a4837e71169fd9b339a7eee1c63bc/README.md#get-started).
10+
11+
Additonally the param `fontSize` is renamed to `themeVariables.fontSize` and type is changed from `number` to `string` to be consistent with the [mermaid config](https://mermaid.js.org/schemas/config.schema.json).
212

313
## v1.0.0 (2024-05-20)
414

README.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,63 @@ yarn build
2424

2525
## Get started
2626

27+
```ts
28+
parseMermaidToExcalidraw(diagramDefinition: string, config?: MermaidConfig)
29+
```
30+
31+
The `diagramDefinition` is the mermaid diagram definition.
32+
and `config` is the mermaid config. You can use the `config` param when you want to pass some custom config to mermaid.
33+
34+
Currently `mermaid-to-excalidraw` only supports the :point_down: config params
35+
36+
```ts
37+
{
38+
/**
39+
* Whether to start the diagram automatically when the page loads.
40+
* @default false
41+
*/
42+
startOnLoad?: boolean;
43+
/**
44+
* The flowchart curve style.
45+
* @default "linear"
46+
*/
47+
flowchart?: {
48+
curve?: "linear" | "basis";
49+
};
50+
/**
51+
* Theme variables
52+
* @default { fontSize: "20px" }
53+
*/
54+
themeVariables?: {
55+
fontSize?: string;
56+
};
57+
/**
58+
* Maximum number of edges to be rendered.
59+
* @default 500
60+
*/
61+
maxEdges?: number;
62+
/**
63+
* Maximum number of characters to be rendered.
64+
* @default 50000
65+
*/
66+
maxTextSize?: number;
67+
}
68+
```
69+
2770
Example code:
2871

2972
```ts
3073
import { parseMermaidToExcalidraw } from "@excalidraw/mermaid-to-excalidraw";
3174

3275
try {
33-
const { elements, files } = await parseMermaidToExcalidraw(diagramDefinition, {
34-
fontSize: DEFAULT_FONT_SIZE,
35-
});
76+
const { elements, files } = await parseMermaidToExcalidraw(
77+
diagramDefinition,
78+
{
79+
themeVariables: {
80+
fontSize: "25px",
81+
},
82+
}
83+
);
3684
// Render elements and files on Excalidraw
3785
} catch (e) {
3886
// Parse error, displaying error message to users

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@excalidraw/mermaid-to-excalidraw",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Mermaid to Excalidraw Diagrams",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ export const SVG_TO_SHAPE_MAPPER: { [key: string]: "rectangle" | "ellipse" } = {
55
circle: "ellipse",
66
};
77

8+
// visit https://mermaid.js.org/schemas/config.schema.json for default schema
89
export const MERMAID_CONFIG = {
910
startOnLoad: false,
1011
flowchart: { curve: "linear" },
1112
themeVariables: {
13+
// Multiplying by 1.25 to increase the font size by 25% and render correctly in Excalidraw
1214
fontSize: `${DEFAULT_FONT_SIZE * 1.25}px`,
1315
},
16+
maxEdges: 500,
17+
maxTextSize: 50000,
1418
};

src/converter/GraphConverter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MermaidOptions } from "../index.js";
1+
import { ExcalidrawConfig } from "../index.js";
22
import { DEFAULT_FONT_SIZE } from "../constants.js";
33
import { MermaidToExcalidrawResult } from "../interfaces.js";
44
import { Flowchart } from "../parser/flowchart.js";
@@ -11,15 +11,15 @@ export class GraphConverter<T = Flowchart | Sequence> {
1111
}: {
1212
converter: (
1313
graph: T,
14-
options: Required<MermaidOptions>
14+
config: Required<ExcalidrawConfig>
1515
) => MermaidToExcalidrawResult;
1616
}) {
1717
this.converter = converter;
1818
}
19-
convert = (graph: T, options: MermaidOptions) => {
19+
convert = (graph: T, config: ExcalidrawConfig) => {
2020
return this.converter(graph, {
21-
...options,
22-
fontSize: options.fontSize || DEFAULT_FONT_SIZE,
21+
...config,
22+
fontSize: config.fontSize || DEFAULT_FONT_SIZE,
2323
});
2424
};
2525
}

src/graphToExcalidraw.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MermaidOptions } from "./index.js";
1+
import { ExcalidrawConfig } from "./index.js";
22
import { FlowchartToExcalidrawSkeletonConverter } from "./converter/types/flowchart.js";
33
import { GraphImageConverter } from "./converter/types/graphImage.js";
44
import { GraphImage, MermaidToExcalidrawResult } from "./interfaces.js";
@@ -10,7 +10,7 @@ import { classToExcalidrawSkeletonConvertor } from "./converter/types/class.js";
1010

1111
export const graphToExcalidraw = (
1212
graph: Flowchart | GraphImage | Sequence | Class,
13-
options: MermaidOptions = {}
13+
options: ExcalidrawConfig = {}
1414
): MermaidToExcalidrawResult => {
1515
switch (graph.type) {
1616
case "graphImage": {

src/index.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,62 @@
1+
import { DEFAULT_FONT_SIZE } from "./constants.js";
12
import { graphToExcalidraw } from "./graphToExcalidraw.js";
23
import { parseMermaid } from "./parseMermaid.js";
34

4-
export interface MermaidOptions {
5+
export interface MermaidConfig {
6+
/**
7+
* Whether to start the diagram automatically when the page loads.
8+
* @default false
9+
*/
10+
startOnLoad?: boolean;
11+
/**
12+
* The flowchart curve style.
13+
* @default "linear"
14+
*/
15+
flowchart?: {
16+
curve?: "linear" | "basis";
17+
};
18+
/**
19+
* Theme variables
20+
* @default { fontSize: "25px" }
21+
*/
22+
themeVariables?: {
23+
fontSize?: string;
24+
};
25+
/**
26+
* Maximum number of edges to be rendered.
27+
* @default 1000
28+
*/
29+
maxEdges?: number;
30+
/**
31+
* Maximum number of characters to be rendered.
32+
* @default 1000
33+
*/
34+
maxTextSize?: number;
35+
}
36+
37+
export interface ExcalidrawConfig {
538
fontSize?: number;
639
}
40+
741
const parseMermaidToExcalidraw = async (
842
definition: string,
9-
options: MermaidOptions = {}
43+
config?: MermaidConfig
1044
) => {
11-
const parsedMermaidData = await parseMermaid(definition);
12-
const excalidrawElements = graphToExcalidraw(parsedMermaidData, options);
45+
const mermaidConfig = config || {};
46+
const fontSize =
47+
parseInt(mermaidConfig.themeVariables?.fontSize ?? "") || DEFAULT_FONT_SIZE;
48+
const parsedMermaidData = await parseMermaid(definition, {
49+
...mermaidConfig,
50+
themeVariables: {
51+
...mermaidConfig.themeVariables,
52+
// Multiplying by 1.25 to increase the font size by 25% and render correctly in Excalidraw
53+
fontSize: `${fontSize * 1.25}px`,
54+
},
55+
});
56+
// Only font size supported for excalidraw elements
57+
const excalidrawElements = graphToExcalidraw(parsedMermaidData, {
58+
fontSize,
59+
});
1360
return excalidrawElements;
1461
};
1562

src/parseMermaid.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import mermaid from "mermaid";
1+
import mermaid, { MermaidConfig } from "mermaid";
22
import { GraphImage } from "./interfaces.js";
3-
import { DEFAULT_FONT_SIZE, MERMAID_CONFIG } from "./constants.js";
3+
import { MERMAID_CONFIG } from "./constants.js";
44
import { encodeEntities } from "./utils.js";
55
import { Flowchart, parseMermaidFlowChartDiagram } from "./parser/flowchart.js";
66
import { Sequence, parseMermaidSequenceDiagram } from "./parser/sequence.js";
@@ -43,10 +43,10 @@ const convertSvgToGraphImage = (svgContainer: HTMLDivElement) => {
4343
};
4444

4545
export const parseMermaid = async (
46-
definition: string
46+
definition: string,
47+
config: MermaidConfig = MERMAID_CONFIG
4748
): Promise<Flowchart | GraphImage | Sequence | Class> => {
48-
mermaid.initialize(MERMAID_CONFIG);
49-
49+
mermaid.initialize({ ...MERMAID_CONFIG, ...config });
5050
// Parse the diagram
5151
const diagram = await mermaid.mermaidAPI.getDiagramFromText(
5252
encodeEntities(definition)

src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ export type ExcalidrawVertexElement =
1616
| ExcalidrawRectangleElement
1717
| ExcalidrawDiamondElement
1818
| ExcalidrawEllipseElement;
19-
20-

0 commit comments

Comments
 (0)