From 35e2afc18bdea09ad9fb7926afd61002fc72da88 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 15 Nov 2023 10:27:37 +0100 Subject: [PATCH] Fixes #566 --- plugs/directive/command.ts | 51 +++++++++++++++++++++++++++++ plugs/directive/directive.plug.yaml | 5 +++ web/components/page_navigator.tsx | 1 - 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/plugs/directive/command.ts b/plugs/directive/command.ts index 4341ab0..8e8a832 100644 --- a/plugs/directive/command.ts +++ b/plugs/directive/command.ts @@ -1,6 +1,7 @@ import { editor, markdown, mq, space, sync } from "$sb/syscalls.ts"; import { addParentPointers, + collectNodesOfType, findParentMatching, nodeAtPos, ParseTree, @@ -264,3 +265,53 @@ export async function convertToLive() { }); } } + +export async function convertSpaceToLive() { + if ( + !await editor.confirm( + "This will convert all directives in the space to live queries. Are you sure?", + ) + ) { + return; + } + const pages = await space.listPages(); + for (const page of pages) { + console.log("Now converting", page); + const text = await space.readPage(page.name); + const newText = await convertDirectivesOnPage(text); + if (text !== newText) { + console.log("Changes were made, writing", page.name); + await space.writePage(page.name, newText); + } + } + await editor.flashNotification("All done!"); +} + +export async function convertDirectivesOnPage(text: string) { + const tree = await markdown.parseMarkdown(text); + collectNodesOfType(tree, "Directive").forEach((directive) => { + const directiveText = renderToText(directive); + console.log("Got this directive", directiveText); + const startNode = directive.children![0]; + const startNodeText = renderToText(startNode); + if (startNodeText.includes("#query")) { + const queryText = renderToText(startNode.children![1]); + text = text.replace(directiveText, "```query\n" + queryText + "\n```"); + } else if ( + startNodeText.includes("#use") || startNodeText.includes("#include") + ) { + const pageRefMatch = /\[\[([^\]]+)\]\]\s*([^\-]+)?/.exec(startNodeText); + if (!pageRefMatch) { + return; + } + const val = pageRefMatch[2]; + text = text.replace( + directiveText, + '```template\npage: "[[' + pageRefMatch[1] + ']]"\n' + + (val ? `val: ${val}\n` : "") + "```", + ); + } + }); + // console.log("Converted page", text); + return text; +} diff --git a/plugs/directive/directive.plug.yaml b/plugs/directive/directive.plug.yaml index 7e73fb5..f9d14f6 100644 --- a/plugs/directive/directive.plug.yaml +++ b/plugs/directive/directive.plug.yaml @@ -33,3 +33,8 @@ functions: path: command.ts:convertToLive command: name: "Directive: Convert to Live Query/Template" + + convertSpaceToLive: + path: command.ts:convertSpaceToLive + command: + name: "Directive: Convert Entire Space to Live/Templates" diff --git a/web/components/page_navigator.tsx b/web/components/page_navigator.tsx index 4b61243..47d854d 100644 --- a/web/components/page_navigator.tsx +++ b/web/components/page_navigator.tsx @@ -35,7 +35,6 @@ export function PageNavigator({ // And deprioritize federated pages too if (isFederationPath(pageMeta.name)) { orderId = Math.round(orderId / 10); // Just 10x lower the timestamp to push them down, should work - console.log("Deprioritizing", pageMeta); } options.push({ ...pageMeta,