diff --git a/caldav.plug.yaml b/caldav.plug.yaml index 3c57dcc..1709f98 100644 --- a/caldav.plug.yaml +++ b/caldav.plug.yaml @@ -3,6 +3,6 @@ requiredPermissions: - fetch functions: queryIcs: - path: ./caldav.ts:queryIcs + path: ./caldav.ts:queryEvents events: - - query:cal-ics + - query:cal-events diff --git a/caldav.ts b/caldav.ts index 51c0e45..192ae26 100644 --- a/caldav.ts +++ b/caldav.ts @@ -7,27 +7,70 @@ import { parseIcsCalendar, type VCalendar } from "ts-ics"; interface Event { summary: string | undefined; description: string | undefined; + created: string | undefined; + lastModified: string | undefined; + start: string | undefined; + end: string | undefined; } -export async function queryIcs({ query }: QueryProviderEvent): Promise { +export async function queryEvents( + { query }: QueryProviderEvent, +): Promise { const secrets = await readYamlPage("SECRETS"); const icsUrl = secrets.icsUrl; + // TODO: Loop over multiple sources (like CalDAV, .ics URLs) and assign calendar name based on X-WR-CALNAME const result = await fetch(icsUrl); const icsData = await result.text(); const calendarParsed: VCalendar = parseIcsCalendar(icsData); - // TODO: Loop over multiple .ics URLs and assign calendar name based on X-WR-CALNAME if (calendarParsed.events === undefined) { editor.flashNotification("Did not parse events from iCalendar"); return []; } - const events: Event[] = calendarParsed.events.map((ics) => { + let events: Event[] = calendarParsed.events.map((ics) => { return { summary: ics.summary, description: ics.description, + // Mimic properties of pages + created: ics.created ? localDateString(ics.created.date) : undefined, + lastModified: ics.lastModified + ? localDateString(ics.lastModified.date) + : undefined, + // Consistent with formats above + start: localDateString(ics.start.date), + end: ics.end ? localDateString(ics.end.date) : undefined, }; }); - return applyQuery(query, events); + // FIXME: Fails whenever any filters are applied, but same code works for github plug + events = applyQuery(query, events); + return events; +} + +// Copied from @silverbulletmd/silverbullet/lib/dates.ts which is not exported in the package +export function localDateString(d: Date): string { + return d.getFullYear() + + "-" + String(d.getMonth() + 1).padStart(2, "0") + + "-" + String(d.getDate()).padStart(2, "0") + + "T" + String(d.getHours()).padStart(2, "0") + + ":" + String(d.getMinutes()).padStart(2, "0") + + ":" + String(d.getSeconds()).padStart(2, "0") + + "." + String(d.getMilliseconds()).padStart(3, "0"); +} + +// didn't help +function flattenObject(obj: any, prefix = ""): any { + let result: any = {}; + for (let [key, value] of Object.entries(obj)) { + if (prefix) { + key = prefix + "_" + key; + } + if (value && typeof value === "object") { + result = { ...result, ...flattenObject(value, key) }; + } else { + result[key] = value; + } + } + return result; } diff --git a/deno.jsonc b/deno.jsonc index a2173b8..eb26172 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -17,7 +17,7 @@ ] }, "imports": { - "@silverbulletmd/silverbullet": "jsr:@silverbulletmd/silverbullet@^0.9.0", + "@silverbulletmd/silverbullet": "jsr:@silverbulletmd/silverbullet@^0.10.1", "tsdav": "npm:tsdav@2.1.3", "ts-ics": "npm:ts-ics@1.6.5", "@js-temporal/polyfill": "https://esm.sh/@js-temporal/polyfill@0.4.4"