phase 3 - ical import/export
This commit is contained in:
43
src/lib/ical.ts
Normal file
43
src/lib/ical.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import ICAL from "ical.js";
|
||||
import type { CalendarEvent } from "@/lib/types";
|
||||
|
||||
export function parseICS(icsString: string): CalendarEvent[] {
|
||||
const jcalData = ICAL.parse(icsString);
|
||||
const comp = new ICAL.Component(jcalData);
|
||||
const vevents = comp.getAllSubcomponents("vevent");
|
||||
|
||||
return vevents.map((v) => {
|
||||
const ev = new ICAL.Event(v);
|
||||
return {
|
||||
id: ev.uid || crypto.randomUUID(),
|
||||
title: ev.summary || "Untitled Event",
|
||||
start: ev.startDate.toJSDate().toISOString().split("T")[0],
|
||||
end: ev.endDate
|
||||
? ev.endDate.toJSDate().toISOString().split("T")[0]
|
||||
: undefined,
|
||||
description: ev.description || "",
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function generateICS(events: CalendarEvent[]): string {
|
||||
const comp = new ICAL.Component(["vcalendar", [], []]);
|
||||
comp.addPropertyWithValue("version", "2.0");
|
||||
comp.addPropertyWithValue("prodid", "-//YourAppName//EN");
|
||||
|
||||
events.forEach((ev) => {
|
||||
const vevent = new ICAL.Component("vevent");
|
||||
vevent.addPropertyWithValue("uid", ev.id);
|
||||
vevent.addPropertyWithValue("summary", ev.title);
|
||||
vevent.addPropertyWithValue("dtstart", ICAL.Time.fromDateString(ev.start));
|
||||
if (ev.end) {
|
||||
vevent.addPropertyWithValue("dtend", ICAL.Time.fromDateString(ev.end));
|
||||
}
|
||||
if (ev.description) {
|
||||
vevent.addPropertyWithValue("description", ev.description);
|
||||
}
|
||||
comp.addSubcomponent(vevent);
|
||||
});
|
||||
|
||||
return comp.toString();
|
||||
}
|
||||
Reference in New Issue
Block a user