Skip to content

Instantly share code, notes, and snippets.

@mjf
Created June 19, 2025 06:01
Show Gist options
  • Save mjf/2efac9dc2fc1c02391752a7f19d27e7d to your computer and use it in GitHub Desktop.
Save mjf/2efac9dc2fc1c02391752a7f19d27e7d to your computer and use it in GitHub Desktop.
tagList
${query[[
  from
    p = index.tag 'page'
  where
    p.perm == 'rw' and
    not table.includes(p.itags, 'meta') and
    not table.includes(p.itags, 'meta/template') and
    not table.includes(p.itags, 'meta/template/page') and
    not p.name.startsWith('Inbox/')
  select {
    ['Name']     = '[[' .. p.name .. '|' .. p.name:gsub('^/', '') .. ']]',
    ['Modified'] = p.lastModified:gsub('T', ' '),
    ['Tags']     = tagList(p.name)['markdown']
  }
  order by
    p.lastModified desc
  limit
    10
]]}
```space-lua
function tagList(page)

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

  for _, str in ipairs(inputTable) do
    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

  for letter in pairs(grouped) do
    table.insert(sortedLetters, letter)
  end
  table.sort(sortedLetters) -- sort alphabetically
   
  local output = ""
  for _, letter in ipairs(sortedLetters) do
    local uniqueWords = {}

    output = output .. '**' .. letter .. '** '

    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

    for i, word in ipairs(uniqueWords) do
      output = output .. '#' .. word

      if counts[word] > 1 then
        output = output .. "^(" .. counts[word] .. ")^"
      end

      if i < #uniqueWords then
        output = output .. " "
      end
    end

    output = output .. "\n" 
  end

  return widget.new{
    markdown   = output;
    display    = "block";
    cssClasses = {
      "taglist"
    }
  }
end
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment