Reimplement tag pages using editor:pageCreating event

This commit is contained in:
Zef Hemel
2025-03-21 16:40:03 +01:00
parent fa6975870a
commit cbbecf2ad0
3 changed files with 54 additions and 4 deletions
+7 -4
View File
@@ -13,14 +13,11 @@ templates.taskItem = template.new([==[
* [${state}] [[${ref}]] ${name}
]==])
-- Renders a task object as a togglable task
-- Renders an item object
templates.itemItem = template.new([==[
* ${name}
]==])
templates.paragraphItem = template.new([==[
* ${name}
]==])
```
@@ -33,3 +30,9 @@ ${template.each(query[[from index.tag "page" limit 3]], templates.pageItem)}
* [ ] Task 2
${template.each(query[[from index.tag "task" where page == _CTX.currentPage.name]], templates.taskItem)}
`template.itemItem`:
* Item 1
* Item 2
${template.each(query[[from index.tag "item" where page == _CTX.currentPage.name]], templates.itemItem)}
+46
View File
@@ -0,0 +1,46 @@
#meta
Implements a tag page, that renders when navigating to a hashtag: #meta based on the `tag:` prefix, by piggy backing on the `editor:pageCreating` event that triggers for non-existing pages.
```space-lua
event.listen {
name = "editor:pageCreating",
run = function(e)
if not e.data.name:startsWith("tag:") then
return
end
-- Extract the tag name from the page name
local tagName = e.data.name:sub(#"tag:" + 1)
local text = "# Objects tagged with " .. tagName .. "\n"
local taggedPages = query[[
from index.tag "page"
where table.includes(_.tags, tagName)
]]
if #taggedPages > 0 then
text = text .. "## Pages\n"
.. template.each(taggedPages, templates.pageItem)
end
local taggedTasks = query[[
from index.tag "task"
where table.includes(_.tags, tagName)
]]
if #taggedTasks > 0 then
text = text .. "## Tasks\n"
.. template.each(taggedTasks, templates.taskItem)
end
local taggedItems = query[[
from index.tag "item"
where table.includes(_.tags, tagName)
]]
if #taggedItems > 0 then
text = text .. "## Items\n"
.. template.each(taggedItems, templates.itemItem)
end
return {
text = text,
-- Read only page
perm = "ro"
}
end
}
```
+1
View File
@@ -1 +1,2 @@
// This page is actually implemented in Library/Std/Tag.md
export const tagPrefix = "tag:";