Skip to content

Commit 37f2e36

Browse files
committed
feat(gantt): support multiple excludes and includes lines (#6270)
1 parent 91b9f01 commit 37f2e36

6 files changed

Lines changed: 83 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'mermaid': minor
3+
---
4+
5+
feat(gantt): support multiple `excludes` / `includes` lines so long exclusion lists can be split into commented groups (#6270)

‎docs/syntax/gantt.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ The `title` is an _optional_ string to be displayed at the top of the Gantt char
189189
The `excludes` is an _optional_ attribute that accepts specific dates in YYYY-MM-DD format, days of the week ("sunday") or "weekends", but not the word "weekdays".
190190
These date will be marked on the graph, and be excluded from the duration calculation of tasks. Meaning that if there are excluded dates during a task interval, the number of 'skipped' days will be added to the end of the task to ensure the duration is as specified in the code.
191191

192+
Multiple `excludes` lines are supported and their tokens are concatenated, so long exclusion lists can be split across grouped lines with comments:
193+
194+
```
195+
gantt
196+
dateFormat DD-MM-YYYY
197+
excludes weekends
198+
%% week 7 is winter break
199+
excludes 10-02-2025 11-02-2025 12-02-2025 13-02-2025 14-02-2025
200+
%% workers holiday 1 maj
201+
excludes 01-05-2025
202+
```
203+
192204
#### Weekend (v\11.0.0+)
193205

194206
When excluding weekends, it is possible to configure the weekends to be either Friday and Saturday or Saturday and Sunday. By default weekends are Saturday and Sunday.

‎packages/mermaid/src/diagrams/gantt/ganttDb.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,23 @@ export const getDateFormat = function () {
130130
return dateFormat;
131131
};
132132

133+
const mergeTokens = (existing, txt) => {
134+
const tokens = txt
135+
.toLowerCase()
136+
.split(/[\s,]+/)
137+
.filter((t) => t !== '');
138+
return [...new Set([...existing, ...tokens])];
139+
};
140+
133141
export const setIncludes = function (txt) {
134-
includes = txt.toLowerCase().split(/[\s,]+/);
142+
includes = mergeTokens(includes, txt);
135143
};
136144

137145
export const getIncludes = function () {
138146
return includes;
139147
};
140148
export const setExcludes = function (txt) {
141-
excludes = txt.toLowerCase().split(/[\s,]+/);
149+
excludes = mergeTokens(excludes, txt);
142150
};
143151

144152
export const getExcludes = function () {

‎packages/mermaid/src/diagrams/gantt/ganttDb.spec.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,34 @@ describe('when using the ganttDb', function () {
292292
expect(() => ganttDb.getTasks()).toThrowError('Failed to find a valid date');
293293

294294
// Fridays are now allowed, so it should not throw an error
295+
ganttDb.clear();
296+
ganttDb.setDateFormat('YYYY-MM-DD');
295297
ganttDb.setExcludes('weekends,monday,tuesday,wednesday,thursday');
298+
ganttDb.setWeekend('saturday');
299+
ganttDb.addSection('weekends skip test');
300+
ganttDb.addTask('test1', 'id1,2019-02-01,7d');
296301
expect(() => ganttDb.getTasks()).not.toThrow();
297302
});
298303

304+
it('should merge tokens across multiple setExcludes calls (issue #6270)', function () {
305+
ganttDb.setExcludes('weekends');
306+
ganttDb.setExcludes('2019-02-06');
307+
ganttDb.setExcludes('friday, monday');
308+
expect(ganttDb.getExcludes()).toEqual(['weekends', '2019-02-06', 'friday', 'monday']);
309+
});
310+
311+
it('should dedupe tokens across multiple setExcludes calls (issue #6270)', function () {
312+
ganttDb.setExcludes('weekends,2019-02-06');
313+
ganttDb.setExcludes('weekends 2019-02-07');
314+
expect(ganttDb.getExcludes()).toEqual(['weekends', '2019-02-06', '2019-02-07']);
315+
});
316+
317+
it('should merge tokens across multiple setIncludes calls (issue #6270)', function () {
318+
ganttDb.setIncludes('2019-02-06');
319+
ganttDb.setIncludes('2019-02-07,2019-02-08');
320+
expect(ganttDb.getIncludes()).toEqual(['2019-02-06', '2019-02-07', '2019-02-08']);
321+
});
322+
299323
it('should maintain the order in which tasks are created', function () {
300324
ganttDb.setAccTitle('Project Execution');
301325
ganttDb.setDateFormat('YYYY-MM-DD');

‎packages/mermaid/src/diagrams/gantt/parser/gantt.spec.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ describe('when parsing a gantt diagram it', function () {
4040

4141
expect(parserFnConstructor(str)).not.toThrow();
4242
});
43+
it('should concatenate tokens across multiple excludes lines (issue #6270)', function () {
44+
const str = [
45+
'gantt',
46+
'dateFormat DD-MM-YYYY',
47+
'excludes weekends',
48+
'%% week 7 is winter break',
49+
'excludes 10-02-2025 11-02-2025 12-02-2025',
50+
'%% workers holiday 1 maj',
51+
'excludes 01-05-2025',
52+
].join('\n');
53+
54+
expect(parserFnConstructor(str)).not.toThrow();
55+
expect(ganttDb.getExcludes()).toEqual([
56+
'weekends',
57+
'10-02-2025',
58+
'11-02-2025',
59+
'12-02-2025',
60+
'01-05-2025',
61+
]);
62+
});
4363
it('should handle a todayMarker definition', function () {
4464
spyOn(ganttDb, 'setTodayMarker');
4565
const str =

‎packages/mermaid/src/docs/syntax/gantt.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ The `title` is an _optional_ string to be displayed at the top of the Gantt char
131131
The `excludes` is an _optional_ attribute that accepts specific dates in YYYY-MM-DD format, days of the week ("sunday") or "weekends", but not the word "weekdays".
132132
These date will be marked on the graph, and be excluded from the duration calculation of tasks. Meaning that if there are excluded dates during a task interval, the number of 'skipped' days will be added to the end of the task to ensure the duration is as specified in the code.
133133

134+
Multiple `excludes` lines are supported and their tokens are concatenated, so long exclusion lists can be split across grouped lines with comments:
135+
136+
```
137+
gantt
138+
dateFormat DD-MM-YYYY
139+
excludes weekends
140+
%% week 7 is winter break
141+
excludes 10-02-2025 11-02-2025 12-02-2025 13-02-2025 14-02-2025
142+
%% workers holiday 1 maj
143+
excludes 01-05-2025
144+
```
145+
134146
#### Weekend (v\11.0.0+)
135147

136148
When excluding weekends, it is possible to configure the weekends to be either Friday and Saturday or Saturday and Sunday. By default weekends are Saturday and Sunday.

0 commit comments

Comments
 (0)