-
Notifications
You must be signed in to change notification settings - Fork 0
/
eleventy.config.js
220 lines (185 loc) · 7.13 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
import { feedPlugin } from "@11ty/eleventy-plugin-rss";
import { eleventyImageTransformPlugin } from "@11ty/eleventy-img";
import browserslist from "browserslist";
import { bundleAsync, browserslistToTargets } from "lightningcss";
import path from "node:path";
import prettier from "prettier";
const PACKAGE = await Bun.file("package.json").json();
const metadata = {
language: "en",
title: "Robin's Blog",
subtitle: "Thoughts and opinions of a teenager from the Netherlands.",
base: "https://geheimesite.nl",
author: { name: "Robin Boers" },
};
const range = (from, to) => [...Array(to - from + 1).keys()].map(i => i + from);
const currentYear = new Date().getFullYear();
export default function (config) {
config.addGlobalData("blog.title", metadata.title);
config.addGlobalData("blog.subtitle", metadata.subtitle);
config.addGlobalData("canonical", metadata.base);
config.addGlobalData("language", metadata.language);
config.addGlobalData("layout", "default.njk");
config.addGlobalData("year", currentYear);
config.addGlobalData("years", range(2018, currentYear).reverse());
// Menu items :D
config.addGlobalData("main_menu", ["", "projects", "books", "blog", "more"]);
config.addGlobalData("more_menu", ["follow", "resume", "colophon", "privacy"]);
// Setup asset pipeline + dev optimizations
config.addPassthroughCopy({ "src/*.txt": "/", ".htaccess": "/.htaccess" });
config.setServerPassthroughCopyBehavior("passthrough");
// My pretty URLs are configured in Apache
config.addGlobalData("permalink", () => ({ page }) => `${page.filePathStem}.${page.outputFileExtension}`);
config.addUrlTransform((page) => {
if (page.url.endsWith(".html")) return page.url.slice(0, -1 * ".html".length);
});
// Shortcodes
config.addShortcode("hr", () => `<hr style="border: none; margin: 2em 0">`);
// Blocks
config.addPairedShortcode("container", (innerContent) => {
return `<div class="container">${innerContent}</div>`;
});
// Setup blog section
config.addCollection("blog", async (collectionApi) => {
return collectionApi.getFilteredByGlob("src/blog/*.md").map((post) => {
post.data.layout = "blog"; return post;
});
});
// Setup RSS feeds
const collection = { name: "blog", limit: 0 };
config.addPlugin(feedPlugin, { type: "rss", outputPath: "/index.xml", collection, metadata });
config.addPlugin(feedPlugin, { type: "atom", outputPath: "/atom.xml", collection, metadata });
config.addPlugin(feedPlugin, { type: "json", outputPath: "/feed.json", collection, metadata });
// Image optimizations
config.addPlugin(eleventyImageTransformPlugin, {
extensions: "html",
formats: ["webp", "jpeg", "svg"],
urlPath: "/optimized/",
defaultAttributes: {
loading: "lazy",
decoding: "async",
},
});
// Smartypants!!
// Turned off, because it's mega inconsistent on where the stuff is smartified.
//config.amendLibrary("md", (md) => md.set({ typographer: true }));
// Custom filters
config.addFilter("date", (date, format = "%Y-%m-%d") => {
const zeroPad = (num) => String(num).padStart(2, "0");
const formatMap = {
"%Y": date.getFullYear(),
"%m": zeroPad(date.getMonth() + 1),
"%d": zeroPad(date.getDate()),
"%H": zeroPad(date.getHours()),
"%M": zeroPad(date.getMinutes()),
"%S": zeroPad(date.getSeconds()),
"%b": date.toLocaleString("en-US", { month: "short" }),
"%B": date.toLocaleString("en-US", { month: "long" }),
};
return format.replace(/%[YmdHMSbB]/g, (match) => formatMap[match] || match);
});
config.addFilter("mdash", text => text.replace(/--(?!>)(?!--)/g, "—"));
config.addFilter("shortc", text => text
.replace(/\(TM\)/g, "™")
.replace(/\(c\)/g, "©")
.replace(/:back:/g, "←")
.replace(/:go:/g, "→")
.replace(/:x:/g, "×")
.replace(/:love:/g, "♡")
.replace(/:hot:/g, "🔥")
.replace(/:sparkles:/g, "✨")
.replace(/:rocket:/g, "🚀")
.replace(/:email:/g, "✉️")
.replace(/:video:/g, "📺")
.replace(/:audio:/g, "🎙️")
.replace(/:shrug:/g, "¯\\_(ツ)_/¯")
.replace(/:dancing:/g, "ᕕ( ᐛ )ᕗ")
.replace(/:fight:/g, "(ง'̀-'́)ง")
.replace(/:flex:/g, "ᕦ(•̀‿•́ )ᕤ")
.replace(/:happy:/g, "(✿◠‿◠)")
.replace(/:cute:/g, "٩(。•́‿•̀。)۶"));
// Format HTML before spitting it out
config.addTransform("prettier", function (content, outputPath) {
const extension = path.extname(outputPath);
switch (extension) {
case ".html":
case ".json":
// Strip leading period from extension and use as the Prettier parser.
const parser = extension.replace(/^./, "");
return prettier.format(content, { parser });
default:
return content;
}
});
// Pre-process CSS using Lightning for
// minimazation and backwards-compatibility.
const TARGETS = browserslistToTargets(browserslist(PACKAGE.browserslists));
const IMPORT_RULE_REGEX = /@import\s+(?:url\()?['"]?([^'"\);]+)['"]?\)?.*;/g;
// Add compile-time dependency on @imported
// files to enable --incremental rebuilds.
const calculateDependencies = function (directory, contents) {
if (!contents.includes("@import")) return [];
const fileList = [];
let match;
while ((match = IMPORT_RULE_REGEX.exec(contents))) {
if (/^https?:\/\//.test(match[1])) continue;
else fileList.push(directory + "/" + path);
}
return fileList;
};
// Inline remote HTTP(s) resources via @import,
// instead of crashing with ENOENT errors.
const remoteResolver = {
resolve(specifier, from) {
if (/^https?:\/\//.test(specifier)) return specifier;
else return path.resolve(path.dirname(from), specifier);
},
async read(filePath) {
if (/^https?:\/\//.test(filePath)) {
const response = await fetch(filePath);
return response.text();
} else {
const file = Bun.file(filePath);
return await file.text();
}
},
};
const bundleStylesheet = async function (filename) {
let { code } = await bundleAsync({
filename,
minify: true,
sourceMap: false,
targets: TARGETS,
resolver: remoteResolver,
drafts: { nesting: true },
});
return code;
};
const lightningCSSPlugin = {
outputFileExtension: "css",
useLayouts: false,
compileOptions: {
permalink: () => (data) => `${data.page.filePathStem}.css`,
},
compile: async function (inputContent, inputPath) {
// Skip files starting with an underscore.
let parsedPath = path.parse(inputPath);
if (parsedPath.name.startsWith("_")) return;
// Calculate compile-time dependencies.
const dependencies = calculateDependencies(parsedPath.dir, inputContent);
this.addDependencies(inputPath, dependencies);
return async () => await bundleStylesheet(inputPath);
},
};
// Process CSS files like templates & skip default layout.
config.addTemplateFormats("css");
config.addExtension("css", lightningCSSPlugin);
return {
dir: {
input: "src",
includes: "../inc",
data: "../data",
output: "dist",
},
};
}