From d33db399c8de6ec196d55108b83a8ddfc964fdeb Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 7 Mar 2025 15:13:47 +0100 Subject: [PATCH] Auto complete and lint fixes --- common/space_lua/stdlib.ts | 1 - plugs/core/Library/Std/Template.md | 9 +++++++ plugs/editor/complete.ts | 4 +-- web/boot.ts | 3 +-- web/cm_plugins/lua_directive.ts | 40 +++++++++++++++++++++++++++--- web/syscalls/editor.ts | 2 +- 6 files changed, 49 insertions(+), 10 deletions(-) diff --git a/common/space_lua/stdlib.ts b/common/space_lua/stdlib.ts index c214e609..5134d23e 100644 --- a/common/space_lua/stdlib.ts +++ b/common/space_lua/stdlib.ts @@ -19,7 +19,6 @@ import { spaceluaApi } from "$common/space_lua/stdlib/space_lua.ts"; import { mathApi } from "$common/space_lua/stdlib/math.ts"; import { parse } from "$common/space_lua/parse.ts"; import { evalStatement } from "$common/space_lua/eval.ts"; -import { parseRef } from "@silverbulletmd/silverbullet/lib/page_ref"; const printFunction = new LuaBuiltinFunction(async (_sf, ...args) => { console.log( diff --git a/plugs/core/Library/Std/Template.md b/plugs/core/Library/Std/Template.md index 1a649e56..4961536f 100644 --- a/plugs/core/Library/Std/Template.md +++ b/plugs/core/Library/Std/Template.md @@ -34,6 +34,15 @@ function template.new(templateStr) end end +function template.fromPage(name) + local fm = index.extractFrontmatter(space.readPage(name), { + removeFrontmatterSection = true, + removeTags = true + }) + + return template.new(fm.text) +end + -- Creates a template-based slash command, keys for def are: -- name: name of the slash command -- description: description of the slash command diff --git a/plugs/editor/complete.ts b/plugs/editor/complete.ts index ccb6249f..674c6ff2 100644 --- a/plugs/editor/complete.ts +++ b/plugs/editor/complete.ts @@ -50,9 +50,7 @@ export async function pageComplete(completeEvent: CompleteEvent) { })); } // Let's try to be smart about the types of completions we're offering based on the context else if ( - completeEvent.parentNodes.find((node) => - node === "LuaDirective" || node.startsWith("FencedCode:space-lua") - ) + completeEvent.parentNodes.find((node) => node === "Query") ) { // Let's just disable page completion entirely in Lua directives and space-lua blocks return; diff --git a/web/boot.ts b/web/boot.ts index dc392b30..c163053c 100644 --- a/web/boot.ts +++ b/web/boot.ts @@ -61,9 +61,8 @@ safeRun(async () => { if (!refreshing) { refreshing = true; console.log( - "New service worker activated, reloading page to apply updates...", + "New service worker activated, please reload to activate the new version.", ); - globalThis.location.reload(); } }); diff --git a/web/cm_plugins/lua_directive.ts b/web/cm_plugins/lua_directive.ts index 1df48874..1c746405 100644 --- a/web/cm_plugins/lua_directive.ts +++ b/web/cm_plugins/lua_directive.ts @@ -5,7 +5,6 @@ import { decoratorStateField, invisibleDecoration, isCursorInRange, - shouldRenderWidgets, } from "./util.ts"; import type { Client } from "../client.ts"; import { parse as parseLua } from "$common/space_lua/parse.ts"; @@ -24,17 +23,48 @@ import { encodeRef } from "@silverbulletmd/silverbullet/lib/page_ref"; import { resolveASTReference } from "$common/space_lua.ts"; import { LuaWidget } from "./lua_widget.ts"; import type { PageMeta } from "@silverbulletmd/silverbullet/types"; +import YAML from "js-yaml"; export function luaDirectivePlugin(client: Client) { return decoratorStateField((state: EditorState) => { const widgets: Range[] = []; - if (!shouldRenderWidgets(client)) { - console.info("Not rendering widgets"); + + let shouldRender = true; + + // Don't render Lua directives of federated pages (security) + if (client.currentPage.startsWith("!")) { return Decoration.set([]); } syntaxTree(state).iterate({ enter: (node) => { + // Disable rendering of Lua directives in #meta/template pages + // Either in frontmatter + if (node.name === "FrontMatterCode") { + const text = state.sliceDoc(node.from, node.to); + try { + const parsedFrontmatter = YAML.load(text); + let tags = parsedFrontmatter.tags || []; + if (typeof tags === "string") { + tags = tags.split(/\s+|,\s*/); + } + if (tags.includes("meta/template")) { + shouldRender = false; + return; + } + } catch { + // Ignore + } + } + // Or with a hash tag + if (node.name === "Hashtag") { + const text = state.sliceDoc(node.from, node.to); + if (text === "#meta/template") { + shouldRender = false; + return; + } + } + if (node.name !== "LuaDirective") { return; } @@ -108,6 +138,10 @@ export function luaDirectivePlugin(client: Client) { }, }); + if (!shouldRender) { + return Decoration.set([]); + } + return Decoration.set(widgets, true); }); } diff --git a/web/syscalls/editor.ts b/web/syscalls/editor.ts index e711b05c..cb7b30e9 100644 --- a/web/syscalls/editor.ts +++ b/web/syscalls/editor.ts @@ -61,7 +61,7 @@ export function editorSyscalls(client: Client): SysCallMapping { ref = parseRef(ref); } else if (ref.kind === undefined) { // @ts-ignore: This is for legacy support - locationRef.kind === "page"; + ref.kind === "page"; } await client.navigate(ref, replaceState, newWindow); },