Skip to content

Commit 33c7c72

Browse files
authored
Merge pull request #7276 from mermaid-js/markdown-specific-changes
Fix non-markdown labels in flowcharts being treated like markdown
1 parent 5959fc0 commit 33c7c72

File tree

28 files changed

+323
-69
lines changed

28 files changed

+323
-69
lines changed

.changeset/curvy-cases-battle.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'mermaid': patch
3+
---
4+
5+
fix: respect `markdownAutoWrap: false` to prevent text auto-wrapping in flowchart markdown labels with `htmlLabels` enabled.
6+
7+
Markdown labels with `markdownAutoWrap: false, htmlLabels: false` set doesn't work
8+
correctly.

.changeset/swift-cloths-run.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
'mermaid': patch
3+
---
4+
5+
fix: Restore proper rendering of plain text flowchart labels without auto line-wrapping
6+
7+
This fix restores backwards compatibility with Mermaid v10 by ensuring that plain text labels in flowcharts are rendered correctly. In Mermaid v11, all labels were incorrectly being treated as markdown by default, which caused issues with text wrapping, multiline breaks, and backwards compatibility.
8+
9+
**What changed:**
10+
11+
- Plain text labels in flowcharts (without markdown syntax) now render as regular text
12+
- For node labels and edge labels, these will line-wrap automatically. Although this isn't backwards compatible with v10, we think this is a minor change and it's worth keeping to avoid too many changes from diagrams created from v11 onwards.
13+
- Plain text labels in other diagrams will continue to not line wrap.
14+
- Plain text labels with `\n` characters now correctly create line breaks
15+
- Plain text that looks like markdown (e.g., "1.", "- x") is no longer misinterpreted
16+
17+
**If you want markdown formatting:**
18+
You can still use markdown in your flowchart labels by using the proper markdown syntax. Wrap your markdown text with double quotes and backticks:
19+
``node["`_markdown_ **text**`"]``
20+
21+
Example:
22+
23+
````markdown
24+
```mermaid
25+
flowchart TD
26+
plain["Plain text\nwith manual line break"]
27+
markdown["`This is a **markdown** _label_ that wraps and doesn't replace \n with newlines`"]
28+
```
29+
````

cypress/integration/rendering/flowchart-v2.spec.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,8 @@ end
11861186
end
11871187
githost["Github, Gitlab, BitBucket, etc."]
11881188
githost2["\`Github, Gitlab, BitBucket, etc.\`"]
1189-
a["1."]
1190-
b["- x"]
1189+
a["\`1.\`"]
1190+
b["\`- x\`"]
11911191
`;
11921192

