Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions helpers/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
RE_YEARLY_NOTE_FILENAME,
isValidCalendarNoteFilename,
isValidCalendarNoteTitleStr,
convertISOToYYYYMMDD,
} from '@helpers/dateTime'
import { clo, clof, JSP, logDebug, logError, logInfo, logWarn } from '@helpers/dev'
import { getFolderListMinusExclusions, getFolderFromFilename, getRegularNotesInFolder, projectNotesFromFilteredFolders } from '@helpers/folders'
Expand Down Expand Up @@ -48,6 +47,7 @@ export function setTitle(note: CoreNoteFields, title: string): void {
if (fmFields.hasOwnProperty('title')) {
const newFmFields = { ...fmFields }
newFmFields.title = title
// $FlowIgnore(incompatible-call)
updateFrontMatterVars(note, newFmFields, true)
titleIsChanged = true
} else {
Expand Down Expand Up @@ -190,10 +190,15 @@ export async function getNote(name?: string, onlyLookInRegularNotes?: boolean |
const hasExtension = noteName.endsWith('.md') || noteName.endsWith('.txt')
const hasFolder = noteName.includes('/')
const isCalendarNote = isValidCalendarNoteFilename(noteName) || isValidCalendarNoteTitleStr(noteName)
logDebug('note/getNote', ` isCalendarNote=${String(isCalendarNote)} ${isValidCalendarNoteFilename(noteName)} ${isValidCalendarNoteTitleStr(noteName)}`)
logDebug(
'note/getNote',
` Will try to open filename: "${name} (${noteName})" using ${onlyLookInRegularNotes ? 'projectNoteByFilename' : 'noteByFilename'} ${hasExtension ? '' : ' (no extension)'} ${
` isCalendarNote=${String(isCalendarNote)} isValidCalendarNoteFilename=${String(isValidCalendarNoteFilename(noteName))} isValidCalendarNoteTitleStr=${String(
isValidCalendarNoteTitleStr(noteName),
)}`,
)
logDebug(
'note/getNote',
` Will try to open: "${name} (${noteName})" using ${onlyLookInRegularNotes ? 'projectNoteByFilename' : 'noteByFilename'} ${hasExtension ? '' : ' (no extension)'} ${
hasFolder ? '' : ' (no folder)'
} ${isCalendarNote ? ' (calendar note)' : ''}`,
)
Expand All @@ -210,15 +215,15 @@ export async function getNote(name?: string, onlyLookInRegularNotes?: boolean |
}
} else {
// not a filename, so try to find a note by title
logDebug('note/getNote', ` Trying to find note by title ${noteName} ${isCalendarNote ? ' (calendar note)' : ''}`)
logDebug('note/getNote', ` Trying to find note by title "${noteName}" ${isCalendarNote ? ' (calendar note)' : ''}`)
if (isCalendarNote) {
logDebug('note/getNote', ` Trying to find calendar note by title ${noteName}`)
logDebug('note/getNote', ` Trying to find calendar note by title "${noteName}"`)
if (onlyLookInRegularNotes) {
logDebug('note/getNote', ` Trying to find calendar note by title ${name}`)
// deal with the edge case of someone who has a project note with a title that could be a calendar note
const potentialNotes = DataStore.projectNoteByTitle(name)
if (potentialNotes && potentialNotes.length > 0) {
theNote = potentialNotes.find((n) => n.filename.startsWith(filePathStartsWith))
theNote = potentialNotes.find((n) => n.filename.startsWith(filePathStartsWith || ''))
}
} else {
logDebug('note/getNote', ` Trying to find calendar note by date string ${noteName}`)
Expand Down Expand Up @@ -250,11 +255,9 @@ export async function getNote(name?: string, onlyLookInRegularNotes?: boolean |
theNote = filteredNotes.length > 0 ? filteredNotes[0] : null

logDebug(
` Found ${potentialNotes.length} notes by title "${noteName}"; ${
filteredNotes.length
} matched path "${pathWithoutTitle}" and filePathStartsWith "${filePathStartsWith}" (${theNote?.filename || ''}); others were: [${potentialNotes
.map((n) => n.filename)
.join(', ')}]`,
` Found ${potentialNotes.length} notes by title "${noteName}"; ${filteredNotes.length} matched path "${pathWithoutTitle}" and filePathStartsWith "${
filePathStartsWith || ''
}" (${theNote?.filename || ''}); others were: [${potentialNotes.map((n) => n.filename).join(', ')}]`,
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions np.Templating/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

See Plugin [Documentation](https://noteplan.co/templates/docs) for details on available commands and use case.

## [2.0.9] 2025-07-27 @dwertheimer
- Fix xcallback bug in append and insert

## [2.0.8] 2025-07-19 @dwertheimer
- Insert/AppendTemplate on a blank notewith folder will create a new note in the folder and move the current note to the trash
- Insert/AppendTemplate on a non-blank note with folder will prompt the user whether to move the current note to the folder
Expand Down
4 changes: 2 additions & 2 deletions np.Templating/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"noteplan.minAppVersion": "3.9.10",
"plugin.id": "np.Templating",
"plugin.name": "📒 Templating",
"plugin.version": "2.0.8",
"plugin.lastUpdateInfo": "2.0.8: Fix edge case where `getValuesForKey` was not working correctly",
"plugin.version": "2.0.9",
"plugin.lastUpdateInfo": "2.0.9: fix xcallback but in append and insert",
"plugin.description": "Templating Plugin for NotePlan",
"plugin.author": "Mike Erickson ( codedungeon)",
"plugin.dependencies": [],
Expand Down
22 changes: 16 additions & 6 deletions np.Templating/src/Templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getCodeBlocksOfType } from '@helpers/codeBlocks'
import NPTemplating from 'NPTemplating'
import FrontmatterModule from '@templatingModules/FrontmatterModule'
import { parseObjectString, validateObjectString } from '@helpers/stringTransforms'

import { getNote } from '@helpers/note'
import { getTemplateFolder } from '../lib/config/configManager'
import { helpInfo } from '../lib/helpers'
import { getSetting } from '@helpers/NPConfiguration'
Expand Down Expand Up @@ -115,9 +115,19 @@ export async function templateInsert(templateName: string = ''): Promise<void> {
try {
if (Editor.type === 'Notes' || Editor.type === 'Calendar') {
const selectedTemplate = templateName.length > 0 ? templateName : await NPTemplating.chooseTemplate()

const templateNote = await DataStore.projectNoteByFilename(selectedTemplate)
const templateData = templateNote?.content || ''
let templateData, templateNote
if (/<current>/i.test(selectedTemplate)) {
if (!Editor.filename.startsWith(`@Templates`)) {
logError(pluginJson, `You cannot use the <current> prompt in a template that is not located in the @Templates folder; Editor.filename=${Editor.filename}`)
await showMessage(pluginJson, `OK`, `You cannot use the <current> prompt in a template that is not located in the @Templates folder`)
return
}
templateNote = Editor.note
templateData = Editor.content
} else {
templateNote = await getNote(selectedTemplate, true, `@Templates`)
templateData = templateNote?.content || ''
}
const { frontmatterBody, frontmatterAttributes } = await NPTemplating.renderFrontmatter(templateData)

// Check if the template wants the note to be created in a folder (or with a new title) and if so, move the empty note to the trash and create a new note in the folder
Expand All @@ -137,9 +147,9 @@ export async function templateInsert(templateName: string = ''): Promise<void> {

export async function templateAppend(templateName: string = ''): Promise<void> {
try {
logDebug('templateAppend', `Starting templateAppend with templateName=${templateName}`)
if (Editor.type === 'Notes' || Editor.type === 'Calendar') {
const content: string = Editor.content || ''

// $FlowIgnore
const selectedTemplate = templateName.length > 0 ? templateName : await NPTemplating.chooseTemplate()
let templateData, templateNote
Expand All @@ -152,7 +162,7 @@ export async function templateAppend(templateName: string = ''): Promise<void> {
templateNote = Editor.note
templateData = Editor.content
} else {
templateNote = await DataStore.projectNoteByFilename(selectedTemplate)
templateNote = await getNote(selectedTemplate, true, `@Templates`)
templateData = templateNote?.content || ''
}

Expand Down
Loading