Skip to content

Commit

Permalink
Merge pull request #5831 from mermaid-js/sidv/returnConfig
Browse files Browse the repository at this point in the history
feat: Return parsed config from `mermaid.parse`
  • Loading branch information
sidharthv96 authored Sep 9, 2024
2 parents 750f58d + d053284 commit ddb51cd
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-steaks-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'mermaid': minor
---

feat: Return parsed config from mermaid.parse
2 changes: 1 addition & 1 deletion docs/config/setup/interfaces/mermaid.ParseOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ The `parseError` function will not be called.

#### Defined in

[packages/mermaid/src/types.ts:43](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L43)
[packages/mermaid/src/types.ts:45](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L45)
14 changes: 13 additions & 1 deletion docs/config/setup/interfaces/mermaid.ParseResult.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@

## Properties

### config

**config**: [`MermaidConfig`](mermaid.MermaidConfig.md)

The config passed as YAML frontmatter or directives

#### Defined in

[packages/mermaid/src/types.ts:56](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L56)

---

### diagramType

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

#### Defined in

[packages/mermaid/src/types.ts:50](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L50)
[packages/mermaid/src/types.ts:52](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L52)
6 changes: 3 additions & 3 deletions docs/config/setup/interfaces/mermaid.RenderResult.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bindFunctions?.(div); // To call bindFunctions only if it's present.
#### Defined in
[packages/mermaid/src/types.ts:73](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L73)
[packages/mermaid/src/types.ts:79](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L79)
---
Expand All @@ -51,7 +51,7 @@ The diagram type, e.g. 'flowchart', 'sequence', etc.
#### Defined in
[packages/mermaid/src/types.ts:63](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L63)
[packages/mermaid/src/types.ts:69](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L69)
---
Expand All @@ -63,4 +63,4 @@ The svg code for the rendered graph.
#### Defined in
[packages/mermaid/src/types.ts:59](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L59)
[packages/mermaid/src/types.ts:65](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L65)
73 changes: 67 additions & 6 deletions packages/mermaid/src/mermaidAPI.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vi, it, expect, describe, beforeEach } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

// -------------------------------------
// Mocks and mocking
Expand Down Expand Up @@ -66,8 +66,8 @@ vi.mock('stylis', () => {
});

import { compile, serialize } from 'stylis';
import { decodeEntities, encodeEntities } from './utils.js';
import { Diagram } from './Diagram.js';
import { decodeEntities, encodeEntities } from './utils.js';
import { toBase64 } from './utils/base64.js';

/**
Expand Down Expand Up @@ -693,18 +693,79 @@ describe('mermaidAPI', () => {
await expect(mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).resolves
.toMatchInlineSnapshot(`
{
"config": {},
"diagramType": "flowchart-v2",
}
`);
});
it('returns config when defined in frontmatter', async () => {
await expect(
mermaidAPI.parse(`---
config:
theme: base
flowchart:
htmlLabels: true
---
graph TD;A--x|text including URL space|B;`)
).resolves.toMatchInlineSnapshot(`
{
"config": {
"flowchart": {
"htmlLabels": true,
},
"theme": "base",
},
"diagramType": "flowchart-v2",
}
`);
});

it('returns config when defined in directive', async () => {
await expect(
mermaidAPI.parse(`%%{init: { 'theme': 'base' } }%%
graph TD;A--x|text including URL space|B;`)
).resolves.toMatchInlineSnapshot(`
{
"config": {
"theme": "base",
},
"diagramType": "flowchart-v2",
}
`);
});

it('returns merged config when defined in frontmatter and directive', async () => {
await expect(
mermaidAPI.parse(`---
config:
theme: forest
flowchart:
htmlLabels: true
---
%%{init: { 'theme': 'base' } }%%
graph TD;A--x|text including URL space|B;`)
).resolves.toMatchInlineSnapshot(`
{
"config": {
"flowchart": {
"htmlLabels": true,
},
"theme": "base",
},
"diagramType": "flowchart-v2",
}
`);
});

it('returns true for valid definition with silent option', async () => {
await expect(
mermaidAPI.parse('graph TD;A--x|text including URL space|B;', { suppressErrors: true })
).resolves.toMatchInlineSnapshot(`
{
"diagramType": "flowchart-v2",
}
`);
{
"config": {},
"diagramType": "flowchart-v2",
}
`);
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseRe
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult | false> {
addDiagrams();
try {
const { code } = processAndSetConfigs(text);
const { code, config } = processAndSetConfigs(text);
const diagram = await getDiagramFromText(code);
return { diagramType: diagram.type };
return { diagramType: diagram.type, config };
} catch (error) {
if (parseOptions?.suppressErrors) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const processDirectives = (code: string) => {
* @param code - The code to preprocess.
* @returns The object containing the preprocessed code, title, and configuration.
*/
export function preprocessDiagram(code: string): DiagramMetadata & { code: string } {
export function preprocessDiagram(code: string) {
const cleanedCode = cleanupText(code);
const frontMatterResult = processFrontmatter(cleanedCode);
const directiveResult = processDirectives(frontMatterResult.text);
Expand All @@ -59,5 +59,5 @@ export function preprocessDiagram(code: string): DiagramMetadata & { code: strin
code,
title: frontMatterResult.title,
config,
};
} satisfies DiagramMetadata & { code: string };
}
6 changes: 6 additions & 0 deletions packages/mermaid/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { MermaidConfig } from './config.type.js';

export interface Point {
x: number;
y: number;
Expand Down Expand Up @@ -48,6 +50,10 @@ export interface ParseResult {
* The diagram type, e.g. 'flowchart', 'sequence', etc.
*/
diagramType: string;
/**
* The config passed as YAML frontmatter or directives
*/
config: MermaidConfig;
}
// 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.
export type D3Element = any;
Expand Down

0 comments on commit ddb51cd

Please sign in to comment.