validate markdown
This commit is contained in:
parent
f161b47fb2
commit
a556160306
5 changed files with 67 additions and 2 deletions
|
|
@ -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 ⛑️
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
||||
|
|
|
|||
12
testdata/d2compiler/TestCompile/unsemantic_markdown.exp.json
generated
vendored
Normal file
12
testdata/d2compiler/TestCompile/unsemantic_markdown.exp.json
generated
vendored
Normal 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>"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
12
testdata/d2compiler/TestCompile/unsemantic_markdown_2.exp.json
generated
vendored
Normal file
12
testdata/d2compiler/TestCompile/unsemantic_markdown_2.exp.json
generated
vendored
Normal 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>"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue