diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index d3ac356a3..256afb7d9 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -4,6 +4,7 @@ - ELK nodes with > 1 connection grow to ensure padding around ports [#981](https://github.com/terrastruct/d2/pull/981) - Using a style keyword incorrectly in connections returns clear error message [#989](https://github.com/terrastruct/d2/pull/989) +- Unsemantic Markdown returns clear error message [#994](https://github.com/terrastruct/d2/pull/994) #### Bugfixes ⛑️ diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 6fa9106f3..729f2e46f 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -1,6 +1,7 @@ package d2compiler import ( + "encoding/xml" "fmt" "io" "net/url" @@ -15,6 +16,7 @@ import ( "oss.terrastruct.com/d2/d2ir" "oss.terrastruct.com/d2/d2parser" "oss.terrastruct.com/d2/d2target" + "oss.terrastruct.com/d2/lib/textmeasure" ) type CompileOptions struct { @@ -224,9 +226,27 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) { if ok { attrs.Language = fullTag } - if attrs.Language == "markdown" || attrs.Language == "latex" { + switch attrs.Language { + case "latex": attrs.Shape.Value = d2target.ShapeText - } else { + case "markdown": + rendered, err := textmeasure.RenderMarkdown(scalar.ScalarString()) + if err != nil { + c.errorf(f.LastPrimaryKey(), "malformed Markdown") + } + rendered = "
+| +`, + expErr: `d2/testdata/d2compiler/TestCompile/unsemantic_markdown.d2:1:1: malformed Markdown: element
closed by `,
+ },
+ {
+ name: "unsemantic_markdown_2",
+
+ text: `test:|
+foo
+bar
+|
+`,
+ expErr: `d2/testdata/d2compiler/TestCompile/unsemantic_markdown_2.d2:1:1: malformed Markdown: element
closed by
closed by "
+ }
+ ]
+ }
+}
diff --git a/testdata/d2compiler/TestCompile/unsemantic_markdown_2.exp.json b/testdata/d2compiler/TestCompile/unsemantic_markdown_2.exp.json
new file mode 100644
index 000000000..adfb7f026
--- /dev/null
+++ b/testdata/d2compiler/TestCompile/unsemantic_markdown_2.exp.json
@@ -0,0 +1,12 @@
+{
+ "graph": null,
+ "err": {
+ "ioerr": null,
+ "errs": [
+ {
+ "range": "d2/testdata/d2compiler/TestCompile/unsemantic_markdown_2.d2,0:0:0-3:1:20",
+ "errmsg": "d2/testdata/d2compiler/TestCompile/unsemantic_markdown_2.d2:1:1: malformed Markdown: element
closed by