Skip to content

Commit

Permalink
feat: script for iCal generation
Browse files Browse the repository at this point in the history
- 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: sebbo2002/ical-generator#185
  • Loading branch information
bguiz committed Mar 23, 2020
1 parent 1865541 commit 46bb9f5
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .scripts/generate-webinar-ical.js
Original file line number Diff line number Diff line change
@@ -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',
};
}

0 comments on commit 46bb9f5

Please sign in to comment.