-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
.eleventy.js
285 lines (252 loc) · 10.4 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
import pluginRss from "@11ty/eleventy-plugin-rss";
import { minify } from "html-minifier";
import { createRequire } from "node:module";
import { readFileSync } from "fs";
const require = createRequire(import.meta.url);
const collectionControl = require("./src/_data/collectionsControl.json");
let gardenStr = "./src/pages/garden/node/**/*.{md,csv}";
import slugify from "./utils/slugify.js";
import synHl from "@11ty/eleventy-plugin-syntaxhighlight";
import scss from "./conf/templating/scss.js";
import markdownIt from "./conf/templating/markdown.js";
import csv from "./conf/templating/csv.js";
import vento from "./conf/templating/vento.js";
import { embedMastodon } from "./conf/components/mastodon.js";
import javascript from "./conf/templating/javascript.js";
import filters from "./conf/filters.js";
Error.stackTraceLimit = 100;
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
export default function (eleventyConfig) {
const markdown = markdownIt();
eleventyConfig.addPlugin(synHl);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addShortcode("getSvg", function (name) {
const data = readFileSync(`./src/pages/garden/node/Assets/${name}.svg`);
return data.toString("utf-8");
});
filters(eleventyConfig);
eleventyConfig.addAsyncShortcode("embedMastodon", embedMastodon);
eleventyConfig.addFilter("renderMd", (content) =>
content ? markdown.render(content) : "",
);
eleventyConfig.setLibrary("md", markdown);
eleventyConfig.setFrontMatterParsingOptions({
// @ts-ignore
excerpt: (file, options) =>
(file.excerpt = file.content.split("\n").slice(0, 4).join(" ")),
});
eleventyConfig.addPassthroughCopy({ "./src/_public/": "." });
eleventyConfig.addPassthroughCopy({
"./src/pages/garden/node/Assets/*": "assets",
});
eleventyConfig.addTransform("html", function (content) {
if (
this.page.outputPath &&
this.page.outputPath.endsWith(".html") &&
process.env.ELEVENTY_ENV == "production"
) {
return minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
});
}
return content;
});
// I won't even attempt to explain this
eleventyConfig.addCollection("wtf", function (collectionApi) {
// ok I lied
// acess the first post that can get the information we need
const firstPost = collectionApi.getFilteredByGlob(gardenStr)[0].data;
// and then pass it to itself to emulate computed
const links = firstPost.eleventyComputed.brokenLinks(firstPost, true);
// return as array for pagination
return Array.from(links);
});
eleventyConfig.addCollection("ogReady", function (collectionApi) {
return collectionApi
.getFilteredByGlob("./src/pages/**/*.*")
.filter(function (item) {
return (
item.outputPath.endsWith("html") &&
item.page.inputPath.endsWith(".md") &&
item.data.title != "missing"
);
});
});
eleventyConfig.addCollection("redirects", function (collectionApi) {
// lets make a variable to hold our redirects
let redirects = [];
// We need to get each post in our posts folder. In my case this is /node
const nodes = collectionApi.getFilteredByGlob(gardenStr);
// next lets iterate over all the nodes
nodes.forEach((node) =>
// for each alias
(node.data.aliases || []).forEach((alias) =>
// push the target url and the old url
redirects.push([
slugify(node.data.page.url),
slugify(node.data.page.url).replace(
/\/[^\/]*?(\..+)?$/,
`/${node.data.dontSlug ? alias : slugify(alias)}$1`,
),
]),
),
);
return redirects;
});
eleventyConfig.addCollection("taxes", function (collectionApi) {
// lets make a variable to hold our taxonomies and values
let taxAndValues = [];
// We need to get each post in our posts folder. In my case this is /node
const nodes = collectionApi.getFilteredByGlob(gardenStr);
// next lets iterate over all the nodes
nodes.forEach((node) => {
// and then iterate over the taxonomies
for (const [taxonomy, value] of Object.entries(collectionControl)) {
// I don't want to paginate date, for instance
// this is why my collectionControl is using objects instead of arrays
// @ts-ignore
if (value.excludeFromPagination) continue;
else if (node?.data?.[taxonomy]) {
// this is typeof on drugs
switch (
{}.toString
.call(node.data[taxonomy])
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase()
) {
// if it is an array (for tags especially)
case "array":
node.data[taxonomy].forEach((item) => {
taxAndValues.push([taxonomy, item]);
});
break;
// otherwise
default:
taxAndValues.push([taxonomy, node.data[taxonomy]]);
}
}
}
});
// custom set, sets don't work with objects
// @ts-ignore
const unique = [...new Set(taxAndValues.map(JSON.stringify))].map(
// @ts-ignore
JSON.parse,
);
return unique;
});
eleventyConfig.addCollection("taxesDiffer", function (collectionApi) {
let taxAndValues = [];
const nodes = collectionApi.getFilteredByGlob(gardenStr);
const differ =
Object.keys(collectionControl)[
Object.values(collectionControl).findIndex(
// @ts-ignore
(value) => value.mode == "differentiator",
)
];
nodes.forEach((node) => {
for (const [taxonomy, value] of Object.entries(collectionControl)) {
// @ts-ignore
if (value.excludeFromPagination) continue;
else if (node?.data?.[taxonomy]) {
switch (
{}.toString
.call(node.data[taxonomy])
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase()
) {
case "array": {
node.data[taxonomy].forEach((item) => {
if (
!(
taxonomy == differ &&
item == node.data[differ]
)
)
taxAndValues.push([
taxonomy,
item,
node.data[differ],
]);
});
break;
}
default:
if (
!(
taxonomy == differ &&
node.data[taxonomy] == node.data[differ]
)
)
taxAndValues.push([
taxonomy,
node.data[taxonomy],
node.data[differ],
]);
}
}
}
});
// custom set, sets don't work with objects
// @ts-ignore
const unique = [...new Set(taxAndValues.map(JSON.stringify))].map(
// @ts-ignore
JSON.parse,
);
return unique;
});
eleventyConfig.addCollection("nestedTax", function (collectionApi) {
let nestedTax = {};
const nodes = collectionApi.getFilteredByGlob(gardenStr);
nodes.forEach((node) => {
for (const [taxonomy, value] of Object.entries(collectionControl)) {
const taxValue = node.data[taxonomy];
// @ts-ignore
if (value.excludeFromPagination) continue;
else if (node?.data?.[taxonomy]) {
if (!nestedTax[taxonomy]) nestedTax[taxonomy] = {};
switch (
{}.toString
.call(taxValue)
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase()
) {
case "array": {
taxValue.forEach((item) => {
// if the value in the object does not yet exist
if (!nestedTax[taxonomy][item])
nestedTax[taxonomy][item] = [];
// then add the entire page to it
nestedTax[taxonomy][item].push(node);
});
break;
}
// otherwise
default: {
// if the value in the object does not yet exist
if (!nestedTax[taxonomy][taxValue])
nestedTax[taxonomy][taxValue] = [];
// then add the entire page to it
nestedTax[taxonomy][taxValue].push(node);
}
}
}
}
});
return nestedTax;
});
javascript(eleventyConfig);
scss(eleventyConfig);
csv(eleventyConfig, markdown);
eleventyConfig.setQuietMode(true);
eleventyConfig.addPlugin(vento);
return {
dir: {
input: "src",
output: "out",
},
};
}