2
2
// This plugin automatically formats all code during build.
3
3
const syntaxHighlight = require ( "@11ty/eleventy-plugin-syntaxhighlight" ) ;
4
4
5
+ // Compresses image plugin
6
+ const Image = require ( "@11ty/eleventy-img" ) ;
7
+
8
+ // Node JS path functions
9
+ const path = require ( 'path' ) ;
10
+
5
11
module . exports = function ( eleventyConfig ) {
6
12
// All folders called "assets" will be blindly copied
7
- // into the _site/ assets output folder.
13
+ // into the assets output folder.
8
14
eleventyConfig . addPassthroughCopy ( { "src/**/assets/*" : "assets/" } ) ;
9
15
10
16
// Displays date in a human-readable way.
@@ -20,16 +26,36 @@ module.exports = function (eleventyConfig) {
20
26
} ) ;
21
27
22
28
// Get all keys of a collection.
23
- eleventyConfig . addFilter ( 'keys' , obj => Object . keys ( obj ) ) ;
29
+ eleventyConfig . addFilter ( 'keys' , obj => Object . keys ( obj ) ) ;
24
30
25
31
// Remove "all" and "post" from this list.
26
32
eleventyConfig . addFilter ( "filterTagList" , ( tags ) => {
27
- return ( tags || [ ] ) . filter ( tag => ! [ "all" , "post" ] . includes ( tag ) ) ;
28
- } ) ;
29
-
33
+ return ( tags || [ ] ) . filter ( tag => ! [ "all" , "post" ] . includes ( tag ) ) ;
34
+ } ) ;
35
+
30
36
31
37
eleventyConfig . addPlugin ( syntaxHighlight ) ;
32
38
39
+ // Image shortcode
40
+ eleventyConfig . addShortcode ( "image" , async function ( src , alt , width = 90 ) {
41
+ let metadata = await Image ( `${ path . dirname ( this . page . inputPath ) } /${ src } ` , {
42
+ widths : [ 400 , 800 , 1200 , "auto" ] ,
43
+ formats : [ "jpeg" ] ,
44
+ urlPath : "/img/" ,
45
+ outputDir : "public/img/"
46
+ } ) ;
47
+
48
+ let imageAttributes = {
49
+ alt,
50
+ sizes : "(max-width: 50rem) 100vw, 50rem" ,
51
+ style : `width: ${ width } %;` ,
52
+ loading : "lazy" ,
53
+ decoding : "async" ,
54
+ } ;
55
+
56
+ return `<a href="${ src } ">${ Image . generateHTML ( metadata , imageAttributes ) } </a>` ;
57
+ } ) ;
58
+
33
59
return {
34
60
// Change the input and output directory
35
61
dir : {
0 commit comments