Skip to content

Instantly share code, notes, and snippets.

@jsliang
Last active May 8, 2024 19:05
Show Gist options
  • Save jsliang/110e611820ea55562a6f60279b77465e to your computer and use it in GitHub Desktop.
Save jsliang/110e611820ea55562a6f60279b77465e to your computer and use it in GitHub Desktop.
Yearly MOC Generator - Obsidian Templater User Script
/**
* @author Jui-Shan (Jenny) Liang <jsliang.tw@gmail.com>
* @param {Object} tp the `tp` object of the Templater plugin
* @param {Object} options options for configuring the year to generate the MOC and the file name format
* @param {string} options.YEAR what year to generate the MOC; default to the current year
* @param {string} options.YEARLY_FORMAT filename format of yearly notes; format reference: https://momentjs.com/docs/#/displaying/format/
* @param {string} options.QUARTER_FORMAT filename format of quarterly notes; format reference: https://momentjs.com/docs/#/displaying/format/
* @param {string} options.MONTH_FORMAT filename format of monthly notes; format reference: https://momentjs.com/docs/#/displaying/format/
* @param {string} options.WEEK_FORMAT filename format of weekly notes; format reference: https://momentjs.com/docs/#/displaying/format/
* @param {string} options.DAY_FORMAT filename format of daily notes; format reference: https://momentjs.com/docs/#/displaying/format/
* @returns {string} a string of MOC that links to all quarterly, monthly, weekly and daily notes
*/
function yearlyMoc(tp, options = {}) {
const {
YEAR = new Date().getFullYear(),
YEARLY_FORMAT = "YYYY",
QUARTER_FORMAT = "YYYY-[Q]Q",
MONTH_FORMAT = "YYYY-MM",
WEEK_FORMAT = "GGGG-[W]WW",
DAY_FORMAT = "YYYY-MM-DD",
} = options;
const quarterPadding = "\t";
const monthPadding = "\t\t";
const weekPadding = "\t\t\t";
const dayPadding = "\t\t\t\t";
let ret = "";
// add link to year note
const yearFilename = tp.date.now(
YEARLY_FORMAT,
0,
`${YEAR}-01-01`,
"YYYY-MM-DD"
);
ret += `- [[${yearFilename}]]\n`;
let i = 0;
// iterate over each day of the year
for (i = 0; i < 366; i++) {
// get file name of current day
const dayFilename = tp.date.now(
DAY_FORMAT,
i,
`${YEAR}-01-01`,
"YYYY-MM-DD"
);
// stop if current day is not within this year
if (!dayFilename.startsWith(YEAR)) break;
const reference = [dayFilename, DAY_FORMAT];
// add link to quarter note
const quarterFilename = tp.date.now(QUARTER_FORMAT, 0, ...reference);
if (!ret.contains(quarterFilename)) {
ret += quarterPadding + `- [[${quarterFilename}]]\n`;
}
// add link to month note
const monthFilename = tp.date.now(MONTH_FORMAT, 0, ...reference);
if (!ret.contains(monthFilename)) {
ret += monthPadding + `- [[${monthFilename}]]\n`;
}
// add link to week note
const weekFilename = tp.date.now(WEEK_FORMAT, 0, ...reference);
const firstDayOfWeekFilename = tp.date.weekday(DAY_FORMAT, 0, ...reference);
if (
!ret.contains(weekFilename) ||
// add link to week note again if the first day of the month starts in the middle of the week
(dayFilename.endsWith("-01") && dayFilename != firstDayOfWeekFilename)
) {
ret += weekPadding + `- [[${weekFilename}]]\n`;
}
// add link to day note
ret += dayPadding + `- [[${dayFilename}]]\n`;
}
return ret;
}
module.exports = yearlyMoc;
@jsliang
Copy link
Author

jsliang commented Mar 9, 2022

How to use this Templater user script

You need to install the Templater plugin and add the yearlyMoc.js file under your user script folder.

Then create a template Yearly MOC with the content:

<%* tR += tp.user.yearlyMoc(tp) %>

Or if you need to customize the year and file formats:

<%* tR += tp.user.yearlyMoc(tp, {
YEAR: "2022",
YEAR_FORMAT: "YYYY-[Personal]",
QUARTER_FORMAT: "YYYY-[Q]Q-[Personal]",
MONTH_FORMAT: "YYYY-MM-[Personal]",
WEEK_FORMAT: "GGGG-[W]WW-[Personal]",
DAY_FORMAT: "YYYY-MM-DD-[Personal]",
}); %>

Then use this Yearly MOC template to create a yearly hierarchy note as mentioned in this forum post.

@Saorsa32
Copy link

I couldn't get it to work. Here is the error I got

VM134:16 CustomJS couldn't import js/yearlyMoc.js
eval @ VM134:16

VM134:16 SyntaxError: Unexpected identifier
at CustomJS.eval (eval at (app.js:1), :211:45)
at Generator.next ()
at fulfilled (eval at (app.js:1), :28:58)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment