Skip to content

Commit

Permalink
Update ICS exporter to encode reserved chars
Browse files Browse the repository at this point in the history
The ICS generator creates a data URI

Unescaped reserved chars like `#` and `?` can break the URL

Escaping these sections resolves this.

I changed the structure of the code to do this without double encoding.

Also updates tests to match

---

Fixes AnandChowdhary#84
  • Loading branch information
Paul-Hebert committed Sep 6, 2019
1 parent 18e351f commit db2cf6e
Showing 1 changed file with 55 additions and 14 deletions.
69 changes: 55 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,60 @@ export const ics = (event: CalendarEvent) => {
const end: string = dayjs(event.end)
.utc()
.format(format);
const calendarUrl: string = [
"BEGIN:VCALENDAR",
"VERSION:2.0",
"BEGIN:VEVENT",
`URL:${document.URL}`,
`DTSTART:${start}`,
`DTEND:${end}`,
`SUMMARY:${event.title}`,
`DESCRIPTION:${formattedDescription}`,
`LOCATION:${event.location}`,
"END:VEVENT",
"END:VCALENDAR"
].join("\n");
const calendarChunks = [
{
key: 'BEGIN',
value: 'VCALENDAR'
},
{
key: 'VERSION',
value: '2.0'
},
{
key: 'BEGIN',
value: 'VEVENT'
},
{
key: 'URL',
value: document.URL
},
{
key: 'DTSTART',
value: start
},
{
key: 'DTEND',
value: end
},
{
key: 'SUMMARY',
value: event.title
},
{
key: 'DESCRIPTION',
value: formattedDescription
},
{
key: 'LOCATION',
value: event.location
},
{
key: 'END',
value: 'VEVENT'
},
{
key: 'END',
value: 'VCALENDAR'
},
];

return encodeURI("data:text/calendar;charset=utf8," + calendarUrl);
let calendarUrl: string = '';

calendarChunks.forEach(chunk => {
if(chunk.value) {
calendarUrl += `${chunk.key}:${encodeURIComponent(`${chunk.value}\n`)}`;
}
});

return `data:text/calendar;charset=utf8,${calendarUrl}`;
};

0 comments on commit db2cf6e

Please sign in to comment.