Skip to main content

Example 4: Full Example

In this example, we will show a more complex method (taken from personal daily note command)


In this example, I have a custom plugin codedungeon.NotePlan which contains a number of commands, one of which is called cdWeekday which I invoke each day in my NotePlan Daily Calendar Note. There is quite a bit to this command, I will try to unpack every that is happening.

// @flow

// import NPTemplating plugin
import NPTemplating from 'NPTemplating'

export async function cdWeekday(userDate: any = ''): Promise<void> {
try {
const content: string = Editor.content || ''
Editor.insertTextAtCursor('Please wait...')

let pivotDate = userDate.length === 0 ? await cdGetSelectedCalendarDate() : userDate
pivotDate = moment(pivotDate).format('MM/DD/YYYY')

// create some templateData (data and methods) that will be used within template
const templateData = {
data: {
pivotDate,
yesterday: moment(pivotDate).subtract(1, 'days').format('YYYY-MM-DD'),
today: moment(pivotDate).format('YYYY-MM-DD'),
tomorrow: moment(pivotDate).add(1, 'days').format('YYYY-MM-DD'),
},
methods: {
handleRetrospect: async () => {
if (new Date(pivotDate).getDay() === 5) {
const retrospectNote = new DateModule().weekOf(moment(pivotDate).format('YYYY-MM-DD'))
const retroData = { ...templateData, retroMeetingDate: templateData.data.today }
await createRetrospectNote(retrospectNote, retroData)

// $FlowFixMe
return `\n#### πŸ—“ 3:00 PM - 4:00 PM - Retrospect\n- [[${retrospectNote}]]\n`
}
},
},
}

// invoke NPTemplating.renderTemplate method which loads template and processes
// using `templateData`
const result = await NPTemplating.renderTemplate('Weekday Overview', templateData)

// create Standup Note (see Focused - 9:30 AM - 10:00 AM)
await cdStandup(pivotDate)

// This replaces the `Please wait...' status message
Editor.replaceTextInCharacterRange(content + result, 0, 16384)

// scrolls cursor to top of note
Editor.highlightByIndex(0, 0)
} catch (error) {
cdLogError('cdWeekday', error)
}
}