Templater - generated list of weekly notes for specific quarter

What I’m trying to do

Hello, I want to create a list of weekly note links for a specific quarter. (Use case: When I manually create a quarter note through the periodic notes plugin I want the list of relevant weekly notes there.)

And sometimes year does have 53 weeks, so I want to take this into account in the code.

And it would be awesome if it used the template I specified for the newly created weekly notes.

Things I have tried

I have created this code but it is not working yet. But you get the idea. I appreciate any suggestions and help.

<%*
const currentYear = tp.date.now(“YYYY”); // Current year
const currentQuarter = Math.ceil((tp.date.now(“M”) - 1) / 3) + 1; // Calculate the current quarter

// Determine if the year has 52 or 53 weeks
const maxWeeksInYear = tp.date.now(“w”, `${currentYear}-12-31`) === “53” ? 53 : 52;

// Set start and end weeks for the current quarter
const startWeek = (currentQuarter - 1) * 13 + 1;
const endWeek = Math.min(currentQuarter * 13, maxWeeksInYear);

let weeklyNotes = ;

// Loop over each week in the quarter range
for (let week = startWeek; week <= endWeek; week++) {
const weekNoteName = `${currentYear}-W${week.toString().padStart(2, '0')}`;
const weekFilePath = `06 - Daily Journal/${currentYear}/${weekNoteName}`; // Define note path


// Check if the file exists, create if it doesn’t
if (!(await tp.file.exists(weekFilePath))) {
await tp.file.create_new(“”, weekNoteName, false, `06 - Daily Journal/${currentYear}`);
}

// Add the note link to the list with a readable label
weeklyNotes.push(`[[${weekNoteName}|Week ${week} ${currentYear}]]`);
}

// Output the weekly notes list for the current quarter
tR += “List of weekly notes:\n” + weeklyNotes.join(“\n”);
%>

I found solution by myself with help of AI and it works!
:

<%*
const title = tp.file.title;
const year = parseInt(title.split("-")[0]);
const quarter = parseInt(title.split("-")[1].replace("Q", "")); // Extrahuje kvartál

// Určuje první a poslední týden pro každý kvartál
const quarterWeeks = {
    1: { start: 1, end: 13 },     // 1. kvartál: týdny 1–13
    2: { start: 14, end: 26 },    // 2. kvartál: týdny 14–26
    3: { start: 27, end: 39 },    // 3. kvartál: týdny 27–39
    4: { start: 40, end: 53 }     // 4. kvartál: týdny 40–53
};

// Načte rozsah týdnů pro aktuální kvartál
const startWeek = quarterWeeks[quarter].start;
const endWeek = quarterWeeks[quarter].end;

// Zkontroluje, zda má rok 53 týdnů
const lastWeekOfYear = moment(`${year}-12-31`, "YYYY-MM-DD").isoWeeksInYear();
const actualEndWeek = endWeek > lastWeekOfYear ? lastWeekOfYear : endWeek;

// Definice měsíců pro kvartál
const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

tR += `# Weekly Notes for ${year}-Q${quarter}\n\n`;

let currentMonth = null;

// Pro každý týden v kvartálu
for (let week = startWeek; week <= actualEndWeek; week++) {
    const weekString = week < 10 ? `0${week}` : week; // Formátování týdne na dvě číslice
    const noteName = `${year}-W${weekString}`;
    
    // Zjištění, do jakého měsíce daný týden spadá
    const weekStartDate = moment().year(year).isoWeek(week).startOf("isoWeek");

    // Pokud se týden protíná s novým měsícem, aktualizujeme currentMonth
    const monthNumber = (week === 1 && weekStartDate.month() === 11) ? 0 : weekStartDate.month(); // První týden vždy k lednu

    // Pokud se změnil měsíc, přidáme odkaz na nový měsíc
    if (currentMonth !== monthNumber) {
        currentMonth = monthNumber;
        const monthName = monthNames[monthNumber];
        const monthLink = `[[${year}-M${String(monthNumber + 1).padStart(2, '0')} ${monthName}]]`;  // Odkaz na měsíční poznámku
        tR += `##### ${monthLink}\n`;
    }

    // Generuje odkaz na týdenní poznámku
    const weekLink = `[[${noteName}]]`;
    tR += `- ${weekLink}\n`;
}

// Přesun poznámky do správné složky
const newFolderPath = `06 - Daily Journal/${year}`;
const newNotePath = `${newFolderPath}/${title}`;

// Vytvoří složku, pokud ještě neexistuje, a přesune poznámku
await tp.file.move(newNotePath);
%>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.