From 95efb9701692ac321f59a544a89d6e2661caf323 Mon Sep 17 00:00:00 2001 From: David Wertheimer Date: Thu, 9 Nov 2023 17:23:29 -0800 Subject: [PATCH] Updated Templating --- np.Templating/CHANGELOG.md | 6 ++++ np.Templating/plugin.json | 59 +++++++++++++++++++-------------- np.Templating/src/Templating.js | 38 ++++++++++++++++----- 3 files changed, 71 insertions(+), 32 deletions(-) diff --git a/np.Templating/CHANGELOG.md b/np.Templating/CHANGELOG.md index b33a93094..25c584415 100644 --- a/np.Templating/CHANGELOG.md +++ b/np.Templating/CHANGELOG.md @@ -4,6 +4,12 @@ See Plugin [README](https://github.com/NotePlan/plugins/blob/main/np.Templating/README.md) for details on available commands and use case. +## [1.10.0] 2023-10-31 @dwertheimer + +- Command name changes require NotePlan 3.9.10 +- Change commands to be more descriptive (using @eduardme's new naming scheme) +- Tweak np:invoke to work correctly with frontmatter + ## [1.9.12] 2023-10-24 @dwertheimer - Changed title to just 'Templating'] diff --git a/np.Templating/plugin.json b/np.Templating/plugin.json index 0c41a9465..e73e1a75a 100644 --- a/np.Templating/plugin.json +++ b/np.Templating/plugin.json @@ -1,10 +1,10 @@ { "macOS.minVersion": "10.13.0", - "noteplan.minAppVersion": "3.5.0", + "noteplan.minAppVersion": "3.9.10", "plugin.id": "np.Templating", "plugin.name": "📒 Templating", - "plugin.version": "1.9.12", - "plugin.lastUpdateInfo": "1.9.12: Changed title to just 'Templating'.\n1.9.11: add 'todayProgressFromTemplate' template command.\n1.9.10: add folder attribute to np:new for xcallback creation inside a specific folder", + "plugin.version": "1.10.0", + "plugin.lastUpdateInfo": "1.10.0: Change command names to be more descriptive.\n1.9.12: Changed title to just 'Templating'.\n1.9.11: add 'todayProgressFromTemplate' template command.\n1.9.10: add folder attribute to np:new for xcallback creation inside a specific folder", "plugin.description": "Templating Plugin for NotePlan", "plugin.author": "Mike Erickson (@codedungeon)", "plugin.dependencies": [], @@ -13,12 +13,13 @@ "plugin.url": "https://nptemplating-docs.netlify.app/docs/intro", "plugin.commands": [ { - "name": "np:append", + "name": "Append template to end of current note", "alias": [ "append", "at", "template", - "npa" + "npa", + "np:append" ], "description": "Append template to end of current note", "jsFunction": "templateAppend", @@ -27,37 +28,42 @@ ] }, { - "name": "np:insert", + "name": "Insert template at cursor position", "alias": [ "insert", "it", "template", - "npi" + "npi", + "np:insert" ], - "description": "Insert template to current note", + "description": "Insert template in current note at cursor position", "jsFunction": "templateInsert", "arguments": [ "template title to run" ] }, { - "name": "np:invoke", + "name": "Invoke/Place template at in template", "alias": [ "invoke", - "npv" + "npv", + "np:invoke", + "place" ], - "description": "Place template using attribute in template", - "jsFunction": "templateInvoke" + "description": "Place template using attribute in template frontmatter", + "jsFunction": "templateInvoke", + "arguments":["Template name to execute (optional)"] }, { - "name": "np:new", + "name": "Create new note using template", "alias": [ "new", "nn", "template", - "npn" + "npn", + "np:new" ], - "description": "Create new note with title, using folder attribute", + "description": "Create new note with title, optionally using folder and newNoteTitle attribute", "jsFunction": "templateNew", "arguments": [ "template title to run (or if you know the folder but want to be prompted at run-time for the template, enter a single space for this argument)", @@ -65,7 +71,7 @@ ] }, { - "name": "np:qtn", + "name": "New note using Quick Note Template", "alias": [ "Quick template note", "quick", @@ -73,7 +79,8 @@ "qnt", "qtn", "qqq", - "npq" + "npq", + "np:qtn" ], "description": "Create new note based on Quick Note Template", "jsFunction": "templateQuickNote", @@ -82,7 +89,7 @@ ] }, { - "name": "np:mtn", + "name": "Create Meeting Note using Meeting Note Template", "alias": [ "Meeting template note", "quick", @@ -90,7 +97,8 @@ "mnt", "mtn", "mmm", - "npm" + "npm", + "np:mtn" ], "description": "Create new note based on Meeting Note Template", "hidden": true, @@ -114,7 +122,8 @@ "npc" ], "description": "Convert Project Note to Frontmatter format", - "jsFunction": "templateConvertNote" + "jsFunction": "templateConvertNote", + "hidden": true }, { "name": "np:about", @@ -123,12 +132,14 @@ "jsFunction": "templateAbout" }, { - "name": "np:execute", + "name": "Execute template script (type: template-fragment)", "alias": [ - "npe" + "npe", + "np:execute" ], - "description": "Execute Template Script", - "jsFunction": "templateExecute" + "description": "Execute Template Script (type: template-fragment) which may or may not output text to current note", + "jsFunction": "templateExecute", + "arguments":["Template name to execute (optional)"] }, { "name": "np:test", diff --git a/np.Templating/src/Templating.js b/np.Templating/src/Templating.js index df75893a8..05c224431 100644 --- a/np.Templating/src/Templating.js +++ b/np.Templating/src/Templating.js @@ -14,6 +14,7 @@ import { timestamp } from '@templatingModules/DateModule' import { getTemplateFolder } from 'NPTemplating' import { helpInfo } from '../lib/helpers' import { getSetting } from '@helpers/NPConfiguration' +import { smartPrependPara, smartAppendPara } from '@helpers/paragraph' // helpers import { getWeatherSummary } from '../lib/support/modules/weatherSummary' @@ -32,6 +33,7 @@ import DateModule from '../lib/support/modules/DateModule' // Editor import { templateFileByTitleEx } from './NPEditor' +import { getNoteByFilename } from '../../helpers/note' export async function init(): Promise { try { @@ -146,13 +148,22 @@ export async function templateAppend(templateName: string = ''): Promise { } } -export async function templateInvoke(): Promise { +export async function templateInvoke(templateName?: string): Promise { try { if (Editor.type === 'Notes' || Editor.type === 'Calendar') { const content: string = Editor.content || '' + let selectedTemplateFilename + if (templateName) { + const notes = await DataStore.projectNoteByTitle(templateName, true, true) + if (notes?.length) { + selectedTemplateFilename = notes[0].filename + } else { + logError(pluginJson, `Unable to locate template: ${templateName} which was passed to templateExecute`) + } + } // $FlowIgnore - const selectedTemplate = await NPTemplating.chooseTemplate() + const selectedTemplate = selectedTemplateFilename ?? (await NPTemplating.chooseTemplate()) const templateData = await NPTemplating.getTemplate(selectedTemplate) let { frontmatterBody, frontmatterAttributes } = await NPTemplating.preRender(templateData) let data = { ...frontmatterAttributes, frontmatter: { ...frontmatterAttributes } } @@ -164,10 +175,12 @@ export async function templateInvoke(): Promise { switch (location) { case 'append': - Editor.insertTextAtCharacterIndex(`\n` + renderedTemplate, content.length) + // Editor.insertTextAtCharacterIndex(`\n` + renderedTemplate, content.length) + smartAppendPara(Editor, renderedTemplate, 'text') break case 'prepend': - Editor.insertTextAtCharacterIndex(renderedTemplate, 0) + // Editor.insertTextAtCharacterIndex(renderedTemplate, 0) + smartPrependPara(Editor, renderedTemplate, 'text') break case 'insert': case 'cursor': @@ -616,11 +629,20 @@ export async function templateConvertNote(): Promise { } } -export async function templateExecute(): Promise { +export async function templateExecute(templateName?: string): Promise { try { - let selectedTemplate = await NPTemplating.chooseTemplate('template-fragment', 'Choose Template Fragment', { templateGroupTemplatesByFolder: false }) - - await NPTemplating.renderTemplate(selectedTemplate) + let selectedTemplateFilename + if (templateName) { + const notes = await DataStore.projectNoteByTitle(templateName, true, true) + if (notes?.length) { + selectedTemplateFilename = notes[0].filename + } else { + logError(pluginJson, `Unable to locate template: ${templateName} which was passed to templateExecute`) + } + } + selectedTemplateFilename = + selectedTemplateFilename ?? (await NPTemplating.chooseTemplate('template-fragment', 'Choose Template Fragment', { templateGroupTemplatesByFolder: false })) + await NPTemplating.renderTemplate(selectedTemplateFilename) } catch (error) { logError(pluginJson, error.message) }