Skip to content

Commit 89385bb

Browse files
committed
feat: adds title and description to flowchart
1 parent 904826b commit 89385bb

File tree

8 files changed

+82
-22
lines changed

8 files changed

+82
-22
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ __The following are some examples of the diagrams, charts and graphs that can be
4040

4141
```
4242
flowchart LR
43+
title Example flow chart
44+
accDescripton Flow chart showing examples of node usage
4345
A[Hard] -->|Text| B(Round)
4446
B --> C{Decision}
4547
C -->|One| D[Result 1]
4648
C -->|Two| E[Result 2]
4749
```
4850
```mermaid
4951
flowchart LR
52+
title Example flow chart
53+
accDescripton Flow chart showing examples of node usage
5054
A[Hard] -->|Text| B(Round)
5155
B --> C{Decision}
5256
C -->|One| D[Result 1]

demos/flowchart.html

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6-
<title>Mermaid Quick Test Page</title>
6+
<title>Mermaid Quick Flowchart Test Page</title>
77
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
88
<style>
99
div.mermaid {
@@ -220,6 +220,8 @@ <h2>Sample 2</h2>
220220
<h3>graph</h3>
221221
<div class="mermaid">
222222
graph TD
223+
title What to buy
224+
accDescription Options of what to buy with Christmas money
223225
A[Christmas] -->|Get money| B(Go shopping)
224226
B --> C{Let me thinksssssx<br/>sssssssssssssssssssuuu<br />tttsssssssssssssssssssssss}
225227
C -->|One| D[Laptop]
@@ -230,8 +232,8 @@ <h3>graph</h3>
230232
<h3>flowchart</h3>
231233
<div class="mermaid">
232234
flowchart TD
233-
title Christmas
234-
accDescription Get money
235+
title What to buy
236+
accDescription Options of what to buy with Christmas money
235237
A[Christmas] -->|Get money| B(Go shopping)
236238
B --> C{Let me thinksssssx<br/>sssssssssssssssssssuuu<br />tttsssssssssssssssssssssss}
237239
C -->|One| D[Laptop]

demos/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,4 @@ <h1 id="link-clicked">Anchor for "link-clicked" test</h1>
872872
</script>
873873
</body>
874874

875-
</html>
875+
</html>

src/diagrams/flowchart/flowDb.js

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ let tooltips = {};
1717
let subCount = 0;
1818
let firstGraphFlag = true;
1919
let direction;
20+
let title = 'Flow chart';
21+
let description = '';
2022

2123
let version; // As in graph
2224

@@ -27,6 +29,22 @@ export const parseDirective = function (statement, context, type) {
2729
mermaidAPI.parseDirective(this, statement, context, type);
2830
};
2931

32+
const setTitle = function (txt) {
33+
title = txt;
34+
};
35+
36+
const getTitle = function () {
37+
return title;
38+
};
39+
40+
const setAccDescription = function (txt) {
41+
description = txt;
42+
};
43+
44+
const getAccDescription = function () {
45+
return description;
46+
};
47+
3048
/**
3149
* Function to lookup domId from id in the graph definition.
3250
*
@@ -736,6 +754,10 @@ const makeUniq = (sg, allSubgraphs) => {
736754
export default {
737755
parseDirective,
738756
defaultConfig: () => configApi.defaultConfig.flowchart,
757+
setTitle,
758+
getTitle,
759+
getAccDescription,
760+
setAccDescription,
739761
addVertex,
740762
lookUpDomId,
741763
addLink,

src/diagrams/flowchart/flowRenderer-v2.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import addHtmlLabel from 'dagre-d3/lib/label/add-html-label.js';
1010
import { log } from '../../logger';
1111
import common, { evaluate } from '../common/common';
1212
import { interpolateToCurve, getStylesFromArray, configureSvgSize } from '../../utils';
13+
import addSVGAccessibilityFields from '../../accessibility';
1314

1415
const conf = {};
1516
export const setConf = function (cnf) {
@@ -181,7 +182,7 @@ export const addVertices = function (vert, g, svgId, root, doc) {
181182
};
182183

183184
/**
184-
* Add edges to graph based on parsed graph defninition
185+
* Add edges to graph based on parsed graph definition
185186
*
186187
* @param {object} edges The edges to add to the graph
187188
* @param {object} g The graph object
@@ -380,7 +381,7 @@ export const draw = function (text, id) {
380381
const rankSpacing = conf.rankSpacing || 50;
381382

382383
const securityLevel = getConfig().securityLevel;
383-
// Handle root and ocument for when rendering in sanbox mode
384+
// Handle root and document for when rendering in sandbox mode
384385
let sandboxElement;
385386
if (securityLevel === 'sandbox') {
386387
sandboxElement = select('#i' + id);
@@ -416,7 +417,7 @@ export const draw = function (text, id) {
416417
flowDb.addVertex(subG.id, subG.title, 'group', undefined, subG.classes, subG.dir);
417418
}
418419

419-
// Fetch the verices/nodes and edges/links from the parsed graph definition
420+
// Fetch the vertices/nodes and edges/links from the parsed graph definition
420421
const vert = flowDb.getVertices();
421422

422423
const edges = flowDb.getEdges();
@@ -444,6 +445,9 @@ export const draw = function (text, id) {
444445
const svg = root.select(`[id="${id}"]`);
445446
svg.attr('xmlns:xlink', 'http://www.w3.org/1999/xlink');
446447

448+
// Adds title and description to the flow chart
449+
addSVGAccessibilityFields(parser.yy, svg, id);
450+
447451
// Run the renderer. This is what draws the final graph.
448452
const element = root.select('#' + id + ' g');
449453
render(element, g, ['point', 'circle', 'cross'], 'flowchart', id);

src/diagrams/flowchart/flowRenderer.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { log } from '../../logger';
1111
import common, { evaluate } from '../common/common';
1212
import { interpolateToCurve, getStylesFromArray, configureSvgSize } from '../../utils';
1313
import flowChartShapes from './flowChartShapes';
14+
import addSVGAccessibilityFields from '../../accessibility';
1415

1516
const conf = {};
1617
export const setConf = function (cnf) {
@@ -158,7 +159,7 @@ export const addVertices = function (vert, g, svgId, root, _doc) {
158159
};
159160

160161
/**
161-
* Add edges to graph based on parsed graph defninition
162+
* Add edges to graph based on parsed graph definition
162163
*
163164
* @param {object} edges The edges to add to the graph
164165
* @param {object} g The graph object
@@ -350,7 +351,7 @@ export const draw = function (text, id) {
350351
flowDb.addVertex(subG.id, subG.title, 'group', undefined, subG.classes);
351352
}
352353

353-
// Fetch the verices/nodes and edges/links from the parsed graph definition
354+
// Fetch the vertices/nodes and edges/links from the parsed graph definition
354355
const vert = flowDb.getVertices();
355356
log.warn('Get vertices', vert);
356357

@@ -426,6 +427,9 @@ export const draw = function (text, id) {
426427

427428
log.warn(g);
428429

430+
// Adds title and description to the flow chart
431+
addSVGAccessibilityFields(parser.yy, svg, id);
432+
429433
// Run the renderer. This is what draws the final graph.
430434
const element = root.select('#' + id + ' g');
431435
render(element, g);

src/diagrams/flowchart/parser/flow.jison

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
/* lexical grammar */
88
%lex
99
%x string
10+
%x title
11+
%x accDescription
1012
%x dir
1113
%x vertex
1214
%x click
@@ -26,6 +28,10 @@
2628
<arg_directive>((?:(?!\}\%\%).|\n)*) return 'arg_directive';
2729
\%\%(?!\{)[^\n]* /* skip comments */
2830
[^\}]\%\%[^\n]* /* skip comments */
31+
title { this.begin("title");return 'title'; }
32+
<title>(?!\n|;|#)*[^\n]* { this.popState(); return "title_value"; }
33+
accDescription { this.begin("accDescription");return 'accDescription'; }
34+
<accDescription>(?!\n|;|#)*[^\n]* { this.popState(); return "description_value"; }
2935
["] this.begin("string");
3036
<string>["] this.popState();
3137
<string>[^"]* return "STR";
@@ -337,6 +343,8 @@ statement
337343
| subgraph separator document end
338344
{$$=yy.addSubGraph(undefined,$3,undefined);}
339345
| direction
346+
| title title_value { $$=$2.trim();yy.setTitle($$); }
347+
| accDescription description_value { $$=$2.trim();yy.setAccDescription($$); }
340348
;
341349

342350
separator: NEWLINE | SEMI | EOF ;

src/diagrams/flowchart/parser/flow.spec.js

+29-13
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ setConfig({
66
securityLevel: 'strict',
77
});
88

9-
describe('when parsing ', function () {
9+
describe('parsing a flow chart', function () {
1010
beforeEach(function () {
1111
flow.parser.yy = flowDb;
1212
flow.parser.yy.clear();
1313
});
1414

15-
it('it should handle a trailing whitespaces after statememnts', function () {
15+
it('should handle a trailing whitespaces after statememnts', function () {
1616
const res = flow.parser.parse('graph TD;\n\n\n %% Comment\n A-->B; \n B-->C;');
1717

1818
const vert = flow.parser.yy.getVertices();
@@ -80,47 +80,47 @@ describe('when parsing ', function () {
8080
flow.parser.yy.clear();
8181
};
8282

83-
it("it should be able to parse a '.'", function () {
83+
it("should be able to parse a '.'", function () {
8484
charTest('.');
8585
charTest('Start 103a.a1');
8686
});
8787

88-
// it('it should be able to parse text containing \'_\'', function () {
88+
// it('should be able to parse text containing \'_\'', function () {
8989
// charTest('_')
9090
// })
9191

92-
it("it should be able to parse a ':'", function () {
92+
it("should be able to parse a ':'", function () {
9393
charTest(':');
9494
});
9595

96-
it("it should be able to parse a ','", function () {
96+
it("should be able to parse a ','", function () {
9797
charTest(',');
9898
});
9999

100-
it("it should be able to parse text containing '-'", function () {
100+
it("should be able to parse text containing '-'", function () {
101101
charTest('a-b');
102102
});
103103

104-
it("it should be able to parse a '+'", function () {
104+
it("should be able to parse a '+'", function () {
105105
charTest('+');
106106
});
107107

108-
it("it should be able to parse a '*'", function () {
108+
it("should be able to parse a '*'", function () {
109109
charTest('*');
110110
});
111111

112-
it("it should be able to parse a '<'", function () {
112+
it("should be able to parse a '<'", function () {
113113
charTest('<', '&lt;');
114114
});
115115

116-
// it("it should be able to parse a '>'", function() {
116+
// it("should be able to parse a '>'", function() {
117117
// charTest('>', '&gt;');
118118
// });
119119

120-
// it("it should be able to parse a '='", function() {
120+
// it("should be able to parse a '='", function() {
121121
// charTest('=', '&equals;');
122122
// });
123-
it("it should be able to parse a '&'", function () {
123+
it("should be able to parse a '&'", function () {
124124
charTest('&');
125125
});
126126
});
@@ -146,6 +146,7 @@ describe('when parsing ', function () {
146146
const classes = flow.parser.yy.getClasses();
147147
expect(vertices['A'].id).toBe('A');
148148
});
149+
149150
it('should be possible to use numbers as labels', function () {
150151
let statement = '';
151152

@@ -155,4 +156,19 @@ describe('when parsing ', function () {
155156
const classes = flow.parser.yy.getClasses();
156157
expect(vertices['1'].id).toBe('1');
157158
});
159+
160+
it('should add title and description to flow chart', function () {
161+
const flowChart = `graph LR
162+
title Big decisions
163+
accDescription Flow chart of the decision making process
164+
A[Hard] -->|Text| B(Round)
165+
B --> C{Decision}
166+
C -->|One| D[Result 1]
167+
C -->|Two| E[Result 2]
168+
`;
169+
170+
flow.parser.parse(flowChart);
171+
expect(flow.parser.yy.getTitle()).toBe('Big decisions');
172+
expect(flow.parser.yy.getAccDescription()).toBe('Flow chart of the decision making process');
173+
});
158174
});

0 commit comments

Comments
 (0)