-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheleventy.config.js
296 lines (234 loc) · 7.41 KB
/
eleventy.config.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const cleanCSS = require("clean-css");
const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
const htmlMinifier = require("html-minifier");
const Image = require("@11ty/eleventy-img");
const markdownIt = require("markdown-it");
const markdownItAbbr = require("markdown-it-abbr");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItAttrs = require("markdown-it-attrs");
const markdownItFootnote = require("markdown-it-footnote");
const path = require("path");
const pluginRss = require("@11ty/eleventy-plugin-rss");
/* Eleventy Upgrade Help V2 */
// const UpgradeHelper = require("@11ty/eleventy-upgrade-help");
/*
Global data files
https://www.11ty.dev/docs/data-global/
*/
// Import global data file
const site = require("./src/_data/site.js");
module.exports = function (config) {
// https://www.11ty.dev/docs/data-deep-merge/ Defaults to true in v1
config.setDataDeepMerge(true);
/*
Plugins
https://www.11ty.dev/docs/plugins/
*/
// https://github.com/11ty/eleventy-navigation
config.addPlugin(eleventyNavigationPlugin);
// https://github.com/11ty/eleventy-plugin-rss
config.addPlugin(pluginRss);
// Disable extensionless layouts
config.setLayoutResolution(false);
// If you have other `addPlugin` calls, it’s important that UpgradeHelper is added last.
//config.addPlugin(UpgradeHelper);
/*
Filters
https://www.11ty.dev/docs/filters/
*/
// Minify CSS in production
config.addFilter("cssMin", function (code) {
if (process.env.ELEVENTY_ENV == "production") {
return new cleanCSS({}).minify(code).styles;
}
return code;
});
// ISO post datetime for search engines to read
config.addFilter("dateTime", function (value) {
const dateObject = new Date(value);
return dateObject.toISOString();
});
// Ordinal dates for humans to read
const appendSuffix = (n) => {
var s = ["th", "st", "nd", "rd"],
v = n % 100;
return n + (s[(v - 20) % 10] || s[v] || s[0]);
};
config.addFilter("ordinalDate", function (value) {
const dateObject = new Date(value);
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const dayWithSuffix = appendSuffix(dateObject.getDate());
// You may change the date format below by uncommenting that line.
// 1. Month Day, Year
// return `${months[dateObject.getMonth()]} ${dayWithSuffix}, ${dateObject.getFullYear()}`;
// 2. Day Month Year
return `${dayWithSuffix} ${months[dateObject.getMonth()]} ${dateObject.getFullYear()}`;
});
// Calculate blog posts reading time
config.addFilter("readingTime", function (text) {
const wordsPerMinute = 200;
const numberOfWords = text.split(/\s/g).length;
return Math.ceil(numberOfWords / wordsPerMinute);
});
/*
Transforms
https://www.11ty.dev/docs/config/#transforms
*/
// https://github.com/kangax/html-minifier
config.addTransform("htmlMin", function (value, outputPath) {
if (
process.env.ELEVENTY_ENV == "production" &&
outputPath &&
outputPath.indexOf(".html") > -1
) {
let minified = htmlMinifier.minify(value, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
minifyCSS: true,
});
return minified;
}
return value;
});
/*
Collections
https://www.11ty.dev/docs/collections/
*/
// Creates a list of blog post within `/posts/` folder in reverse chronological order
config.addCollection("posts", function (collection) {
return collection.getFilteredByGlob("./src/posts/*.md").reverse();
});
// Tags
config.addCollection("tagList", function (collection) {
let tagSet = new Set();
collection.getAll().forEach(function (item) {
if ("tags" in item.data) {
let tags = item.data.tags;
tags = tags.filter(function (item) {
switch (item) {
// this list should match the `filter` list in tags.njk
case "all":
case "nav":
case "post":
case "posts":
return false;
}
return true;
});
for (const tag of tags) {
tagSet.add(tag);
}
}
});
// returning an array in addCollection works in Eleventy 0.5.3
return [...tagSet];
});
/*
Layouts
https://www.11ty.dev/docs/layouts/
*/
// https://www.11ty.dev/docs/layouts/#layout-aliasing
config.addLayoutAlias("archive.njk", "layouts/archive.njk");
config.addLayoutAlias("base.njk", "layouts/base.njk");
config.addLayoutAlias("blog.njk", "layouts/blog.njk");
config.addLayoutAlias("page.njk", "layouts/page.njk");
config.addLayoutAlias("post.njk", "layouts/post.njk");
/*
Passthrough
https://www.11ty.dev/docs/copy/
*/
// Copies all files/folders to output directory
config.addPassthroughCopy("./src/favicons/");
config.addPassthroughCopy("./src/feed.xml");
config.addPassthroughCopy("./src/sitemap.xml");
config.addPassthroughCopy("./src/_redirects");
config.addPassthroughCopy("./src/404.html");
config.addPassthroughCopy("./src/robots.txt");
/*
Shortcodes
https://www.11ty.dev/docs/shortcodes/
*/
config.addNunjucksAsyncShortcode("image", imageShortcode);
async function imageShortcode(src, alt, sizes) {
let metadata = await Image(src, {
widths: [640, 1280, 1600, 1920],
formats: ["avif", "webp", "jpeg"],
urlPath: "/img/",
outputDir: "dist/img/",
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `${name}-${width}w.${format}`;
},
});
let imageAttributes = {
alt,
sizes,
loading: "lazy",
decoding: "async",
};
// You bet we throw an error on missing alt in `imageAttributes` (alt="" works okay)
return Image.generateHTML(metadata, imageAttributes, {
// Strip the whitespace from the output of the <picture> element
whitespaceMode: "inline",
});
}
/*
Markdown Library
https://github.com/markdown-it/markdown-it
*/
const md = markdownIt({
// Enable HTML tags in source. Copies HTML tags to the output directory
html: true,
// Convert '\n' in paragraphs into <br>
breaks: true,
// Autoconvert URL-like text to links
linkify: false,
})
// https://www.npmjs.com/package/markdown-it-abbr
.use(markdownItAbbr)
// https://www.npmjs.com/package/markdown-it-anchor
.use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.ariaHidden({
placement: "after",
class: "al",
symbol: "#",
level: [1, 2, 3, 4],
}),
slugify: config.getFilter("slugify"),
})
// https://www.npmjs.com/package/markdown-it-attrs
.use(markdownItAttrs, {
allowedAttributes: ["id", "class", "rel"],
})
// https://www.npmjs.com/package/markdown-it-footnote
.use(markdownItFootnote);
md.renderer.rules.footnote_block_open = () =>
"<hr>\n" + '<section class="fn">\n' + "<ol>\n";
config.setLibrary("md", md);
return {
passthroughFileCopy: true,
htmlTemplateEngine: "njk",
markdownTemplateEngine: "njk",
dir: {
input: "src",
includes: "_includes",
data: "_data",
output: "dist",
},
};
};