Assigning heading levels to pages from a query

I’ve got some space that returns a list of pages. It grabs a bunch of pages based on a tag, then for each of those pages, it gets the subpages based on tags that match the page name of the parent. It also goes one layer deeper. The return just gives me a flat list. How can I make it so the outer level pages are #, the subpages are ## and the third level are ###?
Here’s my code if it helps to explain what I’m trying to do.

function getJD(tagName)
  data = getTag(tagName)
  ndata = {}
  for i, v in ipairs(data) do
    table.insert(ndata,v)
    for ii, iv in ipairs(getTag(v.name)) do
      table.insert(ndata, iv)
        for iii, iiv in ipairs(getTag(iv.name)) do
          table.insert(ndata, iiv)
      end
    end
  end
  return ndata
end

function getTag(tagName)
  tquery = query[[from index.tag("page") where table.includes(itags, tagName)]]
  return tquery
end

Can you elaborate a bit on the expected output? Probably it’s just me, but I haven’t fully understood what you’re trying to do.

I think the output is supposed to look something like this?


My test layout:

TEST      #toplevel
├─ Foo    #TEST
│  ├─ a   #TEST/Foo
│  ├─ b   #TEST/Foo
├─ Bar    #TEST
│  ├─ 1   #TEST/Bar
│  ├─ 2   #TEST/Bar

If so, what you are trying to achieve is kinda hard to do with a single function, but should work more easily with some inline lua:

${template.each(getTag("<tag of your top-level pages>"), template.new [==[
    # ${_.name}
    ${template.each(getTag(_.name), template.new [[
        ## ${_.name}
        ${template.each(getTag(_.name), template.new "### ${_.name}\n" )}
    ]])}
]==])}

Didn’t manage to format the third-level template like the other two templates, since sb treats " other than [[ or [==[ and escaping quotes isn’t yet implemented.

Let me know if this works for you ;D

Huh, didn’t know you could nest templates! That looks perfect. I’ll give it a go, thanks!
Just a little styling to go, and that’s pretty much done. Thanks for the help!

1 Like