-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.eleventy.js
More file actions
92 lines (80 loc) · 3.55 KB
/
.eleventy.js
File metadata and controls
92 lines (80 loc) · 3.55 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
import {eleventyImageTransformPlugin} from "@11ty/eleventy-img";
import {execSync} from "child_process";
const contentTags = ["concept", "method", "story", "practice", "project", "playground"];
function getGitLastModified(filePath) {
try {
const result = execSync(`git log -1 --format=%ci "${filePath}"`, {encoding: "utf-8"}).trim();
return result ? new Date(result) : new Date();
} catch {
return new Date();
}
}
function sortByTitle(a, b) {
const strip = (title) => title.replace(/^[^a-zA-Z]+/, '');
return strip(a.data.title).localeCompare(strip(b.data.title));
}
// noinspection JSUnusedGlobalSymbols
export default async function (eleventyConfig) {
eleventyConfig.addPassthroughCopy({"_includes/css/output.css": "style.css"});
eleventyConfig.addPassthroughCopy({"_includes/js/bundle.js": "bundle.js"});
eleventyConfig.addPassthroughCopy({"_includes/js/movement-bundle.js": "movement-bundle.js"});
eleventyConfig.addPassthroughCopy({"_includes/fonts": "fonts"});
eleventyConfig.addPassthroughCopy("site.webmanifest");
eleventyConfig.addWatchTarget("_includes/css/output.css");
eleventyConfig.addWatchTarget("_includes/js/bundle.js");
eleventyConfig.addWatchTarget("_includes/js/movement-bundle.js");
eleventyConfig.addPlugin(eleventyImageTransformPlugin);
eleventyConfig.setServerOptions({
watch: ["_site/**/*.css", "_site/**/*.js"],
delay: 1000
});
// Create sorted collections for each content type
for (const tag of contentTags) {
eleventyConfig.addCollection(`${tag}Sorted`, function (collectionApi) {
return collectionApi.getFilteredByTag(tag).sort(sortByTitle);
});
}
// Combine all content into a single sorted collection
eleventyConfig.addCollection("allContentSorted", function (collectionApi) {
return contentTags.flatMap(tag => collectionApi.getFilteredByTag(tag)).sort(sortByTitle);
});
// Sitemap collection sorted by last modified (most recent first)
eleventyConfig.addCollection("sitemapSorted", function (collectionApi) {
const allPages = collectionApi.getAll();
const mostRecentByCollection = {};
let mostRecentOverall = new Date(0);
// Single pass: calculate git dates and track the most recent per collection
for (const page of allPages) {
const gitDate = getGitLastModified(page.inputPath);
page.data.gitLastModified = gitDate;
const tags = page.data.tags || [];
for (const tag of tags) {
if (!mostRecentByCollection[tag] || gitDate > mostRecentByCollection[tag]) {
mostRecentByCollection[tag] = gitDate;
}
if (contentTags.includes(tag) && gitDate > mostRecentOverall) {
mostRecentOverall = gitDate;
}
}
}
// Assign final lastModified and sort
return allPages.map(page => {
const from = page.data["useMostRecentFrom"];
if (from === "all") {
page.data.lastModified = mostRecentOverall;
} else if (from) {
page.data.lastModified = mostRecentByCollection[from] || page.data.gitLastModified;
} else {
page.data.lastModified = page.data.gitLastModified;
}
return page;
}).sort((a, b) => b.data.lastModified - a.data.lastModified);
});
return {
dir: {
input: ".",
output: "_site",
includes: "_includes"
}
};
};