Skip to content

Commit

Permalink
d2ir: extend icon links
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Aug 25, 2024
1 parent 8d60c92 commit 098a27c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 85 deletions.
4 changes: 4 additions & 0 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1431,3 +1431,7 @@ func (i *Import) PathWithPre() string {
}
return path.Join(i.Pre, i.Path[0].Unbox().ScalarString())
}

func (i *Import) Dir() string {
return path.Dir(i.PathWithPre())
}
27 changes: 21 additions & 6 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package d2ir

import (
"html"
"io/fs"
"net/url"
"path"
"strconv"
"strings"

Expand Down Expand Up @@ -558,7 +561,8 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}

OverlayMap(dst, impn.Map())
c.extendLinks(dst, ParentField(dst))
impDir := n.Import.Dir()
c.extendLinks(dst, ParentField(dst), impDir)

if impnf, ok := impn.(*Field); ok {
if impnf.Primary_ != nil {
Expand Down Expand Up @@ -862,7 +866,8 @@ func (c *compiler) _compileField(f *Field, refctx *RefContext) {
}
}
OverlayMap(f.Map(), n)
c.extendLinks(f.Map(), f)
impDir := refctx.Key.Value.Import.Dir()
c.extendLinks(f.Map(), f, impDir)
switch NodeBoardKind(f) {
case BoardScenario, BoardStep:
c.overlayClasses(f.Map())
Expand Down Expand Up @@ -895,9 +900,10 @@ func (c *compiler) ignoreLazyGlob(n Node) bool {
return false
}

// When importing a file, all of its board links need to be extended to reflect their new path
func (c *compiler) extendLinks(m *Map, importF *Field) {
// When importing a file, all of its board and icon links need to be extended to reflect their new path
func (c *compiler) extendLinks(m *Map, importF *Field, importDir string) {
nodeBoardKind := NodeBoardKind(m)
importIDA := IDA(importF)
for _, f := range m.Fields {
if f.Name == "link" {
if nodeBoardKind != "" {
Expand All @@ -914,14 +920,23 @@ func (c *compiler) extendLinks(m *Map, importF *Field) {
continue
}

importIDA := IDA(importF)
extendedIDA := append(importIDA, linkIDA[1:]...)
kp := d2ast.MakeKeyPath(extendedIDA)
s := d2format.Format(kp)
f.Primary_.Value = d2ast.MakeValueBox(d2ast.FlatUnquotedString(s)).ScalarBox().Unbox()
}
if f.Name == "icon" {
val := f.Primary().Value.ScalarString()
u, err := url.Parse(html.UnescapeString(val))
isRemoteImg := err == nil && strings.HasPrefix(u.Scheme, "http")
if isRemoteImg {
continue
}
val = path.Join(importDir, val)
f.Primary_.Value = d2ast.MakeValueBox(d2ast.FlatUnquotedString(val)).ScalarBox().Unbox()
}
if f.Map() != nil {
c.extendLinks(f.Map(), importF)
c.extendLinks(f.Map(), importF, importDir)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,9 @@ steps: {
name: "import_icon_relative",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "hello-world.d2", `...@asdf/x`)
writeFile(t, filepath.Join(dir, "asdf"), "x.d2", `y: { icon: ./blah.svg }`)
writeFile(t, filepath.Join(dir, "asdf"), "x.d2", `y: { icon: ./blah.svg }; z: { icon: ../root.svg }`)
writeFile(t, filepath.Join(dir, "asdf"), "blah.svg", ``)
writeFile(t, dir, "root.svg", ``)
err := runTestMain(t, ctx, dir, env, filepath.Join(dir, "hello-world.d2"))
assert.Success(t, err)
svg := readFile(t, dir, "hello-world.svg")
Expand Down
Loading

0 comments on commit 098a27c

Please sign in to comment.