Skip to content

Commit

Permalink
Merge pull request #2398 from alixander/md-shape
Browse files Browse the repository at this point in the history
patch md label setting
  • Loading branch information
alixander authored Mar 2, 2025
2 parents 42e11d1 + 16b0c89 commit 60ed1dc
Show file tree
Hide file tree
Showing 4 changed files with 871 additions and 15 deletions.
31 changes: 19 additions & 12 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph {
ir = ir.Copy(nil).(*d2ir.Map)
// c.preprocessSeqDiagrams(ir)
c.compileMap(g.Root, ir)
c.setDefaultShapes(g)
if len(c.err.Errors) == 0 {
c.validateKeys(g.Root, ir)
}
Expand Down Expand Up @@ -356,14 +357,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
parent := obj
obj = obj.EnsureChild(([]d2ast.String{f.Name}))
if f.Primary() != nil {
var s string
if obj.OuterSequenceDiagram() != nil {
s = obj.Attributes.Shape.Value
}
c.compileLabel(&obj.Attributes, f)
if s != "" {
obj.Attributes.Shape.Value = s
}
}
if f.Map() != nil {
c.compileMap(obj, f.Map())
Expand Down Expand Up @@ -410,8 +404,6 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
attrs.Language = fullTag
}
switch attrs.Language {
case "latex":
attrs.Shape.Value = d2target.ShapeText
case "markdown":
rendered, err := textmeasure.RenderMarkdown(scalar.ScalarString())
if err != nil {
Expand All @@ -428,9 +420,6 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
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()
default:
Expand Down Expand Up @@ -1577,3 +1566,21 @@ FOR:
}
return nil, nil
}

func (c *compiler) setDefaultShapes(g *d2graph.Graph) {
for _, obj := range g.Objects {
if obj.Shape.Value == "" {
if obj.OuterSequenceDiagram() != nil {
obj.Shape.Value = d2target.ShapeRectangle
} else if obj.Language == "latex" {
obj.Shape.Value = d2target.ShapeText
} else if obj.Language == "markdown" {
obj.Shape.Value = d2target.ShapeText
} else if obj.Language != "" {
obj.Shape.Value = d2target.ShapeCode
} else {
obj.Shape.Value = d2target.ShapeRectangle
}
}
}
}
28 changes: 28 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5605,6 +5605,34 @@ a -> c
tassert.Equal(t, (*d2graph.Scalar)(nil), g.Edges[2].Style.StrokeWidth)
},
},
{
name: "md-shape",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
a.shape: circle
a: |md #hi |
b.shape: circle
b.label: |md #hi |
c: |md #hi |
c.shape: circle
d.label: |md #hi |
d.shape: circle
e: {
shape: circle
label: |md #hi |
}
`, ``)
tassert.Equal(t, 5, len(g.Objects))
for _, obj := range g.Objects {
tassert.Equal(t, "circle", obj.Shape.Value, "Object "+obj.ID+" should have circle shape")
tassert.Equal(t, "markdown", obj.Language, "Object "+obj.ID+" should have md language")
}
},
},
}

for _, tc := range tca {
Expand Down
3 changes: 0 additions & 3 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,6 @@ func (obj *Object) newObject(ids d2ast.String) *Object {
Label: Scalar{
Value: idval,
},
Shape: Scalar{
Value: d2target.ShapeRectangle,
},
},

Graph: obj.Graph,
Expand Down
Loading

0 comments on commit 60ed1dc

Please sign in to comment.