Converting v1 query to v2

Hi, I’m trying to convert this query that worked in v1 to a query that will work in v2. It shows displays all tags and a count of how many time the tag appears eg tag (3)

This is the v1 query

{{#each {tag select name where parent != “builtin” order by name}}}
{{#let @tag_name = name}}
[[:label: {{name}}|:label:{{name}}({{count({tag where name = @tag_name})}})]] {{/let}}
{{/each}}

Any help would be appreciated. Thanks!

Have tried deepwiki silverbullet assistant?
It could interesting to create a prompt spécialized on migration or query.

1 Like

Should be a good (?) start (works for me):
you can filter it more, add limits, tweaks…
if you find an efficient way to avoid the double loop …

${template.each( (function()
  local tb = query[[from index.tag "tag" order by _.name]]
  local counts = {}
  local counted = {}
  for _, it in ipairs(tb) do
      if counts[it.name] then counts[it.name] = counts[it.name] + 1
      else counts[it.name] = 1 end
  end
  for k, obj in pairs(counts) do
      table.insert(counted, {name = k, count = obj})
  end
  return counted
end)(), 
function(obj)
  return spacelua.interpolate([==[
 [[tag:${name}|#${name}]] (${count})
]==], obj)
end)}
1 Like

That works a treat! Thanks rogpi

1 Like

Thanks Malys, I haven’t heard of this but I’ll check it out

Not a direct translation of your “old” query, but you might find it useful, too:

The function displays a List of all tags with their counts (like you described), but additionally sorts and groups by the Alphabet.

function AlphabeticTagList()

  local grouped = {}
  local counts = {}
  local sortedLetters = {}
  
  local inputTable = query[[
      from t = index.tag "tag"
      order by t.name 
      select {N = t.name, P = t.parent, PG = t.page}
  ]]

  -- group strings for first letters
    for _, str in ipairs(inputTable) do
        --local firstChar = str:sub(1, 1):upper() -- get first letter, set to uppercase
        local firstChar = string.sub(str.N,1, 1)
        firstChar = string.upper(firstChar)
      
        if not grouped[firstChar] then
            grouped[firstChar] = {}
        end
        table.insert(grouped[firstChar], str.N)
    end

    -- Create a sorted list of all letters

    for letter in pairs(grouped) do
        table.insert(sortedLetters, letter)
    end
    table.sort(sortedLetters) -- sort alphabetically
   
    -- create output in markdown format
    local output = ""
    for _, letter in ipairs(sortedLetters) do
        output = output .. "**" .. letter .. "**  :  " -- make the letters bold
        local uniqueWords = {}

        -- count the occurances 
        for _, word in ipairs(grouped[letter]) do

            if counts[word] then
                counts[word] = counts[word] + 1
            else
                counts[word] = 1
                table.insert(uniqueWords, word)
            end
        end

        -- add the single occurances
        for i, word in ipairs(uniqueWords) do

            output = output .. "[[tag:" .. word .. "|" .. word .. "]]"

            if counts[word] > 1 then
                output = output .. "(" .. counts[word] .. ")"
            end
            if i < #uniqueWords then
                output = output .. "  " -- set a space between words
            end
           -- print("COUNTB ", word, counts[word])
        end
        output = output .. "\n" 
    end

    return widget.new{
      markdown = output;
      display = "block";
      cssClasses={"taglist"}
    }
end

3 Likes

interesting, I’ll advertise for you:

somewhat related to Super fast tag/page navigator - #10 by Ego

1 Like

Thanks Nico. So this returns a widget? How do I use this function in SB? Thanks

To use this function, simply copy/paste the following into your page:
${AlphabeticTagList()}

2 Likes

Looks like “bau” was faster than me and is correct :slight_smile:

Yes, this function returns a widget. If I find some time, I will do some overhaul and publish it as a Library with appropriate Dokumentation. And then it might become something like widget.AlphabeticTagList() to make it more clear, that it’s a widget. But for now do just like bau wrote:

Put the space-lua code from my previous post somewhere in your space and put ${AlphabeticTagList()} somewhere on your page where you like to show the list.

1 Like