Repository navigation
Expand file tree
/
Copy pathcal.js
More file actions
61 lines (53 loc) · 1.77 KB
/
Copy pathcal.js
File metadata and controls
61 lines (53 loc) · 1.77 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
function calendarTimestamp(date) {
const pad = (value) => String(value).padStart(2, "0");
return [
date.getFullYear(),
pad(date.getMonth() + 1),
pad(date.getDate())
].join("") + "T" + [
pad(date.getHours()),
pad(date.getMinutes()),
"00"
].join("");
}
function buildCalendarUrl(meetup) {
const start = new Date(meetup.start.replace(" ", "T"));
if (Number.isNaN(start.getTime())) {
return null;
}
const durationHours = Number(meetup.duration_hours);
const durationMs = (Number.isFinite(durationHours) && durationHours > 0 ? durationHours : 2) * 60 * 60 * 1000;
const end = new Date(start.getTime() + durationMs);
const details = "Discuss: Zulip thread: " + meetup.discussion_url + "\n\nhttps://bostonopen.dev";
const params = new URLSearchParams({
action: "TEMPLATE",
text: "Boston Open Dev Meetup",
dates: calendarTimestamp(start) + "/" + calendarTimestamp(end),
ctz: meetup.timezone,
location: meetup.venue.name + ", " + meetup.venue.address,
details
});
return "https://calendar.google.com/calendar/render?" + params.toString();
}
function wireCalendarLink() {
const dataNode = document.getElementById("next-meetup-data");
const calendarLink = document.getElementById("gcal-link");
if (!dataNode || !calendarLink) {
return;
}
const meetup = JSON.parse(dataNode.textContent);
const calendarUrl = buildCalendarUrl(meetup);
if (!calendarUrl) {
return;
}
calendarLink.href = calendarUrl;
}
if (typeof module !== "undefined" && module.exports) {
module.exports = {
buildCalendarUrl,
calendarTimestamp
};
}
if (typeof document !== "undefined") {
wireCalendarLink();
}