Reimplement tag pages using editor:pageCreating event
This commit is contained in:
@@ -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)}
|
||||
|
||||
@@ -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 +1,2 @@
|
||||
// This page is actually implemented in Library/Std/Tag.md
|
||||
export const tagPrefix = "tag:";
|
||||
|
||||
Reference in New Issue
Block a user