From c56261a02414c8d5f73d68d3d9459d6b0ac82fae Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 2 Nov 2022 09:06:30 +0100 Subject: [PATCH] Weekly Note command + docs --- plugs/core/core.plug.yaml | 5 +++++ plugs/core/template.ts | 41 +++++++++++++++++++++++++++++++++++++++ website/CHANGELOG.md | 1 + website/🔌 Core.md | 7 ++++++- 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/plugs/core/core.plug.yaml b/plugs/core/core.plug.yaml index 8f170101..b409872c 100644 --- a/plugs/core/core.plug.yaml +++ b/plugs/core/core.plug.yaml @@ -203,6 +203,11 @@ functions: command: name: "Open Daily Note" key: "Alt-Shift-d" + weeklyNoteCommand: + path: ./template.ts:weeklyNoteCommand + command: + name: "Open Weekly Note" + key: "Alt-Shift-w" instantiateTemplateCommand: path: ./template.ts:instantiateTemplateCommand diff --git a/plugs/core/template.ts b/plugs/core/template.ts index efed223c..d717377d 100644 --- a/plugs/core/template.ts +++ b/plugs/core/template.ts @@ -152,6 +152,47 @@ export async function dailyNoteCommand() { } } +function getWeekStartDate(monday = false) { + const d = new Date(); + const day = d.getDay(); + let diff = d.getDate() - day; + if (monday) { + diff += day == 0 ? -6 : 1; + } + return new Date(d.setDate(diff)); +} + +export async function weeklyNoteCommand() { + const { weeklyNoteTemplate, weeklyNotePrefix, weeklyNoteMonday } = + await readSettings({ + weeklyNoteTemplate: "template/page/Weekly Note", + weeklyNotePrefix: "🗓️ ", + weeklyNoteMonday: false, + }); + let weeklyNoteTemplateText = ""; + try { + weeklyNoteTemplateText = await space.readPage(weeklyNoteTemplate); + } catch { + console.warn(`No weekly note template found at ${weeklyNoteTemplate}`); + } + const date = niceDate(getWeekStartDate(weeklyNoteMonday)); + const pageName = `${weeklyNotePrefix}${date}`; + if (weeklyNoteTemplateText) { + try { + await space.getPageMeta(pageName); + } catch { + // Doesn't exist, let's create + await space.writePage( + pageName, + replaceTemplateVars(weeklyNoteTemplateText, pageName), + ); + } + await editor.navigate(pageName); + } else { + await editor.navigate(pageName); + } +} + export async function insertTemplateText(cmdDef: any) { const cursorPos = await editor.getCursor(); const page = await editor.getCurrentPage(); diff --git a/website/CHANGELOG.md b/website/CHANGELOG.md index 5dda708d..3c2f8129 100644 --- a/website/CHANGELOG.md +++ b/website/CHANGELOG.md @@ -10,6 +10,7 @@ release. * New directive `#eval` see [[🔌 Directive@eval]] * New PlugOS feature: redirecting function calls. Instead of specifying a `path` for a function, you can now specify `redirect` pointing to another function name, either in the same plug using the `plugName.functionName` syntax. * `Cmd-click` or `Ctrl-click` now opens page references in a new window. You can `Alt-click` to put your cursor at the target without navigation. +* New {[Open Weekly Note]} command (weeks start on Sunday by default, to allow for planning, but you can change this to Monday by setting the `weeklyNoteMonday` to `true` in [[Settings]]). Like for {[Open Daily Note]} you can create a template in `template/page/Weekly Note`. * The `Create page` option when navigating pages now always appears as the _second_ option. Let me know how you like it. * New `Preview` using a custom markdown renderer offering a lot of extra flexibility (and a much smaller file size). New thing it does: * Render front matter in a table diff --git a/website/🔌 Core.md b/website/🔌 Core.md index 8da88a1a..8e3261a9 100644 --- a/website/🔌 Core.md +++ b/website/🔌 Core.md @@ -65,7 +65,12 @@ 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. +with a 📅 emoji by default, but this is configurable via the `dailyNotePrefix` setting in `SETTINGS`. If you have a page template (see above) named `templates/page/Daily Note` it will use this as a template, otherwise, the page will just be empty. + +### Weekly Note + +The {[Open Weekly Note]} command navigates (or creates) a weekly note prefixed +with a 🗓️ emoji by default, but this is configurable via the `weeklyNotePrefix` setting in `SETTINGS`. If you have a page template (see above) named `templates/page/Weekly Note` it will use this as a template, otherwise, the page will just be empty. ### Quick Note