diff --git a/docs/weekly-notes.md b/docs/weekly-notes.md index 05de797..168eedb 100644 --- a/docs/weekly-notes.md +++ b/docs/weekly-notes.md @@ -24,6 +24,31 @@ would create a page name of: For format, check out [this page](https://date-fns.org/v2.21.1/docs/format) for the syntax on what each symbol means. +## ISO Week Format + +Weekly Notes supports ISO week-numbering formats. Use the following tokens for ISO week-based naming: + +| Token | Description | Example | +|-------|-------------|---------| +| `I` | ISO week number (1-53) | `1`, `52` | +| `II` | ISO week number, 2 digits | `01`, `52` | +| `R` | ISO week-numbering year | `2024` | +| `RRRR` | ISO week-numbering year, 4 digits | `2024` | + +**Examples:** + +```plain text +{monday:RRRR}-W{monday:II} +``` +would create: `[[2024-W52]]` + +```plain text +Week {monday:I} {monday:RRRR} +``` +would create: `[[Week 52 2024]]` + +**Note:** ISO weeks always start on Monday. ISO week 1 is the week containing the first Thursday of the year, so dates in late December may belong to week 1 of the following year. Use `R`/`RRRR` (ISO week-numbering year) instead of `yyyy` (calendar year) when using ISO week numbers to ensure consistency. + # Auto Tagging When a new weekly page is created, the weekly page will be tagged in all of the daily pages that are part of the week. The tag will be added as the top block on the page. This could be toggled on and off in the `roam/js/weekly-notes` page. diff --git a/src/features/weekly-notes.ts b/src/features/weekly-notes.ts index a0a6cee..31191ae 100644 --- a/src/features/weekly-notes.ts +++ b/src/features/weekly-notes.ts @@ -240,56 +240,15 @@ export const toggleFeature = ( }; const hashListener = (newUrl: string) => { + // Clean up navigation buttons when navigating away document.getElementById("roamjs-weekly-mode-nav")?.remove?.(); + + // Clear format cache when visiting config page const urlUid = newUrl.match(/\/page\/(.*)$/)?.[1]; if (urlUid) { const title = getPageTitleByPageUid(urlUid); if (title === CONFIG) { formatCache.current = ""; - return; - } - const { dateArray, valid, formats } = getFormatDateData(title); - if (valid) { - const formattedDateArray = dateArray - .map((d, i) => ({ - cur: dateFnsFormat(d, formats[i]), - prev: dateFnsFormat(subWeeks(d, 1), formats[i]), - next: dateFnsFormat(addWeeks(d, 1), formats[i]), - })) - .filter( - (info): info is { cur: string; prev: string; next: string } => - !!info.cur && !!info.prev && !!info.next - ); - const prevTitle = formattedDateArray.reduce( - (acc, info) => acc.replace(info.cur, info.prev), - title - ); - const nextTitle = formattedDateArray.reduce( - (acc, info) => acc.replace(info.cur, info.next), - title - ); - setTimeout(() => { - const header = document.querySelector( - ".roam-article h1.rm-title-display" - ) as HTMLHeadingElement; - const headerContainer = header.parentElement; - const buttonContainer = document.createElement("div"); - buttonContainer.style.display = "flex"; - buttonContainer.style.justifyContent = "space-between"; - buttonContainer.style.marginBottom = "32px"; - buttonContainer.id = "roamjs-weekly-mode-nav"; - headerContainer?.appendChild(buttonContainer); - - const makeButton = (pagename: string, label: string) => { - const button = document.createElement("button"); - button.className = "bp3-button"; - button.onclick = () => navigateToPage(pagename); - button.innerText = label; - buttonContainer.appendChild(button); - }; - makeButton(prevTitle, "Last Week"); - makeButton(nextTitle, "Next Week"); - }); } } }; @@ -312,8 +271,9 @@ export const toggleFeature = ( className: "rm-title-display", callback: (header: HTMLElement) => { const title = getPageTitleValueByHtmlElement(header); - const { valid } = getFormatDateData(title); + const { dateArray, valid, formats } = getFormatDateData(title); if (valid) { + // Prevent title editing header.onmousedown = (e) => { if (!e.shiftKey) { renderToast({ @@ -323,6 +283,48 @@ export const toggleFeature = ( e.stopPropagation(); } }; + + // Remove any existing navigation buttons + document.getElementById("roamjs-weekly-mode-nav")?.remove?.(); + + // Calculate previous and next week titles + const formattedDateArray = dateArray + .map((d, i) => ({ + cur: dateFnsFormat(d, formats[i]), + prev: dateFnsFormat(subWeeks(d, 1), formats[i]), + next: dateFnsFormat(addWeeks(d, 1), formats[i]), + })) + .filter( + (info): info is { cur: string; prev: string; next: string } => + !!info.cur && !!info.prev && !!info.next + ); + const prevTitle = formattedDateArray.reduce( + (acc, info) => acc.replace(info.cur, info.prev), + title + ); + const nextTitle = formattedDateArray.reduce( + (acc, info) => acc.replace(info.cur, info.next), + title + ); + + // Create navigation buttons below the header + const headerContainer = header.parentElement; + const buttonContainer = document.createElement("div"); + buttonContainer.style.display = "flex"; + buttonContainer.style.justifyContent = "space-between"; + buttonContainer.style.marginBottom = "32px"; + buttonContainer.id = "roamjs-weekly-mode-nav"; + headerContainer?.appendChild(buttonContainer); + + const makeButton = (pagename: string, label: string) => { + const button = document.createElement("button"); + button.className = "bp3-button"; + button.onclick = () => navigateToPage(pagename); + button.innerText = label; + buttonContainer.appendChild(button); + }; + makeButton(prevTitle, "Last Week"); + makeButton(nextTitle, "Next Week"); } }, });