Merge pull request #8 from Mermaid-Chart/sidv/configReturn

fix: Change return type from parse
This commit is contained in:
Ashish Jain
2024-09-16 23:43:28 +02:00
committed by GitHub
18 changed files with 5372 additions and 3086 deletions

View File

@@ -127,7 +127,7 @@ Error.prepareStackTrace
#### Defined in #### Defined in
node_modules/@types/node/globals.d.ts:28 node_modules/@types/node/globals.d.ts:98
--- ---
@@ -141,7 +141,7 @@ Error.stackTraceLimit
#### Defined in #### Defined in
node_modules/@types/node/globals.d.ts:30 node_modules/@types/node/globals.d.ts:100
## Methods ## Methods
@@ -168,4 +168,4 @@ Error.captureStackTrace
#### Defined in #### Defined in
node_modules/@types/node/globals.d.ts:21 node_modules/@types/node/globals.d.ts:91

View File

@@ -28,7 +28,7 @@ page.
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:446](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L446) [packages/mermaid/src/mermaid.ts:451](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L451)
--- ---
@@ -59,7 +59,7 @@ A graph definition key
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:448](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L448) [packages/mermaid/src/mermaid.ts:453](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L453)
--- ---
@@ -89,7 +89,7 @@ Use [initialize](mermaid.Mermaid.md#initialize) and [run](mermaid.Mermaid.md#run
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:441](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L441) [packages/mermaid/src/mermaid.ts:446](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L446)
--- ---
@@ -116,13 +116,13 @@ This function should be called before the run function.
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:445](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L445) [packages/mermaid/src/mermaid.ts:450](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L450)
--- ---
### mermaidAPI ### mermaidAPI
**mermaidAPI**: `Readonly`<{ `defaultConfig`: [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.defaultConfig; `getConfig`: () => [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.getConfig; `getDiagramFromText`: (`text`: `string`, `metadata`: `Pick`<`DiagramMetadata`, `"title"`>) => `Promise`<`Diagram`> ; `getSiteConfig`: () => [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.getSiteConfig; `globalReset`: () => `void` ; `initialize`: (`userOptions`: [`MermaidConfig`](mermaid.MermaidConfig.md)) => `void` ; `parse`: (`text`: `string`, `parseOptions`: [`ParseOptions`](mermaid.ParseOptions.md) & { `suppressErrors`: `true` }) => `Promise`<`Diagram` | `false`>(`text`: `string`, `parseOptions?`: [`ParseOptions`](mermaid.ParseOptions.md)) => `Promise`<`Diagram`> ; `render`: (`id`: `string`, `text`: `string`, `svgContainingElement?`: `Element`, `positions?`: `Positions`) => `Promise`<[`RenderResult`](mermaid.RenderResult.md)> ; `reset`: () => `void` ; `setConfig`: (`conf`: [`MermaidConfig`](mermaid.MermaidConfig.md)) => [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.setConfig; `updateSiteConfig`: (`conf`: [`MermaidConfig`](mermaid.MermaidConfig.md)) => [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.updateSiteConfig }> **mermaidAPI**: `Readonly`<{ `defaultConfig`: [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.defaultConfig; `getConfig`: () => [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.getConfig; `getDiagramFromText`: (`text`: `string`, `metadata`: `Pick`<`DiagramMetadata`, `"title"`>) => `Promise`<`Diagram`> ; `getSiteConfig`: () => [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.getSiteConfig; `globalReset`: () => `void` ; `initialize`: (`userOptions`: [`MermaidConfig`](mermaid.MermaidConfig.md)) => `void` ; `parse`: (`text`: `string`, `parseOptions`: [`ParseOptions`](mermaid.ParseOptions.md) & { `suppressErrors`: `true` }) => `Promise`<`ParseResult` | `false`>(`text`: `string`, `parseOptions?`: [`ParseOptions`](mermaid.ParseOptions.md)) => `Promise`<`ParseResult`> ; `render`: (`id`: `string`, `text`: `string`, `svgContainingElement?`: `Element`, `positions?`: `Positions`) => `Promise`<[`RenderResult`](mermaid.RenderResult.md)> ; `reset`: () => `void` ; `setConfig`: (`conf`: [`MermaidConfig`](mermaid.MermaidConfig.md)) => [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.setConfig; `updateSiteConfig`: (`conf`: [`MermaidConfig`](mermaid.MermaidConfig.md)) => [`MermaidConfig`](mermaid.MermaidConfig.md) = configApi.updateSiteConfig }>
**`Deprecated`** **`Deprecated`**
@@ -130,17 +130,17 @@ Use [parse](mermaid.Mermaid.md#parse) and [render](mermaid.Mermaid.md#render) in
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:435](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L435) [packages/mermaid/src/mermaid.ts:440](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L440)
--- ---
### parse ### parse
**parse**: (`text`: `string`, `parseOptions?`: [`ParseOptions`](mermaid.ParseOptions.md)) => `Promise`<`boolean` | `void` | `Diagram` & `ConfigTuple`> **parse**: (`text`: `string`, `parseOptions?`: [`ParseOptions`](mermaid.ParseOptions.md)) => `Promise`<`boolean` | `void` | [`ParseResult`](mermaid.ParseResult.md)>
#### Type declaration #### Type declaration
▸ (`text`, `parseOptions?`): `Promise`<`boolean` | `void` | `Diagram` & `ConfigTuple`> ▸ (`text`, `parseOptions?`): `Promise`<`boolean` | `void` | [`ParseResult`](mermaid.ParseResult.md)>
Parse the text and validate the syntax. Parse the text and validate the syntax.
@@ -153,7 +153,7 @@ Parse the text and validate the syntax.
##### Returns ##### Returns
`Promise`<`boolean` | `void` | `Diagram` & `ConfigTuple`> `Promise`<`boolean` | `void` | [`ParseResult`](mermaid.ParseResult.md)>
If valid, Diagram otherwise `false` if parseOptions.suppressErrors is `true`. If valid, Diagram otherwise `false` if parseOptions.suppressErrors is `true`.
@@ -180,7 +180,7 @@ console.log(await mermaid.parse('wrong \n a --> b'));
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:436](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L436) [packages/mermaid/src/mermaid.ts:441](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L441)
--- ---
@@ -190,7 +190,7 @@ console.log(await mermaid.parse('wrong \n a --> b'));
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:430](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L430) [packages/mermaid/src/mermaid.ts:435](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L435)
--- ---
@@ -218,7 +218,7 @@ Used to register external diagram types.
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:444](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L444) [packages/mermaid/src/mermaid.ts:449](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L449)
--- ---
@@ -242,7 +242,7 @@ Used to register external diagram types.
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:449](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L449) [packages/mermaid/src/mermaid.ts:454](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L454)
--- ---
@@ -266,7 +266,7 @@ Used to register external diagram types.
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:443](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L443) [packages/mermaid/src/mermaid.ts:448](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L448)
--- ---
@@ -293,7 +293,7 @@ Used to register external diagram types.
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:437](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L437) [packages/mermaid/src/mermaid.ts:442](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L442)
--- ---
@@ -341,7 +341,7 @@ Renders the mermaid diagrams
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:442](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L442) [packages/mermaid/src/mermaid.ts:447](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L447)
--- ---
@@ -376,7 +376,7 @@ to it (eg. dart interop wrapper). (Initially there is no parseError member of me
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:447](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L447) [packages/mermaid/src/mermaid.ts:452](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L452)
--- ---
@@ -386,4 +386,4 @@ to it (eg. dart interop wrapper). (Initially there is no parseError member of me
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:429](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L429) [packages/mermaid/src/mermaid.ts:434](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L434)

View File

@@ -19,4 +19,4 @@ The `parseError` function will not be called.
#### Defined in #### Defined in
[packages/mermaid/src/types.ts:55](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L55) [packages/mermaid/src/types.ts:57](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L57)

View File

@@ -8,26 +8,60 @@
[mermaid](../modules/mermaid.md).ParseResult [mermaid](../modules/mermaid.md).ParseResult
## Hierarchy
- `Omit`<`ParseResult`, `"config"`>
**`ParseResult`**
## Properties ## Properties
### config ### config
**config**: [`MermaidConfig`](mermaid.MermaidConfig.md) **config**: [`MermaidConfig`](mermaid.MermaidConfig.md)
The config passed as YAML frontmatter or directives
#### Defined in #### Defined in
[packages/mermaid/src/types.ts:66](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L66) [packages/mermaid/src/mermaid.ts:328](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L328)
--- ---
### diagramType ### defaultConfig
**diagramType**: `string` **defaultConfig**: [`MermaidConfig`](mermaid.MermaidConfig.md)
The diagram type, e.g. 'flowchart', 'sequence', etc. #### Overrides
Omit.defaultConfig
#### Defined in #### Defined in
[packages/mermaid/src/types.ts:62](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L62) [packages/mermaid/src/mermaid.ts:327](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L327)
---
### diagram
**diagram**: `Diagram`
The diagram AST
#### Inherited from
Omit.diagram
#### Defined in
[packages/mermaid/src/types.ts:72](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L72)
---
### userConfig
**userConfig**: [`MermaidConfig`](mermaid.MermaidConfig.md)
Config the user has defined in the text as frontmatter or directives
#### Defined in
[packages/mermaid/src/mermaid.ts:326](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L326)

View File

@@ -39,7 +39,7 @@ bindFunctions?.(div); // To call bindFunctions only if it's present.
#### Defined in #### Defined in
[packages/mermaid/src/types.ts:89](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L89) [packages/mermaid/src/types.ts:95](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L95)
--- ---
@@ -51,7 +51,7 @@ The diagram type, e.g. 'flowchart', 'sequence', etc.
#### Defined in #### Defined in
[packages/mermaid/src/types.ts:79](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L79) [packages/mermaid/src/types.ts:85](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L85)
--- ---
@@ -63,4 +63,4 @@ The svg code for the rendered graph.
#### Defined in #### Defined in
[packages/mermaid/src/types.ts:75](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L75) [packages/mermaid/src/types.ts:81](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L81)

View File

@@ -87,4 +87,4 @@
#### Defined in #### Defined in
[packages/mermaid/src/mermaid.ts:452](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L452) [packages/mermaid/src/mermaid.ts:457](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaid.ts#L457)

View File

@@ -1,4 +1,4 @@
import type { ExternalDiagramDefinition } from 'mermaid'; import type { ExternalDiagramDefinition } from '@mermaid-chart/mermaid';
const id = 'example-diagram'; const id = 'example-diagram';

View File

@@ -1,5 +1,3 @@
import { curveLinear } from 'd3';
import ELK from 'elkjs/lib/elk.bundled.js';
import type { import type {
InternalHelpers, InternalHelpers,
LayoutData, LayoutData,
@@ -7,6 +5,9 @@ import type {
SVG, SVG,
SVGGroup, SVGGroup,
} from '@mermaid-chart/mermaid'; } from '@mermaid-chart/mermaid';
// @ts-ignore TODO: Investigate D3 issue
import { curveLinear } from 'd3';
import ELK from 'elkjs/lib/elk.bundled.js';
import { type TreeData, findCommonAncestor } from './find-common-ancestor.js'; import { type TreeData, findCommonAncestor } from './find-common-ancestor.js';
export const render = async ( export const render = async (

View File

@@ -1,6 +1,6 @@
{ {
"name": "@mermaid-chart/mermaid", "name": "@mermaid-chart/mermaid",
"version": "11.1.0-b.12", "version": "11.2.0-a.4",
"description": "Markdown-ish syntax for generating flowcharts, mindmaps, sequence diagrams, class diagrams, gantt charts, git graphs and more.", "description": "Markdown-ish syntax for generating flowcharts, mindmaps, sequence diagrams, class diagrams, gantt charts, git graphs and more.",
"type": "module", "type": "module",
"module": "./dist/mermaid.core.mjs", "module": "./dist/mermaid.core.mjs",

View File

@@ -1,16 +1,16 @@
import { addDetector } from './detectType.js';
import { log as _log, setLogLevel as _setLogLevel } from '../logger.js';
import { import {
defaultConfig as _defaultConfig,
getConfig as _getConfig, getConfig as _getConfig,
setConfig as _setConfig, setConfig as _setConfig,
defaultConfig as _defaultConfig,
setSiteConfig as _setSiteConfig, setSiteConfig as _setSiteConfig,
} from '../config.js'; } from '../config.js';
import { sanitizeText as _sanitizeText } from '../diagrams/common/common.js'; import { sanitizeText as _sanitizeText } from '../diagrams/common/common.js';
import * as _commonDb from '../diagrams/common/commonDb.js';
import { log as _log, setLogLevel as _setLogLevel } from '../logger.js';
import { setupGraphViewbox as _setupGraphViewbox } from '../setupGraphViewbox.js'; import { setupGraphViewbox as _setupGraphViewbox } from '../setupGraphViewbox.js';
import { addStylesForDiagram } from '../styles.js'; import { addStylesForDiagram } from '../styles.js';
import { addDetector } from './detectType.js';
import type { DiagramDefinition, DiagramDetector } from './types.js'; import type { DiagramDefinition, DiagramDetector } from './types.js';
import * as _commonDb from '../diagrams/common/commonDb.js';
/* /*
Packaging and exposing resources for external diagrams so that they can import Packaging and exposing resources for external diagrams so that they can import

View File

@@ -2,18 +2,18 @@
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid * Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid
* functionality and to render the diagrams to svg code! * functionality and to render the diagrams to svg code!
*/ */
import { registerIconPacks } from './rendering-util/icons.js';
import { dedent } from 'ts-dedent'; import { dedent } from 'ts-dedent';
import type { MermaidConfig } from './config.type.js'; import type { MermaidConfig } from './config.type.js';
import { detectType, registerLazyLoadedDiagrams } from './diagram-api/detectType.js'; import { detectType, registerLazyLoadedDiagrams } from './diagram-api/detectType.js';
import { addDiagrams } from './diagram-api/diagram-orchestration.js'; import { addDiagrams } from './diagram-api/diagram-orchestration.js';
import { loadRegisteredDiagrams } from './diagram-api/loadDiagram.js'; import { loadRegisteredDiagrams } from './diagram-api/loadDiagram.js';
import type { ExternalDiagramDefinition, SVG, SVGGroup } from './diagram-api/types.js'; import type { ExternalDiagramDefinition, SVG, SVGGroup } from './diagram-api/types.js';
import type { Diagram, ParseErrorFunction } from './Diagram.js'; import type { ParseErrorFunction } from './Diagram.js';
import type { UnknownDiagramError } from './errors.js'; import type { UnknownDiagramError } from './errors.js';
import type { InternalHelpers } from './internals.js'; import type { InternalHelpers } from './internals.js';
import { log } from './logger.js'; import { log } from './logger.js';
import { mermaidAPI } from './mermaidAPI.js'; import { mermaidAPI } from './mermaidAPI.js';
import { registerIconPacks } from './rendering-util/icons.js';
import type { LayoutLoaderDefinition, RenderOptions } from './rendering-util/render.js'; import type { LayoutLoaderDefinition, RenderOptions } from './rendering-util/render.js';
import { registerLayoutLoaders } from './rendering-util/render.js'; import { registerLayoutLoaders } from './rendering-util/render.js';
import type { LayoutData } from './rendering-util/types.js'; import type { LayoutData } from './rendering-util/types.js';
@@ -321,10 +321,6 @@ const executeQueue = async () => {
executionQueueRunning = false; executionQueueRunning = false;
}; };
// interface ConfigTuple {
// defaultConfig: MermaidConfig;
// config: MermaidConfig;
// }
/** /**
* Parse the text and validate the syntax. * Parse the text and validate the syntax.
* @param text - The mermaid diagram definition. * @param text - The mermaid diagram definition.
@@ -354,10 +350,7 @@ const parse = async (
const performCall = () => const performCall = () =>
new Promise((res, rej) => { new Promise((res, rej) => {
mermaidAPI.parse(text, parseOptions).then( mermaidAPI.parse(text, parseOptions).then(
(r) => { (result) => {
const result = r;
result.defaultConfig = mermaidAPI.defaultConfig;
result.config = mermaidAPI.getConfig();
// This resolves for the promise for the queue handling // This resolves for the promise for the queue handling
res(result); res(result);
// This fulfills the promise sent to the value back to the original caller // This fulfills the promise sent to the value back to the original caller

View File

@@ -35,7 +35,7 @@ vi.mock('./diagrams/sequence/sequenceRenderer.js');
import assignWithDepth from './assignWithDepth.js'; import assignWithDepth from './assignWithDepth.js';
import type { MermaidConfig } from './config.type.js'; import type { MermaidConfig } from './config.type.js';
import mermaid from './mermaid.js'; import mermaid, { type ParseResult } from './mermaid.js';
import mermaidAPI, { import mermaidAPI, {
appendDivSvgG, appendDivSvgG,
cleanUpSvgCode, cleanUpSvgCode,
@@ -694,24 +694,95 @@ describe('mermaidAPI', () => {
mermaidAPI.parse('this is not a mermaid diagram definition', { suppressErrors: true }) mermaidAPI.parse('this is not a mermaid diagram definition', { suppressErrors: true })
).resolves.toBe(false); ).resolves.toBe(false);
}); });
// it('resolves for valid definition', async () => { it('resolves for valid definition', async () => {
// await expect(mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).resolves await expect(
// .toMatchInlineSnapshot(` mermaidAPI
// { .parse('graph TD;A--x|text including URL space|B;')
// "diagramType": "flowchart-v2", .then((p) => ({ config: p.config }))
// } ).resolves.toMatchInlineSnapshot(`
// `); {
// }); "config": {},
}
`);
});
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;`
)
.then((p) => ({ config: p.config }))
).resolves.toMatchInlineSnapshot(`
{
"config": {
"flowchart": {
"htmlLabels": true,
},
"theme": "base",
},
}
`);
});
// it('returns true for valid definition with silent option', async () => { it('returns config when defined in directive', async () => {
// await expect( await expect(
// mermaidAPI.parse('graph TD;A--x|text including URL space|B;', { suppressErrors: true }) mermaidAPI
// ).resolves.toMatchInlineSnapshot(` .parse(
// { `%%{init: { 'theme': 'base' } }%%
// "diagramType": "flowchart-v2", graph TD;A--x|text including URL space|B;`
// } )
// `); .then((p) => ({ config: p.config }))
// }); ).resolves.toMatchInlineSnapshot(`
{
"config": {
"theme": "base",
},
}
`);
});
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;`
)
.then((p) => ({ config: p.config }))
).resolves.toMatchInlineSnapshot(`
{
"config": {
"flowchart": {
"htmlLabels": true,
},
"theme": "base",
},
}
`);
});
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 })
.then((p) => ({ config: (p as ParseResult).config }))
).resolves.toMatchInlineSnapshot(`
{
"config": {},
}
`);
});
}); });
describe('render', () => { describe('render', () => {

View File

@@ -68,17 +68,23 @@ function processAndSetConfigs(text: string) {
async function parse( async function parse(
text: string, text: string,
parseOptions: ParseOptions & { suppressErrors: true } parseOptions: ParseOptions & { suppressErrors: true }
): Promise<ParseResult | false>; ): Promise<ParseResult & { error?: unknown }>;
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult>; async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult>;
async function parse(text: string, parseOptions?: ParseOptions): Promise<ParseResult | false> { async function parse(
text: string,
parseOptions?: ParseOptions
): Promise<ParseResult & { error?: unknown }> {
addDiagrams(); addDiagrams();
let code = '';
let title = undefined;
let config: MermaidConfig = {};
try { try {
const { code, config } = processAndSetConfigs(text); ({ code, config, title } = processAndSetConfigs(text));
const diagram = await getDiagramFromText(code); const diagram = await getDiagramFromText(code);
return { diagram, config, diagramType: diagram.type }; return { diagram, code, config, title, success: true };
} catch (error) { } catch (error) {
if (parseOptions?.suppressErrors) { if (parseOptions?.suppressErrors) {
return false; return { code, config, title, success: false, error };
} }
throw error; throw error;
} }

View File

@@ -1,10 +1,10 @@
import { log } from '../../../logger.js';
import { getNodeClasses, updateNodeBounds } from './util.js';
import type { SVG } from '../../../diagram-api/types.js';
import type { Node } from '../../types.d.ts';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs'; import rough from 'roughjs';
import type { SVG } from '../../../diagram-api/types.js';
import { log } from '../../../logger.js';
import type { Node } from '../../types.d.ts';
import intersect from '../intersect/index.js'; import intersect from '../intersect/index.js';
import { userNodeOverrides } from './handDrawnShapeStyles.js';
import { getNodeClasses, updateNodeBounds } from './util.js';
function createLine(r: number) { function createLine(r: number) {
const xAxis45 = Math.cos(Math.PI / 4); // cosine of 45 degrees const xAxis45 = Math.cos(Math.PI / 4); // cosine of 45 degrees

View File

@@ -1,10 +1,10 @@
import { log } from '../../../logger.js';
import { getNodeClasses, updateNodeBounds } from './util.js';
import type { Node, RenderOptions } from '../../types.d.ts';
import type { SVG } from '../../../diagram-api/types.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs'; import rough from 'roughjs';
import type { SVG } from '../../../diagram-api/types.js';
import { log } from '../../../logger.js';
import type { Node, RenderOptions } from '../../types.d.ts';
import intersect from '../intersect/index.js'; import intersect from '../intersect/index.js';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import { getNodeClasses, updateNodeBounds } from './util.js';
export const filledCircle = ( export const filledCircle = (
parent: SVG, parent: SVG,

View File

@@ -1,9 +1,9 @@
import { log } from '../../../logger.js';
import { labelHelper, updateNodeBounds, getNodeClasses, createPathFromPoints } from './util.js';
import intersect from '../intersect/index.js';
import type { Node } from '../../types.d.ts';
import { styles2String, userNodeOverrides } from './handDrawnShapeStyles.js';
import rough from 'roughjs'; import rough from 'roughjs';
import { log } from '../../../logger.js';
import type { Node } from '../../types.d.ts';
import intersect from '../intersect/index.js';
import { userNodeOverrides } from './handDrawnShapeStyles.js';
import { createPathFromPoints, getNodeClasses, labelHelper, updateNodeBounds } from './util.js';
export const hourglass = async (parent: SVGAElement, node: Node) => { export const hourglass = async (parent: SVGAElement, node: Node) => {
node.label = ''; node.label = '';

View File

@@ -1,3 +1,6 @@
import type { MermaidConfig } from './config.type.js';
import type { Diagram } from './Diagram.js';
export interface NodeMetaData { export interface NodeMetaData {
shape?: string; shape?: string;
label?: string; label?: string;
@@ -8,8 +11,6 @@ export interface NodeMetaData {
w?: string; w?: string;
h?: string; h?: string;
} }
import type { MermaidConfig } from './config.type.js';
import type { Diagram } from './Diagram.ts';
export interface Point { export interface Point {
x: number; x: number;
@@ -57,23 +58,23 @@ export interface ParseOptions {
} }
export interface ParseResult { export interface ParseResult {
success: boolean;
/** /**
* The diagram type, e.g. 'flowchart', 'sequence', etc. * The mermaid code after extracting the config.
*/ */
diagramType: string; code: string;
/** /**
* The config passed as YAML frontmatter or directives * The config passed as YAML frontmatter or directives
*/ */
config: MermaidConfig; config: MermaidConfig;
title?: string;
defaultConfig?: MermaidConfig; diagram?: Diagram;
/** /**
* The diagram AST * The error that occurred during parsing, if any.
*
*/ */
diagram: Diagram; error?: unknown;
} }
// 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. // 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; export type D3Element = any;

8154
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff