diff --git a/.changeset/fuzzy-pears-cough.md b/.changeset/fuzzy-pears-cough.md new file mode 100644 index 000000000..6ed777506 --- /dev/null +++ b/.changeset/fuzzy-pears-cough.md @@ -0,0 +1,5 @@ +--- +'mermaid': patch +--- + +fix: handle exclude dates properly in Gantt charts when using dateFormat: 'YYYY-MM-DD HH:mm:ss' diff --git a/cypress/integration/rendering/gantt.spec.js b/cypress/integration/rendering/gantt.spec.js index 2cc67918c..979c27540 100644 --- a/cypress/integration/rendering/gantt.spec.js +++ b/cypress/integration/rendering/gantt.spec.js @@ -647,6 +647,49 @@ describe('Gantt diagram', () => { ); }); + it('should render a gantt diagram excluding a specific date in YYYY-MM-DD HH:mm:ss format', () => { + imgSnapshotTest( + ` + gantt + dateFormat YYYY-MM-DD HH:mm:ss + excludes 2025-07-07 + section Section + A task :a1, 2025-07-04 20:30:30, 2025-07-08 10:30:30 + Another task:after a1, 20h + `, + {} + ); + }); + + it('should render a gantt diagram excluding saturday and sunday in YYYY-MM-DD HH:mm:ss format', () => { + imgSnapshotTest( + ` + gantt + dateFormat YYYY-MM-DD HH:mm:ss + excludes weekends + weekend saturday + section Section + A task :a1, 2025-07-04 20:30:30, 2025-07-08 10:30:30 + Another task:after a1, 20h + `, + {} + ); + }); + it('should render a gantt diagram excluding friday and saturday in YYYY-MM-DD HH:mm:ss format', () => { + imgSnapshotTest( + ` + gantt + dateFormat YYYY-MM-DD HH:mm:ss + excludes weekends + weekend friday + section Section + A task :a1, 2025-07-04 20:30:30, 2025-07-08 10:30:30 + Another task:after a1, 20h + `, + {} + ); + }); + it("should render when there's a semicolon in the title", () => { imgSnapshotTest( ` diff --git a/packages/mermaid/src/diagrams/gantt/ganttDb.js b/packages/mermaid/src/diagrams/gantt/ganttDb.js index 3ae55fb25..b2b5b0566 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttDb.js +++ b/packages/mermaid/src/diagrams/gantt/ganttDb.js @@ -167,7 +167,10 @@ export const getTasks = function () { }; export const isInvalidDate = function (date, dateFormat, excludes, includes) { - if (includes.includes(date.format(dateFormat.trim()))) { + const formattedDate = date.format(dateFormat.trim()); + const dateOnly = date.format('YYYY-MM-DD'); + + if (includes.includes(formattedDate) || includes.includes(dateOnly)) { return false; } if ( @@ -180,7 +183,7 @@ export const isInvalidDate = function (date, dateFormat, excludes, includes) { if (excludes.includes(date.format('dddd').toLowerCase())) { return true; } - return excludes.includes(date.format(dateFormat.trim())); + return excludes.includes(formattedDate) || excludes.includes(dateOnly); }; export const setWeekday = function (txt) { diff --git a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js index f5c8c2e38..dd4824d6b 100644 --- a/packages/mermaid/src/diagrams/gantt/ganttRenderer.js +++ b/packages/mermaid/src/diagrams/gantt/ganttRenderer.js @@ -581,17 +581,11 @@ export const draw = function (text, id, version, diagObj) { rectangles .append('rect') - .attr('id', function (d) { - return 'exclude-' + d.start.format('YYYY-MM-DD'); - }) - .attr('x', function (d) { - return timeScale(d.start) + theSidePad; - }) + .attr('id', (d) => 'exclude-' + d.start.format('YYYY-MM-DD')) + .attr('x', (d) => timeScale(d.start.startOf('day')) + theSidePad) .attr('y', conf.gridLineStartPadding) - .attr('width', function (d) { - const renderEnd = d.end.add(1, 'day'); - return timeScale(renderEnd) - timeScale(d.start); - }) + .attr('width', (d) => timeScale(d.end.endOf('day')) - timeScale(d.start.startOf('day'))) + .attr('height', h - theTopPad - conf.gridLineStartPadding) .attr('transform-origin', function (d, i) { return (