diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md
index 85dce2f75c..f32b367f3b 100644
--- a/ci/release/changelogs/next.md
+++ b/ci/release/changelogs/next.md
@@ -9,3 +9,4 @@
#### Bugfixes ⛑️
- Fixes `d2 fmt` to format all files passed as arguments rather than first non-formatted only [#1523](https://github.com/terrastruct/d2/issues/1523)
+- Fixes Markdown cropping last element in mixed-element blocks (e.g. em and strong) [#1543](https://github.com/terrastruct/d2/issues/1543)
diff --git a/e2etests/stable_test.go b/e2etests/stable_test.go
index 72c8707033..0af331dafb 100644
--- a/e2etests/stable_test.go
+++ b/e2etests/stable_test.go
@@ -2002,6 +2002,15 @@ i am top left: { shape: text; near: top-left }
i am top right: { shape: text; near: top-right }
i am bottom left: { shape: text; near: bottom-left }
i am bottom right: { shape: text; near: bottom-right }
+`,
+ },
+ {
+ name: "md_mixed",
+ script: `example: {
+ explanation: |md
+ *one* __two__ three!
+ |
+}
`,
},
{
diff --git a/e2etests/testdata/stable/md_mixed/dagre/board.exp.json b/e2etests/testdata/stable/md_mixed/dagre/board.exp.json
new file mode 100644
index 0000000000..e26f2eeff2
--- /dev/null
+++ b/e2etests/testdata/stable/md_mixed/dagre/board.exp.json
@@ -0,0 +1,129 @@
+{
+ "name": "",
+ "isFolderOnly": false,
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "example",
+ "type": "rectangle",
+ "pos": {
+ "x": 10,
+ "y": 20
+ },
+ "width": 159,
+ "height": 84,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "B4",
+ "stroke": "B1",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "double-border": false,
+ "tooltip": "",
+ "link": "",
+ "icon": null,
+ "iconPosition": "",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "example",
+ "fontSize": 28,
+ "fontFamily": "DEFAULT",
+ "language": "",
+ "color": "N1",
+ "italic": false,
+ "bold": false,
+ "underline": false,
+ "labelWidth": 99,
+ "labelHeight": 36,
+ "labelPosition": "OUTSIDE_TOP_CENTER",
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "example.explanation",
+ "type": "text",
+ "pos": {
+ "x": 40,
+ "y": 50
+ },
+ "width": 99,
+ "height": 24,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "transparent",
+ "stroke": "N1",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "double-border": false,
+ "tooltip": "",
+ "link": "",
+ "icon": null,
+ "iconPosition": "",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "*one* __two__ three!",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "markdown",
+ "color": "N1",
+ "italic": false,
+ "bold": false,
+ "underline": false,
+ "labelWidth": 99,
+ "labelHeight": 24,
+ "zIndex": 0,
+ "level": 2
+ }
+ ],
+ "connections": [],
+ "root": {
+ "id": "",
+ "type": "",
+ "pos": {
+ "x": 0,
+ "y": 0
+ },
+ "width": 0,
+ "height": 0,
+ "opacity": 0,
+ "strokeDash": 0,
+ "strokeWidth": 0,
+ "borderRadius": 0,
+ "fill": "N7",
+ "stroke": "",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "double-border": false,
+ "tooltip": "",
+ "link": "",
+ "icon": null,
+ "iconPosition": "",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "",
+ "fontSize": 0,
+ "fontFamily": "",
+ "language": "",
+ "color": "",
+ "italic": false,
+ "bold": false,
+ "underline": false,
+ "labelWidth": 0,
+ "labelHeight": 0,
+ "zIndex": 0,
+ "level": 0
+ }
+}
diff --git a/e2etests/testdata/stable/md_mixed/dagre/sketch.exp.svg b/e2etests/testdata/stable/md_mixed/dagre/sketch.exp.svg
new file mode 100644
index 0000000000..729a75894a
--- /dev/null
+++ b/e2etests/testdata/stable/md_mixed/dagre/sketch.exp.svg
@@ -0,0 +1,851 @@
+
\ No newline at end of file
diff --git a/e2etests/testdata/stable/md_mixed/elk/board.exp.json b/e2etests/testdata/stable/md_mixed/elk/board.exp.json
new file mode 100644
index 0000000000..1c804d0339
--- /dev/null
+++ b/e2etests/testdata/stable/md_mixed/elk/board.exp.json
@@ -0,0 +1,129 @@
+{
+ "name": "",
+ "isFolderOnly": false,
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "example",
+ "type": "rectangle",
+ "pos": {
+ "x": 12,
+ "y": 12
+ },
+ "width": 199,
+ "height": 124,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "B4",
+ "stroke": "B1",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "double-border": false,
+ "tooltip": "",
+ "link": "",
+ "icon": null,
+ "iconPosition": "",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "example",
+ "fontSize": 28,
+ "fontFamily": "DEFAULT",
+ "language": "",
+ "color": "N1",
+ "italic": false,
+ "bold": false,
+ "underline": false,
+ "labelWidth": 99,
+ "labelHeight": 36,
+ "labelPosition": "INSIDE_TOP_CENTER",
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "example.explanation",
+ "type": "text",
+ "pos": {
+ "x": 62,
+ "y": 62
+ },
+ "width": 99,
+ "height": 24,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "transparent",
+ "stroke": "N1",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "double-border": false,
+ "tooltip": "",
+ "link": "",
+ "icon": null,
+ "iconPosition": "",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "*one* __two__ three!",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "markdown",
+ "color": "N1",
+ "italic": false,
+ "bold": false,
+ "underline": false,
+ "labelWidth": 99,
+ "labelHeight": 24,
+ "zIndex": 0,
+ "level": 2
+ }
+ ],
+ "connections": [],
+ "root": {
+ "id": "",
+ "type": "",
+ "pos": {
+ "x": 0,
+ "y": 0
+ },
+ "width": 0,
+ "height": 0,
+ "opacity": 0,
+ "strokeDash": 0,
+ "strokeWidth": 0,
+ "borderRadius": 0,
+ "fill": "N7",
+ "stroke": "",
+ "shadow": false,
+ "3d": false,
+ "multiple": false,
+ "double-border": false,
+ "tooltip": "",
+ "link": "",
+ "icon": null,
+ "iconPosition": "",
+ "blend": false,
+ "fields": null,
+ "methods": null,
+ "columns": null,
+ "label": "",
+ "fontSize": 0,
+ "fontFamily": "",
+ "language": "",
+ "color": "",
+ "italic": false,
+ "bold": false,
+ "underline": false,
+ "labelWidth": 0,
+ "labelHeight": 0,
+ "zIndex": 0,
+ "level": 0
+ }
+}
diff --git a/e2etests/testdata/stable/md_mixed/elk/sketch.exp.svg b/e2etests/testdata/stable/md_mixed/elk/sketch.exp.svg
new file mode 100644
index 0000000000..b38de7444a
--- /dev/null
+++ b/e2etests/testdata/stable/md_mixed/elk/sketch.exp.svg
@@ -0,0 +1,851 @@
+example
+
+
+
+
\ No newline at end of file
diff --git a/lib/textmeasure/markdown.go b/lib/textmeasure/markdown.go
index a306446476..d2903343c0 100644
--- a/lib/textmeasure/markdown.go
+++ b/lib/textmeasure/markdown.go
@@ -232,7 +232,7 @@ func (ruler *Ruler) measureNode(depth int, n *html.Node, fontFamily *d2fonts.Fon
switch n.Type {
case html.TextNode:
- if strings.TrimSpace(n.Data) == "" {
+ if strings.Trim(n.Data, "\n\t\b") == "" {
return blockAttrs{}
}
str := n.Data