Skip to content

Commit

Permalink
Merge pull request #994 from alixander/unsemantic-markdown
Browse files Browse the repository at this point in the history
validate markdown
  • Loading branch information
alixander authored Mar 7, 2023
2 parents f161b47 + a556160 commit 3475d10
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ⛑️

Expand Down
24 changes: 22 additions & 2 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package d2compiler

import (
"encoding/xml"
"fmt"
"io"
"net/url"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 = "<div>" + rendered + "</div>"
var xmlParsed interface{}
err = xml.Unmarshal([]byte(rendered), &xmlParsed)
if err != nil {
switch xmlErr := err.(type) {
case *xml.SyntaxError:
c.errorf(f.LastPrimaryKey(), "malformed Markdown: %s", xmlErr.Msg)
default:
c.errorf(f.LastPrimaryKey(), "malformed Markdown: %s", err.Error())
}
}
attrs.Shape.Value = d2target.ShapeText
default:
attrs.Shape.Value = d2target.ShapeCode
}
attrs.Label.Value = scalar.ScalarString()
Expand Down
20 changes: 20 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,26 @@ b.(x -> y)[0]: two
}
},
},
{
name: "unsemantic_markdown",

text: `test:|
foobar
<p>
|
`,
expErr: `d2/testdata/d2compiler/TestCompile/unsemantic_markdown.d2:1:1: malformed Markdown: element <p> closed by </div>`,
},
{
name: "unsemantic_markdown_2",

text: `test:|
foo<br>
bar
|
`,
expErr: `d2/testdata/d2compiler/TestCompile/unsemantic_markdown_2.d2:1:1: malformed Markdown: element <br> closed by </p>`,
},
{
name: "edge_map",

Expand Down
12 changes: 12 additions & 0 deletions testdata/d2compiler/TestCompile/unsemantic_markdown.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions testdata/d2compiler/TestCompile/unsemantic_markdown_2.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3475d10

Please sign in to comment.