diff --git a/d2renderers/d2svg/appendix/appendix.go b/d2renderers/d2svg/appendix/appendix.go
index 3aeb5ab94..f25dc1c02 100644
--- a/d2renderers/d2svg/appendix/appendix.go
+++ b/d2renderers/d2svg/appendix/appendix.go
@@ -51,10 +51,10 @@ var viewboxRegex = regexp.MustCompile(`viewBox=\"([0-9\- ]+)\"`)
var widthRegex = regexp.MustCompile(`width=\"([0-9]+)\"`)
var heightRegex = regexp.MustCompile(`height=\"([0-9]+)\"`)
-func AppendTooltips(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []byte {
+func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []byte {
svg := string(in)
- appendix, w, h := generateTooltipAppendix(diagram, ruler, svg)
+ appendix, w, h := generateAppendix(diagram, ruler, svg)
if h == 0 {
return in
@@ -125,30 +125,37 @@ func AppendTooltips(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []by
svg = strings.Replace(svg, d2svg.TooltipIcon, generateNumberedIcon(i, 0, ICON_RADIUS), 1)
i++
}
+ if s.Link != "" {
+ svg = strings.Replace(svg, d2svg.LinkIcon, generateNumberedIcon(i, 0, ICON_RADIUS), 1)
+ i++
+ }
}
return []byte(svg)
}
-func generateTooltipAppendix(diagram *d2target.Diagram, ruler *textmeasure.Ruler, svg string) (string, int, int) {
+func generateAppendix(diagram *d2target.Diagram, ruler *textmeasure.Ruler, svg string) (string, int, int) {
tl, br := diagram.BoundingBox()
maxWidth, totalHeight := 0, 0
- var tooltipLines []string
+ var lines []string
i := 1
+
for _, s := range diagram.Shapes {
- if s.Tooltip != "" {
- line, w, h := generateTooltipLine(i, br.Y+(PAD_TOP*2)+totalHeight, s.Tooltip, ruler)
- i++
- tooltipLines = append(tooltipLines, line)
- maxWidth = go2.IntMax(maxWidth, w)
- totalHeight += h + SPACER
+ for _, txt := range []string{s.Tooltip, s.Link} {
+ if txt != "" {
+ line, w, h := generateLine(i, br.Y+(PAD_TOP*2)+totalHeight, txt, ruler)
+ i++
+ lines = append(lines, line)
+ maxWidth = go2.IntMax(maxWidth, w)
+ totalHeight += h + SPACER
+ }
}
}
return fmt.Sprintf(`%s
-`, tl.X, br.Y, (br.X - tl.X), strings.Join(tooltipLines, "\n")), maxWidth, totalHeight
+`, tl.X, br.Y, (br.X - tl.X), strings.Join(lines, "\n")), maxWidth, totalHeight
}
func generateNumberedIcon(i, x, y int) string {
@@ -161,7 +168,7 @@ func generateNumberedIcon(i, x, y int) string {
return line
}
-func generateTooltipLine(i, y int, text string, ruler *textmeasure.Ruler) (string, int, int) {
+func generateLine(i, y int, text string, ruler *textmeasure.Ruler) (string, int, int) {
mtext := &d2target.MText{
Text: text,
FontSize: FONT_SIZE,
diff --git a/d2renderers/d2svg/appendix/appendix_test.go b/d2renderers/d2svg/appendix/appendix_test.go
index a589498cf..a4105c38e 100644
--- a/d2renderers/d2svg/appendix/appendix_test.go
+++ b/d2renderers/d2svg/appendix/appendix_test.go
@@ -75,6 +75,13 @@ payment processor behind the scenes: {
acquirer -> store bank: '$10 credit'
}
}
+`,
+ },
+ {
+ name: "links",
+ script: `x: { link: https://d2lang.com }
+ y: { link: https://terrastruct.com; tooltip: Gee, I feel kind of LIGHT in the head now,\nknowing I can't make my satellite dish PAYMENTS! }
+x -> y
`,
},
}
@@ -127,7 +134,7 @@ func run(t *testing.T, tc testCase) {
Pad: d2svg.DEFAULT_PADDING,
})
assert.Success(t, err)
- svgBytes = appendix.AppendTooltips(diagram, ruler, svgBytes)
+ svgBytes = appendix.Append(diagram, ruler, svgBytes)
err = os.MkdirAll(dataPath, 0755)
assert.Success(t, err)
diff --git a/d2renderers/d2svg/appendix/testdata/diagram_wider_than_tooltip/sketch.exp.svg b/d2renderers/d2svg/appendix/testdata/diagram_wider_than_tooltip/sketch.exp.svg
index 268e98514..6c5e1e58b 100644
--- a/d2renderers/d2svg/appendix/testdata/diagram_wider_than_tooltip/sketch.exp.svg
+++ b/d2renderers/d2svg/appendix/testdata/diagram_wider_than_tooltip/sketch.exp.svg
@@ -18,7 +18,7 @@ width="1959" height="2297" viewBox="-175 -47 1959 2297">customerissuerstore1Like starbucks or somethingacquirer2I'm not sure what this isnetworkcustomer bankstore bankinitial transactionpayment processor behind the scenessimplified 1 banana please$10 dollarsthinking: wow, inflationchecks bank accountSavings: $11I can do that, here's my cardRun this cardProcess to card issuerProcess this payment$10 debit$10 creditAn error in judgement is about to occur
+customerissuerstore1Like starbucks or somethingacquirer2I'm not sure what this isnetworkcustomer bankstore bankinitial transactionpayment processor behind the scenessimplified 1 banana please$10 dollarsthinking: wow, inflationchecks bank accountSavings: $11I can do that, here's my cardRun this cardProcess to card issuerProcess this payment$10 debit$10 creditAn error in judgement is about to occur
@@ -42,7 +42,7 @@ width="1959" height="2297" viewBox="-175 -47 1959 2297">1Like starbucks or something
-2I'm not sure what this is
+}]]>1Like starbucks or something
+2I'm not sure what this is
x1y2Gee, I feel kind of LIGHT in the head now,
+knowing I can't make my satellite dish PAYMENTS!3
+
+
+1https://d2lang.com
+2Gee, I feel kind of LIGHT in the head now,knowing I can't make my satellite dish PAYMENTS!
+3https://terrastruct.com
+
\ No newline at end of file
diff --git a/d2renderers/d2svg/appendix/testdata/tooltip_wider_than_diagram/sketch.exp.svg b/d2renderers/d2svg/appendix/testdata/tooltip_wider_than_diagram/sketch.exp.svg
index 8c6640ce4..b2b23e46f 100644
--- a/d2renderers/d2svg/appendix/testdata/tooltip_wider_than_diagram/sketch.exp.svg
+++ b/d2renderers/d2svg/appendix/testdata/tooltip_wider_than_diagram/sketch.exp.svg
@@ -18,12 +18,12 @@ width="564" height="700" viewBox="-100 -100 564 700">x1Total abstinence is easier than perfect moderationy2Gee, I feel kind of LIGHT in the head now,
+x1Total abstinence is easier than perfect moderationy2Gee, I feel kind of LIGHT in the head now,
knowing I can't make my satellite dish PAYMENTS!
1Total abstinence is easier than perfect moderation
-2Gee, I feel kind of LIGHT in the head now,knowing I can't make my satellite dish PAYMENTS!
+}]]>1Total abstinence is easier than perfect moderation
+2Gee, I feel kind of LIGHT in the head now,knowing I can't make my satellite dish PAYMENTS!