-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
139 lines (115 loc) · 4.03 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
const util = require('util');
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const { DateTime } = require("luxon");
const fs = require("fs");
const NOT_FOUND_PATH = "_site/404.html";
const getPostCount = (tag, posts) => {
return posts.filter((post) => post.data.tags?.includes(tag)).length
}
const arrayIncludesTag = (tag, arr) => {
return arr.some((item) => item.title === tag.title)
}
const getTags = (item) => {
return item.data.tags.filter(function (item) {
switch (item) {
// this list should match the `filter` list in tags.njk
case 'all':
case 'nav':
case 'post':
case 'posts':
case 'page':
return false
}
return true
})
}
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addFilter('console', function(value) {
const str = util.inspect(value);
return `<div style="white-space: pre-wrap;">${unescape(str)}</div>;`
});
eleventyConfig.addFilter('dump', obj => {
return util.inspect(obj)
});
eleventyConfig.addFilter("postDate", (dateObj) => {
return DateTime.fromJSDate(dateObj).toLocaleString(DateTime.DATE_FULL);
});
eleventyConfig.addFilter("postDateTime", (dateObj) => {
const shortdate = DateTime.fromJSDate(dateObj).toISODate(DateTime.DATE_SHORT)
if (shortdate) {
return shortdate.replace(/\//g, '-');
} else {
return '';
}
});
eleventyConfig.addCollection("posts", function(collectionApi) {
return collectionApi.getFilteredByGlob("src/posts/**/*.md");
});
eleventyConfig.addCollection('allTags', function (collection) {
let tagSet = new Set()
collection.getAll().forEach(function (item) {
if ('tags' in item.data) {
const tags = getTags(item)
for (const tag of tags) {
tagSet.add({
title: tag,
postCount: getPostCount(tag, collection.getFilteredByGlob("src/posts/**/*.md")),
})
}
}
})
const arr = []
const sortedTags = [...tagSet]
.sort((a, b) => a.postCount - b.postCount)
.reverse()
sortedTags.forEach((tag) => {
if (arrayIncludesTag(tag, arr)) return
arr.push(tag)
})
return arr
})
eleventyConfig.addShortcode("year", () => `${new Date().getFullYear()}`);
eleventyConfig.addPassthroughCopy("src/images");
eleventyConfig.addShortcode("topcap", function(cloudinaryImageName) {
return `
<div class="top-cap">
<img aria-hidden="true"
src="https://res.cloudinary.com/chipcullen/image/upload/b_rgb:000000,c_scale,o_98,w_300/v1669146466/${cloudinaryImageName}.webp"
srcset="https://res.cloudinary.com/chipcullen/image/upload/b_rgb:000000,c_scale,o_98,w_600/v1669146466/${cloudinaryImageName}.webp 600w,
https://res.cloudinary.com/chipcullen/image/upload/b_rgb:000000,c_scale,o_98,w_800/v1669146466/${cloudinaryImageName}.webp 800w,
https://res.cloudinary.com/chipcullen/image/upload/b_rgb:000000,c_scale,o_98,w_1200/v1669146466/${cloudinaryImageName}.webp 1200w"
loading="lazy"
sizes="40vw"
alt="" />
</div>`;
});
// for 404 page development
eleventyConfig.setBrowserSyncConfig({
callbacks: {
ready: function(err, bs) {
bs.addMiddleware("*", (req, res) => {
if (!fs.existsSync(NOT_FOUND_PATH)) {
throw new Error(`Expected a \`${NOT_FOUND_PATH}\` file but could not find one. Did you create a 404.html template?`);
}
const content_404 = fs.readFileSync(NOT_FOUND_PATH);
// Add 404 http status code in request header.
res.writeHead(404, { "Content-Type": "text/html; charset=UTF-8" });
// Provides the 404 content without redirect.
res.write(content_404);
res.end();
});
}
}
});
return {
dir: {
input: "src",
output: "dist",
},
templateFormats: ["html", "md", "njk"],
passthroughFileCopy: true,
};
};