-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathics.ts
More file actions
137 lines (114 loc) · 3.48 KB
/
Copy pathics.ts
File metadata and controls
137 lines (114 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
export interface IcsEventInput {
title: string;
deadline: string;
description?: string;
uid: string;
}
const CRLF = '\r\n';
const DEFAULT_DURATION = 'PT30M';
const PROD_ID = '-//Disciplr//Vault Deadline//EN';
function isValidDate(date: Date): boolean {
return Number.isFinite(date.getTime());
}
export function isValidIcsDeadline(deadline: string): boolean {
return isValidDate(new Date(deadline));
}
function formatIcsDate(date: Date): string {
const pad = (value: number) => String(value).padStart(2, '0');
return [
date.getUTCFullYear(),
pad(date.getUTCMonth() + 1),
pad(date.getUTCDate()),
'T',
pad(date.getUTCHours()),
pad(date.getUTCMinutes()),
pad(date.getUTCSeconds()),
'Z',
].join('');
}
function escapeIcsText(value: string): string {
return value
.replace(/\\/g, '\\\\')
.replace(/\r\n|\r|\n/g, '\\n')
.replace(/;/g, '\\;')
.replace(/,/g, '\\,');
}
function byteLength(value: string): number {
if (typeof TextEncoder !== 'undefined') {
return new TextEncoder().encode(value).length;
}
return value.length;
}
export function foldIcsLine(line: string): string {
const lines: string[] = [];
let remaining = line;
while (byteLength(remaining) > 75) {
let splitAt = 0;
let bytes = 0;
for (const char of remaining) {
const nextBytes = bytes + byteLength(char);
if (nextBytes > 75) break;
bytes = nextBytes;
splitAt += char.length;
}
if (splitAt === 0) break;
lines.push(remaining.slice(0, splitAt));
remaining = ` ${remaining.slice(splitAt)}`;
}
lines.push(remaining);
return lines.join(CRLF);
}
function sanitizeFilenamePart(value: string): string {
return value
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.slice(0, 60);
}
export function icsFilename(title: string, uid: string): string {
const base = sanitizeFilenamePart(title) || sanitizeFilenamePart(uid) || 'vault-deadline';
return `${base}.ics`;
}
export function buildIcsEvent({ title, deadline, description = '', uid }: IcsEventInput): string {
const deadlineDate = new Date(deadline);
if (!isValidDate(deadlineDate)) {
throw new Error('Cannot build calendar event for an invalid deadline.');
}
const timestamp = formatIcsDate(deadlineDate);
const lines = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
`PRODID:${PROD_ID}`,
'CALSCALE:GREGORIAN',
'METHOD:PUBLISH',
'BEGIN:VEVENT',
`UID:${escapeIcsText(uid)}`,
`DTSTAMP:${timestamp}`,
`DTSTART:${timestamp}`,
`DURATION:${DEFAULT_DURATION}`,
`SUMMARY:${escapeIcsText(title)}`,
`DESCRIPTION:${escapeIcsText(description)}`,
'END:VEVENT',
'END:VCALENDAR',
];
return `${lines.map(foldIcsLine).join(CRLF)}${CRLF}`;
}
export function downloadIcs(calendar: string, filename: string): void {
if (typeof document === 'undefined' || typeof URL === 'undefined') return;
const blob = new Blob([calendar], { type: 'text/calendar;charset=utf-8;' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', filename);
link.style.display = 'none';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
export function downloadIcsEvent(input: IcsEventInput): boolean {
if (!isValidIcsDeadline(input.deadline)) return false;
downloadIcs(buildIcsEvent(input), icsFilename(input.title, input.uid));
return true;
}