Escape and fold the iCal export, serve it as text/calendar, add subscription links
- iCal export: RFC 5545 text escaping for SUMMARY, DESCRIPTION and LOCATION,
line folding at 75 octets (UTF-8 aware), CRLF line endings, and
Content-Type text/calendar instead of the inferred text/html. A line break
in a description used to end the property early; the new calendar
frontend has a multi-line description field.
- formatDate no longer shifts the event's end date in place.
- GET /calendar/events/subscriptions: the iCal URL of every calendar, with
its shared password where needed, for the calendar app's "Abonnieren"
dialog. Editors only (requireAppAccess('calendar')), the shared password
is not accepted there, never cached; calendars without a configured
credential are left out.
- Tests for both; documented in docs/calendar-ical.md.
Backwards compatible with the current Angular calendar app.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
6 files changed
+355
-8
No files matched your search
@@ -13,7 +13,7 @@ export const convertToIcal = async (events: Event[]): Promise<string> => {
|
||||
addEventToFile(ical, event);
|
||||
}
|
||||
|
||||
return serializeIcalFile(ical);
|
||||
return toContentLines(serializeIcalFile(ical));
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
@@ -116,9 +116,11 @@ const addEventToFile = (ical: iCalFile, event: Event) => {
|
||||
* @param event
|
||||
*/
|
||||
const createIcalEvent = (event: Event): iCalEvent => {
|
||||
let description = event.description ? event.description + '\n' : '';
|
||||
let location = event.location ? event.location + '\n' : '';
|
||||
let url = event.url ? event.url + '\n' : '';
|
||||
// Free text is escaped (see escapeText); the URL is a URI value, which has
|
||||
// no escapes - it only must not break the line.
|
||||
let description = event.description ? escapeText(event.description) + '\n' : '';
|
||||
let location = event.location ? escapeText(event.location) + '\n' : '';
|
||||
let url = event.url ? event.url.replace(/[\r\n]+/g, '') + '\n' : '';
|
||||
|
||||
return {
|
||||
header: 'BEGIN:VEVENT\n',
|
||||
@@ -128,7 +130,7 @@ const createIcalEvent = (event: Event): iCalEvent => {
|
||||
start: formatDate(event.startDateTime, event.wholeDay) + '\n',
|
||||
end: formatDate(event.endDateTime, event.wholeDay, true) + '\n',
|
||||
repeatFrequency: event.repeatFrequency ? event.repeatFrequency + '\n' : '',
|
||||
summary: event.name + '\n',
|
||||
summary: escapeText(event.name) + '\n',
|
||||
description: description,
|
||||
location: location,
|
||||
url: url,
|
||||
@@ -143,8 +145,11 @@ const createIcalEvent = (event: Event): iCalEvent => {
|
||||
* @param wholeDayFormat
|
||||
* @param isEndDate
|
||||
*/
|
||||
const formatDate = (date: Date, wholeDayFormat: boolean = false, isEndDate: boolean = false): string => {
|
||||
const formatDate = (input: Date, wholeDayFormat: boolean = false, isEndDate: boolean = false): string => {
|
||||
let returnString = '';
|
||||
// A copy: this used to shift the caller's Date in place, so the event object
|
||||
// came out of the export with its end a day later than it went in.
|
||||
const date = new Date(input);
|
||||
|
||||
// We need to do this for whole day events as otherwise the event ends one day too early
|
||||
if(wholeDayFormat && isEndDate) date.setDate(date.getDate() + 1)
|
||||
@@ -184,6 +189,58 @@ export interface iCalEvent {
|
||||
footer: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* RFC 5545 §3.3.11 TEXT escaping. Without it a description with a line break
|
||||
* ended the DESCRIPTION property early and the rest of the text was read as a
|
||||
* new (garbage) property, and an unescaped `,` or `;` is invalid in the value.
|
||||
* The backslash goes first, or the escapes added after it would be doubled.
|
||||
*/
|
||||
export const escapeText = (value: string): string =>
|
||||
value
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/;/g, '\\;')
|
||||
.replace(/,/g, '\\,')
|
||||
.replace(/\r\n|\r|\n/g, '\\n');
|
||||
|
||||
const MAX_LINE_OCTETS = 75;
|
||||
|
||||
/**
|
||||
* RFC 5545 §3.1 line folding: no content line longer than 75 octets, a longer
|
||||
* one continues on the next line after a single leading space. Counts UTF-8
|
||||
* octets, not characters, and never splits inside a character - "ü" is two
|
||||
* octets and a fold between them corrupts it.
|
||||
*/
|
||||
export const foldLine = (line: string): string => {
|
||||
const parts: string[] = [];
|
||||
let current = '';
|
||||
let octets = 0;
|
||||
for (const char of line) {
|
||||
const size = Buffer.byteLength(char, 'utf8');
|
||||
// A continuation line's leading space counts towards its 75.
|
||||
const limit = parts.length === 0 ? MAX_LINE_OCTETS : MAX_LINE_OCTETS - 1;
|
||||
if (octets + size > limit) {
|
||||
parts.push(current);
|
||||
current = '';
|
||||
octets = 0;
|
||||
}
|
||||
current += char;
|
||||
octets += size;
|
||||
}
|
||||
parts.push(current);
|
||||
return parts.join('\r\n ');
|
||||
};
|
||||
|
||||
/**
|
||||
* The generator builds the file with bare `\n` line ends; the format wants
|
||||
* every content line folded and terminated by CRLF (RFC 5545 §3.1).
|
||||
*/
|
||||
const toContentLines = (ical: string): string =>
|
||||
ical
|
||||
.split('\n')
|
||||
.filter((line) => line !== '')
|
||||
.map(foldLine)
|
||||
.join('\r\n') + '\r\n';
|
||||
|
||||
/**
|
||||
* Checks if a given string is null, undefined or blank
|
||||
* @param str The string to check
|
||||
|
||||
Reference in new issue
Block a user