# Clickable link from Lua query

**URL:** <https://community.silverbullet.md/t/clickable-link-from-lua-query/3310>\
**Category:** Lua\
**Created:** [October 1, 2025, 10:24am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310 "2025-10-01T10:24:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![amandasystems](https://community.silverbullet.md/user_avatar/community.silverbullet.md/amandasystems/32/1662_2.png) [@amandasystems](https://community.silverbullet.md/u/amandasystems)\
**Post date:** [October 1, 2025, 10:24am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/1 "2025-10-01T10:24:08Z")

</div>

I’ve laboriously trialled-and-error’d the following terrible query to do the standard “find pages tagged such and such”:

```lua
${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:

```markdown
- [[foo]]
- [[bar]]

```

I assume there’s a way to apply rendering, but it’s not mentioned in the article on [Lua Integrated Queries](https://silverbullet.md/Space%20Lua/Lua%20Integrated%20Query) and the closest similar question I can find in the forums is [Tables and Clickable Links](https://community.silverbullet.md/t/tables-and-clickable-links/2182), 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:

```space-script
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?

---

<div class="post-metadata">

**Author:** ![zef](https://community.silverbullet.md/user_avatar/community.silverbullet.md/zef/32/7_2.png) [@zef](https://community.silverbullet.md/u/zef)\
**Post date:** [October 1, 2025, 10:35am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/2 "2025-10-01T10:35:06Z")

</div>

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"`):

```auto
${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:

```auto
${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:

```auto
${template.each(query[[
  from tags.draft
  order by lastModified desc
]], templates.pageItem)}

```

---

<div class="post-metadata">

**Author:** ![amandasystems](https://community.silverbullet.md/user_avatar/community.silverbullet.md/amandasystems/32/1662_2.png) [@amandasystems](https://community.silverbullet.md/u/amandasystems)\
**Post date:** [October 1, 2025, 10:48am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/3 "2025-10-01T10:48:57Z")

</div>

Nice!

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.

---

<div class="post-metadata">

**Author:** ![zef](https://community.silverbullet.md/user_avatar/community.silverbullet.md/zef/32/7_2.png) [@zef](https://community.silverbullet.md/u/zef)\
**Post date:** [October 1, 2025, 10:53am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/4 "2025-10-01T10:53:36Z")

</div>

We can do that too, for this you can use the `templates.fullPageItem` template or if you end up using the select thing:

```auto
select {name = "[[" .. _.name .. "|" .. _.name .. "]]"}

```

This is a bit of a hack, where it uses the page name itself as custom link text.

---

<div class="post-metadata">

**Author:** ![amandasystems](https://community.silverbullet.md/user_avatar/community.silverbullet.md/amandasystems/32/1662_2.png) [@amandasystems](https://community.silverbullet.md/u/amandasystems)\
**Post date:** [October 1, 2025, 10:55am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/5 "2025-10-01T10:55:52Z")

</div>

Is there a way for me to contribute this to the docs?

---

<div class="post-metadata">

**Author:** ![zef](https://community.silverbullet.md/user_avatar/community.silverbullet.md/zef/32/7_2.png) [@zef](https://community.silverbullet.md/u/zef)\
**Post date:** [October 1, 2025, 10:57am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/6 "2025-10-01T10:57:03Z")

</div>

Technically you can do this here: [silverbullet/website at main · silverbulletmd/silverbullet · GitHub](https://github.com/silverbulletmd/silverbullet/tree/main/website) the website is simply a SB space folder in the repo. Happy to take PRs.

The challenge for me is always: where do I put this? 😉

---

<div class="post-metadata">

**Author:** ![MrMugame](https://community.silverbullet.md/user_avatar/community.silverbullet.md/mrmugame/32/358_2.png) [@MrMugame](https://community.silverbullet.md/u/MrMugame)\
**Post date:** [October 1, 2025, 11:00am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/7 "2025-10-01T11:00:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![amandasystems](https://community.silverbullet.md/user_avatar/community.silverbullet.md/amandasystems/32/1662_2.png) [@amandasystems](https://community.silverbullet.md/u/amandasystems)\
**Post date:** [October 1, 2025, 11:05am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/8 "2025-10-01T11:05:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zef](https://community.silverbullet.md/user_avatar/community.silverbullet.md/zef/32/7_2.png) [@zef](https://community.silverbullet.md/u/zef)\
**Post date:** [October 1, 2025, 11:09am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/9 "2025-10-01T11:09:24Z")

</div>

> [@MrMugame](#):
>
> This would be the main thing breaking if we ever want to move away from names being unique. `ref` should be used instead.

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).

---

<div class="post-metadata">

**Author:** ![amandasystems](https://community.silverbullet.md/user_avatar/community.silverbullet.md/amandasystems/32/1662_2.png) [@amandasystems](https://community.silverbullet.md/u/amandasystems)\
**Post date:** [October 1, 2025, 11:16am UTC](https://community.silverbullet.md/t/clickable-link-from-lua-query/3310/10 "2025-10-01T11:16:47Z")

</div>

> [@zef](#):
>
> The challenge for me is always: where do I put this? 😉

I went looking for it in the Lua Integrated Query page, so [my PR](https://github.com/silverbulletmd/silverbullet/pull/1578) added it there. Feel free to do whatever you want with it!
