From e3d7ad41b8e1fb8e305c44ac43528d9b9aa210c6 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 24 Mar 2025 13:04:46 -0600 Subject: [PATCH] fix markdown nested variables --- d2compiler/compile_test.go | 16 ++ d2ir/compile.go | 5 + .../var_nested_in_markdown.exp.json | 205 ++++++++++++++++++ 3 files changed, 226 insertions(+) create mode 100644 testdata/d2compiler/TestCompile/var_nested_in_markdown.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 5135be5a8..5b44dcfe1 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -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: { diff --git a/d2ir/compile.go b/d2ir/compile.go index 68e19b6da..3a2e2996c 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -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) } } diff --git a/testdata/d2compiler/TestCompile/var_nested_in_markdown.exp.json b/testdata/d2compiler/TestCompile/var_nested_in_markdown.exp.json new file mode 100644 index 000000000..ae3af4a94 --- /dev/null +++ b/testdata/d2compiler/TestCompile/var_nested_in_markdown.exp.json @@ -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 +}