diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index fcbba795f9..7a1f11e20d 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -31,3 +31,4 @@ - CLI: fixes theme flag not being passed to GIF outputs [#2071](https://github.com/terrastruct/d2/pull/2071) - CLI: fixes scale flag not being passed to animated SVG outputs [#2071](https://github.com/terrastruct/d2/pull/2071) - CLI: pptx exports use theme flags correctly [#2099](https://github.com/terrastruct/d2/pull/2099) +- Imports: importing files with url links is fixed [#2105](https://github.com/terrastruct/d2/pull/2105) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 662fe1062b..fe842e2d96 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1504,6 +1504,25 @@ x -> y: { } }, }, + { + name: "import_url_link", + + text: `...@test +`, + files: map[string]string{ + "test.d2": `elem: elem { + link: https://google.com +}`, + }, + assertions: func(t *testing.T, g *d2graph.Graph) { + if len(g.Objects) != 1 { + t.Fatal(g.Objects) + } + if g.Objects[0].Link.Value != "https://google.com" { + t.Fatal(g.Objects[0].Link.Value) + } + }, + }, { name: "url_tooltip", text: `x: {tooltip: https://google.com}`, diff --git a/d2ir/compile.go b/d2ir/compile.go index d1dd2e7341..8fb31aad95 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -945,6 +945,13 @@ func (c *compiler) extendLinks(m *Map, importF *Field, importDir string) { continue } val := f.Primary().Value.ScalarString() + + u, err := url.Parse(html.UnescapeString(val)) + isRemote := err == nil && strings.HasPrefix(u.Scheme, "http") + if isRemote { + continue + } + link, err := d2parser.ParseKey(val) if err != nil { continue diff --git a/testdata/d2compiler/TestCompile/import_url_link.exp.json b/testdata/d2compiler/TestCompile/import_url_link.exp.json new file mode 100644 index 0000000000..66f878da6c --- /dev/null +++ b/testdata/d2compiler/TestCompile/import_url_link.exp.json @@ -0,0 +1,106 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/import_url_link.d2,0:0:0-1:0:9", + "nodes": [ + { + "import": { + "range": "d2/testdata/d2compiler/TestCompile/import_url_link.d2,0:0:0-0:8:8", + "spread": true, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/import_url_link.d2,0:4:4-0:8:8", + "value": [ + { + "string": "test", + "raw_string": "test" + } + ] + } + } + ] + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "elem", + "id_val": "elem", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/test.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/test.d2,0:0:0-0:4:4", + "value": [ + { + "string": "elem", + "raw_string": "elem" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "elem" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "link": { + "value": "https://google.com" + }, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": null +}