-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
350 lines (306 loc) · 11.7 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const { DateTime } = require("luxon");
const fs = require("fs");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItFootnote = require("markdown-it-footnote");
module.exports = function (eleventyConfig) {
/* =================================================================
/* SECTION Configuration
================================================================= */
// Add Plugins
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginSyntaxHighlight);
// Data Deep Merge
// https://www.11ty.dev/docs/data-deep-merge/
eleventyConfig.setDataDeepMerge(true);
// Enable Static Passthrough
eleventyConfig.addPassthroughCopy({ "src/_static/": "/" });
/* ==============================
/* SECTION Markdown
============================== */
let markdownLibrary = markdownIt({
html: true,
breaks: true,
linkify: false,
}).use(markdownItAnchor, {
permalink: true,
permalinkClass: "direct-link",
permalinkSymbol: "§",
}).use(markdownItFootnote);
// Custom renderer for footnote back link
markdownLibrary.renderer.rules.footnote_anchor = (tokens, idx) => {
let n = Number(tokens[idx].meta.id + 1).toString();
let id = `fnref${n}`;
return ` <span class="footnote-backref"><a href="#${id}">↑</a></span>`;
};
eleventyConfig.setLibrary("md", markdownLibrary);
/* !SECTION Markdown */
/* ==============================
/* SECTION BrowserSync Overrides (used only with --serve)
============================== */
eleventyConfig.setBrowserSyncConfig({
callbacks: {
ready: function (err, browserSync) {
const content_404 = fs.readFileSync("dist/404.html");
browserSync.addMiddleware("*", (req, res) => {
// Provides the 404 content without redirect.
res.writeHead(404, { "Content-Type": "text/html; charset=UTF-8" });
res.write(content_404);
res.end();
});
},
},
ui: false,
ghostMode: false,
});
/* !SECTION BrowserSync Overrides */
/* !SECTION Configuration */
/* =================================================================
/* SECTION Filters
================================================================= */
// Get the first `n` elements of a collection.
// eleventyConfig.addFilter("head", (array, n) => {
// if (n < 0) {
// return array.slice(n);
// }
// return array.slice(0, n);
// });
// Return the smallest number argument
eleventyConfig.addFilter("min", (...numbers) => {
return Math.min.apply(null, numbers);
});
/* ==============================
/* SECTION Dates
============================== */
eleventyConfig.addFilter("readableDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat(
"dd LLLL yyyy"
);
});
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat("yyyy-LL-dd");
});
/* !SECTION Dates */
/* ==============================
/* SECTION Tag Filters
============================== */
// Filters out unwanted / utility tags
function filterTagList(tags) {
return (tags || []).filter(
(tag) => ["posts", "projects"].indexOf(tag) === -1
);
}
eleventyConfig.addFilter("filterTagList", filterTagList);
/* !SECTION Tag Filters */
/* !SECTION Filters */
/* =================================================================
/* SECTION Collection Functions
================================================================= */
// Return a list of tags in a given collection
function getTagList(collection) {
let tagSet = new Set();
collection.forEach((item) => {
(item.data.tags || []).forEach((tag) => tagSet.add(tag));
});
return filterTagList([...tagSet]);
}
// Return a list of years in a given collection
function getYearList(collection) {
let years = new Set();
collection.forEach((item) => {
years.add(
DateTime.fromJSDate(item.data.date, { zone: "utc" }).toFormat("yyyy")
);
});
return [...years];
}
// Find collection item by URL
// {%- set page1 = collections.projects | find("/slug/") -%}
// <h2>{{ page1.data.title }}</h2>
eleventyConfig.addFilter("find", function find(collection = [], slug = "") {
return collection.find((post) => post.url === slug);
});
// Return an object with arrays of posts by tag from the provided collection
function createCollectionsByTag(collection) {
// set the result as an object
let resultArrays = {};
// loop through each item in the provided collection
collection.forEach((item) => {
// loop through the tags of each item
item.data.tags.forEach((tag) => {
// If the tag has not already been added to the object, add it as an empty array
if (!resultArrays[tag]) {
resultArrays[tag] = [];
}
// Add the item to the tag's array
resultArrays[tag].push(item);
});
});
// Return the object containing tags and their arrays of posts
// { tag-name: [post-object, post-object], tag-name: [post-object, post-object] }
return resultArrays;
}
// Return an object with arrays of posts by year from the provided collection
function createCollectionsByYear(collection) {
// set the result as an object
let resultArrays = {};
// loop through each item in the provided collection
collection.forEach((item) => {
year = DateTime.fromJSDate(item.data.date, { zone: "utc" }).toFormat(
"yyyy"
);
if (!resultArrays[year]) {
resultArrays[year] = [];
}
resultArrays[year].push(item);
});
// Return the object containing tags and their arrays of posts
// { year: [post-object, post-object], year: [post-object, post-object] }
return resultArrays;
}
/* !SECTION Collection Functions */
/* =================================================================
/* SECTION Collections
================================================================= */
/* ==============================
/* SECTION Posts
============================== */
// set up collections for posts
const POSTS = (collectionAPI) => {
return collectionAPI.getFilteredByGlob("./src/posts/*/*.md");
};
// collections.posts => Returns list of all posts
eleventyConfig.addCollection("posts", function (collectionAPI) {
return POSTS(collectionAPI);
});
// collections.postTags => Returns list of all tags
eleventyConfig.addCollection("postTags", function (collectionAPI) {
return getTagList(POSTS(collectionAPI));
});
// collections.postsByTag[tag] => Returns list of all posts that match tag-name
eleventyConfig.addCollection("postsByTag", function (collectionAPI) {
return createCollectionsByTag(POSTS(collectionAPI));
});
// Create a collection for post years (required for the generation of archive pages)
eleventyConfig.addCollection("postYears", function (collectionAPI) {
return getYearList(POSTS(collectionAPI));
});
// Create a collection for posts by year
eleventyConfig.addCollection("postsByYear", function (collectionAPI) {
return createCollectionsByYear(POSTS(collectionAPI));
});
/* !SECTION Posts */
/* ==============================
/* SECTION Projects
============================== */
// Set up collections for projects
const PROJECTS = (collectionAPI) => {
return collectionAPI.getFilteredByGlob("./src/projects/*/*.md");
};
// collections.projects => Returns list of all projects
eleventyConfig.addCollection("projects", function (collectionAPI) {
return PROJECTS(collectionAPI);
});
// collections.projectTags => Returns list of all tags
eleventyConfig.addCollection("projectTags", function (collectionAPI) {
return getTagList(PROJECTS(collectionAPI));
});
// collections.projectsByTag[tag] => Returns list of all projects that match tag-name
eleventyConfig.addCollection("projectsByTag", function (collectionAPI) {
return createCollectionsByTag(PROJECTS(collectionAPI));
});
/* !SECTION Projects */
/* ==============================
/* SECTION Sketchbook
============================== */
// Setup collections for sketchbook
const SKETCHBOOK = (collectionAPI) => {
return collectionAPI.getFilteredByGlob("./src/sketchbook/*/*.md");
};
// collections.sketchbook => Returns list of all sketches
eleventyConfig.addCollection("sketchbook", function (collectionAPI) {
return SKETCHBOOK(collectionAPI);
});
// collections.sketchbookTags => Returns list of all tags
eleventyConfig.addCollection("sketchbookTags", function (collectionAPI) {
return getTagList(SKETCHBOOK(collectionAPI));
});
// collections.sketchbookByTag[tag] => Returns list of all sketches that match tag-name
eleventyConfig.addCollection("sketchbookByTag", function (collectionAPI) {
return createCollectionsByTag(SKETCHBOOK(collectionAPI));
});
// Create a collection for sketchbook years (required for the generation of archive pages)
eleventyConfig.addCollection("sketchbookYears", function (collectionAPI) {
return getYearList(SKETCHBOOK(collectionAPI));
});
// Create a collection for sketches by year
eleventyConfig.addCollection("sketchbookByYear", function (collectionAPI) {
return createCollectionsByYear(SKETCHBOOK(collectionAPI));
});
/* !SECTION Sketchbook */
/* ==============================
/* SECTION Logs
============================== */
// Setup collection for logs
const LOGS = (collectionAPI) => {
return collectionAPI.getFilteredByGlob("./src/log/*/*.md");
};
// collections.logs => Returns list of all journal entries
eleventyConfig.addCollection("logs", function (collectionAPI) {
return LOGS(collectionAPI);
});
// Create a collection for journal years (required for the generation of archive pages)
eleventyConfig.addCollection("logYears", function (collectionAPI) {
return getYearList(LOGS(collectionAPI));
});
// Create a collection for log entries by year
eleventyConfig.addCollection("logsByYear", function (collectionAPI) {
return createCollectionsByYear(LOGS(collectionAPI));
});
/* !SECTION Logs */
// collections.allContent => Returns list of all content
eleventyConfig.addCollection("allContent", function (collectionAPI) {
return collectionAPI.getFilteredByGlob([
"./src/sketchbook/*/*.md",
"./src/posts/*/*.md",
"./src/projects/*/*.md",
"./src/log/*/*.md",
]);
});
/* !SECTION Collections */
/* =================================================================
/* SECTION Shortcodes
================================================================= */
eleventyConfig.addShortcode("lbrace", function () {
return `{`;
});
eleventyConfig.addShortcode("rbrace", function () {
return `}`;
});
/* !SECTION Shortcodes */
/* =================================================================
/* SECTION Return
================================================================= */
return {
// Control which files Eleventy will process
// e.g.: *.md, *.njk, *.html, *.liquid
templateFormats: ["md", "njk", "html", "liquid"],
// Pre-process *.md files with:
markdownTemplateEngine: "njk",
// Pre-process *.html files with:
htmlTemplateEngine: "njk",
// Opt-out of pre-processing global data JSON files:
dataTemplateEngine: false,
// Set directories
dir: {
input: "src",
includes: "_includes",
data: "_data",
output: "dist",
},
};
/* !SECTION Return */
};