Skip to content

Commit

Permalink
Updated Templating
Browse files Browse the repository at this point in the history
  • Loading branch information
dwertheimer committed Nov 10, 2023
1 parent 5788611 commit 95efb97
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 32 deletions.
6 changes: 6 additions & 0 deletions np.Templating/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
59 changes: 35 additions & 24 deletions np.Templating/plugin.json
Original file line number Diff line number Diff line change
@@ -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": [],
Expand All @@ -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",
Expand All @@ -27,53 +28,59 @@
]
},
{
"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 <location> in template",
"alias": [
"invoke",
"npv"
"npv",
"np:invoke",
"place"
],
"description": "Place template using <location> attribute in template",
"jsFunction": "templateInvoke"
"description": "Place template using <location> 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)",
"folder to create note in (optional-leave empty to be prompted for it)"
]
},
{
"name": "np:qtn",
"name": "New note using Quick Note Template",
"alias": [
"Quick template note",
"quick",
"template",
"qnt",
"qtn",
"qqq",
"npq"
"npq",
"np:qtn"
],
"description": "Create new note based on Quick Note Template",
"jsFunction": "templateQuickNote",
Expand All @@ -82,15 +89,16 @@
]
},
{
"name": "np:mtn",
"name": "Create Meeting Note using Meeting Note Template",
"alias": [
"Meeting template note",
"quick",
"template",
"mnt",
"mtn",
"mmm",
"npm"
"npm",
"np:mtn"
],
"description": "Create new note based on Meeting Note Template",
"hidden": true,
Expand All @@ -114,7 +122,8 @@
"npc"
],
"description": "Convert Project Note to Frontmatter format",
"jsFunction": "templateConvertNote"
"jsFunction": "templateConvertNote",
"hidden": true
},
{
"name": "np:about",
Expand All @@ -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",
Expand Down
38 changes: 30 additions & 8 deletions np.Templating/src/Templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<void> {
try {
Expand Down Expand Up @@ -146,13 +148,22 @@ export async function templateAppend(templateName: string = ''): Promise<void> {
}
}

export async function templateInvoke(): Promise<void> {
export async function templateInvoke(templateName?: string): Promise<void> {
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 } }
Expand All @@ -164,10 +175,12 @@ export async function templateInvoke(): Promise<void> {

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':
Expand Down Expand Up @@ -616,11 +629,20 @@ export async function templateConvertNote(): Promise<void> {
}
}

export async function templateExecute(): Promise<void> {
export async function templateExecute(templateName?: string): Promise<void> {
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)
}
Expand Down

0 comments on commit 95efb97

Please sign in to comment.