generated from danurbanowicz/eleventy-netlify-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
129 lines (108 loc) · 3.79 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const { DateTime } = require("luxon");
const CleanCSS = require("clean-css");
const UglifyJS = require("uglify-js");
const htmlmin = require("html-minifier");
const yaml = require("js-yaml");
const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
const Image = require("@11ty/eleventy-img");
module.exports = function(eleventyConfig) {
// https://www.11ty.dev/docs/plugins/image/
eleventyConfig.addShortcode("generateImage", async function(src, alt, sizes) {
let metadata = await Image(src, {
widths: [500, 1000, "auto"],
formats: ["avif", "jpeg"],
urlPath: "/assets/img/",
outputDir: "./_site/assets/img/"
});
let imageAttributes = {
alt,
sizes,
loading: "lazy",
decoding: "async",
};
return Image.generateHTML(metadata, imageAttributes);
});
// Eleventy Navigation https://www.11ty.dev/docs/plugins/navigation/
eleventyConfig.addPlugin(eleventyNavigationPlugin);
// Add support for YAML data files with .yml extension
eleventyConfig.addDataExtension("yml", contents => yaml.load(contents));
// Merge data instead of overriding
// https://www.11ty.dev/docs/data-deep-merge/
eleventyConfig.setDataDeepMerge(true);
// Add support for post authors
eleventyConfig.addCollection("myAuthors", collection => {
const blogs = collection.getFilteredByGlob("posts/*.md");
return blogs.reduce((coll, post) => {
const author = post.data.author;
if (!author) {
return coll;
}
if (!coll.hasOwnProperty(author)) {
coll[author] = [];
}
coll[author].push(post);
return coll;
}, {});
});
// Date formatting (human readable)
eleventyConfig.addFilter("readableDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("LLL d yyyy");
});
// Date formatting (machine readable)
eleventyConfig.addFilter("machineDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("yyyy-MM-dd");
});
// Faux-pluralize an English string (used in components/postsList.njk)
eleventyConfig.addFilter("pluralize", function (term, count = 1) {
return count === 1 ? term : `${term}s`;
});
// Minify CSS
eleventyConfig.addFilter("cssmin", function(code) {
return new CleanCSS({}).minify(code).styles;
});
// Minify JS
eleventyConfig.addFilter("jsmin", function(code) {
let minified = UglifyJS.minify(code);
if (minified.error) {
console.log("UglifyJS error: ", minified.error);
return code;
}
return minified.code;
});
// Minify HTML output
eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
if (outputPath.indexOf(".html") > -1) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
// Don't process folders with static assets e.g. images
eleventyConfig.addPassthroughCopy("assets/img"); // don't process the image folder
eleventyConfig.addPassthroughCopy("admin/"); // don't process the CMS folder
// Disable 11ty dev server live reload when using CMS locally
eleventyConfig.setServerOptions({
liveReload: false
});
return {
templateFormats: ["md", "njk", "liquid"],
// If your site lives in a different subdirectory, change this.
// Leading or trailing slashes are all normalized away, so don’t worry about it.
// If you don’t have a subdirectory, use "" or "/" (they do the same thing)
// This is only used for URLs (it does not affect your file structure)
pathPrefix: "/",
markdownTemplateEngine: "liquid",
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
dir: {
input: ".",
includes: "_includes",
data: "_data",
output: "_site"
}
};
};