From f7fbbda41c72d853433eeb1d694ee89ff7754315 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Tue, 19 May 2026 10:56:55 +0200 Subject: [PATCH] New convenience index.* APIs for built-in tags --- client/data/object_index.ts | 34 +++++++++++++----- client/plugos/syscalls/index.ts | 29 +++++++++++++++- plug-api/syscalls/index.ts | 61 ++++++++++++++++++++++++++++++++- 3 files changed, 114 insertions(+), 10 deletions(-) diff --git a/client/data/object_index.ts b/client/data/object_index.ts index 1a1ee92e..b99768da 100644 --- a/client/data/object_index.ts +++ b/client/data/object_index.ts @@ -120,7 +120,11 @@ export class ObjectIndex { return value; } - tag(tagName: string): LuaQueryCollection { + /** + * Returns a LuaQueryCollection representation of all objects with a specific tag + * @param tagName name of the tag + */ + objectsWithTag(tagName: string): LuaQueryCollection { if (!tagName) { throw new Error("Tag name is required"); } @@ -154,6 +158,20 @@ export class ObjectIndex { ); } + aspiringPages(): LuaQueryCollection { + return this.objectsWithTag("aspiring-page"); + } + + rootTaggedObjects(rootTag: string, tag?: string): LuaQueryCollection { + if (tag) { + return this.filteredTag( + tag, (varName) => `${varName}.tag == "${rootTag}"` + ); + } else { + return this.objectsWithTag(rootTag); + } + } + private filteredTag( tagName: string, buildFilterExpr: (varName: string) => string, @@ -164,12 +182,12 @@ export class ObjectIndex { const filter = parseExpressionString(buildFilterExpr(varName)); const where = query.where ? { - type: "Binary" as const, - operator: "and", - left: filter, - right: query.where, - ctx: {}, - } + type: "Binary" as const, + operator: "and", + left: filter, + right: query.where, + ctx: {}, + } : filter; return this.ds.luaQuery( ["idx", tagName], @@ -362,7 +380,7 @@ export class ObjectIndex { key: [indexKey, ...key, page], value, }, - { + { // Reverse key storage for quick deletions key: [pageKey, page, ...key], value: true, }, diff --git a/client/plugos/syscalls/index.ts b/client/plugos/syscalls/index.ts index b7b8ee8d..2f96a307 100644 --- a/client/plugos/syscalls/index.ts +++ b/client/plugos/syscalls/index.ts @@ -17,8 +17,16 @@ export function indexSyscalls( client: Client, ): SysCallMapping { return { + // Query collection API "index.tag": (_ctx, tagName: string): LuaQueryCollection => { - return objectIndex.tag(tagName); + return objectIndex.objectsWithTag(tagName); + }, + // Alias for tag + "index.objects": (_ctx, tagName: string): LuaQueryCollection => { + return objectIndex.objectsWithTag(tagName); + }, + "index.pages": (_ctx, tagName?: string): LuaQueryCollection => { + return objectIndex.rootTaggedObjects("page", tagName); }, "index.contentPages": (): LuaQueryCollection => { return objectIndex.contentPages(); @@ -26,6 +34,25 @@ export function indexSyscalls( "index.metaPages": (): LuaQueryCollection => { return objectIndex.metaPages(); }, + "index.tasks": (_ctx, tagName?: string): LuaQueryCollection => { + return objectIndex.rootTaggedObjects("task", tagName); + }, + "index.headers": (_ctx, tagName?: string): LuaQueryCollection => { + return objectIndex.rootTaggedObjects("header", tagName); + }, + "index.items": (_ctx, tagName?: string): LuaQueryCollection => { + return objectIndex.rootTaggedObjects("item", tagName); + }, + "index.paragraphs": (_ctx, tagName?: string): LuaQueryCollection => { + return objectIndex.rootTaggedObjects("paragraph", tagName); + }, + "index.tables": (_ctx, tagName?: string): LuaQueryCollection => { + return objectIndex.rootTaggedObjects("table", tagName); + }, + "index.aspiringPages": (): LuaQueryCollection => { + return objectIndex.aspiringPages(); + }, + // Internals "index.aggregates": (): LuaQueryCollection => { return objectIndex.aggregates(); }, diff --git a/plug-api/syscalls/index.ts b/plug-api/syscalls/index.ts index f1eaa38c..3ca982b7 100644 --- a/plug-api/syscalls/index.ts +++ b/plug-api/syscalls/index.ts @@ -1,4 +1,7 @@ -import type { LuaCollectionQuery } from "../../client/space_lua/query_collection.ts"; +import type { + LuaCollectionQuery, + LuaQueryCollection, +} from "../../client/space_lua/query_collection.ts"; import { syscall } from "@silverbulletmd/silverbullet/syscall"; import type { ObjectValue } from "../../plug-api/types/index.ts"; @@ -39,6 +42,62 @@ export function previewProcessedObjects( return syscall("index.previewProcessedObjects", page, objects); } +/** + * Returns objects carrying the given tag as a query collection. + */ +export function objects(tagName: string): Promise { + return syscall("index.objects", tagName); +} + +/** + * Returns all pages (optionally filtered by an additional tag) as a query collection. + */ +export function pages(tagName?: string): Promise { + return syscall("index.pages", tagName); +} + +/** + * Returns all tasks (optionally filtered by an additional tag) as a query collection. + */ +export function tasks(tagName?: string): Promise { + return syscall("index.tasks", tagName); +} + +/** + * Returns all headers (optionally filtered by an additional tag) as a query collection. + */ +export function headers(tagName?: string): Promise { + return syscall("index.headers", tagName); +} + +/** + * Returns all list items (optionally filtered by an additional tag) as a query collection. + */ +export function items(tagName?: string): Promise { + return syscall("index.items", tagName); +} + +/** + * Returns all paragraphs (optionally filtered by an additional tag) as a query collection. + */ +export function paragraphs(tagName?: string): Promise { + return syscall("index.paragraphs", tagName); +} + +/** + * Returns all tables (optionally filtered by an additional tag) as a query collection. + */ +export function tables(tagName?: string): Promise { + return syscall("index.tables", tagName); +} + +/** + * Returns all aspiring (referenced but not yet created) pages as a query collection. + */ +export function aspiringPages(): Promise { + return syscall("index.aspiringPages"); +} + /** * Queries objects using a Lua-based collection query * @param tag - The tag to filter objects by