-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
239 lines (206 loc) · 7.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
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
module.exports = (eleventyConfig) => {
const md = require("markdown-it")({
linkify: true,
typographer: true,
html: true
});
const mdAttrs = require("markdown-it-link-attributes");
const mdTocAndAnchor = require('markdown-it-toc-and-anchor').default;
const mdPlainText = require('markdown-it-plain-text');
const siteData = require("./src/_data/site.json")
const Image = require("@11ty/eleventy-img");
const path = require("path");
const he = require("he");
const _chunk = require("lodash.chunk");
// Prevent clashing with static_assets folder (which is git-ignored)
eleventyConfig.setUseGitIgnore(false);
// Copy static_assets folder to dist
eleventyConfig.addPassthroughCopy("src/static_assets");
// Register `markdownify` filter
md.use(mdTocAndAnchor, {
anchorLink: false,
toc: true,
tocClassName: 'toc__list',
tocFirstLevel: 2,
slugify: string => eleventyConfig.getFilter('slugify')(string)
});
md.use(mdAttrs, {
matcher(href, config) {
return href.startsWith("https://");
},
attrs: {
target: "_blank",
rel: "noopener noreferrer",
},
});
md.use(mdPlainText)
eleventyConfig.setLibrary("md", md);
eleventyConfig.addFilter("markdownify", (string) => {
return md.renderInline(string);
});
eleventyConfig.addFilter("plainText", (string) => {
md.render(string);
const plainText = md.plainText.replace(/{%([^%}])*%}/g, '')
return plainText
});
// Register `unique` filter
eleventyConfig.addFilter("unique", (list) => {
const listStr = list.join();
const sortedList = listStr.split(",").sort();
const uniqueList = Array.from(new Set(sortedList));
return uniqueList;
});
// Register `local_date` filter
eleventyConfig.addFilter("local_date", (input) => {
const date = new Date(input)
return date.toLocaleDateString('pt-PT', { year: 'numeric', month: 'long', day: 'numeric' })
});
// Register `absolute_url` filter
eleventyConfig.addFilter("absolute_url", (pageUrl) => `${siteData.url.slice(0, -1)}${pageUrl}`);
// Register `image` tag
// https://www.11ty.dev/docs/plugins/image/
eleventyConfig.addShortcode("image", async function (src, alt, sizes, classname, wantsAbsoluteURL) {
let metadata = await Image(src, {
widths: ["auto", 384, 768, 1440, 2160],
formats: ["jpeg"],
urlPath: wantsAbsoluteURL ? 'https://pedropinto.me/static_assets/photos/' : '/static_assets/photos/',
outputDir: './dist/static_assets/photos/',
sharpJpegOptions: {
quality: 80
},
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `${name}-${width}w.${format}`;
},
});
let imageAttributes = {
alt,
sizes,
class: classname,
loading: "lazy",
draggable: "false",
decoding: "async",
};
// You bet we throw an error on a missing alt (alt="" works okay)
const imageHTML = Image.generateHTML(metadata, imageAttributes)
const imageHTMLEscaped = wantsAbsoluteURL ? he.escape(imageHTML).replaceAll('https:/pedropinto.me', 'https://pedropinto.me') : imageHTML
if (classname === 'post__body-image') {
const baseClassname = classname.replace('-image', '')
const figureHTML = `
<figure class="${baseClassname}-figure">
${imageHTMLEscaped}
<figcaption class="${baseClassname}-caption">${alt}</figcaption>
</figure>
`
return figureHTML;
}
return imageHTMLEscaped
});
// Register `image_url` filter
// Based on the `image` tag, because we can't use `post.data.image` without it
eleventyConfig.addFilter("image_url", async function (src) {
let metadata = await Image(src, {
widths: [1440],
formats: ["jpeg"],
urlFormat: function ({ hash, src, width, format }) {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `https://pedropinto.me/static_assets/photos/${name}-${width}w.${format}`;
},
outputDir: './dist/static_assets/photos/',
sharpJpegOptions: {
quality: 80
}
})
return metadata.jpeg[metadata.jpeg.length - 1].url;
});
// Register `fromUntil` filter
eleventyConfig.addFilter('fromUntil', (collection, start, end) => {
if (!end) {
return collection.splice(start)
}
return collection.splice(start, end)
})
// Register `getItem` filter
eleventyConfig.addFilter('getItem', (collection, index) => {
if (typeof index !== 'number') {
throw new Error('The `getItem` filter needs an `index` to work with')
}
return collection[index]
})
// Register `local_date` filter
eleventyConfig.addFilter('local_date', (input) => {
const date = new Date(input)
return date.toLocaleDateString('pt-PT', {
year: 'numeric',
month: 'long',
day: 'numeric',
})
})
// Register `getMakes` filter
eleventyConfig.addFilter('getMakes', (posts) => {
const makes = posts.map((post) => post.data.make?.toLowerCase()).filter((make) => make)
const uniqueMakes = [...new Set(makes)].sort();
return uniqueMakes
})
// Register `getTotalPhotos` filter
eleventyConfig.addFilter('getTotalPhotos', (posts) => {
const photosLength = posts
.filter((post) => post.data.photos)
.map((post) => post.data.photos.length)
.reduce((accumulator, currentValue) => accumulator + currentValue, 0)
return photosLength
})
// Register `where` filter
// Returns 1 item of collection
eleventyConfig.addFilter('where', (collection, field, value) => {
if (!field || !value) {
return collection
}
return collection.find((item) => item[field] === value)
})
// Register `whereList` filter
// Returns a list
eleventyConfig.addFilter('whereList', (collection, field, value) => {
if (!field || !value) {
return collection
}
return collection.filter((item) => {
if (!item.data[field]) return false
return item.data[field] === value || item.data[field].includes(value)
})
})
// Register `without` filter
// Removes an item from a collection
eleventyConfig.addFilter('without', (collection, page) => {
if (!page) {
return collection
}
return collection.filter((item) => item.inputPath !== page.inputPath)
})
// Register `with` filter
// Returns an array
eleventyConfig.addFilter('with', (collection, field) => {
if (!field) {
return collection
}
return collection.filter((item) => item.data.hasOwnProperty(field))
})
// Register `titleCase` filter
// Returns a string with the first letter of each word in caps
eleventyConfig.addFilter('titleCase', (string) => string.replace(/^(\w)/, (match) => match.toUpperCase()))
return {
dir: {
input: "src",
output: "dist",
layouts: "_layouts",
data: "_data",
includes: "_includes",
},
dataTemplateEngine: "njk",
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk,md",
setUseGitIgnore: false,
};
};