diff --git a/plugs/core/core.plug.yaml b/plugs/core/core.plug.yaml index b130697..67f1602 100644 --- a/plugs/core/core.plug.yaml +++ b/plugs/core/core.plug.yaml @@ -272,6 +272,11 @@ functions: slashCommand: name: snippet description: Insert a snippet + applyPageTemplateCommand: + path: ./template.ts:applyPageTemplateCommand + slashCommand: + name: page-template + description: Apply a page template insertTodayCommand: path: "./template.ts:insertTemplateText" slashCommand: diff --git a/plugs/core/template.ts b/plugs/core/template.ts index b3d4d42..4d7a4e5 100644 --- a/plugs/core/template.ts +++ b/plugs/core/template.ts @@ -119,6 +119,44 @@ export async function insertSnippet() { } } +export async function applyPageTemplateCommand() { + const allPages = await space.listPages(); + const { pageTemplatePrefix } = await readSettings({ + pageTemplatePrefix: "template/page/", + }); + const cursorPos = await editor.getCursor(); + const page = await editor.getCurrentPage(); + const pageMeta = await space.getPageMeta(page); + const allSnippets = allPages + .filter((pageMeta) => pageMeta.name.startsWith(pageTemplatePrefix)) + .map((pageMeta) => ({ + ...pageMeta, + name: pageMeta.name.slice(pageTemplatePrefix.length), + })); + + const selectedPage = await editor.filterBox( + "Page template", + allSnippets, + `Select the page template to apply (listing any page starting with ${pageTemplatePrefix})`, + ); + + if (!selectedPage) { + return; + } + + const text = await space.readPage( + `${pageTemplatePrefix}${selectedPage.name}`, + ); + let templateText = replaceTemplateVars(text, pageMeta); + const carretPos = templateText.indexOf("|^|"); + templateText = templateText.replace("|^|", ""); + templateText = replaceTemplateVars(templateText, pageMeta); + await editor.insertAtCursor(templateText); + if (carretPos !== -1) { + await editor.moveCursor(cursorPos + carretPos); + } +} + // TODO: This should probably be replaced with handlebards somehow? export function replaceTemplateVars(s: string, pageMeta: PageMeta): string { const template = Handlebars.compile(s, { noEscape: true });