Skip to content

Commit 64abf29

Browse files
committed
feat: Return parsed config from mermaid.parse
1 parent bfc4abe commit 64abf29

File tree

7 files changed

+97
-13
lines changed

7 files changed

+97
-13
lines changed

.changeset/giant-steaks-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'mermaid': minor
3+
---
4+
5+
feat: Return parsed config from mermaid.parse

docs/config/setup/interfaces/mermaid.ParseOptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ The `parseError` function will not be called.
1919

2020
#### Defined in
2121

22-
[packages/mermaid/src/types.ts:43](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L43)
22+
[packages/mermaid/src/types.ts:45](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L45)

docs/config/setup/interfaces/mermaid.ParseResult.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010

1111
## Properties
1212

13+
### config
14+
15+
`Optional` **config**: [`MermaidConfig`](mermaid.MermaidConfig.md)
16+
17+
The config passed as YAML frontmatter or directives
18+
19+
#### Defined in
20+
21+
[packages/mermaid/src/types.ts:56](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L56)
22+
23+
---
24+
1325
### diagramType
1426

1527
**diagramType**: `string`
@@ -18,4 +30,4 @@ The diagram type, e.g. 'flowchart', 'sequence', etc.
1830

1931
#### Defined in
2032

21-
[packages/mermaid/src/types.ts:50](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L50)
33+
[packages/mermaid/src/types.ts:52](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L52)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bindFunctions?.(div); // To call bindFunctions only if it's present.
3939
4040
#### Defined in
4141
42-
[packages/mermaid/src/types.ts:73](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L73)
42+
[packages/mermaid/src/types.ts:79](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L79)
4343
4444
---
4545
@@ -51,7 +51,7 @@ The diagram type, e.g. 'flowchart', 'sequence', etc.
5151
5252
#### Defined in
5353
54-
[packages/mermaid/src/types.ts:63](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L63)
54+
[packages/mermaid/src/types.ts:69](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L69)
5555
5656
---
5757
@@ -63,4 +63,4 @@ The svg code for the rendered graph.
6363
6464
#### Defined in
6565
66-
[packages/mermaid/src/types.ts:59](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L59)
66+
[packages/mermaid/src/types.ts:65](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L65)

packages/mermaid/src/mermaidAPI.spec.ts

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { vi, it, expect, describe, beforeEach } from 'vitest';
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
22

33
// -------------------------------------
44
// Mocks and mocking
@@ -66,8 +66,8 @@ vi.mock('stylis', () => {
6666
});
6767

6868
import { compile, serialize } from 'stylis';
69-
import { decodeEntities, encodeEntities } from './utils.js';
7069
import { Diagram } from './Diagram.js';
70+
import { decodeEntities, encodeEntities } from './utils.js';
7171
import { toBase64 } from './utils/base64.js';
7272

7373
/**
@@ -693,18 +693,79 @@ describe('mermaidAPI', () => {
693693
await expect(mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).resolves
694694
.toMatchInlineSnapshot(`
695695
{
696+
"config": {},
696697
"diagramType": "flowchart-v2",
697698
}
698699
`);
699700
});
701+
it('returns config when defined in frontmatter', async () => {
702+
await expect(
703+
mermaidAPI.parse(`---
704+
config:
705+
theme: base
706+
flowchart:
707+
htmlLabels: true
708+
---
709+
graph TD;A--x|text including URL space|B;`)
710+
).resolves.toMatchInlineSnapshot(`
711+
{
712+
"config": {
713+
"flowchart": {
714+
"htmlLabels": true,
715+
},
716+
"theme": "base",
717+
},
718+
"diagramType": "flowchart-v2",
719+
}
720+
`);
721+
});
722+
723+
it('returns config when defined in directive', async () => {
724+
await expect(
725+
mermaidAPI.parse(`%%{init: { 'theme': 'base' } }%%
726+
graph TD;A--x|text including URL space|B;`)
727+
).resolves.toMatchInlineSnapshot(`
728+
{
729+
"config": {
730+
"theme": "base",
731+
},
732+
"diagramType": "flowchart-v2",
733+
}
734+
`);
735+
});
736+
737+
it('returns merged config when defined in frontmatter and directive', async () => {
738+
await expect(
739+
mermaidAPI.parse(`---
740+
config:
741+
theme: forest
742+
flowchart:
743+
htmlLabels: true
744+
---
745+
%%{init: { 'theme': 'base' } }%%
746+
graph TD;A--x|text including URL space|B;`)
747+
).resolves.toMatchInlineSnapshot(`
748+
{
749+
"config": {
750+
"flowchart": {
751+
"htmlLabels": true,
752+
},
753+
"theme": "base",
754+
},
755+
"diagramType": "flowchart-v2",
756+
}
757+
`);
758+
});
759+
700760
it('returns true for valid definition with silent option', async () => {
701761
await expect(
702762
mermaidAPI.parse('graph TD;A--x|text including URL space|B;', { suppressErrors: true })
703763
).resolves.toMatchInlineSnapshot(`
704-
{
705-
"diagramType": "flowchart-v2",
706-
}
707-
`);
764+
{
765+
"config": {},
766+
"diagramType": "flowchart-v2",
767+
}
768+
`);
708769
});
709770
});
710771

packages/mermaid/src/mermaidAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseRe
7373
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult | false> {
7474
addDiagrams();
7575
try {
76-
const { code } = processAndSetConfigs(text);
76+
const { code, config } = processAndSetConfigs(text);
7777
const diagram = await getDiagramFromText(code);
78-
return { diagramType: diagram.type };
78+
return { diagramType: diagram.type, config };
7979
} catch (error) {
8080
if (parseOptions?.suppressErrors) {
8181
return false;

packages/mermaid/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { MermaidConfig } from './config.type.js';
2+
13
export interface Point {
24
x: number;
35
y: number;
@@ -48,6 +50,10 @@ export interface ParseResult {
4850
* The diagram type, e.g. 'flowchart', 'sequence', etc.
4951
*/
5052
diagramType: string;
53+
/**
54+
* The config passed as YAML frontmatter or directives
55+
*/
56+
config?: MermaidConfig;
5157
}
5258
// 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.
5359
export type D3Element = any;

0 commit comments

Comments
 (0)