-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
241 lines (190 loc) · 5.95 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
232
233
234
235
236
237
238
239
240
241
const cleanCSS = require("clean-css");
const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
const htmlMinifier = require("html-minifier");
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 pluginRss = require("@11ty/eleventy-plugin-rss");
/*
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/
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);
/*
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", "layouts/archive.njk");
config.addLayoutAlias("base", "layouts/base.njk");
config.addLayoutAlias("blog", "layouts/blog.njk");
config.addLayoutAlias("page", "layouts/page.njk");
config.addLayoutAlias("post", "layouts/post.njk");
/*
Passthrough
https://www.11ty.dev/docs/copy/
*/
// Copies all files/folders to `/dist/` folder
config.addPassthroughCopy("./src/assets/");
config.addPassthroughCopy("./src/feed.xml");
config.addPassthroughCopy("./src/sitemap.xml");
config.addPassthroughCopy("./src/netlify.toml");
config.addPassthroughCopy("./src/404.html");
/*
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("slug"),
})
// 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",
dir: {
input: "src",
includes: "_includes",
data: "_data",
output: "dist",
},
};
};