-
Notifications
You must be signed in to change notification settings - Fork 21
/
.hygen.js
94 lines (83 loc) · 2.59 KB
/
.hygen.js
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
const today = new Date();
const offsetMs = today.getTimezoneOffset() * 60 * 1000;
const msLocal = today.getTime() - offsetMs;
const dateLocal = new Date(msLocal);
const iso = dateLocal.toISOString().slice(0, 19);
const events = require('./src/_data/speaking_engagements.json');
const series = require('./_cache/series.json');
const tags = require('./_cache/tags.json');
const citations = require('./src/_data/citations.json');
module.exports = {
helpers: {
escapeQuotes: (str) => {
return str.replace('"', '\\"');
},
getTimestamp: () => {
return `${iso.replace('T', ' ')} -07:00`;
},
getTags: () => {
return tags;
},
getSeriesName: (tag) => {
return series[tag];
},
getDate: () => {
return iso.substring(0, 10);
},
getFilename: (locals, include_date) => {
include_date = include_date !== false ? true : false;
const date = iso.substring(0, 10);
// Slugify the title
var text = locals.title;
text = text.replace(/^\s+|\s+$/g, '');
// Make the string lowercase
text = text.toLowerCase();
// Remove accents, swap ñ for n, etc
var from = "ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđßÆa·/_,:;";
var to = "AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------";
for (var i=0, l=from.length ; i<l ; i++) {
text = text.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
// Remove invalid chars
text = text.replace(/[^a-z0-9 -]/g, '')
// Collapse whitespace and replace by -
.replace(/\s+/g, '-')
// Collapse dashes
.replace(/-+/g, '-');
return include_date ? `${date}-${text}` : text;
},
addEvent: (locals) => {
var new_events = events.sort((a, b) => {
return a.id > b.id ? 1 : -1
});
new_events.reverse();
var event = {
id: new_events[0].id + 1,
title: locals.title,
date: `${locals.date} 00:09:00 -0800`,
location: locals.location || 'Online'
};
if (locals.url) {
event.url = locals.url;
}
new_events.unshift(event)
return JSON.stringify(new_events, null, 2);
},
addCitation: (locals) => {
var citation = {
title: locals.title,
author: locals.author,
url: locals.url
};
if (locals.lang) {
citation.lang = locals.lang;
citation.dir = locals.dir;
}
if (locals.dir) {
citation.dir = locals.dir;
}
citations[locals.type].push(citation);
return JSON.stringify(citations, null, 2);
}
}
};