I’ve laboriously trialled-and-error’d the following terrible query to do the standard “find pages tagged such and such”:
${query[[
from index.tag "page"
where table.includes(tags, "draft") or _.draft
order by lastModified desc
select [[ \[\[]] .. _.name .. [[\]\] ]]
]]}
Note the absolutely horrifying string interpolation in the select statement.
This doesn’t work as intended; the links are never rendered, but rather printed as strings, e.g:
- [[foo]]
- [[bar]]
I assume there’s a way to apply rendering, but it’s not mentioned in the article on Lua Integrated Queries and the closest similar question I can find in the forums is Tables and Clickable Links, which doesn’t help me since I’m not producing a table.
The original space script I’m trying to translate from v1 looked like this:
page
order by lastModified desc
where "draft" in tags or draft = true
select name
render [[Library/Core/Query/Page]]
I assume there is a straightforward solution that everyone is using and thought was so trivial it’s not mentioned in the docs?
How about this (note that the new tags.draft thing is new in 2.1.5, previously you’d have to write index.tag "draft"):
${query[[
from tags.draft
order by lastModified desc
select {name = "[[" .. name .. "]]"}
]]}
When you tag something (a page, an item, a task) it will automatically create a type of “table” (but we call them tags) you can query directly, without having to go through the “page” table (tag), so this is more succinct. However what will happen is that if you also have items or tasks or other things tagged with #draft they will appear here too. So if you want to more closely resemble your original query you can indeed do:
${query[[
from tags.post
where table.includes(_.tags, "draft") or _.draft
order by _.lastModified desc
select {name = "[[" .. _.name .. "]]"}
]]}
This will render the results in a table with a name column. If you want to render this a list of page links (common scenario), you can use the built-in templates.pageItem template and simplify this even further:
${template.each(query[[
from tags.draft
order by lastModified desc
]], templates.pageItem)}
That…almost does what I want, and it’s close enough to be Good Enough tm, but I’d like for it to render the links with the full local path. E.g. foo/bar renders like bar but should render like foo/bar. I think Silverbullet is built assuming you are using a much flatter workspace than the one I am using.
This is extremely funny to me, as I strongly prefer flat ish file structures compare to other people and this is literally the first time I have had that problem.
This is more of a nitpick/general thought, but I think it should be discouraged to link with the name inside of queries. This would be the main thing breaking if we ever want to move away from names being unique. ref should be used instead.
Oh that makes a ton of sense to me. This separation would be extremely useful since it means page IDs can be stable and robust against file system shenanigans if desired, and pages can be renamed without having to manually trace and update backlinks, which is difficult in the face of external edits.
I like this. Perhaps we can have more generic templates like templates.refItem or whatever that work for anything with a ref that is actually navigatable (which should be almost all of them).