Example 4: Full Example
In this example, we will show a more complex method (taken from personal daily note command)
- Plugin Code
- Template
- Output
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)
}
}
Weekday Overview Template
Templates / π§° Dungeon / Weekday Overview
# Weekday Overview
*****
# <%- date.format('ddd, MMM DD, YYYY', `${np.pivotDate}`) %>
*****
## π Overview
> βοΈ <%- web.weather() %>
> π β<%- web.advice() %>β
*****
## π§ Focused
#### βοΈ 5:00 AM - 6:00 AM - Planning & Solitude
<%- Bible.votd() %>
#### π§ 6:00 AM - 9:30 AM
- Follow-up Email, Slack, etc.
- Prepare for [[<%- date.format('YYYY-MM-DD', `${np.pivotDate}`) %> Standup]]
#### π§ 9:30 AM - 10:00 AM - Standup
- [[<%- date.format('YYYY-MM-DD', `${np.pivotDate}`) %> Standup]]
> π£ β<%- web.affirmation() %>β
#### π΄ 1:00 PM - 2:00 PM - Mental Break / Lunch
π± Lunch
<%- await handleRetrospect() %>
#### π 5:00 PM - 5:30 PM β Shutdown
π EOD Tasks
*****
## π Review
*Daily review, provide a summary of what was accomplished and what open items need to be carried over to tomorrow*
**π Tomorrow:** [[<%- np.tomorrow %>]]
Would produce the following output when the custom command is invoked on 2021-12-31
# Fri, Dec 31, 2021
*****
## π Overview
> βοΈ Fountain Valley, California, United States: βοΈ +58Β°F
> π βFor every complex problem there is an answer that is clear, simple, and wrong.β
*****
## π§ Focused
#### βοΈ 5:00 AM - 6:00 AM - Planning & Solitude
> ππ» Numbers 7:34
> π£ one male goat for a purification offering;
#### π§ 6:00 AM - 9:30 AM
- Prepare for [[2021-12-31 Standup]]
#### π§ 9:30 AM - 10:00 AM - Standup
- [[2021-12-31 Standup]]
> π£ βYou're a smart cookieβ
#### π 3:00 PM - 4:00 PM - Retrospect
- [[W52 (2021-12-26..2022-01-01)]]
*****
## π Review
**π Tomorrow:** [[2022-01-01]]
Template Breakdown
The breakdown of this template is as follows (for brevity, I have extracted only the lines which use template tags)
...
3: # <%- date.format('ddd, MMM DD, YYYY', `${np.pivotDate}`) %>
...
7: > βοΈ <%- weather() %>
8: > π β<%- advice() %>β
...
14: <%- await Bible.votd() %>
...
21: * Prepare for [[<%- date.format('YYYY-MM-DD', `${np.pivotDate}`) %> Standup]]
22: > π£ β<%- web.affirmation() %>β
...
26: <%- await handleRetrospect() %>
...
34: **π Tomorrow:** [[<%- np.tomorrow %>]]
Line | Description |
---|---|
3 | Displays date using DateModule.format method, and supply the date using data.pivotDate variable |
7 | Displays current weather using WebModule.weather method |
8 | Displays daily advice using WebModule.advice method |
14 | Displays personal Bible.votd method (Bible Plugin , verse of the day) |
See Templating Plugins section for information on creating custom np.Templating Plugins | |
18 | Task to prepare for current day "Stand Up" (internal work function) |
21 | Time Block: Link to current day "Stand Up" note |
22 | Displays daily affirmation using WebModule.affirmation method |
26 | Creates an additional time block entry for Friday retrospect, using handleRetrospect method (defined in templateData.methods) |
34 | Daily review section, link to tomorrow date |