diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 1c83edf99..2e85a27be 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -270,7 +270,7 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) { // TODO: Delete instead. attrs.Label.Value = scalar.ScalarString() case *d2ast.BlockString: - if scalar.ScalarString() == "" { + if strings.TrimSpace(scalar.ScalarString()) == "" { c.errorf(f.LastPrimaryKey(), "block string cannot be empty") } attrs.Language = scalar.Tag diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index c7c6267da..3a73a9b08 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -611,9 +611,22 @@ x: { d2/testdata/d2compiler/TestCompile/md_block_string_err.d2:5:1: block string must be terminated with |`, }, { - name: "no_empty_block_strings", + name: "no_empty_block_string", text: `Text: |md |`, - expErr: `d2/testdata/d2compiler/TestCompile/no_empty_block_strings.d2:1:1: block string cannot be empty`, + expErr: `d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2:1:1: block string cannot be empty`, + }, + { + name: "no_white_spaces_only_block_string", + text: `Text: |md |`, + expErr: `d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2:1:1: block string cannot be empty`, + }, + { + name: "no_new_lines_only_block_string", + text: `Text: |md + + +|`, + expErr: `d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2:1:1: block string cannot be empty`, }, { name: "underscore_edge_existing", diff --git a/testdata/d2compiler/TestCompile/no_empty_block_string.exp.json b/testdata/d2compiler/TestCompile/no_empty_block_string.exp.json new file mode 100644 index 000000000..bf0a8c3a8 --- /dev/null +++ b/testdata/d2compiler/TestCompile/no_empty_block_string.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2,0:0:0-0:11:11", + "errmsg": "d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2:1:1: block string cannot be empty" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.exp.json b/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.exp.json new file mode 100644 index 000000000..2dff86029 --- /dev/null +++ b/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2,0:0:0-3:1:13", + "errmsg": "d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2:1:1: block string cannot be empty" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.exp.json b/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.exp.json new file mode 100644 index 000000000..ab68a6cc0 --- /dev/null +++ b/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2,0:0:0-0:16:16", + "errmsg": "d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2:1:1: block string cannot be empty" + } + ] + } +}