-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
127 lines (110 loc) · 3.69 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
const { DateTime } = require('luxon')
const navigationPlugin = require('@11ty/eleventy-navigation')
const rssPlugin = require('@11ty/eleventy-plugin-rss')
const tinyHTML = require('@sardine/eleventy-plugin-tinyhtml')
const safeLinks = require('@sardine/eleventy-plugin-external-links')
const CleanCSS = require("clean-css")
const { minify } = require("terser")
const Image = require("@11ty/eleventy-img")
const path = require('path')
async function imageShortcode(src, alt) {
let sizes = "(min-width: 1000px) 100vw, 50vw"
let srcPrefix = `./images/`
src = srcPrefix + src
console.log(`Generating image(s) from: ${src}`)
if(alt === undefined) {
throw new Error(`Missing \`alt\` on responsiveimage from: ${src}`)
}
let metadata = await Image(src, {
widths: [300, 600, 1000],
formats: ["webp", "jpeg"],
sharpJpegOptions: {
quality: 100,
},
urlPath: "/images/",
outputDir: "./_site/images/",
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src)
const name = path.basename(src, extension)
return `${name}-${width}w.${format}`
}
})
let lowsrc = metadata.jpeg[0]
let highsrc = metadata.jpeg[metadata.jpeg.length - 1]
return `<picture>
${Object.values(metadata).map(imageFormat => {
return ` <source type="${imageFormat[0].sourceType}" srcset="${imageFormat.map(entry => entry.srcset).join(", ")}" sizes="${sizes}">`
}).join("\n")}
<img
src="${lowsrc.url}"
width="${highsrc.width}"
height="${highsrc.height}"
alt="${alt}"
loading="lazy"
decoding="async">
</picture>`
}
module.exports = (config) => {
config.addPlugin(navigationPlugin);
config.addPlugin(rssPlugin);
config.addPlugin(tinyHTML);
config.addPlugin(safeLinks);
config.addPlugin(require('./plugins'));
config.addPassthroughCopy('static');
config.addPassthroughCopy({ static: "/" })
config.addNunjucksAsyncShortcode("image", imageShortcode)
config.addLiquidShortcode("image", imageShortcode)
config.addJavaScriptFunction("image", imageShortcode)
config.setDataDeepMerge(true);
config.addFilter('htmlDateString', (dateObj) => {
return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat('yyyy-LL-dd');
});
config.addFilter("readableDate", dateObj => {
return DateTime.fromJSDate(dateObj, {zone: 'utc'}).toFormat("dd LLL, yyyy");
});
config.addFilter("cssmin", function(code) {
return new CleanCSS({}).minify(code).styles;
});
config.addNunjucksAsyncFilter("jsmin", async function (
code,
callback
) {
try {
const minified = await minify(code);
callback(null, minified.code);
} catch (err) {
console.error("Terser error: ", err);
callback(null, code);
}
});
config.addCollection("tagList", collection => {
const tagsObject = {}
collection.getAll().forEach(item => {
if (!item.data.tags) return;
item.data.tags
.filter(tag => !['post', 'all'].includes(tag))
.forEach(tag => {
if(typeof tagsObject[tag] === 'undefined') {
tagsObject[tag] = 1
} else {
tagsObject[tag] += 1
}
});
});
const tagList = []
Object.keys(tagsObject).forEach(tag => {
tagList.push({ tagName: tag, tagCount: tagsObject[tag] })
})
return tagList.sort((a, b) => b.tagCount - a.tagCount)
});
config.addCollection("post", function(collection) {
const coll = collection.getFilteredByTag("post");
for(let i = 0; i < coll.length ; i++) {
const prevPost = coll[i-1];
const nextPost = coll[i + 1];
coll[i].data["prevPost"] = prevPost;
coll[i].data["nextPost"] = nextPost;
}
return coll;
});
}