Merge pull request #1606 from alixander/nested-import

Fix link paths in nested imports
This commit is contained in:
Alexander Wang 2023-09-25 11:55:31 -07:00 committed by GitHub
commit 2490b449e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 1270 additions and 2 deletions

View file

@ -5,6 +5,7 @@
#### Improvements 🧹
- Globs are lazily-evaluated [#1552](https://github.com/terrastruct/d2/pull/1552)
- Latex now includes Mathjax's ASM extension for more powerful Latex functionality [#1544](https://github.com/terrastruct/d2/pull/1544)
- `font-color` works on Markdown [#1546](https://github.com/terrastruct/d2/pull/1546)
- `font-color` works on arrowheads [#1582](https://github.com/terrastruct/d2/pull/1582)

View file

@ -833,8 +833,35 @@ func (c *compiler) updateLinks(m *Map) {
aida := IDA(f)
if len(bida) != len(aida) {
prependIDA := aida[:len(aida)-len(bida)]
kp := d2ast.MakeKeyPath(prependIDA)
s := d2format.Format(kp) + strings.TrimPrefix(f.Primary_.Value.ScalarString(), "root")
fullIDA := []string{"root"}
// With nested imports, a value may already have been updated with part of the absolute path
// E.g.,
// The import prepends path a b c
// The existing path is b c d
// So the new path is
// a b c
// b c d
// -------
// a b c d
OUTER:
for i := 1; i < len(prependIDA); i += 2 {
for j := 0; i+j < len(prependIDA); j++ {
if prependIDA[i+j] != linkIDA[1+j] {
break
}
// Reached the end and all common
if i+j == len(prependIDA)-1 {
break OUTER
}
}
fullIDA = append(fullIDA, prependIDA[i])
fullIDA = append(fullIDA, prependIDA[i+1])
}
// Chop off "root"
fullIDA = append(fullIDA, linkIDA[1:]...)
kp := d2ast.MakeKeyPath(fullIDA)
s := d2format.Format(kp)
f.Primary_.Value = d2ast.MakeValueBox(d2ast.FlatUnquotedString(s)).ScalarBox().Unbox()
}
}

View file

@ -84,6 +84,19 @@ label: meow`,
assertQuery(t, m, 0, 0, "root.layers.x.layers.y", "layers.x.y.link")
},
},
{
name: "boards-deep",
run: func(t testing.TB) {
m, err := compileFS(t, "index.d2", map[string]string{
"index.d2": `a.link: layers.b; layers: { b: @b }`,
"b.d2": `b.link: layers.c; layers: { c: @c }`,
"c.d2": `c.link: layers.d; layers: { d: @d }`,
"d.d2": `d`,
})
assert.Success(t, err)
assertQuery(t, m, 0, 0, "root.layers.b.layers.c.layers.d", "layers.b.layers.c.c.link")
},
},
{
name: "steps-inheritence",
run: func(t testing.TB) {

1227
testdata/d2ir/TestCompile/imports/boards-deep.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff