silverbulletmd / silverbullet

The hackable notebook
https://silverbullet.md
MIT License
2.03k stars 140 forks source link

Querying on custom yaml for sub-items #825

Closed ColorfulAvacado closed 3 months ago

ColorfulAvacado commented 3 months ago

According to Objects manual page I can use code blocks to embed yaml data

I am playing around with this I was wondering if it is possible to work with nested yaml data as well.

Let's say I have this yaml block: (the backslashes are just to show how I am using the code block)

\```#test_data
simple_key1: val1
simple-key2: 2
list1: [1, 2, 3, 4]
list2:
  - 1
  - 2
  - 3
  - 4
object1:
  param1: val2
  param2: val2
\```

I can query it just fine using, for instance:

\```query
test_data
select list2
\```

The above query results one object named "list2" whose value is "1, 2, 3, 4"

However I am not able to query the sub-items of the list (nor of the object):

\```template
{{#each @i in {test_data select list2}}}
- {{@i}}
{{/each}}
\```

Expected result:

Current result:

I also tried the following syntax to access nested values, neither of which worked:

\```query
test_data
select object1.param1
\```
\```query
test_data
select object1[param1]
\```

Is there any means by which I can achieve this kind of behavior?

ColorfulAvacado commented 3 months ago

I figured it out, my mistake was that the query returns a list (in this case with one element), and the object I was trying to query was inside that previous list. One can use the provided "at" function to strip that out first and then access the objects as intended. So, while the code below did not work:

\```template
{{#each @i in {test_data select list2}}}
- {{@i}}
{{/each}}
\```

This does work as expected:

\```template
{{#each @i in at({test_data}, 0).list2}}
{{@i}}
{{/each}}
\```