-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
180 lines (151 loc) · 5.73 KB
/
.eleventy.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const { DateTime } = require("luxon");
const CleanCSS = require("clean-css");
const cities = require("./_data/cities.json");
const markdownIt = require("markdown-it");
const markdownItFootnote = require("markdown-it-footnote");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const { hasDitheredCopy, getDitheredPath } = require("./bin/dither");
const { generateLocationMap, generateOverviewMap } = require("./bin/map-maker");
const { request } = require("undici");
module.exports = function (eleventyConfig) {
// swap out markdown engines & add support for footnote syntax
const options = {
html: true,
breaks: true,
linkify: true,
};
const markdownParser = markdownIt(options).use(markdownItFootnote);
// fiddle with the default formatting
markdownParser.renderer.rules.footnote_block_open = () =>
'<ol class="footnotes-list">\n';
markdownParser.renderer.rules.footnote_block_close = () => "</ol>\n";
markdownParser.renderer.rules.footnote_anchor = () => "";
eleventyConfig.setLibrary("md", markdownParser);
// make sure dev blogs are visually appealing!
eleventyConfig.addPlugin(syntaxHighlight);
// This is little trick makes all my css inline (i.e. fast)
eleventyConfig.addFilter("cssmin", function (code) {
return new CleanCSS({}).minify(code).styles;
});
// Process book data
eleventyConfig.addFilter("books", function (books) {
if (books.length === 0) {
return 'nothing... but probably eyeing <a href="https://oku.club/user/riastrad/collection/to-read">one of these</a>.';
}
let booklinks;
for (let i = 0; i < books.length; i++) {
const linked = `<a href="${books[i].link}">${books[i].title}</a>`;
if (i === 0) {
booklinks = linked;
} else if (i !== 0 && i !== books.length - 1) {
booklinks += `, ${linked}`;
} else {
booklinks += `${books.length > 2 ? "," : ""} & ${linked}`;
}
}
return booklinks;
});
// keep a list of unique tags
eleventyConfig.addFilter("taglist", function (collection) {
const tags = [];
for (let i = 0; i < collection.length; i++) {
tags.push(...collection[i].data.tags);
}
const uniqueTags = new Set(tags.sort());
uniqueTags.delete("post");
return uniqueTags;
});
// This is to keep track of all my posts
eleventyConfig.addCollection("posts", function (collection) {
return collection.getAllSorted().filter(function (item) {
return item.inputPath.match(/^\.\/posts\//) !== null;
});
});
// Generate travel overview map & specific location maps
eleventyConfig.on("eleventy.after", async () => {
await generateOverviewMap(cities);
for (const city in cities) {
generateLocationMap(cities, city);
}
});
// currently this only returns an HTML widget that
// shows the aqi for Mumbai, though this could easily be changed
eleventyConfig.addAsyncShortcode("aqi", async (location) => {
console.log(`[cyberb] pulling AQI data for ${location}`);
const { statusCode, body } = await request(
"https://airnowgovapi.com/reportingarea/get",
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
},
body: "latitude=19.0842541&longitude=72.8851751&maxDistance=50",
},
);
if (statusCode !== 200) {
console.log(`[cyberb] AQI widget failed with error ${statusCode}`);
return `<table align=center><tr><td>⚠️ failed to retrieve AQI reading for ${location} ⚠️</td></tr></table>`;
}
const content = await body.json();
const { issueDate, time, timezone, aqi } = content[0];
const widget = `<table align=center>
<tr>
<td colspan=2>The last time this site was built the most recent AQI reading was:</td>
</tr>
<tr>
<td style='text-align: center; font-size: 6em;' colspan=2>${aqi}</td>
</tr>
<tr>
<td>date: ${issueDate}</td>
<td>time: ${time} ${timezone}</td>
</tr>
</table>`;
return widget;
});
eleventyConfig.addShortcode("cartographer", (location) => {
if (location === "all") {
return `<img id="overviewMap" class="svgMap" src="/places/all-cities.svg" />`;
}
const { url, display_name } = cities[location];
const svgImg = `<img class="svgMap" src="/places/${location}.svg" />`;
return `
<div align=center><b>a dispatch from:</b> <a href="${url}">${display_name}</a></div>
${svgImg}
<br />
`;
});
// image dithering
eleventyConfig.addAsyncShortcode("dither", async (filepath) => {
if (!hasDitheredCopy(filepath)) {
throw new Error(
`Cannot create dithering effect for ${filepath} if no dithered twin has been created.`,
);
}
const hoverableHTML = `<div class="dithered-hover">
<img src="${getDitheredPath(filepath)}" class="blog-pic" />
<img src="${filepath}" class="blog-pic" />
</div>`;
return hoverableHTML;
});
eleventyConfig.addFilter("encodeURI", (link) => {
return encodeURI(link);
});
// Date formatting stuff
eleventyConfig.addFilter("readableDate", (dateObj) => {
return DateTime.fromJSDate(dateObj).toFormat("MMM dd, yyyy");
});
eleventyConfig.addFilter("machineDate", (dateObj) => {
return DateTime.fromJSDate(dateObj).toFormat("yyyy-MM-dd");
});
eleventyConfig.addFilter("feedDate", (dateObj) => {
return DateTime.fromJSDate(dateObj).toUTC().toISO();
});
eleventyConfig.addFilter("dateYear", (dateObj) => {
return DateTime.fromJSDate(dateObj).toFormat("yyyy");
});
// Make sure assets carry through
eleventyConfig.addPassthroughCopy("img");
eleventyConfig.addPassthroughCopy("noise");
eleventyConfig.addPassthroughCopy("docs");
eleventyConfig.addPassthroughCopy("scripts");
};