Skip to content

Commit

Permalink
🐛 Fix ICS file generation with CLRF and absent fields (#553)
Browse files Browse the repository at this point in the history
* Fix ics file generation (CLRF, absent fields)

PRODID, UID, DTSTAMP

* Update src/index.ts

---------

Co-authored-by: Anand Chowdhary <github@anandchowdhary.com>
  • Loading branch information
MrsSima and AnandChowdhary committed Jul 19, 2023
1 parent bd51044 commit e4a2eaa
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export const ics = (calendarEvent: CalendarEvent): string => {
.replace(/(\\n)[\s\t]+/gm, "\\n");

const { start, end } = formatTimes(event, event.allDay ? "allDay" : "dateTimeUTC");
const dateStamp = dayjs(new Date()).utc().format(TimeFormats["dateTimeUTC"]);
const calendarChunks = [
{
key: "BEGIN",
Expand All @@ -186,6 +187,10 @@ export const ics = (calendarEvent: CalendarEvent): string => {
key: "VERSION",
value: "2.0",
},
{
key: "PRODID",
value: event.title
},
{
key: "BEGIN",
value: "VEVENT",
Expand All @@ -202,6 +207,10 @@ export const ics = (calendarEvent: CalendarEvent): string => {
key: "DTEND",
value: end,
},
{
key: "DTSTAMP",
value: dateStamp,
},
{
key: "RRULE",
value: event.rRule,
Expand All @@ -222,6 +231,10 @@ export const ics = (calendarEvent: CalendarEvent): string => {
key: "ORGANIZER",
value: event.organizer,
},
{
key: "UID",
value: Math.floor(Math.random() * 100000).toString().replace(".", ""),
},
{
key: "END",
value: "VEVENT",
Expand All @@ -239,10 +252,10 @@ export const ics = (calendarEvent: CalendarEvent): string => {
if (chunk.key == "ORGANIZER") {
const value = chunk.value as CalendarEventOrganizer;
calendarUrl += `${chunk.key};${encodeURIComponent(
`CN=${value.name}:MAILTO:${value.email}\n`
`CN=${value.name}:MAILTO:${value.email}\r\n`
)}`;
} else {
calendarUrl += `${chunk.key}:${encodeURIComponent(`${chunk.value}\n`)}`;
calendarUrl += `${chunk.key}:${encodeURIComponent(`${chunk.value}\r\n`)}`;
}
}
});
Expand Down

0 comments on commit e4a2eaa

Please sign in to comment.