Heading tags?

Hey there!

I’m currently trying to move a bunch of random notes over to Silverbullet. In the process, I saw that it is possible to add tags to things, which is awesome and very handy for my use-case. However, one thing I cannot figure out how to add tags to headings (if that’s even possible).

One can easily add tags to paragraphs and list items, and because headings are indexed as well, I assumed it should be possible to add custom tags to them. Putting the tag in the heading itself doesn’t seem to do anything (as in: it doesn’t show up in the tagged objects). Putting the tag under the heading will tag the entire page (apparently, an empty line doesn’t count as a paragraph).

A simple workaround would be to add some dummy text under the heading and add the tag to that paragraph, but it’s not ideal.

Am I missing something obvious here?

Actually you just need to add the tag simply in the heading. But currently in the Tag Virtual page tagged headings are not yet displayed, but that doesn’t mean that the heading isn’t tagged correctly. It just means it’s not implemented to show up on the virtual page:

But you can override this by adding your own entries to your custom Virtual page.

As a fast check you can run a query for the headers to see if they got correctly tagged:

For example:

${query[[from index.tag "header" limit 10]]}

1 Like

Nice! Thank you for steering me in the right direction. It’s all totally new to me, and I’m still just getting an overview over the basics.

I did manage to solve this by adding a custom override as you suggested. What was a bit tricky is that one cannot just copy the approach taken for e.g. paragraphs. For those, the definition is

local taggedParagraphs = query[[
  from allObjects where table.includes(_.itags, "paragraph")
]]
if #taggedParagraphs > 0 then
  text = text .. "## Paragraphs\n"
      .. template.each(taggedParagraphs, templates.paragraphItem)
end

Unfortunately, for tags added to headings, the itags field is nil (why?). This took me a moment to figure out, and having something like a REPL would have been very helpful, though I don’t think that’s a thing (yet) in Silverbullet? Anyway, my final solution was to add the following to the virtual page definition:

local taggedSections = query[[
  -- Note: for some reason itags is nil for heading tags
  from allObjects where _.tag == "header"
]]
if #taggedSections > 0 then
  text = text .. "## Sections\n"
      -- Note: the item template works for headings as well
      .. template.each(taggedSections, templates.itemItem)
end

Thanks again for your help!

glad you figured it out.

what the reason is behind that headers don’t have itags attributes is unknown for me.

but for headings the similar attribute is called tags.

when you’re in doubt about the existence of a certain attribute of an object the best test is to do a quick query on the object like i told you in my previous post:

${query[[from index.tag "header" order by tags desc limit 10]]}

and check them at the top of the resulting table:

if you do this, you can see that headers also have the level attribute which can be handy if you want to filter for a certain heading level

1 Like