diff --git a/caldav.plug.yaml b/caldav.plug.yaml index 42d676c..3c57dcc 100644 --- a/caldav.plug.yaml +++ b/caldav.plug.yaml @@ -1,6 +1,8 @@ name: caldav +requiredPermissions: + - fetch functions: - helloWorld: - path: caldav.ts:helloWorld - command: - name: "Say hello" + queryIcs: + path: ./caldav.ts:queryIcs + events: + - query:cal-ics diff --git a/caldav.ts b/caldav.ts index 569f139..51c0e45 100644 --- a/caldav.ts +++ b/caldav.ts @@ -1,5 +1,33 @@ import { editor } from "@silverbulletmd/silverbullet/syscalls"; +import { QueryProviderEvent } from "@silverbulletmd/silverbullet/types"; +import { readYamlPage } from "@silverbulletmd/silverbullet/lib/yaml_page"; +import { applyQuery } from "@silverbulletmd/silverbullet/lib/query"; +import { parseIcsCalendar, type VCalendar } from "ts-ics"; -export async function helloWorld() { - await editor.flashNotification("Hello world!"); +interface Event { + summary: string | undefined; + description: string | undefined; +} + +export async function queryIcs({ query }: QueryProviderEvent): Promise { + const secrets = await readYamlPage("SECRETS"); + const icsUrl = secrets.icsUrl; + 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) => { + return { + summary: ics.summary, + description: ics.description, + }; + }); + + return applyQuery(query, events); } diff --git a/deno.jsonc b/deno.jsonc index 29f889e..a2173b8 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -18,6 +18,8 @@ }, "imports": { "@silverbulletmd/silverbullet": "jsr:@silverbulletmd/silverbullet@^0.9.0", - "tsdav": "npm:tsdav@2.1.3" + "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" } } diff --git a/example.ts b/example.ts new file mode 100644 index 0000000..2536555 --- /dev/null +++ b/example.ts @@ -0,0 +1,15 @@ +import { createDAVClient } from "tsdav"; + +const client = await createDAVClient({ + serverUrl: + "https://calendar.google.com/calendar/ical/marqus34%40gmail.com/private-df2ea81c13f2fa2b9d837aef069effe6/basic.ics", + credentials: { + username: "exemplar", + password: Deno.env.get("DAV_PASSWORD"), + }, + authMethod: "Basic", + defaultAccountType: "caldav", +}); + +const calendars = await client.fetchCalendars(); +console.log(calendars);