diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md
index 999f9677a..c6a3996c5 100644
--- a/ci/release/changelogs/next.md
+++ b/ci/release/changelogs/next.md
@@ -20,3 +20,4 @@
- Fix importing a file with underscores in links [#1999](https://github.com/terrastruct/d2/pull/1999)
- Replace a panic with an error message resulting from invalid `link` usage [#2011](https://github.com/terrastruct/d2/pull/2011)
- Fix globs not applying to scenarios on keys that were applied in earlier scenarios [#2021](https://github.com/terrastruct/d2/pull/2021)
+- Fix edge case of invalid SVG from code blocks [#2031](https://github.com/terrastruct/d2/pull/2031)
diff --git a/d2renderers/d2svg/code.go b/d2renderers/d2svg/code.go
index a01828c53..2e4f56756 100644
--- a/d2renderers/d2svg/code.go
+++ b/d2renderers/d2svg/code.go
@@ -49,8 +49,23 @@ func styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string {
}
}
// Custom code
- out := strings.Replace(styles[tt], `font-weight="bold"`, `class="text-mono-bold"`, -1)
- return strings.Replace(out, `font-style="italic"`, `class="text-mono-italic"`, -1)
+ out := styles[tt]
+ classes := []string{}
+
+ if strings.Contains(out, `font-weight="bold"`) {
+ out = strings.Replace(out, `font-weight="bold"`, ``, 1)
+ classes = append(classes, "text-mono-bold")
+ }
+ if strings.Contains(out, `font-style="italic"`) {
+ out = strings.Replace(out, `font-style="italic"`, ``, 1)
+ classes = append(classes, "text-mono-italic")
+ }
+
+ if len(classes) > 0 {
+ out += `class="` + strings.Join(classes, " ") + `"`
+ }
+
+ return strings.TrimSpace(out)
}
// <<< END
diff --git a/e2etests/stable_test.go b/e2etests/stable_test.go
index 0b8ed4af1..376545c87 100644
--- a/e2etests/stable_test.go
+++ b/e2etests/stable_test.go
@@ -2834,6 +2834,16 @@ y: profits {
icon.near: outside-bottom-center
}
`,
+ },
+ {
+ name: "shebang-codeblock",
+ script: `
+"test.sh": {
+ someid: |sh
+ #!/usr/bin/env bash
+ echo testing
+ |
+}`,
},
loadFromFile(t, "arrowhead_scaling"),
loadFromFile(t, "teleport_grid"),
diff --git a/e2etests/testdata/stable/shebang-codeblock/dagre/board.exp.json b/e2etests/testdata/stable/shebang-codeblock/dagre/board.exp.json
new file mode 100644
index 000000000..fd5d32f71
--- /dev/null
+++ b/e2etests/testdata/stable/shebang-codeblock/dagre/board.exp.json
@@ -0,0 +1,129 @@
+{
+ "name": "",
+ "isFolderOnly": false,
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "\"test.sh\"",
+ "type": "rectangle",
+ "pos": {
+ "x": 10,
+ "y": 20
+ },
+ "width": 259,
+ "height": 117,
+ "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": "test.sh",
+ "fontSize": 28,
+ "fontFamily": "DEFAULT",
+ "language": "",
+ "color": "N1",
+ "italic": false,
+ "bold": false,
+ "underline": false,
+ "labelWidth": 78,
+ "labelHeight": 36,
+ "labelPosition": "OUTSIDE_TOP_CENTER",
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "\"test.sh\".someid",
+ "type": "code",
+ "pos": {
+ "x": 40,
+ "y": 50
+ },
+ "width": 199,
+ "height": 57,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "N7",
+ "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": "#!/usr/bin/env bash\necho testing",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "sh",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 183,
+ "labelHeight": 41,
+ "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/shebang-codeblock/dagre/sketch.exp.svg b/e2etests/testdata/stable/shebang-codeblock/dagre/sketch.exp.svg
new file mode 100644
index 000000000..37d1ea324
--- /dev/null
+++ b/e2etests/testdata/stable/shebang-codeblock/dagre/sketch.exp.svg
@@ -0,0 +1,119 @@
+
\ No newline at end of file
diff --git a/e2etests/testdata/stable/shebang-codeblock/elk/board.exp.json b/e2etests/testdata/stable/shebang-codeblock/elk/board.exp.json
new file mode 100644
index 000000000..094f273f6
--- /dev/null
+++ b/e2etests/testdata/stable/shebang-codeblock/elk/board.exp.json
@@ -0,0 +1,129 @@
+{
+ "name": "",
+ "isFolderOnly": false,
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "\"test.sh\"",
+ "type": "rectangle",
+ "pos": {
+ "x": 12,
+ "y": 12
+ },
+ "width": 299,
+ "height": 157,
+ "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": "test.sh",
+ "fontSize": 28,
+ "fontFamily": "DEFAULT",
+ "language": "",
+ "color": "N1",
+ "italic": false,
+ "bold": false,
+ "underline": false,
+ "labelWidth": 78,
+ "labelHeight": 36,
+ "labelPosition": "INSIDE_TOP_CENTER",
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "\"test.sh\".someid",
+ "type": "code",
+ "pos": {
+ "x": 62,
+ "y": 62
+ },
+ "width": 199,
+ "height": 57,
+ "opacity": 1,
+ "strokeDash": 0,
+ "strokeWidth": 2,
+ "borderRadius": 0,
+ "fill": "N7",
+ "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": "#!/usr/bin/env bash\necho testing",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "sh",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 183,
+ "labelHeight": 41,
+ "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/shebang-codeblock/elk/sketch.exp.svg b/e2etests/testdata/stable/shebang-codeblock/elk/sketch.exp.svg
new file mode 100644
index 000000000..fd2ec410f
--- /dev/null
+++ b/e2etests/testdata/stable/shebang-codeblock/elk/sketch.exp.svg
@@ -0,0 +1,119 @@
+test.sh#!/usr/bin/env bash
+echo testing#!/usr/bin/env bash
+echo testing
+
+
+
+
\ No newline at end of file