-
Notifications
You must be signed in to change notification settings - Fork 0
/
eleventy.config.js
111 lines (89 loc) · 3.91 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
const Image = require("@11ty/eleventy-img");
const gallery = require('./src/_data/gallery.json');
const htmlnano = require("htmlnano");
const path = require("path");
module.exports = function(eleventyConfig) {
eleventyConfig.addCollection("processedGallery", async function() {
const processedGallery = [];
for (const country in gallery) {
const data = gallery[country];
for (const image of data.images) {
let src = `src/images/${country}/${image.filename}.webp`;
let metadata = await Image(src, {
widths: [12, 300, 600, 960, 1200, 2140],
formats: ["webp", "jpeg"],
outputDir: "./dist/images/",
urlPath: "/images/",
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src);
const name = path.basename(src, extension).replace(/_/g, '-'); // замена нижних подчеркиваний на дефисы
return `${name}-${width}w.${format}`;
}
});
let lowestSrc = metadata["jpeg"][0];
let filteredMetadata = { ...metadata };
// Удалите из метаданных изображения большие размеры
for (let format in filteredMetadata) {
filteredMetadata[format] = filteredMetadata[format].filter(image => image.width <= 600);
}
// Создание srcset
const srcsetWebp = filteredMetadata["webp"].map(
item => `${item.url} ${item.width}w`).join(", ");
const srcsetJpeg = filteredMetadata["jpeg"].map(
item => `${item.url} ${item.width}w`).join(", ");
processedGallery.push({
...image,
country: country,
html: `
<a href="${metadata.webp[metadata.webp.length - 1].url}" id="${image.filename.replace(/_/g, '-')}" data-fancybox="gallery">
<picture>
<source type="image/webp" data-srcset="${srcsetWebp}" sizes="(min-width: 1024px) 1024px, 100vw">
<img
class="lazyload blur-up"
src="${lowestSrc.url}"
data-src="${lowestSrc.url}"
sizes="(min-width: 1024px) 1024px, 100vw"
data-srcset="${srcsetJpeg}"
alt="${image.alt}">
</picture>
</a>`,
url: metadata.webp[metadata.webp.length - 1].url
});
}
}
return processedGallery;
});
eleventyConfig.addNunjucksFilter('declOfNum', function(number) {
const cases = [2, 0, 1, 1, 1, 2];
let titles = ['марка', 'марки', 'марок'];
return titles[(number % 100 > 4 && number % 100 < 20) ? 2 : cases[(number % 10 < 5) ? number % 10 : 5]];
});
eleventyConfig.addNunjucksFilter("sortAlpha", function(array) {
return array.sort();
});
eleventyConfig.addNunjucksFilter("sortNumeric", function(array) {
return array.sort((a, b) => a - b);
});
eleventyConfig.addCollection("gallery", function() {
return gallery;
});
eleventyConfig.addPassthroughCopy({
"src/public/": "/"
});
eleventyConfig.addTransform("htmlnano", async function(content, outputPath) {
if( outputPath && outputPath.endsWith(".html") ) {
const options = {
collapseWhitespace: "conservative"
};
const result = await htmlnano.process(content, options);
return result.html;
}
return content;
});
return {
dir: {
input: "src",
output: "dist"
}
}
};