Skip to content

Commit

Permalink
Merge pull request #1955 from danielsuh05/issue1827
Browse files Browse the repository at this point in the history
feature: supports link on connections
  • Loading branch information
alixander authored Dec 14, 2024
2 parents d826be0 + 5b1122c commit 557477a
Show file tree
Hide file tree
Showing 844 changed files with 55,787 additions and 51,289 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#### Features 🚀

- Connections now support `link` [#1955](https://github.com/terrastruct/d2/pull/1955)
- Vars: vars in markdown blocks are substituted [#2218](https://github.com/terrastruct/d2/pull/2218)
- Markdown: Github-flavored tables work in `md` blocks [#2221](https://github.com/terrastruct/d2/pull/2221)

Expand Down
7 changes: 7 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,13 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
case "classes":
}

if attrs.Link != nil && attrs.Label.Value != "" {
u, err := url.ParseRequestURI(attrs.Label.Value)
if err == nil && u.Host != "" {
c.errorf(scalar, "Label cannot be set to URL when link is also set (for security)")
}
}

if attrs.Link != nil && attrs.Tooltip != nil {
u, err := url.ParseRequestURI(attrs.Tooltip.Value)
if err == nil && u.Host != "" {
Expand Down
21 changes: 21 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,27 @@ a.style.fill: null
}
},
},
{
name: "no_url_link_and_path_url_label_concurrently",
text: `x -> y: https://google.com {link: https://not-google.com }`,
expErr: `d2/testdata/d2compiler/TestCompile/no_url_link_and_path_url_label_concurrently.d2:1:35: Label cannot be set to URL when link is also set (for security)`,
},
{
name: "url_link_and_path_url_label_concurrently",
text: `x -> y: hello world {link: https://google.com}`,
assertions: func(t *testing.T, g *d2graph.Graph) {
if len(g.Edges) != 1 {
t.Fatal(len(g.Edges))
}
if g.Edges[0].Link.Value != "https://google.com" {
t.Fatal(g.Edges[0].Link.Value)
}

if g.Edges[0].Label.Value != "hello world" {
t.Fatal(g.Edges[0].Label.Value)
}
},
},
{
name: "nil_scope_obj_regression",

Expand Down
3 changes: 3 additions & 0 deletions d2exporter/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection
if edge.Style.Font != nil {
connection.FontFamily = edge.Style.Font.Value
}
if edge.Link != nil {
connection.Link = edge.Link.Value
}
connection.Label = text.Text
connection.LabelWidth = text.Dimensions.Width
connection.LabelHeight = text.Dimensions.Height
Expand Down
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/all_shapes/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/all_shapes_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/animated/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/animated_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/arrowheads/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/arrowheads_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/basic/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/basic_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/child_to_child/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/child_to_child_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/connection-style-fill/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/connection_label/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/connection_label_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/crows_feet/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/crows_feet_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/dots-all/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/dots-multiple/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/dots-real/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/elk_corners/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/long_arrowhead_label/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
568 changes: 284 additions & 284 deletions d2renderers/d2sketch/testdata/opacity/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
568 changes: 284 additions & 284 deletions d2renderers/d2sketch/testdata/opacity_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/paper-real/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
562 changes: 281 additions & 281 deletions d2renderers/d2sketch/testdata/root-fill/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/sql_tables/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
154 changes: 77 additions & 77 deletions d2renderers/d2sketch/testdata/sql_tables_dark/sketch.exp.svg

Large diffs are not rendered by default.

166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/terminal/sketch.exp.svg

Large diffs are not rendered by default.

568 changes: 284 additions & 284 deletions d2renderers/d2sketch/testdata/twitter/sketch.exp.svg

Large diffs are not rendered by default.

568 changes: 284 additions & 284 deletions d2renderers/d2sketch/testdata/twitter_dark/sketch.exp.svg

Large diffs are not rendered by default.

160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/unfilled_triangle/sketch.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/appendix/testdata/links/sketch.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/appendix/testdata/links_dark/sketch.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/appendix/testdata/tooltip_fill/sketch.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion d2renderers/d2svg/d2svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,23 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co
textEl := d2themes.NewThemableElement("text", inlineTheme)
textEl.X = labelTL.X + float64(connection.LabelWidth)/2
textEl.Y = labelTL.Y + float64(connection.FontSize)
textEl.Fill = connection.GetFontColor()
textEl.ClassName = fontClass
textEl.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx", "middle", connection.FontSize)
textEl.Content = RenderText(connection.Label, textEl.X, float64(connection.LabelHeight))

if connection.Link != "" {
textEl.ClassName += " text-underline text-link"

fmt.Fprintf(writer, `<a href="%s" xlink:href="%[1]s">`, svg.EscapeText(connection.Link))
} else {
textEl.Fill = connection.GetFontColor()
}

fmt.Fprint(writer, textEl.Render())

if connection.Link != "" {
fmt.Fprintf(writer, "</a>")
}
}

if connection.SrcLabel != nil && connection.SrcLabel.Label != "" {
Expand Down Expand Up @@ -1590,6 +1602,22 @@ func EmbedFonts(buf *bytes.Buffer, diagramHash, source string, fontFamily *d2fon
}`,
)

appendOnTrigger(
buf,
source,
[]string{
`text-link`,
},
`
.text-link {
fill: blue;
}
.text-link:visited {
fill: purple;
}`,
)

appendOnTrigger(
buf,
source,
Expand Down
152 changes: 76 additions & 76 deletions d2renderers/d2svg/dark_theme/testdata/all_shapes/dark_theme.exp.svg

Large diffs are not rendered by default.

158 changes: 79 additions & 79 deletions d2renderers/d2svg/dark_theme/testdata/animated/dark_theme.exp.svg

Large diffs are not rendered by default.

158 changes: 79 additions & 79 deletions d2renderers/d2svg/dark_theme/testdata/arrowheads/dark_theme.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/dark_theme/testdata/basic/dark_theme.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

568 changes: 284 additions & 284 deletions d2renderers/d2svg/dark_theme/testdata/code/dark_theme.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

568 changes: 284 additions & 284 deletions d2renderers/d2svg/dark_theme/testdata/opacity/dark_theme.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/dark_theme/testdata/sql_tables/dark_theme.exp.svg

Large diffs are not rendered by default.

568 changes: 284 additions & 284 deletions d2renderers/d2svg/dark_theme/testdata/twitter/dark_theme.exp.svg

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions d2target/d2target.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,9 @@ type Connection struct {
LabelPosition string `json:"labelPosition"`
LabelPercentage float64 `json:"labelPercentage"`

Link string `json:"link"`
PrettyLink string `json:"prettyLink,omitempty"`

Route []*geo.Point `json:"route"`
IsCurve bool `json:"isCurve,omitempty"`

Expand Down
152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/abspath.exp.svg

Large diffs are not rendered by default.

180 changes: 90 additions & 90 deletions e2etests-cli/testdata/TestCLI_E2E/animation.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/center.exp.svg

Large diffs are not rendered by default.

160 changes: 80 additions & 80 deletions e2etests-cli/testdata/TestCLI_E2E/empty-base.exp.svg

Large diffs are not rendered by default.

Binary file modified e2etests-cli/testdata/TestCLI_E2E/hello_world_png.exp.png
Binary file modified e2etests-cli/testdata/TestCLI_E2E/hello_world_png_pad.exp.png
Binary file modified e2etests-cli/testdata/TestCLI_E2E/hello_world_png_sketch.exp.png
152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/import.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/import_vars.exp.svg

Large diffs are not rendered by default.

Binary file modified e2etests-cli/testdata/TestCLI_E2E/internal_linked_pdf.exp.pdf
Binary file not shown.
152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/multiboard/life/index.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Binary file modified e2etests-cli/testdata/TestCLI_E2E/no-nav-pdf.exp.pdf
Binary file not shown.
Binary file modified e2etests-cli/testdata/TestCLI_E2E/no-nav-pptx.exp.pptx
Binary file not shown.
Binary file modified e2etests-cli/testdata/TestCLI_E2E/pptx-theme-overrides.exp.pptx
Binary file not shown.
Binary file modified e2etests-cli/testdata/TestCLI_E2E/renamed-board.exp.pdf
Binary file not shown.
152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/sequence-layer/index.exp.svg
Loading

0 comments on commit 557477a

Please sign in to comment.