-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
118 lines (103 loc) · 3.15 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
const util = require("node:util");
const slugify = require("slugify");
const htmlmin = require("html-minifier");
const sass = require("node-sass");
const {
getAllPublishedPosts,
getMarkdownRenderer,
sortContentMostRecent,
getAllPublishedProjects,
} = require("./.eleventy-utils");
module.exports = function (eleventyConfig) {
eleventyConfig.addPassthroughCopy({
"src/static": "/static",
});
// Add layouts
eleventyConfig.addLayoutAlias("post", "layouts/post.njk");
eleventyConfig.addLayoutAlias("project", "layouts/project.njk");
// Collections
eleventyConfig.addCollection("mostRecentPosts", (collectionApi) => {
return getAllPublishedPosts(collectionApi).sort((post1, post2) =>
sortContentMostRecent(post1, post2)
);
});
eleventyConfig.addCollection("postsGroupedByCategories", (collectionApi) => {
const publishedPosts = getAllPublishedPosts(collectionApi);
const categories = new Map();
publishedPosts.forEach((post) => {
if (!categories.has(post.category_name)) {
categories.set(post.category_name, []);
}
categories.get(post.category_name).push(post);
});
return Array.from(categories.keys())
.sort((a, b) => a.localeCompare(b))
.map((key) => {
return {
label: key,
slug: slugify(key, { lower: true }),
posts: categories
.get(key)
.slice(0)
.sort((post1, post2) => sortContentMostRecent(post1, post2)),
};
});
});
eleventyConfig.addCollection("postsGroupedByTags", (collectionApi) => {
const publishedPosts = getAllPublishedPosts(collectionApi);
const tags = new Map();
publishedPosts.forEach((post) => {
post.tags.forEach((tag) => {
if (!tags.has(tag.label)) {
tags.set(tag.label, []);
}
tags.get(tag.label).push(post);
});
});
return Array.from(tags.keys())
.sort((a, b) => a.localeCompare(b))
.map((key) => {
return {
label: key,
slug: slugify(key, { lower: true }),
posts: tags
.get(key)
.slice(0)
.sort((post1, post2) => sortContentMostRecent(post1, post2)),
};
});
});
eleventyConfig.addCollection("projects", (collectionApi) => {
return getAllPublishedProjects(collectionApi);
});
// Markdown renderer
eleventyConfig.setLibrary("md", getMarkdownRenderer());
// Debug
eleventyConfig.addFilter("console", function (value) {
const str = util.inspect(value);
return `<div style="white-space: pre-wrap;">${unescape(str)}</div>;`;
});
// CSS (SASS) Minifier
eleventyConfig.addFilter("cssminify", function (code) {
return sass
.renderSync({ data: code, outputStyle: "compressed" })
.css.toString("utf8");
});
// HTML Minifier
eleventyConfig.addTransform("html-minify", (content, outputPath) => {
if (outputPath.endsWith(".html")) {
return htmlmin.minify(content, {
collapseWhitespace: true,
minifyURLs: true,
removeComments: true,
useShortDoctype: true,
});
}
return content;
});
return {
dir: {
input: "src",
},
};
};