Skip to content

Commit

Permalink
Merge pull request #5076 from ad1992/aakansha/bug/5075-fix-getDiagram…
Browse files Browse the repository at this point in the history
…FromText-api

fix: clean comments in text in getDiagramFromText API so flowchart works well
  • Loading branch information
sidharthv96 authored Nov 28, 2023
2 parents 252a8a7 + 4ed7b2b commit 4499926
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/config/setup/modules/mermaidAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ mermaid.initialize(config);

#### Defined in

[mermaidAPI.ts:603](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L603)
[mermaidAPI.ts:608](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L608)

## Functions

Expand Down
13 changes: 13 additions & 0 deletions packages/mermaid/src/mermaidAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ vi.mock('stylis', () => {
});
import { compile, serialize } from 'stylis';
import { decodeEntities, encodeEntities } from './utils.js';
import { Diagram } from './Diagram.js';

/**
* @see https://vitest.dev/guide/mocking.html Mock part of a module
Expand Down Expand Up @@ -744,4 +745,16 @@ describe('mermaidAPI', () => {
});
});
});

describe('getDiagramFromText', () => {
it('should clean up comments when present in diagram definition', async () => {
const diagram = await mermaidAPI.getDiagramFromText(
`flowchart LR
%% This is a comment A -- text --> B{node}
A -- text --> B -- text2 --> C`
);
expect(diagram).toBeInstanceOf(Diagram);
expect(diagram.type).toBe('flowchart-v2');
});
});
});
9 changes: 7 additions & 2 deletions packages/mermaid/src/mermaidAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { compile, serialize, stringify } from 'stylis';
import { version } from '../package.json';
import * as configApi from './config.js';
import { addDiagrams } from './diagram-api/diagram-orchestration.js';
import { Diagram, getDiagramFromText } from './Diagram.js';
import { Diagram, getDiagramFromText as getDiagramFromTextInternal } from './Diagram.js';
import errorRenderer from './diagrams/error/errorRenderer.js';
import { attachFunctions } from './interactionDb.js';
import { log, setLogLevel } from './logger.js';
Expand All @@ -28,7 +28,7 @@ import type { MermaidConfig } from './config.type.js';
import { evaluate } from './diagrams/common/common.js';
import isEmpty from 'lodash-es/isEmpty.js';
import { setA11yDiagramInfo, addSVGa11yTitleDescription } from './accessibility.js';
import type { DiagramStyleClassDef } from './diagram-api/types.js';
import type { DiagramMetadata, DiagramStyleClassDef } from './diagram-api/types.js';
import { preprocessDiagram } from './preprocess.js';
import { decodeEntities } from './utils.js';

Expand Down Expand Up @@ -519,6 +519,11 @@ function initialize(options: MermaidConfig = {}) {
addDiagrams();
}

const getDiagramFromText = (text: string, metadata: Pick<DiagramMetadata, 'title'> = {}) => {
const { code } = preprocessDiagram(text);
return getDiagramFromTextInternal(code, metadata);
};

/**
* Add accessibility (a11y) information to the diagram.
*
Expand Down

0 comments on commit 4499926

Please sign in to comment.