From cbbecf2ad06c73802e5ea68fb43724795a2fd7f9 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 21 Mar 2025 16:40:03 +0100 Subject: [PATCH] Reimplement tag pages using editor:pageCreating event --- plugs/core/Library/Std/Query Templates.md | 11 ++++-- plugs/core/Library/Std/Tag.md | 46 +++++++++++++++++++++++ plugs/index/constants.ts | 1 + 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 plugs/core/Library/Std/Tag.md diff --git a/plugs/core/Library/Std/Query Templates.md b/plugs/core/Library/Std/Query Templates.md index 54e1cc1d..7216a4e4 100644 --- a/plugs/core/Library/Std/Query Templates.md +++ b/plugs/core/Library/Std/Query Templates.md @@ -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)} diff --git a/plugs/core/Library/Std/Tag.md b/plugs/core/Library/Std/Tag.md new file mode 100644 index 00000000..c777afc3 --- /dev/null +++ b/plugs/core/Library/Std/Tag.md @@ -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 +} +``` diff --git a/plugs/index/constants.ts b/plugs/index/constants.ts index 0152fac3..ba37327a 100644 --- a/plugs/index/constants.ts +++ b/plugs/index/constants.ts @@ -1 +1,2 @@ +// This page is actually implemented in Library/Std/Tag.md export const tagPrefix = "tag:";