|
| 1 | +import { DEFAULT_FONT_SIZE } from "./constants.js"; |
1 | 2 | import { graphToExcalidraw } from "./graphToExcalidraw.js"; |
2 | 3 | import { parseMermaid } from "./parseMermaid.js"; |
3 | 4 |
|
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 { |
5 | 38 | fontSize?: number; |
6 | 39 | } |
| 40 | + |
7 | 41 | const parseMermaidToExcalidraw = async ( |
8 | 42 | definition: string, |
9 | | - options: MermaidOptions = {} |
| 43 | + config?: MermaidConfig |
10 | 44 | ) => { |
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 | + }); |
13 | 60 | return excalidrawElements; |
14 | 61 | }; |
15 | 62 |
|
|
0 commit comments