-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
130 lines (102 loc) · 3.35 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
const pluginBookshop = require("@bookshop/eleventy-bookshop");
const pluginCloudCannonBookshop = require("@bookshop/cloudcannon-eleventy-bookshop");
const yaml = require("js-yaml");
const Image = require("@11ty/eleventy-img");
const sitemap = require("@quasibit/eleventy-plugin-sitemap");
const metagen = require('eleventy-plugin-metagen');
const inspect = require("util").inspect;
////---------------
//// FUNCTIONS
////---------------
// For eleventy-img plugin via: {% image "[path]", "[class]", "[alt]", "[sizes]", "[widths]" %
// Usage exaple: {% image "./assets/img/myImg.jpg", "myClass", "A description", "(max-width: 768px) 90vw, 300px", "300, 600, 900" %}
const imageShortcode = async (
relativeSrc,
alt,
className,
sizes = '100vw',
widths = '400, 800, 1280',
) => {
// Turn a string of numbers into an array
const widthsArray = widths.split(',').map(Number);
// If local add a '.' to path, if remote pass full url
const fullSrc = relativeSrc.startsWith('/') ? `.${relativeSrc}` : relativeSrc;
// coonfig for generating
const imageMetaData = await Image(fullSrc, {
widths: widthsArray,
formats: ['webp', 'jpeg', 'svg'],
urlPath: "/assets/img/generated",
outputDir: "./_site/assets/img/generated",
svgShortCircuit: true
})
//attributes for generateHTML
let imageAttributes = {
class: className,
alt,
sizes,
loading: "lazy",
decoding: "async",
};
// Throws an error when alt is missing (alt="" works okay)
return Image.generateHTML(imageMetaData, imageAttributes);
}
module.exports = function(eleventyConfig) {
////---------------
//// FILTERS
////---------------
// Returns readable js-data
eleventyConfig.addFilter("debug", (content) => `<pre>${inspect(content)}</pre>`);
// Returns usable js-data
eleventyConfig.addFilter("printJS", (data) => {
const string = `${inspect(data)}`;
return string.replace(/["]+/g, '')
});
// Filters collection by a frontmatter key + value
eleventyConfig.addFilter("filterByFrontmatter", (collection, key, value) => {
const found = collection.filter(item => {
return item.data[key].includes(value)
})
return found
});
// Returns each collection item which fileSlug is included in an array
eleventyConfig.addFilter("filterBySlug", function(collection, slugList) {
const found = collection.filter(item => {
return slugList.includes(item.fileSlug)
})
return found
});
////---------------
//// SHORTCODES
////---------------
eleventyConfig.addLiquidShortcode("image", imageShortcode);
////---------------
//// PLUGINS
////---------------
eleventyConfig.addPlugin(sitemap, {
sitemap: {
hostname: "https://example.com",
},
});
eleventyConfig.addPlugin(pluginBookshop({
bookshopLocations: ["component-library"]
}));
eleventyConfig.addPlugin(pluginCloudCannonBookshop);
eleventyConfig.addPlugin(metagen);
////---------------
//// CONFIG
////---------------
eleventyConfig.addPassthroughCopy("assets");
eleventyConfig.addDataExtension("yaml", contents => yaml.load(contents));
eleventyConfig.setBrowserSyncConfig({
files: './_site/assets/css/**/*.css'
});
return {
dir: {
input: "views",
// ⚠️ Values below are relative to the input directory.
includes: '_includes',
layouts: '_layouts',
data: '_data',
},
};
};