From 102b05f53404aab359619f76b8a1ef8383453428 Mon Sep 17 00:00:00 2001 From: sstent Date: Thu, 19 Feb 2026 09:49:47 -0800 Subject: [PATCH] Fix: Expand recurrences from event start but filter to last 7 days and iterate to 0.3.28 --- PLUG.md | 2 +- deno.json | 2 +- icalendar.plug.yaml | 2 +- icalendar.ts | 33 +++++++++++++++++---------------- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/PLUG.md b/PLUG.md index 2b76e26..dfae538 100644 --- a/PLUG.md +++ b/PLUG.md @@ -1,6 +1,6 @@ --- name: Library/sstent/icalendar -version: "0.3.27" +version: "0.3.28" tags: meta/library files: - icalendar.plug.js diff --git a/deno.json b/deno.json index 978b597..4121d9f 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "icalendar-plug", - "version": "0.3.27", + "version": "0.3.28", "nodeModulesDir": "auto", "tasks": { "sync-version": "deno run -A scripts/sync-version.ts", diff --git a/icalendar.plug.yaml b/icalendar.plug.yaml index f3a0ab8..176ee37 100644 --- a/icalendar.plug.yaml +++ b/icalendar.plug.yaml @@ -1,5 +1,5 @@ name: icalendar -version: 0.3.27 +version: 0.3.28 author: sstent index: icalendar.ts # Legacy SilverBullet permission name diff --git a/icalendar.ts b/icalendar.ts index 72081a8..2a575b6 100644 --- a/icalendar.ts +++ b/icalendar.ts @@ -3,7 +3,7 @@ import { convertIcsCalendar } from "https://esm.sh/ts-ics@2.4.0"; import { RRule, RRuleSet } from "rrule"; import { getUtcOffsetMs, resolveIanaName } from "./timezones.ts"; -const VERSION = "0.3.27"; +const VERSION = "0.3.28"; const CACHE_KEY = "icalendar:lastSync"; console.log(`[iCalendar] Plug script executing at top level (Version ${VERSION})`); @@ -176,24 +176,25 @@ export function expandRecurrences(icsEvent: any, windowDays = 365): any[] { } const now = new Date(); - // Start the window 7 days ago to catch recent past events - const windowStart = new Date(now.getTime() - 7 * 86400000); + // Start our visible window 7 days ago to catch recent past events + const filterStart = new Date(now.getTime() - 7 * 86400000); const windowEnd = new Date(now.getTime() + windowDays * 86400000); - // Expand from the start of our window - const occurrences = set.between(windowStart, windowEnd, true); + // Expand from the event's actual start date to ensure all recurrences are calculated correctly + // but only take occurrences between (now - 7 days) and (now + windowDays) + const occurrences = set.between(dtstart, windowEnd, true); - if (occurrences.length === 0) return [icsEvent]; - - return occurrences.map(occurrenceDate => { - const localIso = localDateString(occurrenceDate); - return { - ...icsEvent, - start: localIso, - recurrent: true, - rrule: undefined, - }; - }); + return occurrences + .filter(occurrenceDate => occurrenceDate >= filterStart) + .map(occurrenceDate => { + const localIso = localDateString(occurrenceDate); + return { + ...icsEvent, + start: localIso, + recurrent: true, + rrule: undefined, + }; + }); } catch (err) { console.error(`[iCalendar] Error expanding recurrence for ${icsEvent.summary}:`, err); return [icsEvent];