Skip to content

Commit

Permalink
Merge pull request #6187 from mermaid-js/flowchart-new-syntax-bugs
Browse files Browse the repository at this point in the history
#6186 Fixes the flowchart node metadata syntax bugs
  • Loading branch information
knsv authored Jan 15, 2025
2 parents bc2cc61 + 7809b5a commit 04800ff
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .changeset/great-ghosts-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'mermaid': minor
---

Flowchart new syntax for node metadata bugs

- Incorrect label mapping for nodes when using `&`
- Syntax error when `}` with trailing spaces before new line
28 changes: 28 additions & 0 deletions cypress/integration/rendering/flowchart-v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1076,4 +1076,32 @@ end
);
});
});
describe('New @ sytax for node metadata edge cases', () => {
it('should be possible to use @ syntax to add labels on multi nodes', () => {
imgSnapshotTest(
`flowchart TB
n2["label for n2"] & n4@{ label: "labe for n4"} & n5@{ label: "labe for n5"}
`,
{}
);
});
it('should be possible to use @ syntax to add labels with trail spaces and &', () => {
imgSnapshotTest(
`flowchart TB
n2["label for n2"] & n4@{ label: "labe for n4"} & n5@{ label: "labe for n5"}
`,
{}
);
});
it('should be possible to use @ syntax to add labels with trail spaces', () => {
imgSnapshotTest(
`flowchart TB
n2["label for n2"]
n4@{ label: "labe for n4"}
n5@{ label: "labe for n5"}
`,
{}
);
});
});
});
3 changes: 2 additions & 1 deletion packages/mermaid/src/diagrams/flowchart/flowDiagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { setConfig } from '../../diagram-api/diagramAPI.js';
import flowDb from './flowDb.js';
import renderer from './flowRenderer-v3-unified.js';
// @ts-ignore: JISON doesn't support types
import flowParser from './parser/flow.jison';
//import flowParser from './parser/flow.jison';
import flowParser from './parser/flowParser.ts';
import flowStyles from './styles.js';

export const diagram = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';
import { cleanupComments } from '../../../diagram-api/comments.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';
import { vi } from 'vitest';
const spyOn = vi.spyOn;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down Expand Up @@ -290,4 +290,28 @@ describe('when parsing directions', function () {
expect(data4Layout.nodes[0].shape).toEqual('squareRect');
expect(data4Layout.nodes[0].label).toEqual('This is a string with}');
});

it(' should be possible to use @ syntax to add labels on multi nodes', function () {
const res = flow.parser.parse(`flowchart TB
n2["label for n2"] & n4@{ label: "labe for n4"} & n5@{ label: "labe for n5"}
`);

const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(3);
expect(data4Layout.nodes[0].label).toEqual('label for n2');
expect(data4Layout.nodes[1].label).toEqual('labe for n4');
expect(data4Layout.nodes[2].label).toEqual('labe for n5');
});
it.skip(' should be possible to use @ syntax to add labels with trail spaces', function () {
const res = flow.parser.parse(
`flowchart TB
n2["label for n2"] & n4@{ label: "labe for n4"} & n5@{ label: "labe for n5"} `
);

const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(3);
expect(data4Layout.nodes[0].label).toEqual('label for n2');
expect(data4Layout.nodes[1].label).toEqual('labe for n4');
expect(data4Layout.nodes[2].label).toEqual('labe for n5');
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/diagrams/flowchart/parser/flow.jison
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ vertexStatement: vertexStatement link node shapeData
|node spaceList { /*console.warn('vertexStatement: node spaceList', $node);*/ $$ = {stmt: $node, nodes:$node }}
|node shapeData {
/*console.warn('vertexStatement: node shapeData', $node[0], $shapeData);*/
yy.addVertex($node[0],undefined,undefined,undefined, undefined,undefined, undefined,$shapeData);
yy.addVertex($node[$node.length-1],undefined,undefined,undefined, undefined,undefined, undefined,$shapeData);
$$ = {stmt: $node, nodes:$node, shapeData: $shapeData}
}
|node { /* console.warn('vertexStatement: single node', $node); */ $$ = {stmt: $node, nodes:$node }}
Expand All @@ -416,7 +416,7 @@ vertexStatement: vertexStatement link node shapeData
node: styledVertex
{ /*console.warn('nod', $styledVertex);*/ $$ = [$styledVertex];}
| node shapeData spaceList AMP spaceList styledVertex
{ yy.addVertex($node[0],undefined,undefined,undefined, undefined,undefined, undefined,$shapeData); $$ = $node.concat($styledVertex); /*console.warn('pip2', $node[0], $styledVertex, $$);*/ }
{ yy.addVertex($node[$node.length-1],undefined,undefined,undefined, undefined,undefined, undefined,$shapeData); $$ = $node.concat($styledVertex); /*console.warn('pip2', $node[0], $styledVertex, $$);*/ }
| node spaceList AMP spaceList styledVertex
{ $$ = $node.concat($styledVertex); /*console.warn('pip', $node[0], $styledVertex, $$);*/ }
;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { cleanupComments } from '../../../diagram-api/comments.js';
import { setConfig } from '../../../config.js';

Expand Down
12 changes: 12 additions & 0 deletions packages/mermaid/src/diagrams/flowchart/parser/flowParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @ts-ignore: JISON doesn't support types
import flowJisonParser from './flow.jison';

const newParser = Object.assign({}, flowJisonParser);

newParser.parse = (src: string): unknown => {
// remove the trailing whitespace after closing curly braces when ending a line break
const newSrc = src.replace(/}\s*\n/g, '}\n');
return flowJisonParser.parse(newSrc);
};

export default newParser;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import flow from './flowParser.ts';
import { setConfig } from '../../../config.js';

setConfig({
Expand Down

0 comments on commit 04800ff

Please sign in to comment.