From 1bbfbe19e2c0c4afbd981a892934d52549c7d4a2 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Mon, 8 Aug 2022 13:56:04 +0200 Subject: [PATCH] Improvements to templates --- packages/plugs/core/template.ts | 52 ++++++++++++++++++----- website/CHANGELOG.md | 5 ++- website/🔌 Core.md | 73 +++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 website/🔌 Core.md diff --git a/packages/plugs/core/template.ts b/packages/plugs/core/template.ts index a226649..388e12b 100644 --- a/packages/plugs/core/template.ts +++ b/packages/plugs/core/template.ts @@ -2,6 +2,7 @@ import { listPages, readPage, writePage, + getPageMeta, } from "@silverbulletmd/plugos-silverbullet-syscall/space"; import { filterBox, @@ -24,14 +25,15 @@ export async function instantiateTemplateCommand() { pageTemplatePrefix: "template/page/", }); - let allPageTemplates = allPages.filter((pageMeta) => - pageMeta.name.startsWith(pageTemplatePrefix) - ); - let selectedTemplate = await filterBox( "Template", - allPageTemplates, - "Select the template to create a new page from" + allPages + .filter((pageMeta) => pageMeta.name.startsWith(pageTemplatePrefix)) + .map((pageMeta) => ({ + ...pageMeta, + name: pageMeta.name.slice(pageTemplatePrefix.length), + })), + `Select the template to create a new page from (listing any page starting with ${pageTemplatePrefix})` ); if (!selectedTemplate) { @@ -39,11 +41,12 @@ export async function instantiateTemplateCommand() { } console.log("Selected template", selectedTemplate); - let { text } = await readPage(selectedTemplate.name); + let { text } = await readPage( + `${pageTemplatePrefix}${selectedTemplate.name}` + ); let parseTree = await parseMarkdown(text); let additionalPageMeta = extractMeta(parseTree, ["PAGENAME"]); - console.log("Page meta", additionalPageMeta); let pageName = await prompt("Name of new page", additionalPageMeta.PAGENAME); if (!pageName) { @@ -115,18 +118,45 @@ export function replaceTemplateVars(s: string, pageName: string): string { } export async function quickNoteCommand() { + let { quickNotePrefix } = await readSettings({ + quickNotePrefix: "📥 ", + }); let isoDate = new Date().toISOString(); let [date, time] = isoDate.split("T"); time = time.split(".")[0]; - let pageName = `📥 ${date} ${time}`; + let pageName = `${quickNotePrefix}${date} ${time}`; await navigate(pageName); } export async function dailyNoteCommand() { + let { dailyNoteTemplate, dailyNotePrefix } = await readSettings({ + dailyNoteTemplate: "template/page/Daily Note", + dailyNotePrefix: "📅 ", + }); + let dailyNoteTemplateText = ""; + try { + let { text } = await readPage(dailyNoteTemplate); + dailyNoteTemplateText = text; + } catch { + console.warn(`No daily note template found at ${dailyNoteTemplate}`); + } let isoDate = new Date().toISOString(); let date = isoDate.split("T")[0]; - let pageName = `📅 ${date}`; - await navigate(pageName); + let pageName = `${dailyNotePrefix}${date}`; + if (dailyNoteTemplateText) { + try { + await getPageMeta(pageName); + } catch { + // Doesn't exist, let's create + await writePage( + pageName, + replaceTemplateVars(dailyNoteTemplateText, pageName) + ); + } + await navigate(pageName); + } else { + await navigate(pageName); + } } export async function insertTemplateText(cmdDef: any) { diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index a4bc237..92aa4b5 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -1,7 +1,10 @@ An attempt at documenting of the changes/new features introduced in each (pre) release. ## 0.0.31 -* For the `Template: Instantiate Page` command, the page meta value `PAGENAME` is now used to configure the page name (was `name` before). Also if `PAGENAME` is the only page meta defined, it will remove the page meta entirely when instantiating. +* Updates to templates: + * For the `Template: Instantiate Page` command, the page meta value `PAGENAME` is now used to configure the page name (was `name` before). Also if `PAGENAME` is the only page meta defined, it will remove the page meta entirely when instantiating. + * You can now configure a daily note prefix with `dailyNotePrefix` in `SETTINGS` and create a template for your daily note under `template/page/Daily Note` (configurable via the `dailyNoteTemplate` setting). + * You can now a quick note prefix with `quickNotePrefix` in `SETTINGS`. ## 0.0.30 * Slash commands now only trigger after a non-word character to avoid "false positives" like "hello/world". diff --git a/website/🔌 Core.md b/website/🔌 Core.md new file mode 100644 index 0000000..67cfd0d --- /dev/null +++ b/website/🔌 Core.md @@ -0,0 +1,73 @@ +```meta +uri: builtin:core +repo: https://github.com/silverbulletmd/silverbullet +author: Silver Bullet Authors +``` + +This documentation is still a WIP. + +## Templating +The core plug implements a few templating mechanisms. + +### Page Templates +The {[Template: Instantiate Page]} command enables you to create a new page based on a page template. + +Page templates, by default, are looked for in the `template/page/` prefix. So creating e.g. a `template/page/Meeting Notes` page, will create a “Meeting Notes” template. You can override this prefix by setting the `pageTemplatePrefix` in `SETTINGS`. + +Page template have one “magic” type of page meta data that is used during instantiation: + +* `PAGENAME` is used as a default value for a new page based on this template + +In addition, any standard template placeholders are available (see below) + +For instance: + + ```meta + PAGENAME: "📕 " + ``` + + # {{page}} + As recorded on {{today}}. + + ## Introduction + ## Notes + ## Conclusions + +Will prompt you to pick a page name (defaulting to “📕 “), and then create the following page (on 2022-08-08) when you pick “📕 Harry Potter” as a page name: + + # 📕 Harry Potter + As recorded on 2022-08-08. + + ## Introduction + ## Notes + ## Conclusions + +### Snippets +Snippets are similar to page templates, except you insert them into an existing page with the `/snippet` slash command. The default prefix is `snippet/` which is configurable via the `snippetPrefix` setting in `SETTINGS`. + +Snippet templates do not support the `PAGENAME` page meta, because it doesn’t apply. + +However, snippets do support the special `|^|` placeholder for placing the cursor caret after injecting the snippet. If you leave it out, the cursor will simply be placed at the end, but if you like to insert the cursor elsewhere, that position can be set with the `|^|` placeholder. + +For instance to replicate the `/query` slash command as a snippet: + + + + + +Which would insert the cursor right after `#query`. + +### Daily Note +The {[Open Daily Note]} command navigates (or creates) a daily note prefixed with a 📅 emoji by default, but this is configurable via the `dailyNotePrefix` setting in `SETTINGS`. If you have a page template (see above) named `Daily Note` it will use this as a template, otherwise the page will just be empty. + +### Quick Note +The {[Quick Note]} command will navigate to an empty page named with the current date and time prefixed with a 📥 emoji, but this is configurable via the `quickNotePrefix` in `SETTINGS`. The use case is to take a quick note outside of you current context. + +### Template placeholders +Currently supported (hardcoded in the code): + +* `{{today}}`: Today’s date in the usual YYYY-MM-DD format +* `{{tomorrow}}`: Tomorrow’s date in the usual YYY-MM-DD format +* `{{yesterday}}`: Yesterday’s date in the usual YYY-MM-DD format +* `{{lastWeek}}`: Current date - 7 days +* `{{page}}`: The name of the current page