Merge pull request #2434 from alixander/relative-url

d2ir: allow root URL paths
This commit is contained in:
Alexander Wang 2025-03-20 09:06:43 -07:00 committed by GitHub
commit 4a73ca9473
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 169 additions and 3 deletions

View file

@ -28,6 +28,8 @@
- d2cli:
- Support `validate` command. [#2415](https://github.com/terrastruct/d2/pull/2415)
- d2compiler:
- `link`s can be set to root path, e.g. `/xyz`. [#2357](https://github.com/terrastruct/d2/issues/2357)
#### Bugfixes ⛑️

View file

@ -1265,7 +1265,7 @@ func (c *compiler) validateBoardLinks(g *d2graph.Graph) {
}
u, err := url.Parse(html.UnescapeString(obj.Link.Value))
isRemote := err == nil && u.Scheme != ""
isRemote := err == nil && (u.Scheme != "" || strings.HasPrefix(u.Path, "/"))
if isRemote {
continue
}

View file

@ -1658,6 +1658,22 @@ x -> y: {
}
},
},
{
name: "url_relative_link",
text: `x: {
link: /google
}
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
if len(g.Objects) != 1 {
t.Fatal(g.Objects)
}
if g.Objects[0].Link.Value != "/google" {
t.Fatal(g.Objects[0].Link.Value)
}
},
},
{
name: "non_url_link",

View file

@ -1146,7 +1146,7 @@ func (c *compiler) extendLinks(m *Map, importF *Field, importDir string) {
val := f.Primary().Value.ScalarString()
u, err := url.Parse(html.UnescapeString(val))
isRemote := err == nil && u.Scheme != ""
isRemote := err == nil && (u.Scheme != "" || strings.HasPrefix(u.Path, "/"))
if isRemote {
continue
}
@ -1184,7 +1184,7 @@ func (c *compiler) extendLinks(m *Map, importF *Field, importDir string) {
continue
}
u, err := url.Parse(html.UnescapeString(val))
isRemoteImg := err == nil && u.Scheme != ""
isRemoteImg := err == nil && (u.Scheme != "" || strings.HasPrefix(u.Path, "/"))
if isRemoteImg {
continue
}

View file

@ -0,0 +1,148 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,0:0:0-3:0:23",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,0:0:0-2:1:22",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,0:3:3-2:1:22",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,1:2:7-1:15:20",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,1:2:7-1:6:11",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,1:2:7-1:6:11",
"value": [
{
"string": "link",
"raw_string": "link"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,1:8:13-1:15:20",
"value": [
{
"string": "/google",
"raw_string": "/google"
}
]
}
}
}
}
]
}
}
}
}
]
},
"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": "x",
"id_val": "x",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/url_relative_link.d2,0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "x"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"link": {
"value": "/google"
},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}