-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
191 lines (174 loc) · 5.67 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
import Image, { generateHTML } from "@11ty/eleventy-img";
import browserslist from "browserslist";
import { browserslistToTargets } from "lightningcss";
import commonjs from "@rollup/plugin-commonjs";
import eleventyNavigationPlugin from "@11ty/eleventy-navigation";
import { full } from "markdown-it-emoji";
import lightningCssPlugin from "./lib/lightning-css/index.js";
import { load } from "js-yaml";
import loadJson from "@rollup/plugin-json";
import markdownItContainer from "markdown-it-container";
import pluginRss from "@11ty/eleventy-plugin-rss";
import resolve from "@rollup/plugin-node-resolve";
import rollupPlugin from "eleventy-plugin-rollup";
import shikier from "./lib/shikier/index.js";
import typescript from "@rollup/plugin-typescript";
function generateImages(src) {
return Image(src, {
formats: [
"avif",
"webp",
src.toLowerCase().endsWith(".png") ? "png" : "jpeg",
],
outputDir: "_site/img/",
widths: [256, 512, 1024, null],
});
}
async function imageShortcode(src, alt, sizes = "(min-width: 50rem) 50rem, 100vw") {
const metadata = await generateImages(src);
const imageAttributes = {
alt,
decoding: "async",
loading: "lazy",
sizes,
};
// You bet we throw an error on missing alt in `imageAttributes` (alt="" works okay)
return generateHTML(metadata, imageAttributes);
}
function generateFavicon(src) {
return Image(src, {
formats: ["svg", "png", "webp", "avif"],
outputDir: "_site/img/",
widths: [64, 128, 180, 256, 512, 1024, 2048, null],
});
}
async function generateFaviconHTML(src) {
const metadata = await generateFavicon(src);
return `
<link rel="icon" href="${metadata.svg[0].url}" type="image/svg+xml">
<link rel="apple-touch-icon" href="${
metadata.png.find((png) => png.width === 180).url
}">
`;
}
function tagCategory(tag) {
if (tag.includes(":")) {
return tag.split(":")[0];
}
}
function tagValue(tag) {
if (!tag.includes(":")) return tag;
return tag.split(":").slice(1);
}
const registerPlugins = (eleventyConfig) => {
eleventyConfig.addPlugin(shikier);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.addPlugin(rollupPlugin, {
rollupOptions: {
output: {
dir: "_site/js",
format: "es",
sourcemap: process.env.NETLIFY !== "true",
},
plugins: [typescript(), commonjs(), resolve(), loadJson()],
},
});
const targets = browserslistToTargets(
browserslist("last 2 versions, not dead, > 0.2%")
);
eleventyConfig.addPlugin(lightningCssPlugin, {
lightningOptions: {
drafts: {
nesting: true,
},
minify: true,
targets,
},
});
};
const registerFileConfigs = (eleventyConfig) => {
eleventyConfig.addWatchTarget("assets/css");
eleventyConfig.addWatchTarget("assets/js/");
eleventyConfig.addPassthroughCopy("assets/img");
eleventyConfig.addPassthroughCopy("assets/video");
eleventyConfig.addPassthroughCopy("assets/fonts");
};
function registerCollections(eleventyConfig) {
eleventyConfig.addCollection("blogposts", (collectionApi) =>
collectionApi.getFilteredByGlob("src/blog/*.md")
);
eleventyConfig.addCollection("sins", (collectionApi) =>
collectionApi.getFilteredByGlob("src/webdev-sins/*.md")
);
eleventyConfig.addCollection("posts", (collectionApi) =>
collectionApi.getFilteredByGlob(["src/blog/*.md", "src/webdev-sins/*.md"])
);
}
const addFilters = (eleventyConfig) => {
eleventyConfig.addFilter("logging", (input, label, passthrough) => {
console.log(`logging-${label}:`, input);
if (passthrough) return input;
});
eleventyConfig.addFilter("encodeURIComponent", encodeURIComponent);
eleventyConfig.addNunjucksAsyncFilter("faviconData", (src, callback) =>
generateFavicon(src).then((data) => callback(null, data))
);
eleventyConfig.addNunjucksAsyncFilter("imageData", (src, callback) =>
generateImages(src).then((data) => callback(null, data))
);
eleventyConfig.addFilter("tagCategory", tagCategory);
eleventyConfig.addFilter("tagValue", tagValue);
eleventyConfig.addFilter("niceDate", (date) => {
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Okt",
"Nov",
"Dec",
];
return `${date.getDate()}. ${
months[date.getMonth()]
} ${date.getFullYear()}`;
});
};
const addShortcodes = (eleventyConfig) => {
eleventyConfig.addDataExtension("yaml", (contents) => load(contents));
eleventyConfig.addDataExtension("yml", (contents) => load(contents));
eleventyConfig.addShortcode("currentTime", () => Date.now().toString());
eleventyConfig.addAsyncShortcode("image", imageShortcode);
eleventyConfig.addAsyncShortcode("favicon", generateFaviconHTML);
};
export default function(eleventyConfig) {
eleventyConfig.amendLibrary("md", (mdLib) =>
mdLib
.set({
breaks: true,
html: true,
linkify: true,
})
.use(full)
.use(markdownItContainer, "sidenote")
.use(markdownItContainer, "commentBlock")
.use(markdownItContainer, "actionBlock")
.use(markdownItContainer, "reader-thought")
.use(markdownItContainer, "writer-thought")
);
registerPlugins(eleventyConfig);
addShortcodes(eleventyConfig);
addFilters(eleventyConfig);
registerFileConfigs(eleventyConfig);
registerCollections(eleventyConfig);
// Return your Object options:
return {
dir: { input: "src" },
markdownTemplateEngine: "njk",
};
}