validate markdown

This commit is contained in:
Alexander Wang 2023-03-07 09:00:45 -08:00
parent f161b47fb2
commit a556160306
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
5 changed files with 67 additions and 2 deletions

View file

@ -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 ⛑️

View file

@ -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 = "<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()

View file

@ -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",

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/unsemantic_markdown.d2,0:0:0-3:1:19",
"errmsg": "d2/testdata/d2compiler/TestCompile/unsemantic_markdown.d2:1:1: malformed Markdown: element <p> closed by </div>"
}
]
}
}

View file

@ -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 <br> closed by </p>"
}
]
}
}