diff --git a/common/space_lua.ts b/common/space_lua.ts index 9293696a..b246f9c0 100644 --- a/common/space_lua.ts +++ b/common/space_lua.ts @@ -5,11 +5,15 @@ import { LuaRuntimeError, LuaStackFrame, } from "$common/space_lua/runtime.ts"; -import { parse as parseLua } from "$common/space_lua/parse.ts"; +import { + parse as parseLua, + parseExpressionString, +} from "$common/space_lua/parse.ts"; import { evalStatement } from "$common/space_lua/eval.ts"; import { parseRef, type Ref } from "@silverbulletmd/silverbullet/lib/page_ref"; import type { ASTCtx } from "$common/space_lua/ast.ts"; import { buildLuaEnv } from "$common/space_lua_api.ts"; +import type { LuaCollectionQuery } from "$common/space_lua/query_collection.ts"; export class SpaceLuaEnvironment { env: LuaEnv = new LuaEnv(); @@ -22,13 +26,14 @@ export class SpaceLuaEnvironment { system: System, ) { const allScripts: ScriptObject[] = await system.invokeFunction( - "index.queryObjects", + "index.queryLuaObjects", ["space-lua", { + objectVariable: "script", orderBy: [{ - expr: ["attr", "priority"], + expr: parseExpressionString("script.priority"), desc: true, }], - }], + } as LuaCollectionQuery], ); try { this.env = buildLuaEnv(system); diff --git a/common/space_lua/runtime.ts b/common/space_lua/runtime.ts index 9d77e37e..8b3e2b7e 100644 --- a/common/space_lua/runtime.ts +++ b/common/space_lua/runtime.ts @@ -67,7 +67,7 @@ export class LuaEnv implements ILuaSettable, ILuaGettable { if (this.parent) { return this.parent.get(name, sf); } - return undefined; + return null; } /** @@ -607,6 +607,8 @@ export function luaGet( if (typeof val === "function") { // Automatically bind the function to the object return val.bind(obj); + } else if (val === undefined) { + return null; } else { return val; } diff --git a/common/space_script.ts b/common/space_script.ts index aa13ce45..d30e5988 100644 --- a/common/space_script.ts +++ b/common/space_script.ts @@ -143,7 +143,7 @@ export class ScriptEnvironment { return; } const allScripts: ScriptObject[] = await system.invokeFunction( - "index.queryObjects", + "index.queryLuaObjects", ["space-script", {}], ); for (const script of allScripts) { diff --git a/common/syscalls/lua.ts b/common/syscalls/lua.ts index e5703ef6..aa804724 100644 --- a/common/syscalls/lua.ts +++ b/common/syscalls/lua.ts @@ -9,12 +9,16 @@ import { } from "$common/space_lua/runtime.ts"; import { buildThreadLocalEnv } from "$common/space_lua_api.ts"; import { isSendable } from "$lib/plugos/util.ts"; +import type { LuaBlock, LuaExpression } from "$common/space_lua/ast.ts"; export function luaSyscalls(commonSystem: CommonSystem): SysCallMapping { return { - "lua.parse": (_ctx, code: string) => { + "lua.parse": (_ctx, code: string): LuaBlock => { return parse(code); }, + "lua.parseExpression": (_ctx, expression: string): LuaExpression => { + return parseExpressionString(expression); + }, /** * Evaluates a Lua expression and returns the result as a JavaScript value * @param _ctx diff --git a/plug-api/syscalls/lua.ts b/plug-api/syscalls/lua.ts index ce365554..25d789af 100644 --- a/plug-api/syscalls/lua.ts +++ b/plug-api/syscalls/lua.ts @@ -1,12 +1,18 @@ import { syscall } from "../syscall.ts"; -import type { ParseTree } from "../lib/tree.ts"; +import type { LuaBlock, LuaExpression } from "$common/space_lua/ast.ts"; export function parse( code: string, -): Promise { +): Promise { return syscall("lua.parse", code); } +export function parseExpression( + expression: string, +): Promise { + return syscall("lua.parseExpression", expression); +} + export function evalExpression( expression: string, ): Promise { diff --git a/plugs/core/Library/Std/Template.md b/plugs/core/Library/Std/Template.md index 0b08bcd5..e2162c3b 100644 --- a/plugs/core/Library/Std/Template.md +++ b/plugs/core/Library/Std/Template.md @@ -40,7 +40,7 @@ function template.fromPage(name) removeTags = true }) - return template.new(fm.text) + return template.new(string.trimStart(fm.text)) end -- Creates a template-based slash command, keys for def are: diff --git a/web/client.ts b/web/client.ts index 1b5ef6e1..314bf38f 100644 --- a/web/client.ts +++ b/web/client.ts @@ -75,10 +75,10 @@ import { LimitedMap } from "$lib/limited_map.ts"; import { plugPrefix } from "$common/spaces/constants.ts"; import { lezerToParseTree } from "$common/markdown_parser/parse_tree.ts"; import { findNodeMatching } from "@silverbulletmd/silverbullet/lib/tree"; -import type { AspiringPageObject } from "../plugs/index/page_links.ts"; import type { Config, ConfigContainer } from "../type/config.ts"; import { diffAndPrepareChanges } from "./cm_util.ts"; import { DocumentEditor } from "./document_editor.ts"; +import { parseExpressionString } from "$common/space_lua/parse.ts"; const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/; @@ -836,20 +836,22 @@ export class Client implements ConfigContainer { console.warn("Index plug not loaded, cannot update page list cache"); return; } - const allPages = await this.clientSystem.queryObjects("page", {}); - const allAspiringPages = (await this.clientSystem.queryObjects< - AspiringPageObject - >("aspiring-page", { - select: [{ name: "name" }], - })).map((aspiringPage): PageMeta => ({ - ref: aspiringPage.name, - tag: "page", - _isAspiring: true, - name: aspiringPage.name, - created: "", - lastModified: "", - perm: "rw", - })); + const allPages = await this.clientSystem.queryLuaObjects( + "page", + {}, + ); + const allAspiringPages = + (await this.clientSystem.queryLuaObjects("aspiring-page", { + select: parseExpressionString("name"), + })).map((name): PageMeta => ({ + ref: name, + tag: "page", + _isAspiring: true, + name: name, + created: "", + lastModified: "", + perm: "rw", + })); this.ui.viewDispatch({ type: "update-page-list", @@ -860,7 +862,7 @@ export class Client implements ConfigContainer { async updateDocumentListCache() { console.log("Updating document list cache"); - const allDocuments = await this.clientSystem.queryObjects( + const allDocuments = await this.clientSystem.queryLuaObjects( "document", {}, ); @@ -1511,7 +1513,7 @@ export class Client implements ConfigContainer { } async loadCustomStyles() { - const spaceStyles = await this.clientSystem.queryObjects( + const spaceStyles = await this.clientSystem.queryLuaObjects( "space-style", {}, ); diff --git a/web/client_system.ts b/web/client_system.ts index cfd74f4f..dbd4b2e6 100644 --- a/web/client_system.ts +++ b/web/client_system.ts @@ -49,6 +49,7 @@ import { indexSyscalls } from "$common/syscalls/index.ts"; import { commandSyscalls } from "$common/syscalls/command.ts"; import { eventListenerSyscalls } from "$common/syscalls/event.ts"; import { DocumentEditorHook } from "./hooks/document_editor.ts"; +import type { LuaCollectionQuery } from "$common/space_lua/query_collection.ts"; const plugNameExtractRegex = /\/(.+)\.plug\.js$/; @@ -240,11 +241,8 @@ export class ClientSystem extends CommonSystem { return this.system.localSyscall(name, args); } - queryObjects(tag: string, query: Query): Promise { - return this.localSyscall( - "system.invokeFunction", - ["index.queryObjects", tag, query], - ); + queryLuaObjects(tag: string, query: LuaCollectionQuery): Promise { + return this.system.invokeFunction("index.queryLuaObjects", [tag, query]); } getObjectByRef(page: string, tag: string, ref: string) {