diff --git a/e2etests/regression_test.go b/e2etests/regression_test.go
index e542d469e..30362e97d 100644
--- a/e2etests/regression_test.go
+++ b/e2etests/regression_test.go
@@ -423,6 +423,108 @@ no leading: |python
print "world"
+|
+`,
+ },
+ {
+ name: "code_leading_newlines",
+ script: `
+5 leading: |python
+
+
+
+
+
+ def hello():
+
+ print "world"
+|
+8 total: |python
+ # 1 leading
+ # 2 leading
+ # 3 leading
+ # 4 leading
+ # 5 leading
+ def hello():
+
+ print "world"
+|
+1 leading: |python
+
+ def hello():
+
+ print "world"
+|
+4 total: |python
+ # 1 leading
+ def hello():
+
+ print "world"
+|
+2 leading: |python
+
+
+ def hello():
+
+ print "world"
+|
+5 total: |python
+ # 1 leading
+ # 2 leading
+ def hello():
+
+ print "world"
+|
+`,
+ },
+ {
+ name: "code_trailing_newlines",
+ script: `
+5 trailing: |python
+ def hello():
+
+ print "world"
+
+
+
+
+
+|
+8 total: |python
+ def hello():
+
+ print "world"
+ # 1 trailing
+ # 2 trailing
+ # 3 trailing
+ # 4 trailing
+ # 5 trailing
+|
+1 trailing: |python
+ def hello():
+
+ print "world"
+
+|
+4 total: |python
+ def hello():
+
+ print "world"
+ # 1 trailing
+|
+2 trailing: |python
+ def hello():
+
+ print "world"
+
+
+|
+5 total: |python
+ def hello():
+
+ print "world"
+ # 1 trailing
+ # 2 trailing
|
`,
},
diff --git a/e2etests/testdata/regression/code_leading_newlines/dagre/board.exp.json b/e2etests/testdata/regression/code_leading_newlines/dagre/board.exp.json
new file mode 100644
index 000000000..af6109b05
--- /dev/null
+++ b/e2etests/testdata/regression/code_leading_newlines/dagre/board.exp.json
@@ -0,0 +1,288 @@
+{
+ "name": "",
+ "isFolderOnly": false,
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "5 leading",
+ "type": "code",
+ "pos": {
+ "x": 0,
+ "y": 0
+ },
+ "width": 160,
+ "height": 150,
+ "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": "\n\n\n\n\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 145,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "8 total",
+ "type": "code",
+ "pos": {
+ "x": 220,
+ "y": 0
+ },
+ "width": 160,
+ "height": 150,
+ "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": "# 1 leading\n# 2 leading\n# 3 leading\n# 4 leading\n# 5 leading\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 145,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "1 leading",
+ "type": "code",
+ "pos": {
+ "x": 440,
+ "y": 32
+ },
+ "width": 160,
+ "height": 86,
+ "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": "\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 81,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "4 total",
+ "type": "code",
+ "pos": {
+ "x": 660,
+ "y": 32
+ },
+ "width": 160,
+ "height": 86,
+ "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": "# 1 leading\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 81,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "2 leading",
+ "type": "code",
+ "pos": {
+ "x": 880,
+ "y": 24
+ },
+ "width": 160,
+ "height": 102,
+ "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": "\n\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 97,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "5 total",
+ "type": "code",
+ "pos": {
+ "x": 1100,
+ "y": 24
+ },
+ "width": 160,
+ "height": 102,
+ "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": "# 1 leading\n# 2 leading\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 97,
+ "zIndex": 0,
+ "level": 1
+ }
+ ],
+ "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/regression/code_leading_newlines/dagre/sketch.exp.svg b/e2etests/testdata/regression/code_leading_newlines/dagre/sketch.exp.svg
new file mode 100644
index 000000000..6d8f71f70
--- /dev/null
+++ b/e2etests/testdata/regression/code_leading_newlines/dagre/sketch.exp.svg
@@ -0,0 +1,170 @@
+
\ No newline at end of file
diff --git a/e2etests/testdata/regression/code_leading_newlines/elk/board.exp.json b/e2etests/testdata/regression/code_leading_newlines/elk/board.exp.json
new file mode 100644
index 000000000..7b9fa1332
--- /dev/null
+++ b/e2etests/testdata/regression/code_leading_newlines/elk/board.exp.json
@@ -0,0 +1,288 @@
+{
+ "name": "",
+ "isFolderOnly": false,
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "5 leading",
+ "type": "code",
+ "pos": {
+ "x": 12,
+ "y": 12
+ },
+ "width": 160,
+ "height": 150,
+ "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": "\n\n\n\n\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 145,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "8 total",
+ "type": "code",
+ "pos": {
+ "x": 192,
+ "y": 12
+ },
+ "width": 160,
+ "height": 150,
+ "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": "# 1 leading\n# 2 leading\n# 3 leading\n# 4 leading\n# 5 leading\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 145,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "1 leading",
+ "type": "code",
+ "pos": {
+ "x": 372,
+ "y": 44
+ },
+ "width": 160,
+ "height": 86,
+ "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": "\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 81,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "4 total",
+ "type": "code",
+ "pos": {
+ "x": 552,
+ "y": 44
+ },
+ "width": 160,
+ "height": 86,
+ "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": "# 1 leading\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 81,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "2 leading",
+ "type": "code",
+ "pos": {
+ "x": 732,
+ "y": 36
+ },
+ "width": 160,
+ "height": 102,
+ "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": "\n\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 97,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "5 total",
+ "type": "code",
+ "pos": {
+ "x": 912,
+ "y": 36
+ },
+ "width": 160,
+ "height": 102,
+ "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": "# 1 leading\n# 2 leading\ndef hello():\n\n print \"world\"",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 97,
+ "zIndex": 0,
+ "level": 1
+ }
+ ],
+ "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/regression/code_leading_newlines/elk/sketch.exp.svg b/e2etests/testdata/regression/code_leading_newlines/elk/sketch.exp.svg
new file mode 100644
index 000000000..34d05ff21
--- /dev/null
+++ b/e2etests/testdata/regression/code_leading_newlines/elk/sketch.exp.svg
@@ -0,0 +1,170 @@
+
+
+
+
+
+def hello():
+
+ print "world"
+
+
+
+
+def hello():
+
+ print "world"# 1 leading
+# 2 leading
+# 3 leading
+# 4 leading
+# 5 leading
+def hello():
+
+ print "world"# 1 leading
+# 2 leading
+# 3 leading
+# 4 leading
+# 5 leading
+def hello():
+
+ print "world"
+def hello():
+
+ print "world"
+def hello():
+
+ print "world"# 1 leading
+def hello():
+
+ print "world"# 1 leading
+def hello():
+
+ print "world"
+
+def hello():
+
+ print "world"
+
+def hello():
+
+ print "world"# 1 leading
+# 2 leading
+def hello():
+
+ print "world"# 1 leading
+# 2 leading
+def hello():
+
+ print "world"
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/e2etests/testdata/regression/code_trailing_newlines/dagre/board.exp.json b/e2etests/testdata/regression/code_trailing_newlines/dagre/board.exp.json
new file mode 100644
index 000000000..089bc332b
--- /dev/null
+++ b/e2etests/testdata/regression/code_trailing_newlines/dagre/board.exp.json
@@ -0,0 +1,288 @@
+{
+ "name": "",
+ "isFolderOnly": false,
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "5 trailing",
+ "type": "code",
+ "pos": {
+ "x": 0,
+ "y": 0
+ },
+ "width": 160,
+ "height": 150,
+ "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": "def hello():\n\n print \"world\"\n\n\n\n\n",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 145,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "8 total",
+ "type": "code",
+ "pos": {
+ "x": 220,
+ "y": 0
+ },
+ "width": 160,
+ "height": 150,
+ "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": "def hello():\n\n print \"world\"\n# 1 trailing\n# 2 trailing\n# 3 trailing\n# 4 trailing\n# 5 trailing",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 145,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "1 trailing",
+ "type": "code",
+ "pos": {
+ "x": 440,
+ "y": 32
+ },
+ "width": 160,
+ "height": 86,
+ "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": "def hello():\n\n print \"world\"\n",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 81,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "4 total",
+ "type": "code",
+ "pos": {
+ "x": 660,
+ "y": 32
+ },
+ "width": 160,
+ "height": 86,
+ "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": "def hello():\n\n print \"world\"\n# 1 trailing",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 81,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "2 trailing",
+ "type": "code",
+ "pos": {
+ "x": 880,
+ "y": 24
+ },
+ "width": 160,
+ "height": 102,
+ "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": "def hello():\n\n print \"world\"\n\n",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 97,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "5 total",
+ "type": "code",
+ "pos": {
+ "x": 1100,
+ "y": 24
+ },
+ "width": 160,
+ "height": 102,
+ "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": "def hello():\n\n print \"world\"\n# 1 trailing\n# 2 trailing",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 97,
+ "zIndex": 0,
+ "level": 1
+ }
+ ],
+ "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/regression/code_trailing_newlines/dagre/sketch.exp.svg b/e2etests/testdata/regression/code_trailing_newlines/dagre/sketch.exp.svg
new file mode 100644
index 000000000..d9b09f3ef
--- /dev/null
+++ b/e2etests/testdata/regression/code_trailing_newlines/dagre/sketch.exp.svg
@@ -0,0 +1,170 @@
+def hello():
+
+ print "world"
+
+
+
+
+def hello():
+
+ print "world"
+
+
+
+
+def hello():
+
+ print "world"
+# 1 trailing
+# 2 trailing
+# 3 trailing
+# 4 trailing
+# 5 trailingdef hello():
+
+ print "world"
+# 1 trailing
+# 2 trailing
+# 3 trailing
+# 4 trailing
+# 5 trailingdef hello():
+
+ print "world"
+def hello():
+
+ print "world"
+def hello():
+
+ print "world"
+# 1 trailingdef hello():
+
+ print "world"
+# 1 trailingdef hello():
+
+ print "world"
+
+def hello():
+
+ print "world"
+
+def hello():
+
+ print "world"
+# 1 trailing
+# 2 trailingdef hello():
+
+ print "world"
+# 1 trailing
+# 2 trailing
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/e2etests/testdata/regression/code_trailing_newlines/elk/board.exp.json b/e2etests/testdata/regression/code_trailing_newlines/elk/board.exp.json
new file mode 100644
index 000000000..1f43e7e2b
--- /dev/null
+++ b/e2etests/testdata/regression/code_trailing_newlines/elk/board.exp.json
@@ -0,0 +1,288 @@
+{
+ "name": "",
+ "isFolderOnly": false,
+ "fontFamily": "SourceSansPro",
+ "shapes": [
+ {
+ "id": "5 trailing",
+ "type": "code",
+ "pos": {
+ "x": 12,
+ "y": 12
+ },
+ "width": 160,
+ "height": 150,
+ "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": "def hello():\n\n print \"world\"\n\n\n\n\n",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 145,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "8 total",
+ "type": "code",
+ "pos": {
+ "x": 192,
+ "y": 12
+ },
+ "width": 160,
+ "height": 150,
+ "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": "def hello():\n\n print \"world\"\n# 1 trailing\n# 2 trailing\n# 3 trailing\n# 4 trailing\n# 5 trailing",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 145,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "1 trailing",
+ "type": "code",
+ "pos": {
+ "x": 372,
+ "y": 44
+ },
+ "width": 160,
+ "height": 86,
+ "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": "def hello():\n\n print \"world\"\n",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 81,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "4 total",
+ "type": "code",
+ "pos": {
+ "x": 552,
+ "y": 44
+ },
+ "width": 160,
+ "height": 86,
+ "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": "def hello():\n\n print \"world\"\n# 1 trailing",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 81,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "2 trailing",
+ "type": "code",
+ "pos": {
+ "x": 732,
+ "y": 36
+ },
+ "width": 160,
+ "height": 102,
+ "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": "def hello():\n\n print \"world\"\n\n",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 97,
+ "zIndex": 0,
+ "level": 1
+ },
+ {
+ "id": "5 total",
+ "type": "code",
+ "pos": {
+ "x": 912,
+ "y": 36
+ },
+ "width": 160,
+ "height": 102,
+ "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": "def hello():\n\n print \"world\"\n# 1 trailing\n# 2 trailing",
+ "fontSize": 16,
+ "fontFamily": "DEFAULT",
+ "language": "python",
+ "color": "N1",
+ "italic": false,
+ "bold": true,
+ "underline": false,
+ "labelWidth": 155,
+ "labelHeight": 97,
+ "zIndex": 0,
+ "level": 1
+ }
+ ],
+ "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/regression/code_trailing_newlines/elk/sketch.exp.svg b/e2etests/testdata/regression/code_trailing_newlines/elk/sketch.exp.svg
new file mode 100644
index 000000000..52c1f0ce1
--- /dev/null
+++ b/e2etests/testdata/regression/code_trailing_newlines/elk/sketch.exp.svg
@@ -0,0 +1,170 @@
+def hello():
+
+ print "world"
+
+
+
+
+def hello():
+
+ print "world"
+
+
+
+
+def hello():
+
+ print "world"
+# 1 trailing
+# 2 trailing
+# 3 trailing
+# 4 trailing
+# 5 trailingdef hello():
+
+ print "world"
+# 1 trailing
+# 2 trailing
+# 3 trailing
+# 4 trailing
+# 5 trailingdef hello():
+
+ print "world"
+def hello():
+
+ print "world"
+def hello():
+
+ print "world"
+# 1 trailingdef hello():
+
+ print "world"
+# 1 trailingdef hello():
+
+ print "world"
+
+def hello():
+
+ print "world"
+
+def hello():
+
+ print "world"
+# 1 trailing
+# 2 trailingdef hello():
+
+ print "world"
+# 1 trailing
+# 2 trailing
+
+
+
+
+
+
+
+
\ No newline at end of file