From 4f24489d8148dfd78d5586274c51280025976379 Mon Sep 17 00:00:00 2001 From: anderium <33520919+anderium@users.noreply.github.com> Date: Wed, 13 Aug 2025 01:47:25 +0200 Subject: [PATCH 1/2] Make elk not force model order, but strongly consider it instead This improves the ordering in models with crossings that can be resolved by reordering nodes. It keeps the node order to still avoid #6647 which cause this regression to be introduced. [See this example on the ELK Editor.](https://rtsys.informatik.uni-kiel.de/elklive/elkgraph.html?compressedContent=IYGw5g9gTglgLgCwLYC4AEJgE8CmUcAmAUAPQloDGUEAzjTAHZgCyjMSMAXsHDBAwDoAZtAo4AchAI5mUnCADyUaVHRwoAVxxEK-eitnTFyvAJrqeOMFnTiFAEQCiAZQD6AQXH3Xj+wHEXIlJyABUEGBo0CLQAI2ACNAB3BBwGNA16JjQRKDFJaUN5JRU1TRwBIIY5NAYABjQAbwxgGPk0ACJa9rQAXyIq6Rr3RubWkA7gbr6BnBqARhHMMY65qf7qhgAmRZa29s21mZqADR3l9uPDjYAhM72Yq8GGa+GmpfuBVd71p+vbt9243aMQEB2+R2epwB5wAWmsiIQwLM6mgALQAPiGaARBCRNXqGPmQURyIWhK2xNxyOG5OulLxzzRmOe7hxDNutLpbORLyZNWux25-I5zMFQq2fIYYuCaGuGjgaEQ0RoCAgGhACVaaAA1jgAA4KxCzGjAJDlGUASQV+GgKkicAgaAAzAAaNAAFjdczd2xgQkoehgBjkxhUZgscCsNjQdnEjjQAAppEJgOq4ABKSrVABu9Whe1zj1m2YW+aBJaLaGz2zLHWrlezTru5adDfdzbr7vhJKrBMxJaFub51cHfarTtHw-dRCAA) --- packages/mermaid-layout-elk/src/render.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mermaid-layout-elk/src/render.ts b/packages/mermaid-layout-elk/src/render.ts index 9f361bf7b..c3799fedd 100644 --- a/packages/mermaid-layout-elk/src/render.ts +++ b/packages/mermaid-layout-elk/src/render.ts @@ -766,7 +766,7 @@ export const render = async ( id: 'root', layoutOptions: { 'elk.hierarchyHandling': 'INCLUDE_CHILDREN', - 'elk.layered.crossingMinimization.forceNodeModelOrder': true, + 'elk.layered.considerModelOrder.strategy': 'NODES_AND_EDGES', 'elk.algorithm': algorithm, 'nodePlacement.strategy': data4Layout.config.elk?.nodePlacementStrategy, 'elk.layered.mergeEdges': data4Layout.config.elk?.mergeEdges, From 2260948b7bda08f00616c2ce678bed1da69eb96c Mon Sep 17 00:00:00 2001 From: anderium <33520919+anderium@users.noreply.github.com> Date: Wed, 13 Aug 2025 14:39:09 +0200 Subject: [PATCH 2/2] Add changeset and integration test --- .changeset/spicy-ties-see.md | 5 +++++ .../integration/rendering/flowchart-elk.spec.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 .changeset/spicy-ties-see.md diff --git a/.changeset/spicy-ties-see.md b/.changeset/spicy-ties-see.md new file mode 100644 index 000000000..25fc643e0 --- /dev/null +++ b/.changeset/spicy-ties-see.md @@ -0,0 +1,5 @@ +--- +'@mermaid-js/layout-elk': patch +--- + +Make elk not force node model order, but strongly consider it instead diff --git a/cypress/integration/rendering/flowchart-elk.spec.js b/cypress/integration/rendering/flowchart-elk.spec.js index 27af2c40c..312e1d5b4 100644 --- a/cypress/integration/rendering/flowchart-elk.spec.js +++ b/cypress/integration/rendering/flowchart-elk.spec.js @@ -1053,6 +1053,21 @@ flowchart LR }); }); }); + + it('6647-elk: should keep node order when using elk layout unless it would add crossings', () => { + imgSnapshotTest( + `--- +config: + layout: elk +--- + flowchart TB + a --> a1 & a2 & a3 & a4 + b --> b1 & b2 + b2 --> b3 + b1 --> b4 + ` + ); + }); }); describe('Title and arrow styling #4813', () => {