-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheleventy.config.images.js
62 lines (54 loc) · 1.78 KB
/
eleventy.config.images.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
import path from 'path';
import eleventyImage from '@11ty/eleventy-img';
import markdownIt from 'markdown-it';
import markdownItEleventyImg from 'markdown-it-eleventy-img';
export default async (eleventyConfig) => {
function relativeToInputPath(inputPath, relativeFilePath) {
if (relativeFilePath.startsWith('/')) {
return `./content${ relativeFilePath }`;
}
const split = inputPath.split('/');
split.pop();
return path.resolve(split.join(path.sep), relativeFilePath);
}
eleventyConfig.addAsyncShortcode('image', async function imageShortcode(src, alt, widths, sizes, attributes) {
const formats = ['avif', 'webp', 'auto', 'png'];
const file = relativeToInputPath(this.page.inputPath, src);
const metadata = await eleventyImage(file, {
widths: widths || ['auto'],
formats,
outputDir: path.join(eleventyConfig.dir.output, 'img')
});
const imageAttributes = {
alt,
sizes,
loading: 'lazy',
decoding: 'async',
title: alt
};
return eleventyImage.generateHTML(metadata, Object.assign(imageAttributes, attributes || {}));
});
eleventyConfig.addAsyncShortcode('rawImageUrl', async (post, src, width) => {
const formats = ['webp'];
const file = relativeToInputPath(post.inputPath, src);
const metadata = await eleventyImage(file, {
widths: width ? [width] : ['auto'],
formats,
outputDir: path.join(eleventyConfig.dir.output, 'img')
});
return metadata.webp[0].url;
});
eleventyConfig
.setLibrary('md', markdownIt({
html: true,
breaks: true,
linkify: true
})
.use(markdownItEleventyImg, {
imgOptions: {
widths: [720],
outputDir: path.join('_site', 'img'),
formats: ['jpeg', 'png']
}
}));
};