Skip to content
This repository was archived by the owner on May 2, 2021. It is now read-only.

Commit 8115918

Browse files
committed
webp image support
1 parent f083bae commit 8115918

File tree

5 files changed

+743
-5
lines changed

5 files changed

+743
-5
lines changed

.eleventy.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const yaml = require('js-yaml');
2+
const { join } = require('path');
23
const rss = require('@11ty/eleventy-plugin-rss');
34
const { DateTime } = require('luxon');
5+
const Image = require('@11ty/eleventy-img');
46
const markdown = require('markdown-it')({
57
html: true,
68
});
@@ -30,13 +32,41 @@ const MONTHS = [
3032
'Dicembre',
3133
];
3234

33-
module.exports = function(eleventyConfig) {
35+
module.exports = function (eleventyConfig) {
3436
const assets = ['js', 'css', 'fonts', 'img', 'odx-assets', 'admin'];
3537

3638
for (const folder of assets) {
3739
eleventyConfig.addPassthroughCopy({ [`static/${folder}`]: folder });
3840
}
3941

42+
eleventyConfig.addNunjucksAsyncShortcode(
43+
'image',
44+
async function (
45+
src,
46+
alt,
47+
imgClass = 'img-rounded img-responsive center-block',
48+
) {
49+
if (alt === undefined) {
50+
// You bet we throw an error on missing alt (alt="" works okay)
51+
throw new Error(`Missing \`alt\` on myImage from: ${src}`);
52+
}
53+
54+
const image = await Image(join('static', src), {
55+
widths: [null],
56+
formats: ['webp', 'jpeg'],
57+
urlPath: '/img/',
58+
outputDir: './_site/img/',
59+
});
60+
61+
const { url, width, height } = image.jpeg[0];
62+
63+
return `<picture>
64+
<source srcset="${image.webp[0].url}" type="image/webp" />
65+
<img class="${imgClass}" src="${url}" width="${width}" height="${height}" alt="${alt}" loading="lazy" />
66+
</picture>`;
67+
},
68+
);
69+
4070
eleventyConfig.addPlugin(rss);
4171

4272
eleventyConfig.addPassthroughCopy({ 'static/*.*': '.' });
@@ -94,7 +124,7 @@ module.exports = function(eleventyConfig) {
94124
});
95125

96126
// redirect collection
97-
eleventyConfig.addCollection('redirects', function(collection) {
127+
eleventyConfig.addCollection('redirects', function (collection) {
98128
const redirs = collection.getAll().filter(({ data }) => data.redirect_from);
99129
const redirects = new Map();
100130
for (item of redirs) {

0 commit comments

Comments
 (0)