Skip to content

Commit

Permalink
Merge pull request #2387 from alixander/legend-label
Browse files Browse the repository at this point in the history
add legend-label
  • Loading branch information
alixander authored Feb 26, 2025
2 parents 9ce6595 + 92390b5 commit 024a99b
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 1 deletion.
1 change: 1 addition & 0 deletions d2ast/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var ReservedKeywords map[string]struct{}
// Non Style/Holder keywords.
var SimpleReservedKeywords = map[string]struct{}{
"label": {},
"legend-label": {},
"shape": {},
"icon": {},
"constraint": {},
Expand Down
4 changes: 4 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
attrs.Tooltip = &d2graph.Scalar{}
attrs.Tooltip.Value = scalar.ScalarString()
attrs.Tooltip.MapKey = f.LastPrimaryKey()
case "legend-label":
attrs.LegendLabel = &d2graph.Scalar{}
attrs.LegendLabel.Value = scalar.ScalarString()
attrs.LegendLabel.MapKey = f.LastPrimaryKey()
case "width":
_, err := strconv.Atoi(scalar.ScalarString())
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5433,6 +5433,23 @@ b -> c
assert.Equal(t, "red", g.Edges[0].Style.Stroke.Value)
},
},
{
name: "legend-label",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
a.legend-label: This is A
b: {legend-label: This is B}
a -> b: {
legend-label: "This is a->b"
}
`, ``)
assert.Equal(t, "a", g.Objects[0].ID)
assert.Equal(t, "This is A", g.Objects[0].LegendLabel.Value)
assert.Equal(t, "b", g.Objects[1].ID)
assert.Equal(t, "This is B", g.Objects[1].LegendLabel.Value)
assert.Equal(t, "This is a->b", g.Edges[0].LegendLabel.Value)
},
},
}

for _, tc := range tca {
Expand Down
3 changes: 3 additions & 0 deletions d2exporter/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func toShape(obj *d2graph.Object, g *d2graph.Graph) d2target.Shape {
if obj.Tooltip != nil {
shape.Tooltip = obj.Tooltip.Value
}
if obj.LegendLabel != nil {
shape.LegendLabel = obj.LegendLabel.Value
}
if obj.Style.Animated != nil {
shape.Animated, _ = strconv.ParseBool(obj.Style.Animated.Value)
}
Expand Down
3 changes: 2 additions & 1 deletion d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ type Attributes struct {

// These names are attached to the rendered elements in SVG
// so that users can target them however they like outside of D2
Classes []string `json:"classes,omitempty"`
Classes []string `json:"classes,omitempty"`
LegendLabel *Scalar `json:"legendLabel,omitempty"`
}

// ApplyTextTransform will alter the `Label.Value` of the current object based
Expand Down
2 changes: 2 additions & 0 deletions d2target/d2target.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ type Shape struct {
PrettyLink string `json:"prettyLink,omitempty"`
Icon *url.URL `json:"icon"`
IconPosition string `json:"iconPosition"`
LegendLabel string `json:"legendLabel,omitempty"`

// Whether the shape should allow shapes behind it to bleed through
// Currently just used for sequence diagram groups
Expand Down Expand Up @@ -613,6 +614,7 @@ type Connection struct {

Animated bool `json:"animated"`
Tooltip string `json:"tooltip"`
LegendLabel string `json:"legendLabel,omitempty"`
Icon *url.URL `json:"icon"`
IconPosition string `json:"iconPosition,omitempty"`

Expand Down
Loading

0 comments on commit 024a99b

Please sign in to comment.