From ae17385dd967afd102d7ac93502237c90f94c7ae Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 11 Oct 2023 10:27:12 +0200 Subject: [PATCH] Add ability to convert #use and #import via a command --- plugs/directive/command.ts | 43 +++++++++++++++++++++++------ plugs/directive/directive.plug.yaml | 4 +-- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/plugs/directive/command.ts b/plugs/directive/command.ts index 1e4bb18..40bc342 100644 --- a/plugs/directive/command.ts +++ b/plugs/directive/command.ts @@ -209,7 +209,7 @@ export async function updateDirectives( return text; } -export async function convertToLiveQuery() { +export async function convertToLive() { const text = await editor.getText(); const pos = await editor.getCursor(); const tree = await markdown.parseMarkdown(text); @@ -226,12 +226,37 @@ export async function convertToLiveQuery() { ); return; } - const queryText = renderToText(directive!.children![0].children![1]); - await editor.dispatch({ - changes: { - from: directive.from, - to: directive.to, - insert: "```query\n" + queryText + "\n```", - }, - }); + console.log("Got this directive", directive); + const startNode = directive.children![0]; + const startNodeText = renderToText(startNode); + if (startNodeText.includes("#query")) { + const queryText = renderToText(startNode.children![1]); + await editor.dispatch({ + changes: { + from: directive.from, + to: directive.to, + insert: "```query\n" + queryText + "\n```", + }, + }); + } else if ( + startNodeText.includes("#use") || startNodeText.includes("#include") + ) { + const pageRefMatch = /\[\[([^\]]+)\]\]\s*([^\-]+)?/.exec(startNodeText); + if (!pageRefMatch) { + await editor.flashNotification( + "No page reference found in directive", + "error", + ); + return; + } + const val = pageRefMatch[2]; + await editor.dispatch({ + changes: { + from: directive.from, + to: directive.to, + insert: '```template\npage: "[[' + pageRefMatch[1] + ']]"\n' + + (val ? `val: ${val}\n` : "") + "```", + }, + }); + } } diff --git a/plugs/directive/directive.plug.yaml b/plugs/directive/directive.plug.yaml index 151d45a..0b99a7a 100644 --- a/plugs/directive/directive.plug.yaml +++ b/plugs/directive/directive.plug.yaml @@ -31,6 +31,6 @@ functions: # Conversion convertToLiveQuery: - path: command.ts:convertToLiveQuery + path: command.ts:convertToLive command: - name: "Directive: Convert Query to Live Query" + name: "Directive: Convert to Live Query/Template"