-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheleventy.config.images.js
63 lines (57 loc) · 2.1 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
63
import path from 'path'
import eleventyImage from '@11ty/eleventy-img'
const postsPath = './src/posts/'
export default (eleventyConfig) => {
function relativeToInputPath(inputPath, relativeFilePath) {
const split = inputPath.split('/')
split.pop()
return path.resolve(split.join(path.sep), relativeFilePath)
}
// Eleventy Image shortcode
// https://www.11ty.dev/docs/plugins/image/
eleventyConfig.addAsyncShortcode(
'image',
async function imageShortcode(src, alt, widths, sizes) {
// Full list of formats here: https://www.11ty.dev/docs/plugins/image/#output-formats
// Warning: Avif can be resource-intensive so take care!
const formats = ['webp', 'auto']
const file = relativeToInputPath(this.page.inputPath, src)
const metadata = await eleventyImage(file, {
widths: widths || [400, 800, 1200],
formats,
outputDir: path.join(eleventyConfig.dir.output, 'img'), // Advanced usage note: `eleventyConfig.dir` works here because we’re using addPlugin.
})
// TODO loading=eager and fetchpriority=high
const imageAttributes = {
alt,
sizes: sizes || '(max-width: 1200px) 100vw, 1200px',
loading: 'lazy',
decoding: 'async',
}
return eleventyImage.generateHTML(metadata, imageAttributes)
}
)
// Add markdown support
eleventyConfig.amendLibrary('md', (markdown) => {
markdown.renderer.rules.image = function (tokens, idx) {
const token = tokens[idx]
const file = relativeToInputPath(postsPath, token.attrGet('src'))
const alt = token.content
const formats = ['webp', 'auto']
const imageOptions = {
widths: [400, 800, 1200],
formats,
outputDir: path.join(eleventyConfig.dir.output, 'img'),
}
const metadata = eleventyImage.statsSync(file, imageOptions)
eleventyImage(file, imageOptions)
const imageAttributes = {
alt,
sizes: '(max-width: 1200px) 100vw, 1200px',
loading: 'lazy',
decoding: 'async',
}
return eleventyImage.generateHTML(metadata, imageAttributes)
}
})
}