-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
310 lines (264 loc) · 9.36 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
const site = require("./src/_data/site")();
const redirects = require("./src/_data/redirects.json");
const rssPlugin = require("@11ty/eleventy-plugin-rss");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const readingTime = require("eleventy-plugin-reading-time");
const nunjucksDate = require("nunjucks-date-filter");
const slugify = require("slugify");
const { zonedTimeToUtc } = require("date-fns-tz");
const processCss = require("./src/_utils/processCss.js");
const minifyCss = require("./src/_utils/minifyCss.js");
const {
imageShortcode,
svgShortcode,
} = require("./src/_utils/imageShortcodes.js");
const videoShortcode = require("./src/_utils/videoShortcode.js");
const markdownIt = require("markdown-it");
const markdownItLinkAttributes = require("markdown-it-link-attributes");
const markdownItAnchor = require("markdown-it-anchor");
const demoImageShortcode = require("./src/_utils/demoImageShortcode.js");
const demo = require("eleventy-plugin-embedded-demos");
const avatars = require("./eleventy-plugin-link-avatars");
const webmentions = require("eleventy-plugin-webmentions");
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
module.exports = function (eleventyConfig) {
eleventyConfig.addWatchTarget("./src/_styles/");
const slug = (input) => {
const options = {
replacement: "-",
remove: /[&,+()$~%.'":*!?<>{}]/g,
lower: true,
};
return slugify(input, options);
};
const linkRegex = new RegExp(`^(?!(${escapeRegExp(site.url)}|#|\/)).*$`);
const linkAttributes = {
pattern: linkRegex,
attrs: {
target: "_blank",
rel: "external noopener noreferrer",
},
};
// Markdown config
const markdownLib = markdownIt({ html: true })
.use(markdownItLinkAttributes, linkAttributes)
.use(markdownItAnchor, { slugify: slug });
eleventyConfig.setLibrary("md", markdownLib);
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
excerpt_separator: "<!-- excerpt -->",
});
// Build processes
eleventyConfig.on("beforeBuild", processCss);
eleventyConfig.addTransform("inlinecode", (content) => {
if (this.outputPath && this.outputPath.endsWith(".html")) {
return content.replace(/<code>/g, '<code class="language-inline">');
}
return content;
});
eleventyConfig.addPlugin(avatars, {
excludedUrls: ["https://lukeb.co.uk", site.url],
includeSelectors: ["#main-content"],
excludeSelectors: [],
outputDir: "./dist/static/images/",
urlPath: "/static/images/",
});
eleventyConfig.addPlugin((eleventyConfig) =>
eleventyConfig.addTransform("minifycss", minifyCss)
);
// Passthrough copy
eleventyConfig.addPassthroughCopy("src/service-worker.js");
eleventyConfig.addPassthroughCopy("src/static");
eleventyConfig.addPassthroughCopy("src/.well-known");
eleventyConfig.addPassthroughCopy("src/_redirects");
eleventyConfig.addPassthroughCopy({ "src/favicons/*": "/" });
// Watch targets
eleventyConfig.addWatchTarget("src/static/css/");
// Shortcodes
eleventyConfig.addNunjucksAsyncShortcode("image", imageShortcode);
eleventyConfig.addNunjucksAsyncShortcode("svg", svgShortcode);
eleventyConfig.addNunjucksAsyncShortcode("demoImage", demoImageShortcode);
eleventyConfig.addNunjucksAsyncShortcode("video", videoShortcode);
eleventyConfig.addShortcode("year", () => `${new Date().getFullYear()}`);
// Filters
eleventyConfig.addFilter("date", nunjucksDate);
eleventyConfig.addFilter("debug", (data) => {
console.log("");
console.log("********** DEBUG **********");
console.log(data);
console.log("******** END DEBUG ********");
console.log("");
debugger;
});
eleventyConfig.addFilter("md", (content = "") => {
return markdownLib.render(content);
});
eleventyConfig.addFilter("w3DateFilter", (value) => {
const dateObject = new Date(value);
return dateObject.toISOString();
});
eleventyConfig.addFilter("lowercase", (text) => {
return text.toLowerCase();
});
eleventyConfig.addFilter("slug", slug);
eleventyConfig.addFilter("livePosts", (posts) => {
const now = new Date();
return posts.filter((post) => post.date <= now && !post.data.draft);
});
eleventyConfig.addFilter("stripExcerpt", (content, excerptMarkdown) => {
if (!excerptMarkdown) {
return content;
}
const excerpt = markdownLib.render(excerptMarkdown);
return content.replace(excerpt, "");
});
eleventyConfig.addFilter("uniq", (array) => {
return [...new Set(array)];
});
eleventyConfig.addFilter("split", (string, separator) => {
return string.split(separator);
});
// Plugins
eleventyConfig.addPlugin(webmentions, {
domain: ["lukeb.co.uk", site.domain],
token: ["pDjIX81PRC-fGTpGYOXOMQ", "cIsyUadBGmlYmU069BrhQQ"],
truncationMarker:
'… <span class="webmention__truncated">Truncated</span>',
pageAliases: Object.keys(redirects).reduce((aliases, key) => {
if (key.match(/^\/[0-9]/)) {
aliases[redirects[key]] = key;
}
return aliases;
}, {}),
sanitizeOptions: {
...webmentions.defaults.sanitizeOptions,
allowedAttributes: {
a: [...Object.keys(linkAttributes.attrs), "href"],
},
transformTags: {
a: (tagName, attribs) => {
if (attribs.href && attribs.href.match(linkAttributes.pattern)) {
return {
tagName,
attribs: {
href: attribs.href,
...linkAttributes.attrs,
},
};
}
const returnObj = {
tagName,
};
if (attribs.href) {
returnObj.attribs = {
href: attribs.href,
};
}
return returnObj;
},
},
},
});
eleventyConfig.addPlugin(readingTime);
eleventyConfig.addPlugin(rssPlugin);
eleventyConfig.addPlugin(demo, {
path: "./src/demos",
});
eleventyConfig.addPlugin(syntaxHighlight, {
init: ({ Prism }) => {
Prism.languages.nunjucks = {
keyword:
/\b(?:comment|endcomment|if|elsif|else|endif|unless|endunless|for|endfor|case|endcase|when|in|break|assign|continue|limit|offset|range|reversed|raw|endraw|capture|endcapture|tablerow|endtablerow)\b/,
number:
/\b0b[01]+\b|\b0x(?:\.[\da-fp-]+|[\da-f]+(?:\.[\da-fp-]+)?)\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?[df]?/i,
operator: {
pattern:
/(^|[^.])(?:\+[+=]?|-[-=]?|!=?|<<?=?|>>?>?=?|==?|&[&=]?|\|[|=]?|\*=?|\/=?|%=?|\^=?|[?:~])/m,
lookbehind: true,
},
function: {
pattern:
/(^|[\s;|&])(?:append|prepend|capitalize|cycle|cols|increment|decrement|abs|at_least|at_most|ceil|compact|concat|date|default|divided_by|downcase|escape|escape_once|first|floor|join|last|lstrip|map|minus|modulo|newline_to_br|plus|remove|remove_first|replace|replace_first|reverse|round|rstrip|size|slice|sort|sort_natural|split|strip|strip_html|strip_newlines|times|truncate|truncatewords|uniq|upcase|url_decode|url_encode|include|paginate)(?=$|[\s;|&])/,
lookbehind: true,
},
};
Prism.languages.njk = Prism.languages.nunjucks;
},
});
function getUTCPostDate(date) {
const padded = (val) => val.toString().padStart(2, "0");
return zonedTimeToUtc(
`${date.getFullYear()}-${padded(date.getMonth() + 1)}-${padded(
date.getDate()
)} ${padded(date.getHours())}:${padded(date.getMinutes())}:${padded(
date.getSeconds()
)}`,
site.timezone
);
}
// Collections
const now = new Date();
const livePosts = (post) => {
return getUTCPostDate(post.date) <= now && !post.data.draft;
};
const futurePosts = (post) =>
getUTCPostDate(post.date) > now && !post.data.draft;
const posts = (collectionApi, filter = livePosts) =>
collectionApi.getFilteredByGlob("./src/posts/*").filter(filter).reverse();
eleventyConfig.addCollection("posts", (collectionApi) =>
posts(collectionApi)
);
eleventyConfig.addCollection("futurePosts", (collectionApi) =>
posts(collectionApi, futurePosts).reverse()
);
eleventyConfig.addCollection("tagList", function (collection) {
let tagList = [];
collection
.getAll()
.filter(livePosts)
.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 "pages":
case "posts":
case "postFeed":
return false;
}
return true;
});
for (const tag of tags) {
const tagIndex = tagList.findIndex(
(element) => element.name === tag
);
if (tagIndex > -1) {
tagList[tagIndex].count += 1;
} else {
tagList.push({ name: tag, count: 1 });
}
}
}
});
return tagList.sort((a, b) => {
const diff = b.count - a.count;
if (diff === 0) {
return a.name < b.name ? -1 : 1;
}
return diff;
});
});
return {
dir: {
input: "src",
output: "dist",
},
passthroughFileCopy: true,
markdownTemplateEngine: "njk",
};
};