fix markdown nested variables

This commit is contained in:
Alexander Wang 2025-03-24 13:04:46 -06:00
parent 30f67a0e24
commit e3d7ad41b8
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 226 additions and 0 deletions

View file

@ -3258,6 +3258,22 @@ x: |md
tassert.True(t, strings.Contains(g.Objects[0].Attributes.Label.Value, "bye ${v}"))
},
},
{
name: "var_nested_in_markdown",
text: `vars: {
v: {
g: ok
}
}
x: |md
m${v.g}y
|
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.True(t, strings.Contains(g.Objects[0].Attributes.Label.Value, "moky"))
},
},
{
name: "var_in_vars",
text: `vars: {

View file

@ -380,6 +380,11 @@ func (c *compiler) collectVariables(vars *Map, variables map[string]string) {
if f.Primary() != nil {
variables[f.Name.ScalarString()] = f.Primary().Value.ScalarString()
} else if f.Map() != nil {
nestedVars := make(map[string]string)
c.collectVariables(f.Map(), nestedVars)
for k, v := range nestedVars {
variables[f.Name.ScalarString()+"."+k] = v
}
c.collectVariables(f.Map(), variables)
}
}

View file

@ -0,0 +1,205 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,0:0:0-9:0:50",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,0:0:0-4:1:28",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,0:0:0-0:4:4",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,0:0:0-0:4:4",
"value": [
{
"string": "vars",
"raw_string": "vars"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,0:6:6-4:1:28",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,1:2:10-3:3:26",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,1:2:10-1:3:11",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,1:2:10-1:3:11",
"value": [
{
"string": "v",
"raw_string": "v"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,1:5:13-3:3:26",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,2:2:17-2:7:22",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,2:2:17-2:3:18",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,2:2:17-2:3:18",
"value": [
{
"string": "g",
"raw_string": "g"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,2:5:20-2:7:22",
"value": [
{
"string": "ok",
"raw_string": "ok"
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,6:0:30-8:1:49",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,6:0:30-6:1:31",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,6:0:30-6:1:31",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"block_string": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,6:3:33-8:1:49",
"quote": "",
"tag": "md",
"value": "moky"
}
}
}
}
]
},
"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/var_nested_in_markdown.d2,6:0:30-6:1:31",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/var_nested_in_markdown.d2,6:0:30-6:1:31",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "moky"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"language": "markdown",
"shape": {
"value": "text"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}