From 46bb9f55e368019fe4c02619a4a988da72ad7215 Mon Sep 17 00:00:00 2001 From: bguiz Date: Mon, 23 Mar 2020 14:52:19 +0800 Subject: [PATCH] feat: script for iCal generation - make use of ical-generator - use prefix to specify yaml front matter containing permalink for jekyll - NOTE that X-attributes are not yet able to be specified in this manner - REF: https://github.com/sebbo2002/ical-generator/pull/185 --- .scripts/generate-webinar-ical.js | 83 +++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 .scripts/generate-webinar-ical.js diff --git a/.scripts/generate-webinar-ical.js b/.scripts/generate-webinar-ical.js new file mode 100755 index 0000000000..989835efeb --- /dev/null +++ b/.scripts/generate-webinar-ical.js @@ -0,0 +1,83 @@ +#!/usr/bin/env node + +const fs = require('fs'); + +const icalGenerator = require('ical-generator'); + +const webinarsJson = require('../_data/webinars.json'); + +const events = webinarsJson.events.map(processEvent); + +const icalData = { + domain: 'developers.rsk.co/', + prodId: '//developers.rsk.co//events-calendar//EN', + name: 'RSK and RIF Webinars', + timezone: 'Etc/UTC', + scale: 'gregorian', + ttl: 3600, + organizer: 'RSK', + events, +}; + +const ical = icalGenerator(icalData); + +const icalPrefix = '---\npermalink: /webinars/calendar.ical\n---\n'; +const icalSuffix = '\n'; +const icalPath = './webinars/calendar.ical'; + +fs.writeFile( + icalPath, + icalPrefix + ical.toString() + icalSuffix, + function (err) { + if (err) { + throw err; + } else { + console.log(`iCal output successfully: ${icalPath}`); + } + }, +); + +function processEvent(event) { + switch (event.type) { + case 'event/1': + return processEventV1(event); + default: + throw new Error(`Unsupported type: ${event.type}`); + } +} + +function processEventV1(event) { + const { + id, + timestamp, + url, + location, + category, + language, + title, + subtitle, + lastModified, + } = event; + const timestampDate = new Date(timestamp); + const lastModifiedDate = new Date(lastModified); + return { + uid: id, + start: timestampDate, + timestamp: timestampDate, + lastModified: lastModifiedDate, + url, + summary: ([title, subtitle].join(' ')), + location, + categories: [ + { + name: category, + }, + ], + // NOTE that X-attributes are not yet able to be specified in this manner + // REF: https://github.com/sebbo2002/ical-generator/pull/185 + x: [ + ['LANGUAGE', language], + ], + status: 'confirmed', + }; +}