11931193
it('should render raw strings', () => {
@@ -1211,4 +1211,32 @@ class link myClass
12111211
`
12121212
);
12131213
});
1214+
1215+
describe('Edge label autowrapping', () => {
1216+
it('should wrap edge labels', () => {
1217+
imgSnapshotTest(
1218+
[
1219+
{
1220+
markdownAutoWrap: true,
1221+
htmlLabels: true,
1222+
},
1223+
{ markdownAutoWrap: true, htmlLabels: false },
1224+
{ markdownAutoWrap: false, htmlLabels: true },
1225+
// TODO: currently broken
1226+
// {markdownAutoWrap: false, htmlLabels: false},
1227+
].map(
1228+
({ markdownAutoWrap, htmlLabels }) => `---
1229+
config: ${JSON.stringify({ markdownAutoWrap, htmlLabels })}
1230+
title: Testing with ${JSON.stringify({ markdownAutoWrap, htmlLabels })}
1231+
---
1232+
flowchart TD
1233+
A["This is a really long line of plain text that will autowrap and support \\n newlines too."]
1234+
B["\`This is a really long line of **markdown** text that will autowrap, unless markdownAutoWrap:false is set.\`"]
1235+
A -- "Plain text **labels** in flowcharts will autowrap,like node labels. \\n Newline characters work too." --> B
1236+
B -- "\`**Markdown** edge labels will autowrap, unless markdownAutoWrap: false is set\`" --> C
1237+
`
1238+
)
1239+
);
1240+
});
1241+
});
12141242
});

cypress/integration/rendering/flowchart.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,3 +1004,48 @@ graph TD
10041004
);
10051005
});
10061006
});
1007+
it('#5824: should be able to render string and markdown labels', () => {
1008+
imgSnapshotTest(
1009+
`
1010+
flowchart TB
1011+
mermaid{"What is\nyourmermaid version?"} --> v10["<11"] --"\`<**1**1\`"--> fine["No bug"]
1012+
mermaid --> v11[">= v11"] -- ">= v11" --> broken["Affected by https://github.com/mermaid-js/mermaid/issues/5824"]
1013+
subgraph subgraph1["\`How to fix **fix**\`"]
1014+
broken --> B["B"]
1015+
end
1016+
githost["Github, Gitlab, BitBucket, etc."]
1017+
githost2["\`Github, Gitlab, BitBucket, etc.\`"]
1018+
a["1."]
1019+
b["- x"]
1020+
`,
1021+
{
1022+
flowchart: { htmlLabels: true },
1023+
securityLevel: 'loose',
1024+
}
1025+
);
1026+
});
1027+
it('69: should render subgraphs with adhoc list headings', () => {
1028+
imgSnapshotTest(
1029+
`
1030+
graph TB
1031+
subgraph "1. first"
1032+
a1-->a2
1033+
end
1034+
subgraph 2. second
1035+
b1-->b2
1036+
end
1037+
`,
1038+
{ fontFamily: 'courier' }
1039+
);
1040+
});
1041+
it('70: should render subgraphs with markdown headings', () => {
1042+
imgSnapshotTest(
1043+
`
1044+
graph TB
1045+
subgraph "\`**strong**\`"
1046+
a1-->a2
1047+
end
1048+
`,
1049+
{ fontFamily: 'courier' }
1050+
);
1051+
});

docs/config/setup/mermaid/interfaces/ParseOptions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
# Interface: ParseOptions
1212

13-
Defined in: [packages/mermaid/src/types.ts:89](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L89)
13+
Defined in: [packages/mermaid/src/types.ts:90](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L90)
1414

1515
## Properties
1616

1717
### suppressErrors?
1818

1919
> `optional` **suppressErrors**: `boolean`
2020
21-
Defined in: [packages/mermaid/src/types.ts:94](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L94)
21+
Defined in: [packages/mermaid/src/types.ts:95](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L95)
2222

2323
If `true`, parse will return `false` instead of throwing error when the diagram is invalid.
2424
The `parseError` function will not be called.

docs/config/setup/mermaid/interfaces/ParseResult.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
# Interface: ParseResult
1212

13-
Defined in: [packages/mermaid/src/types.ts:97](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L97)
13+
Defined in: [packages/mermaid/src/types.ts:98](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L98)
1414

1515
## Properties
1616

1717
### config
1818

1919
> **config**: [`MermaidConfig`](MermaidConfig.md)
2020
21-
Defined in: [packages/mermaid/src/types.ts:105](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L105)
21+
Defined in: [packages/mermaid/src/types.ts:106](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L106)
2222

2323
The config passed as YAML frontmatter or directives
2424

@@ -28,6 +28,6 @@ The config passed as YAML frontmatter or directives
2828

2929
> **diagramType**: `string`
3030
31-
Defined in: [packages/mermaid/src/types.ts:101](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L101)
31+
Defined in: [packages/mermaid/src/types.ts:102](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L102)
3232

3333
The diagram type, e.g. 'flowchart', 'sequence', etc.

docs/config/setup/mermaid/interfaces/RenderResult.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
# Interface: RenderResult
1212

13-
Defined in: [packages/mermaid/src/types.ts:115](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L115)
13+
Defined in: [packages/mermaid/src/types.ts:116](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L116)
1414

1515
## Properties
1616

1717
### bindFunctions()?
1818

1919
> `optional` **bindFunctions**: (`element`) => `void`
2020
21-
Defined in: [packages/mermaid/src/types.ts:133](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L133)
21+
Defined in: [packages/mermaid/src/types.ts:134](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L134)
2222

2323
Bind function to be called after the svg has been inserted into the DOM.
2424
This is necessary for adding event listeners to the elements in the svg.
@@ -45,7 +45,7 @@ bindFunctions?.(div); // To call bindFunctions only if it's present.
4545
4646
> **diagramType**: `string`
4747
48-
Defined in: [packages/mermaid/src/types.ts:123](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L123)
48+
Defined in: [packages/mermaid/src/types.ts:124](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L124)
4949
5050
The diagram type, e.g. 'flowchart', 'sequence', etc.
5151
@@ -55,6 +55,6 @@ The diagram type, e.g. 'flowchart', 'sequence', etc.
5555
5656
> **svg**: `string`
5757
58-
Defined in: [packages/mermaid/src/types.ts:119](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L119)
58+
Defined in: [packages/mermaid/src/types.ts:120](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/types.ts#L120)
5959
6060
The svg code for the rendered graph.

packages/mermaid/src/dagre-wrapper/edges.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,32 @@ export const clear = () => {
2121
export const insertEdgeLabel = async (elem, edge) => {
2222
const config = getConfig();
2323
const useHtmlLabels = getEffectiveHtmlLabels(config);
24-
// Create the actual text element
25-
const labelElement =
26-
edge.labelType === 'markdown'
27-
? // TODO: the createText function returns a `Promise`, so do we need an wait here?
28-
createText(
29-
elem,
30-
edge.label,
31-
{
32-
style: edge.labelStyle,
33-
useHtmlLabels,
34-
addSvgBackground: true,
35-
},
36-
config
37-
)
38-
: await createLabel(elem, edge.label, edge.labelStyle);
3924

4025
// Create outer g, edgeLabel, this will be positioned after graph layout
4126
const edgeLabel = elem.insert('g').attr('class', 'edgeLabel');
4227

4328
// Create inner g, label, this will be positioned now for centering the text
4429
const label = edgeLabel.insert('g').attr('class', 'label');
30+
31+
// Create the actual text element
32+
const isMarkdown = edge.labelType === 'markdown';
33+
const labelElement = await createText(
34+
elem,
35+
edge.label,
36+
{
37+
style: edge.labelStyle,
38+
useHtmlLabels,
39+
// TODO: The old code only set addSvgBackground when using markdown, but
40+
// this function is only used by block diagrams which never use markdown.
41+
addSvgBackground: isMarkdown,
42+
isNode: false,
43+
markdown: isMarkdown,
44+
// If using markdown, wrap using default width
45+
width: isMarkdown ? undefined : Number.POSITIVE_INFINITY,
46+
},
47+
config
48+
);
49+
4550
label.node().appendChild(labelElement);
4651

4752
// Center the label

packages/mermaid/src/diagrams/block/styles.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ const getStyles = (options: BlockChartStyleOptions) =>
9191
9292
.edgeLabel {
9393
background-color: ${options.edgeLabelBackground};
94+
/*
95+
* This is for backward compatibility with existing code that didn't
96+
* add a \`<p>\` around edge labels.
97+
*
98+
* TODO: We should probably remove this in a future release.
99+
*/
100+
p {
101+
margin: 0;
102+
padding: 0;
103+
display: inline;
104+
}
94105
rect {
95106
opacity: 0.5;
96107
background-color: ${options.edgeLabelBackground};
@@ -101,8 +112,7 @@ const getStyles = (options: BlockChartStyleOptions) =>
101112
102113
/* For html labels only */
103114
.labelBkg {
104-
background-color: ${fade(options.edgeLabelBackground, 0.5)};
105-
// background-color:
115+
background-color: ${options.edgeLabelBackground};
106116
}
107117
108118
.node .cluster {

packages/mermaid/src/diagrams/class/classDb.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ export class ClassDB implements DiagramDB {
679679
],
680680
look: config.look,
681681
parentId: note.parent,
682+
labelType: 'markdown',
682683
};
683684
nodes.push(noteNode);
684685

@@ -741,6 +742,7 @@ export class ClassDB implements DiagramDB {
741742
style: classRelation.style || '',
742743
pattern: classRelation.relation.lineType == 1 ? 'dashed' : 'solid',
743744
look: config.look,
745+
labelType: 'markdown',
744746
};
745747
edges.push(edge);
746748
}

0 commit comments

Comments
 (0)