Calendar Plugin

Name

Calendar

Description

Create a month calendar view

Icon

calendar_today

Instructions


This will create a current month view with the option to link to the daily jot note if it exists for a day under a configurable header that must exist (defaults to Calendar).

The whole section under the header will be replaced, but the rest of the note will be left intact.

Quick Start
- On any Note (e.g. a note meant for the Peek Viewer) create a heading (any of H1, H2, or H3) named Calendar
- From the Note menu, choose Calendar: Month
- Marvel at the calendar now appearing on your note.

Settings
Section Header
The section header name is configurable in the plugin settings. If you have content under this header make sure that there is another header in place above that content or it will be replaced by this plugin.

Link to Daily Jot
There is an setting to link each day to the Daily Jot not if it exists. You can set this to falseif you just want the month itself displayed.

Setting

Link to Daily Jot (true/false, default true)

Setting

Section header (default 'Calendar')


{
Settings: class {
constructor(dailyJotLink, sectionHeader) {
this.dailyJotLink = dailyJotLink;
this.sectionHeader = sectionHeader
}
},
 
constants: {
version: "1.0.0",
settingDailyJotLinkName: "Link to Daily Jot (true/false, default true)",
settingSectionHeaderName: "Section header (default 'Calendar')",
},
 
// --------------------------------------------------------------------------
// https://www.amplenote.com/help/developing_amplenote_plugins#noteOption
noteOption: {
"Month": async function(app) {
const settings = new this.Settings(
app.settings[this.constants.settingDailyJotLinkName] !== "false",
app.settings[this.constants.settingSectionHeaderName] || "Calendar",
);
 
const sections = await app.getNoteSections({ uuid: app.context.noteUUID });
const section = sections.find((section) => section.heading?.text === "Calendar");
if (section === undefined) {
app.alert("There needs to be a 'Calendar' section");
return;
}
 
const dailyJots = settings.dailyJotLink ? await this._getDailyJotsForMonth(app) : new Map();
app.replaceNoteContent({ uuid: app.context.noteUUID }, this._createMonthlyCalendar(dailyJots), { section });
},
},
 
// --------------------------------------------------------------------------
// Impure Functions
async _getDailyJotsForMonth(app) {
const today = new Date();
const month = today.toLocaleString("default", { month: "long" });
const year = today.getFullYear();
const dailyJots = await app.filterNotes({ tag: "daily-jots", query: `${month} ${year}` });
const map = dailyJots.reduce((map, jot) => {
map.set(jot.name.split(" ")[1].replace(/(st,|rd,|th,|nd,)/, ""), jot);
return map;
}, new Map());
return map;
},
 
// --------------------------------------------------------------------------
// Pure Functions
_createMonthlyCalendar(dailyJots) {
const today = new Date();
today.setDate(1);
const dayOfWeek = today.getDay();
const totalDays = (new Date(today.getFullYear(), today.getMonth() + 1, 0)).getDate();
const daysToPrint = Array.from(" ".repeat(dayOfWeek)).concat(Array.from({length: totalDays}, (e,i) => `${i + 1}`));
 
const reducer = (content, day, index) => {
const dayCell = dailyJots.has(day) ? `[${day}](https://www.amplenote.com/notes/${dailyJots.get(day).uuid})` : day;
return content +
"|" +
dayCell +
((index + 1) % 7 === 0 ? "|\n" : ""); // If we have reached Sunday start a new row
};
 
const initialValue = "|S|M|T|W|T|F|S|\n|-|-|-|-|-|-|-|-|\n";
 
const calendar = daysToPrint.reduce(reducer, initialValue);
return calendar;
},
}