-
Notifications
You must be signed in to change notification settings - Fork 1
/
eleventy.config.js
183 lines (160 loc) · 5.22 KB
/
eleventy.config.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
import path from 'node:path';
import { EleventyRenderPlugin as pluginRender } from '@11ty/eleventy';
import { eleventyImageTransformPlugin as pluginImage } from '@11ty/eleventy-img';
import pluginWebc from '@11ty/eleventy-plugin-webc';
import pluginRss from '@11ty/eleventy-plugin-rss';
import slugify from '@sindresorhus/slugify';
import markdownIt from 'markdown-it';
import markdownItAnchor from 'markdown-it-anchor';
import markdownItAttr from 'markdown-it-attrs';
import markdownItMathJax from 'markdown-it-mathjax3';
import markdownItPrism from 'markdown-it-prism';
import { JSDOM } from 'jsdom';
/**
* @param {import("@11ty/eleventy/src/UserConfig")} eleventyConfig
*/
export default function (eleventyConfig) {
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginRender);
eleventyConfig.addPlugin(pluginImage, {
extensions: 'html',
formats: ['webp', 'jpeg'],
widths: ['auto', 400, 800, 1400],
defaultAttributes: {
loading: 'lazy',
decoding: 'async',
sizes: '100vw',
},
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `${name}-${width}w.${format}`;
},
sharpOptions: {
animated: true,
limitInputPixels: false,
},
});
eleventyConfig.addPlugin(pluginWebc, {
components: ['./src/_components/**/*.webc'],
});
eleventyConfig.addWatchTarget('./src/scss/**/*');
eleventyConfig.addWatchTarget('./src/js/**/*');
eleventyConfig.addPassthroughCopy('./src/js/vendor', 'js');
eleventyConfig.addPassthroughCopy('./src/meta');
eleventyConfig.addPassthroughCopy('./src/fonts');
eleventyConfig.addPassthroughCopy('./src/img');
eleventyConfig.addPassthroughCopy('./src/functions');
eleventyConfig.addCollection('hosted case study', function (collectionsApi) {
return collectionsApi.getFilteredByTag('case study').filter((el) => {
return el.page.outputPath;
});
});
eleventyConfig.addFilter('getTOC', (md) => {
const { document } = new JSDOM(md).window;
const h2s = [...document.querySelectorAll('h2')];
const tocData = h2s.map((h2) => {
if (h2.textContent) {
return { text: h2.textContent, id: h2.id };
}
});
return tocData;
});
eleventyConfig.addFilter('youtubeIdFromUrl', (youtubeUrl) => {
const url = new URL(youtubeUrl);
const params = new URLSearchParams(url.search);
if (params.get('v')) {
return params.get('v');
}
return youtubeUrl;
});
eleventyConfig.addFilter(
'findPostByPath',
function (collectionsAll, postUrl) {
const matchedPost = collectionsAll.find(
(post) => post.outputPath && post.outputPath?.includes(postUrl)
);
return { ...matchedPost.data, url: matchedPost.url };
}
);
eleventyConfig.addFilter('sortedByDate', (arr) => {
return arr.sort((a, b) => b.date - a.date);
});
// TODO: This should probably just be a sortBy function that takes a property to sort by as an argument
eleventyConfig.addFilter('sortedByPublishDate', (arr) => {
return arr.sort(
(a, b) => new Date(b.data.publishDate) - new Date(a.data.publishDate)
);
});
eleventyConfig.addFilter('toSet', (arr) => {
return [...new Set(arr)];
});
eleventyConfig.addFilter('readTime', (str) => {
const { document } = new JSDOM(`${str}`).window;
// Calculate read time without code samples or mathJax.
const elementsToRemove = [
...document.querySelectorAll('pre, mjx-container'),
];
elementsToRemove.forEach((element) => element.remove());
const text = document.body.textContent;
const wordCount = text.split(' ').length;
const wordsPerMinute = 200;
const readingTime = Math.ceil(wordCount / wordsPerMinute);
return `${readingTime} minute read`;
});
eleventyConfig.addFilter('monthYearDate', (date) => {
return new Date(date).toLocaleDateString('en-US', {
month: 'short',
year: 'numeric',
});
});
eleventyConfig.addFilter('humanReadableDateTime', (dateStr) => {
const date = new Date(dateStr);
const timezoneDiff = date.getTimezoneOffset() * 60000;
const adjustedDate = new Date(date.valueOf() + timezoneDiff);
return adjustedDate.toLocaleDateString('en-US', {
month: 'long',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
});
eleventyConfig.addFilter('humanReadableDate', (dateStr) => {
const date = new Date(dateStr);
const timezoneDiff = date.getTimezoneOffset() * 60000;
const adjustedDate = new Date(date.valueOf() + timezoneDiff);
return adjustedDate.toLocaleDateString('en-US', {
month: 'long',
day: 'numeric',
year: 'numeric',
});
});
eleventyConfig.addFilter('toISOString', (str) => {
return str.toISOString();
});
eleventyConfig.setLibrary(
'md',
markdownIt({
html: true,
xhtmlOut: false,
typographer: true,
})
.use(markdownItMathJax, {
svg: {
scale: 2,
},
chtml: { displayAlign: 'left' },
})
.use(markdownItAttr)
.use(markdownItAnchor, {
slugify,
})
.use(markdownItPrism)
);
return {
dir: {
input: 'src',
},
};
}