-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy path.eleventy.js
More file actions
448 lines (399 loc) · 18.2 KB
/
Copy path.eleventy.js
File metadata and controls
448 lines (399 loc) · 18.2 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import mdit from 'markdown-it';
import md_abbr from 'markdown-it-abbr';
import md_anchor from 'markdown-it-anchor';
import md_attrs from 'markdown-it-attrs';
import { full as md_emoji } from 'markdown-it-emoji';
import md_footnote from 'markdown-it-footnote';
import md_plainText from 'markdown-it-plain-text';
import { stripHtml } from "string-strip-html";
import twemoji from 'twemoji';
import { RenderPlugin } from "@11ty/eleventy";
import webc from '@11ty/eleventy-plugin-webc';
import embedEverything from 'eleventy-plugin-embed-everything';
import fs from "node:fs/promises";
import path, { resolve } from "node:path";
import * as sass from "sass";
import { DateTime } from "luxon";
import * as pagefind from "pagefind";
import browserslist from 'browserslist';
import { transform, browserslistToTargets } from 'lightningcss';
export default function(config) {
function dateSort(a, b) {
let ad = a.page.date;
let bd = b.page.date;
if (Object.hasOwn(a.data, "payload")) {
ad = a.data.payload.created ?? ad;
}
if (Object.hasOwn(b.data, "payload")) {
bd = b.data.payload.created ?? bd;
}
return bd - ad;
}
// https://www.11ty.dev/docs/config/#configuration-2
config.setInputDirectory("src");
// https://www.11ty.dev/docs/copy/#configuration-api-method
// Doesnt appear to be relative from the input directory. https://github.com/11ty/eleventy/issues/2043#issuecomment-948826977
config.addPassthroughCopy("src/img");
config.addPassthroughCopy("src/svg");
config.addPassthroughCopy("src/js");
config.addPassthroughCopy("src/twemoji/svg");
config.addPassthroughCopy("src/.well-known");
config.addPassthroughCopy("src/robots.txt");
config.addPassthroughCopy("src/_headers");
// https://www.11ty.dev/docs/copy/#emulate-passthrough-copy-during-serve
config.setServerPassthroughCopyBehavior("passthrough");
// https://www.11ty.dev/docs/ignores/#configuration-api
// Can not add a glob pattern and then delete a single path
// thats included in that glob pattern.
config.ignores.add("./src/@andy_li/**");
config.ignores.add("./src/@b_garcia/**");
config.ignores.add("./src/@chman/**");
config.ignores.add("./src/@fponticelli/**");
config.ignores.add("./src/@gamehaxe/**");
config.ignores.add("./src/@grayhaze/**");
config.ignores.add("./src/@ncannasse/**");
config.ignores.add("./src/@omgjjd/**");
config.ignores.add("**/copy_me.*");
// @see https://jonathanyeong.com/writing/excerpts-with-eleventy/
config.setFrontMatterParsingOptions( { excerpt:true } );
//https://www.11ty.dev/docs/languages/webc/#installation
config.addPlugin(RenderPlugin);
config.addPlugin(webc, {
components: "src/_components/**/*.webc"
});
config.addPlugin(embedEverything, {
add: ["youtube"]
});
// v4 only
//config.setHtmlTemplateEngine("webc");
// https://www.11ty.dev/docs/languages/markdown/
// https://www.11ty.dev/docs/languages/markdown/#add-your-own-plugins
// https://github.com/markdown-it/markdown-it#init-with-presets-and-options
config.amendLibrary("md", (md) => {
// https://markdown-it.github.io/markdown-it/#MarkdownIt.new
md = md
//https://www.11ty.dev/docs/languages/markdown/#optional-amend-the-library-instance
.set({ html: true, typographer: true, linify: true})
//https://www.11ty.dev/docs/languages/markdown/#add-your-own-plugins
.use(md_abbr)
.use(md_anchor, {
level: [2,3,4],
permalink: md_anchor.permalink.headerLink({
safariReaderFix: true,
class: "header-anchor contrast"
})
})
.use(md_attrs)
.use(md_emoji)
.use(md_footnote)
.use((md) => {
let index = md.block.ruler.__find__('heading');
let original = md.block.ruler.__rules__[index].fn;
md.block.ruler.at('heading', function(state, startLine, _endLine, silent) {
let result = original(state, startLine, _endLine, silent);
let tokens = state.tokens;
for (let i = 0; i < tokens.length-2; i++) {
let a = tokens[i];
let b = tokens[i+1];
let c = tokens[i+2];
let d = tokens[i+3];
if (a.type == 'heading_open') {
const headings = ['In case you missed it', 'People & Projects to support', 'Current Proposals & Discussions'];
headings.forEach(function(ignore) {
if (b.content.indexOf(ignore) > -1) {
a.attrSet('data-pagefind-ignore', '');
d?.attrSet('data-pagefind-ignore', '');
}
});
}
if (a.type == 'heading_open' && a.tag == 'h1' && c.type == 'heading_close') {
a.attrSet('slot', 'title');
}
}
return result;
})
});
// https://github.com/markdown-it/markdown-it-emoji
md.renderer.rules.emoji = function(token, idx) {
return twemoji.parse(token[idx].content, { base: "/twemoji/", folder: "svg", ext: ".svg" });
}
return md;
});
// https://www.11ty.dev/docs/filters/#asynchronous-filters
config.addFilter("minus_months", function (date, amount) {
return DateTime
.fromJSDate(date)
.minus( { months: amount })
.toJSDate();
})
config.addFilter("date_lessthan", function (lhs, rhs) {
return lhs < rhs;
})
config.addFilter("object_keys", function (object) {
return Object.keys(object);
});
// https://jonathanyeong.com/writing/excerpts-with-eleventy/
config.addFilter("md", function(content = "") {
return mdit({ html:true }).render( content );
});
config.addFilter("md2txt", function(content = "") {
return stripHtml( mdit().use( md_plainText ).render( content ) ).result;
});
config.addFilter("date_as_rfc822", function(date) {
return DateTime
.fromJSDate(date)
.toHTTP();
})
config.addFilter("txt2bin", function(content = "") {
let result = "";
for (let i = 0; i < content.length; i++) {
let codepoint = content.codePointAt(i);
let binary = codepoint.toString(2);
binary = binary.padStart(8, "0");
result += binary;
}
return result;
})
// https://www.11ty.dev/docs/languages/sass/#configuration
/**
* Not as simple as first thought:
* Read:
* - https://danburzo.ro/eleventy-sass/
* - https://jkc.codes/blog/using-sass-with-eleventy/
* - https://11ty.rocks/posts/process-css-with-lightningcss/
*/
config.addTemplateFormats("scss")
config.addExtension("scss", {
outputFileExtension: "css",
// opt-out of Eleventy Layouts
useLayouts: false,
// and they don't need to be in rss or sitemap feeds.
//eleventyExcludeFromCollections: true,
// inherit liquid syntax so we can use and access data cascade.
key: "liquid",
compile: async function (inputContent, inputPath) {
let parsed = path.parse(inputPath);
// Don’t compile file names that start with an underscore
if(parsed.name.startsWith("_")) {
return;
}
return async (data) => {
let value = await this.defaultRenderer(data);
//console.log(value);
let result = await sass.compileStringAsync(value, {
loadPaths: [
parsed.dir || ".",
this.config.dir.input,
this.config.dir.includes,
"./node_modules",
],
// Compressing here squeezes out a few more w/ lightningcss
style: 'compressed'
});
// Map dependencies for incremental builds
this.addDependencies(inputPath, result.loadedUrls);
// Minify with lighningcss
// From https://11ty.rocks/posts/process-css-with-lightningcss/#autoprefixing-and-minification-with-lightningcss
let targets = browserslistToTargets(
browserslist("defaults, not dead, baseline widely available with downstream")
);
let { code } = await transform({
code: Buffer.from(result.css),
minify: true,
sourceMap: false,
targets: targets,
});
return code;
};
},
});
// https://www.11ty.dev/docs/languages/custom/#get-data-and-get-instance-from-input-path
// https://rknight.me/blog/adding-cooklang-support-to-eleventy-two-ways/
config.addExtension("md", {
key:"md",
getData: async function (inputPath) {
let content = await fs.readFile(inputPath, {encoding: "utf8"});
let md = mdit()
.use(md_emoji);
md.renderer.rules.emoji = function(token, idx) {
return twemoji.parse(token[idx].content, { base: "https://haxe.io/twemoji/", folder: "svg", ext: ".svg" });
}
var refdef = {};
var payload = {
authors: [],
contributors: []
};
let index = md.block.ruler.__find__('reference');
let original = md.block.ruler.__rules__[index].fn;
md.block.ruler.at('reference', function(state, startLine, _endLine, silent) {
var result = original(state, startLine, _endLine, silent);
var object = state.env;
if (object?.references) {
// Multiple items
if (object.references?.AUTHOR) {
let auth = object.references.AUTHOR;
if (!payload.authors.some((o) => o == auth.title)) {
payload.authors.push( { display: auth.title, url: auth.href } );
delete object.references.AUTHOR;
}
}
if (object.references?._AUTHOR) {
let auth = object.references._AUTHOR
if (!payload.authors.some((o) => o == auth.title)) {
payload.authors.push( { display: auth.title, url: auth.href } );
delete object.references._AUTHOR;
}
}
if (object.references?.CONTRIBUTOR) {
let con = object.references.CONTRIBUTOR;
if (!payload.contributors.some((o) => o == con.title)) {
payload.contributors.push( { display: con.title, url: con.href } );
delete object.references.CONTRIBUTOR;
}
}
// Single items
if (object.references?.DATE) {
let timestamp = DateTime.fromISO(object.references.DATE.title);
if (timestamp.isValid) {
payload.created = timestamp.toJSDate();
} else {
let date = DateTime.fromFormat(object.references.DATE.title, 'ccc. LLLL d, yyyy @ t');
if (date.isValid) {
payload.created = date.toJSDate();
} else {
// Fallback to standard Date
let date = new Date();
let timestamp = Date.parse(object.references.DATE.title);
if (!Number.isNaN(timestamp)) {
date.setTime(timestamp);
payload.created = date;
}
}
}
delete object.references.DATE;
}
if (object.references?.MODIFIED) {
let date = new Date();
let timestamp = Date.parse(object.references.MODIFIED.title);
if (!Number.isNaN(timestamp)) {
date.setTime(timestamp);
payload.modified = date;
}
delete object.references.MODIFIED;
}
if (object.references?.PUBLISHED) {
let date = new Date();
let timestamp = Date.parse(object.references.PUBLISHED.title);
if (!Number.isNaN(timestamp)) {
date.setTime(timestamp);
payload.published = date;
}
delete object.references.PUBLISHED;
}
if (object.references?.DESCRIPTION) {
payload.description = object.references.DESCRIPTION.title;
delete object.references.DESCRIPTION;
}
}
return result;
});
let tokens = md.parse(content.toString(), refdef);
for (let i = 0; i < tokens.length-2; i++) {
let a = tokens[i];
let b = tokens[i+1];
let c = tokens[i+2];
if (a.type == 'heading_open' && a.tag == 'h1' && c.type == 'heading_close' && !payload.title) {
payload.title = stripHtml( md.renderInline(b.content) ).result;
break;
}
}
// Use package.json author details if no author was specified.
var extra = {};
if (payload.authors.length == 0) {
// https://www.11ty.dev/docs/data-computed/#using-a-template-string
extra = {
eleventyComputed: {
payload: {
authors: [{
display: "{{ pkg.author.name }}",
url: "{{ pkg.author.url }}"
}]
}
}
};
}
// Overwrite titles for roundups & older content
if (!Object.hasOwn(payload, "title") && inputPath.indexOf('/roundups') > -1) {
extra.eleventyComputed.payload.title = "Haxe Roundup {{ page.fileSlug }}";
if (inputPath.indexOf('/wwx/') > -1) {
extra.eleventyComputed = extra.eleventyComputed || {};
extra.eleventyComputed.payload = extra.eleventyComputed.payload || {};
extra.eleventyComputed.payload.title = "WWX {{ page.fileSlug }} Roundup";
extra.eleventyComputed.date = "{{ page.fileSlug }}";
}
}
if (inputPath.indexOf('src/wwx/') > -1) {
let parts = inputPath.split('/');
let year = parts.filter((str) => !Number.isNaN(Number.parseInt(str)));
if (year.length > 0) {
extra.eleventyComputed = extra.eleventyComputed || {};
extra.eleventyComputed.payload = extra.eleventyComputed.payload || {};
extra.eleventyComputed.payload.title = "WWX " + year[0] + " Developer Interview: {{ page.fileSlug }}";
if (!payload?.published) payload.published = new Date(year);
if (!payload?.modified) payload.modified = new Date(year);
if (!payload?.created) payload.created = new Date(year);
}
}
//console.log(payload);
return { ...refdef, payload:payload, ...extra };
}
});
// https://www.11ty.dev/docs/collections-api/#example-get-all-sort
config.addCollection("everything", async (collections) => {
return collections.getAll().sort( dateSort );
})
// https://www.11ty.dev/docs/collections/#collection-item-data-structure
config.addCollection("roundups", async (collections) => {
let roundups = collections.getFilteredByTag("roundups");
roundups = roundups.filter(function(r) {
return r.data.tags.length == 1 && r.data.tags[0] == "roundups";
})
roundups = roundups.sort( function(a, b) {
let an = Number.parseInt(a.page.fileSlug);
let bn = Number.parseInt(b.page.fileSlug);
if (Number.isNaN(an)) {
an = 0;
}
if (Number.isNaN(bn)) {
bn = 0;
}
return bn - an;
});
return roundups;
});
config.addCollection("wwx", async (collections) => {
let wwx = collections.getFilteredByTag("wwx");
wwx = wwx.filter( function(a) {
if (a.data?.tags && a.data.tags.length == 1) return a.data.tags[0] != "roundups";
return true;
})
wwx = wwx.sort( dateSort );
return wwx;
});
["gamejam", "releases", "videos", "blog"].forEach( function(tag) {
config.addCollection(tag, async (collections) => {
return collections.getFilteredByTag(tag).sort( dateSort );
});
} );
// https://www.11ty.dev/docs/events/#eleventy-after
// https://pagefind.app/docs/node-api/
config.on("eleventy.after", async function (directories, results, runMode, outputMode) {
const {index} = await pagefind.createIndex();
await index.addDirectory({
path: "_site"
});
await index.writeFiles({
outputPath: "_site/pagefind"
});
});
}