Skip to content

Instantly share code, notes, and snippets.

@AntonNiklasson
Last active February 27, 2024 21:29
Show Gist options
  • Save AntonNiklasson/977ab4feb7c520dc668f4d7c263785ac to your computer and use it in GitHub Desktop.
Save AntonNiklasson/977ab4feb7c520dc668f4d7c263785ac to your computer and use it in GitHub Desktop.
Generate a list of the daily notes for a weekly note template in Obsidian
module.exports = (title) => {
if (!/^\d{4}-W\d{2}$/.test(title)) {
throw Error(`Invalid file title. Received: ${title}`);
}
const [year, week] = title.split("-");
const days = getISOWeekDates(week.substr(1), year);
return days
.map((day) =>
link(
`calendar/days/${day.format("YYYY-MM-DD")}`,
//`${day.format("dddd")} (${day.format("YYYY-MM-DD")})`,
),
)
.map((day) => `- ${day}`)
.join("\n");
};
function getMoment() {
if (typeof window !== "undefined" && window.moment !== undefined) {
return window.moment;
}
return require("moment")();
}
function getISOWeekDates(isoWeekNum, year) {
let date = getMoment()
.year(year)
.isoWeek(isoWeekNum)
.startOf("isoWeek")
.add(-1, "day");
const days = [];
for (let i = 0; i < 7; i++) {
days.push(date);
date = date.add(1, "days").clone();
}
return days;
}
function link(path, label) {
if (label !== undefined) {
return `[[${path}|${label}]]`;
}
return `[[${path}]]`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment