-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
96 lines (86 loc) · 3.54 KB
/
.eleventy.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
// imports for the various eleventy plugins (navigation & image)
const eleventyNavigationPlugin = require('@11ty/eleventy-navigation');
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const { DateTime } = require('luxon');
const Image = require('@11ty/eleventy-img');
const path = require('path');
const util = require('util');
// allows the use of {% image... %} to create responsive, optimised images
// CHANGE DEFAULT MEDIA QUERIES AND WIDTHS
async function imageShortcode(src, alt, className, loading, sizes = '(max-width: 600px) 400px, 850px') {
// don't pass an alt? chuck it out. passing an empty string is okay though
if (alt === undefined) {
throw new Error(`Missing \`alt\` on responsiveimage from: ${src}`);
}
// create the metadata for an optimised image
let metadata = await Image(`${src}`, {
widths: [200, 400, 850, 1920, 2500],
formats: ['webp', 'jpeg'],
urlPath: '/images/',
outputDir: './public/images',
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src);
const name = path.basename(src, extension);
return `${name}-${width}w.${format}`;
},
});
// get the smallest and biggest image for picture/image attributes
let lowsrc = metadata.jpeg[0];
let highsrc = metadata.jpeg[metadata.jpeg.length - 1];
// when {% image ... %} is used, this is what's returned
return `<picture class="${className}">
${Object.values(metadata)
.map((imageFormat) => {
return ` <source type="${imageFormat[0].sourceType}" srcset="${imageFormat
.map((entry) => entry.srcset)
.join(', ')}" sizes="${sizes}">`;
})
.join('\n')}
<img
src="${lowsrc.url}"
width="${highsrc.width}"
height="${highsrc.height}"
alt="${alt}"
loading="${loading}"
decoding="async">
</picture>`;
}
module.exports = function (eleventyConfig) {
// adds the navigation plugin for easy navs
// eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addFilter("to_json", function(value) {
return JSON.stringify(value);
});
eleventyConfig.addFilter("contains", function(arr, str) {
return arr.includes(str);
});
eleventyConfig.addFilter('dump', obj => {
return util.inspect(obj)
});
// allows css, assets, robots.txt and CMS config files to be passed into /public
eleventyConfig.addPassthroughCopy('./src/css/**/*.css');
eleventyConfig.addPassthroughCopy('./src/assets');
eleventyConfig.addPassthroughCopy('./src/admin');
eleventyConfig.addPassthroughCopy('./src/_redirects');
eleventyConfig.addPassthroughCopy({ './src/robots.txt': '/robots.txt' });
// open on npm start and watch CSS files for changes - doesn't trigger 11ty rebuild
eleventyConfig.setBrowserSyncConfig({
open: true,
files: './public/css/**/*.css',
});
// allows the {% image %} sheortcode to be used for optimised iamges (in webp if possible)
eleventyConfig.addNunjucksAsyncShortcode('image', imageShortcode);
// normally, 11ty will render dates on blog posts in full JSDate format (Fri Dec 02 18:00:00 GMT-0600). That's ugly
// this filter allows dates to be converted into a normal, locale format. view the docs to learn more (https://moment.github.io/luxon/api-docs/index.html#datetime)
return {
dir: {
input: 'src',
includes: '_includes',
layouts: "_layouts",
output: 'public',
},
// allows .html files to contain nunjucks templating language
htmlTemplateEngine: 'njk',
};
};