-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
231 lines (202 loc) · 7.43 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const CleanCSS = require("clean-css");
const embedTwitter = require("eleventy-plugin-embed-twitter");
const luxon = require("luxon");
const htmlMinifier = require("html-minifier-terser");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItFootnote = require("markdown-it-footnote");
const markdownItPrism = require("markdown-it-prism");
const markdownItReplaceLink = require("markdown-it-replace-link");
const minifyXML = require("minify-xml");
const yaml = require("js-yaml");
const isProduction = process.env.NODE_ENV === `production`;
const MARKDOWN_OPTIONS = {
html: true,
linkify: true,
replaceLink: function (link, env) {
let parts = link.split("#", 2);
let base = parts[0];
let fragmentIdentifier = parts[1];
let additional = fragmentIdentifier ? "#" + fragmentIdentifier : "";
// Convert relative post markdown links to correct paths
if (
(post = env.collections.posts.find(
(post) => post.template.parsed.base == base,
))
) {
return post.data.page.url + additional;
}
return link;
},
typographer: true,
};
module.exports = function (eleventyConfig) {
// Embed Twitter posts
eleventyConfig.addPlugin(embedTwitter, {
cacheText: true,
});
// Support .yaml Extension in _data
eleventyConfig.addDataExtension("yaml", (contents) => yaml.load(contents));
// Copy favicon, images, and netlify to /_site
eleventyConfig.addPassthroughCopy({ "./src/static/favicon": "." });
eleventyConfig.addPassthroughCopy("./src/static/img");
eleventyConfig.addPassthroughCopy({ "./src/static/netlify": "." });
// Prevent widows
// Copied from https://github.com/ekalinin/typogr.js/blob/4c1d4afc5457c4b1456dc1d56af2d9cf8171b8e2/typogr.js#L137-L154
eleventyConfig.addTransform("widont", (content, outputPath) => {
if (!outputPath.endsWith(".html")) {
return content;
}
var inline_tags = "a|em|span|strong|i|b";
var word =
"(?:<(?:" +
inline_tags +
")[^>]*?>)*?[^\\s<>]+(?:</(?:" +
inline_tags +
")[^>]*?>)*?";
var re_widont = new RegExp(
"(" + // matching group 1
"\\s+" +
word + // space and a word with a possible bordering tag
"\\s+" +
word + // space and a word with a possible bordering tag
")" +
"(?:\\s+)" + // one or more space characters
"(" + // matching group 2
"[^<>\\s]+" + // nontag/nonspace characters
"(?:\\s*</(?:a|em|span|strong|i|b)[^>]*?>\\s*\\.*)*?" + // one or more inline closing tags
// can be surrounded by spaces
// and followed by a period.
"(?:\\s*?</(?:p|h[1-6]|li|dt|dd)>|$)" + // allowed closing tags or end of line
")",
"gi",
);
return content.replace(re_widont, '$1<span class="widont"> </span>$2');
});
// Minify HTML/XML
eleventyConfig.addTransform("minify", (content, outputPath) => {
if (!isProduction) {
return content;
}
if (outputPath.endsWith(".html")) {
return htmlMinifier.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
});
} else if (outputPath.endsWith(".xml")) {
return minifyXML.minify(content);
} else if (outputPath.endsWith(".css")) {
return new CleanCSS({ level: 2 }).minify(content).styles;
}
return content;
});
// Set custom excerpt separator
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
excerpt_separator: "<!-- excerpt -->",
});
// Set custom markdown options
eleventyConfig.setLibrary("md", markdownIt(MARKDOWN_OPTIONS));
// Add anchor links to headings
eleventyConfig.amendLibrary("md", (md) =>
md.use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.headerLink({
safariReaderFix: true,
}),
}),
);
// Support footnotes
eleventyConfig.amendLibrary("md", (md) => md.use(markdownItFootnote));
// Add syntax highlighting
eleventyConfig.amendLibrary("md", (md) => md.use(markdownItPrism));
// TODO: Find a way to add `starlark` to `Prism.languages`. Then change all
// references from `python` to `starlark`.
// Prism.languages["starlark"] = Prism.languages.extend("python", {});
// See `replaceLink` in `MARKDOWN_OPTIONS`
eleventyConfig.amendLibrary("md", (md) => md.use(markdownItReplaceLink));
// Add `absoluteURL` filter. Used for Atom and RSS.
eleventyConfig.addFilter("absoluteURL", (href, base) => {
return new URL(href, base).toString();
});
// Add `rfc2822` filter. Used for RSS.
eleventyConfig.addFilter("rfc2822", (date) => {
return luxon.DateTime.fromJSDate(date).setZone("CST").toRFC2822();
});
// Add `mostRecentRFC2822` filter. Used for Atom.
eleventyConfig.addFilter("rfc3339", (date) => {
return date.toISOString();
});
// Add `mostRecentRFC2822` filter. Used for Atom and RSS.
eleventyConfig.addFilter("mostRecentUpdated", (collection) => {
if (!collection || !collection.length) {
throw new Error("Collection is empty in mostRecentRFC2822 filter.");
}
return new Date(
Math.max(
...collection.map((item) => {
return item.data.updated ?? item.date;
}),
),
);
});
// Add `readableDate` filter. Used fto show post dates.
let readableDate = (date) => {
return luxon.DateTime.fromJSDate(date).setZone("CST").toFormat("DDD");
};
eleventyConfig.addFilter("readableDate", readableDate);
// Add `toHTML` filter for inline markdown.
// Used for Atom, RSS, and post lists.
eleventyConfig.addFilter("toHTML", (str) => {
return new markdownIt(MARKDOWN_OPTIONS).renderInline(str);
});
// Add `outdated`, `update`, and `updates` shortcodes. Used to display update
// banners.
eleventyConfig.addPairedLiquidShortcode("outdated", (content, timestamp) => {
let date = new Date(timestamp);
return `<aside class="o">
<h1><time datetime="${date.toISOString()}">${readableDate(date)}</time></h1>
<p>The information in this post is out of date, and exists only as historical record.</p>
${content}
</aside>`;
});
eleventyConfig.addPairedLiquidShortcode("update", (content, timestamp) => {
let date = new Date(timestamp);
return `<aside class="u">
<h1><time datetime="${date.toISOString()}">${readableDate(date)}</time></h1>
${content}
</aside>`;
});
eleventyConfig.addPairedLiquidShortcode("updates", (content) => {
return content + "<hr/>";
});
// Add `file` shortcode. Used to add a filename to a code snippet.
eleventyConfig.addPairedLiquidShortcode("file", (content, filename) => {
return `<figure class="f">
<figurecaption><strong><code>${filename}</code></strong></figurecaption>
${content}
</figure>`;
});
// Add `note` shortcode. Used to display notes.
eleventyConfig.addPairedLiquidShortcode("note", (content) => {
// We don't use `renderInline` because it doesn't handle links correctly
let body = new markdownIt(MARKDOWN_OPTIONS).render(content);
return `<div class="n">
<p><span class="t">Note<span class="h">:</span></span> ${body.substring(3)}
</div>`;
});
// Add `version` shortcode. Used to display version numbers pills.
eleventyConfig.addLiquidShortcode("version", (version) => {
return `<span class="v"><span class="h">[</span>${version}<span class="h">]</span></span>`;
});
return {
dir: {
input: "src",
includes: "_includes",
layouts: "_layouts",
output: "_site",
data: "_data",
},
htmlTemplateEngine: "njk",
};
};