diff --git a/d2ir/compile.go b/d2ir/compile.go index 592e6efbb..b320dbd18 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -201,18 +201,7 @@ func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) switch n := n.(type) { case *Field: if n.Primary_ != nil { - refctx2 := refctx.Copy() - key2 := *refctx.Key - refctx.Key = &key2 - refctx2.Key.Value = d2ast.MakeValueBox(n.Primary_.Value) - // If the link is a board, we need to transform it into an absolute path. - if f.Name == "link" { - c.compileLink(refctx2) - } - f.Primary_ = &Scalar{ - parent: f, - Value: refctx2.Key.Value.ScalarBox().Unbox(), - } + f.Primary_ = n.Primary_.Copy(f).(*Scalar) } if n.Composite != nil { f.Composite = n.Composite.Copy(f).(Composite) @@ -238,6 +227,7 @@ func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) } } OverlayMap(f.Map(), n) + c.updateLinks(f.Map()) switch NodeBoardKind(f) { case BoardScenario, BoardStep: c.compileClasses(f.Map()) @@ -255,6 +245,24 @@ func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext) } } +func (c *compiler) updateLinks(m *Map) { + for _, f := range m.Fields { + if f.Name == "link" { + bida := BoardIDA(f) + aida := IDA(f) + if len(bida) != len(aida) { + prependIDA := aida[:len(aida)-len(bida)] + kp := d2ast.MakeKeyPath(prependIDA) + s := d2format.Format(kp) + "." + f.Primary_.Value.ScalarString() + f.Primary_.Value = d2ast.MakeValueBox(d2ast.RawString(s, true)).ScalarBox().Unbox() + } + } + if f.Map() != nil { + c.updateLinks(f.Map()) + } + } +} + func (c *compiler) compileLink(refctx *RefContext) { val := refctx.Key.Value.ScalarBox().Unbox().ScalarString() link, err := d2parser.ParseKey(val) diff --git a/e2etests-cli/main_test.go b/e2etests-cli/main_test.go index 5530b2cba..2b3f5b46c 100644 --- a/e2etests-cli/main_test.go +++ b/e2etests-cli/main_test.go @@ -410,7 +410,11 @@ steps: { writeFile(t, dir, "y.d2", `meow`) err := runTestMain(t, ctx, dir, env, filepath.Join(dir, "hello-world.d2")) assert.Success(t, err) - svg := readFile(t, dir, "hello-world.svg") + svg := readFile(t, dir, "hello-world/x/y.svg") + assert.Testdata(t, ".svg", svg) + svg = readFile(t, dir, "hello-world/x/index.svg") + assert.Testdata(t, ".svg", svg) + svg = readFile(t, dir, "hello-world/index.svg") assert.Testdata(t, ".svg", svg) }, }, diff --git a/e2etests-cli/testdata/TestCLI_E2E/board_import.exp.svg b/e2etests-cli/testdata/TestCLI_E2E/board_import.exp.svg new file mode 100644 index 000000000..a10fcfca5 --- /dev/null +++ b/e2etests-cli/testdata/TestCLI_E2E/board_import.exp.svg @@ -0,0 +1,110 @@ +x + + + + + + + + + + + + + + +