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}}
[[ {{name}}|:label:{{name}}({{count({tag where name = @tag_name})}})]] {{/let}}
{{/each}}
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)}
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
Looks like “bau” was faster than me and is correct
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.