Skip to content

Commit

Permalink
feat: support maxEdges, maxTextSize and more params in mermaid config…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
ad1992 authored Jul 10, 2024
1 parent 1a77181 commit 73ab442
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 23 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
## Unreleased
## v1.1.0 (2024-07-10)

## Library

### Features

- 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.

You can read about it [here](https://github.com/excalidraw/mermaid-to-excalidraw/blob/f2c4908acd1a4837e71169fd9b339a7eee1c63bc/README.md#get-started).

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).

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

Expand Down
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,63 @@ yarn build

## Get started

```ts
parseMermaidToExcalidraw(diagramDefinition: string, config?: MermaidConfig)
```

The `diagramDefinition` is the mermaid diagram definition.
and `config` is the mermaid config. You can use the `config` param when you want to pass some custom config to mermaid.

Currently `mermaid-to-excalidraw` only supports the :point_down: config params

```ts
{
/**
* Whether to start the diagram automatically when the page loads.
* @default false
*/
startOnLoad?: boolean;
/**
* The flowchart curve style.
* @default "linear"
*/
flowchart?: {
curve?: "linear" | "basis";
};
/**
* Theme variables
* @default { fontSize: "20px" }
*/
themeVariables?: {
fontSize?: string;
};
/**
* Maximum number of edges to be rendered.
* @default 500
*/
maxEdges?: number;
/**
* Maximum number of characters to be rendered.
* @default 50000
*/
maxTextSize?: number;
}
```

Example code:

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

try {
const { elements, files } = await parseMermaidToExcalidraw(diagramDefinition, {
fontSize: DEFAULT_FONT_SIZE,
});
const { elements, files } = await parseMermaidToExcalidraw(
diagramDefinition,
{
themeVariables: {
fontSize: "25px",
},
}
);
// Render elements and files on Excalidraw
} catch (e) {
// Parse error, displaying error message to users
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@excalidraw/mermaid-to-excalidraw",
"version": "1.0.0",
"version": "1.1.0",
"description": "Mermaid to Excalidraw Diagrams",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ export const SVG_TO_SHAPE_MAPPER: { [key: string]: "rectangle" | "ellipse" } = {
circle: "ellipse",
};

// visit https://mermaid.js.org/schemas/config.schema.json for default schema
export const MERMAID_CONFIG = {
startOnLoad: false,
flowchart: { curve: "linear" },
themeVariables: {
// Multiplying by 1.25 to increase the font size by 25% and render correctly in Excalidraw
fontSize: `${DEFAULT_FONT_SIZE * 1.25}px`,
},
maxEdges: 500,
maxTextSize: 50000,
};
10 changes: 5 additions & 5 deletions src/converter/GraphConverter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MermaidOptions } from "../index.js";
import { ExcalidrawConfig } from "../index.js";
import { DEFAULT_FONT_SIZE } from "../constants.js";
import { MermaidToExcalidrawResult } from "../interfaces.js";
import { Flowchart } from "../parser/flowchart.js";
Expand All @@ -11,15 +11,15 @@ export class GraphConverter<T = Flowchart | Sequence> {
}: {
converter: (
graph: T,
options: Required<MermaidOptions>
config: Required<ExcalidrawConfig>
) => MermaidToExcalidrawResult;
}) {
this.converter = converter;
}
convert = (graph: T, options: MermaidOptions) => {
convert = (graph: T, config: ExcalidrawConfig) => {
return this.converter(graph, {
...options,
fontSize: options.fontSize || DEFAULT_FONT_SIZE,
...config,
fontSize: config.fontSize || DEFAULT_FONT_SIZE,
});
};
}
4 changes: 2 additions & 2 deletions src/graphToExcalidraw.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MermaidOptions } from "./index.js";
import { ExcalidrawConfig } from "./index.js";
import { FlowchartToExcalidrawSkeletonConverter } from "./converter/types/flowchart.js";
import { GraphImageConverter } from "./converter/types/graphImage.js";
import { GraphImage, MermaidToExcalidrawResult } from "./interfaces.js";
Expand All @@ -10,7 +10,7 @@ import { classToExcalidrawSkeletonConvertor } from "./converter/types/class.js";

export const graphToExcalidraw = (
graph: Flowchart | GraphImage | Sequence | Class,
options: MermaidOptions = {}
options: ExcalidrawConfig = {}
): MermaidToExcalidrawResult => {
switch (graph.type) {
case "graphImage": {
Expand Down
55 changes: 51 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
import { DEFAULT_FONT_SIZE } from "./constants.js";
import { graphToExcalidraw } from "./graphToExcalidraw.js";
import { parseMermaid } from "./parseMermaid.js";

export interface MermaidOptions {
export interface MermaidConfig {
/**
* Whether to start the diagram automatically when the page loads.
* @default false
*/
startOnLoad?: boolean;
/**
* The flowchart curve style.
* @default "linear"
*/
flowchart?: {
curve?: "linear" | "basis";
};
/**
* Theme variables
* @default { fontSize: "25px" }
*/
themeVariables?: {
fontSize?: string;
};
/**
* Maximum number of edges to be rendered.
* @default 1000
*/
maxEdges?: number;
/**
* Maximum number of characters to be rendered.
* @default 1000
*/
maxTextSize?: number;
}

export interface ExcalidrawConfig {
fontSize?: number;
}

const parseMermaidToExcalidraw = async (
definition: string,
options: MermaidOptions = {}
config?: MermaidConfig
) => {
const parsedMermaidData = await parseMermaid(definition);
const excalidrawElements = graphToExcalidraw(parsedMermaidData, options);
const mermaidConfig = config || {};
const fontSize =
parseInt(mermaidConfig.themeVariables?.fontSize ?? "") || DEFAULT_FONT_SIZE;
const parsedMermaidData = await parseMermaid(definition, {
...mermaidConfig,
themeVariables: {
...mermaidConfig.themeVariables,
// Multiplying by 1.25 to increase the font size by 25% and render correctly in Excalidraw
fontSize: `${fontSize * 1.25}px`,
},
});
// Only font size supported for excalidraw elements
const excalidrawElements = graphToExcalidraw(parsedMermaidData, {
fontSize,
});
return excalidrawElements;
};

Expand Down
10 changes: 5 additions & 5 deletions src/parseMermaid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mermaid from "mermaid";
import mermaid, { MermaidConfig } from "mermaid";
import { GraphImage } from "./interfaces.js";
import { DEFAULT_FONT_SIZE, MERMAID_CONFIG } from "./constants.js";
import { MERMAID_CONFIG } from "./constants.js";
import { encodeEntities } from "./utils.js";
import { Flowchart, parseMermaidFlowChartDiagram } from "./parser/flowchart.js";
import { Sequence, parseMermaidSequenceDiagram } from "./parser/sequence.js";
Expand Down Expand Up @@ -43,10 +43,10 @@ const convertSvgToGraphImage = (svgContainer: HTMLDivElement) => {
};

export const parseMermaid = async (
definition: string
definition: string,
config: MermaidConfig = MERMAID_CONFIG
): Promise<Flowchart | GraphImage | Sequence | Class> => {
mermaid.initialize(MERMAID_CONFIG);

mermaid.initialize({ ...MERMAID_CONFIG, ...config });
// Parse the diagram
const diagram = await mermaid.mermaidAPI.getDiagramFromText(
encodeEntities(definition)
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ export type ExcalidrawVertexElement =
| ExcalidrawRectangleElement
| ExcalidrawDiamondElement
| ExcalidrawEllipseElement;


0 comments on commit 73ab442

Please sign in to comment.