diff --git a/.changeset/great-ghosts-rule.md b/.changeset/great-ghosts-rule.md new file mode 100644 index 0000000000..f11c6e2a93 --- /dev/null +++ b/.changeset/great-ghosts-rule.md @@ -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 diff --git a/cypress/integration/rendering/flowchart-v2.spec.js b/cypress/integration/rendering/flowchart-v2.spec.js index 66452f4b2f..bb37914cbe 100644 --- a/cypress/integration/rendering/flowchart-v2.spec.js +++ b/cypress/integration/rendering/flowchart-v2.spec.js @@ -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"} + `, + {} + ); + }); + }); }); diff --git a/packages/mermaid/src/diagrams/flowchart/flowDiagram.ts b/packages/mermaid/src/diagrams/flowchart/flowDiagram.ts index 67cdf918fe..ddb2eb3ed2 100644 --- a/packages/mermaid/src/diagrams/flowchart/flowDiagram.ts +++ b/packages/mermaid/src/diagrams/flowchart/flowDiagram.ts @@ -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 = { diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-arrows.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-arrows.spec.js index e89398ab40..50236b60c8 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-arrows.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-arrows.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-comments.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-comments.spec.js index 9c2a740af5..487705cd75 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-comments.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-comments.spec.js @@ -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'; diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-direction.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-direction.spec.js index ce6b0b0c47..fcf3aa21f8 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-direction.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-direction.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-edges.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-edges.spec.js index 5682c9bedf..a23528fd6d 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-edges.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-edges.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-huge.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-huge.spec.js index 8931c6ee1f..7fef14f5b5 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-huge.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-huge.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-interactions.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-interactions.spec.js index cb3f48ccae..893a5e7577 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-interactions.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-interactions.spec.js @@ -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; diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-lines.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-lines.spec.js index ec157e646f..116878f6f7 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-lines.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-lines.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-md-string.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-md-string.spec.js index 55e749a22d..3c39dbc4fa 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-md-string.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-md-string.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-node-data.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-node-data.spec.js index 1669cfada0..df3782a6bc 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-node-data.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-node-data.spec.js @@ -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({ @@ -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'); + }); }); diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-singlenode.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-singlenode.spec.js index f6ed123d7e..bf152c9bfe 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-singlenode.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-singlenode.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-style.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-style.spec.js index 22fd48a334..c24873e7c9 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-style.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-style.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-text.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-text.spec.js index 3754766f44..61774d7c07 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-text.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-text.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow-vertice-chaining.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow-vertice-chaining.spec.js index a5b6a2b6d9..95494ab44a 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow-vertice-chaining.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow-vertice-chaining.spec.js @@ -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({ diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow.jison b/packages/mermaid/src/diagrams/flowchart/parser/flow.jison index fbd30fa9e5..ade90382bd 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow.jison +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow.jison @@ -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 }} @@ -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, $$);*/ } ; diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flow.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/flow.spec.js index 8081c8fe47..e9d646a5aa 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/flow.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/flow.spec.js @@ -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'; diff --git a/packages/mermaid/src/diagrams/flowchart/parser/flowParser.ts b/packages/mermaid/src/diagrams/flowchart/parser/flowParser.ts new file mode 100644 index 0000000000..acf4fa5250 --- /dev/null +++ b/packages/mermaid/src/diagrams/flowchart/parser/flowParser.ts @@ -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; diff --git a/packages/mermaid/src/diagrams/flowchart/parser/subgraph.spec.js b/packages/mermaid/src/diagrams/flowchart/parser/subgraph.spec.js index 12b2e4a39a..e2dd59284d 100644 --- a/packages/mermaid/src/diagrams/flowchart/parser/subgraph.spec.js +++ b/packages/mermaid/src/diagrams/flowchart/parser/subgraph.spec.js @@ -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({