New convenience index.* APIs for built-in tags

This commit is contained in:
Zef Hemel
2026-05-19 10:56:55 +02:00
parent ce97cf2450
commit f7fbbda41c
3 changed files with 114 additions and 10 deletions
+26 -8
View File
@@ -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,
},
+28 -1
View File
@@ -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();
},
+60 -1
View File
@@ -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<LuaQueryCollection> {
return syscall("index.objects", tagName);
}
/**
* Returns all pages (optionally filtered by an additional tag) as a query collection.
*/
export function pages(tagName?: string): Promise<LuaQueryCollection> {
return syscall("index.pages", tagName);
}
/**
* Returns all tasks (optionally filtered by an additional tag) as a query collection.
*/
export function tasks(tagName?: string): Promise<LuaQueryCollection> {
return syscall("index.tasks", tagName);
}
/**
* Returns all headers (optionally filtered by an additional tag) as a query collection.
*/
export function headers(tagName?: string): Promise<LuaQueryCollection> {
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<LuaQueryCollection> {
return syscall("index.items", tagName);
}
/**
* Returns all paragraphs (optionally filtered by an additional tag) as a query collection.
*/
export function paragraphs(tagName?: string): Promise<LuaQueryCollection> {
return syscall("index.paragraphs", tagName);
}
/**
* Returns all tables (optionally filtered by an additional tag) as a query collection.
*/
export function tables(tagName?: string): Promise<LuaQueryCollection> {
return syscall("index.tables", tagName);
}
/**
* Returns all aspiring (referenced but not yet created) pages as a query collection.
*/
export function aspiringPages(): Promise<LuaQueryCollection> {
return syscall("index.aspiringPages");
}
/**
* Queries objects using a Lua-based collection query
* @param tag - The tag to filter objects by