-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.eleventy.js
148 lines (124 loc) · 4.38 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const pluginNavigation = require("@11ty/eleventy-navigation");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const rssPlugin = require('@11ty/eleventy-plugin-rss');
const slugify = require("slugify");
const htmlmin = require("html-minifier");
const Image = require("@11ty/eleventy-img");
const sharp = require("sharp");
// Filters
const dateFilter = require('./src/filters/date-filter.js');
const w3DateFilter = require('./src/filters/w3-date-filter.js');
// Create a helpful production flag
const isProduction = process.env.NODE_ENV === 'production';
const sortByDisplayOrder = require('./src/utils/sort-by-display-order.js');
module.exports = (eleventyConfig) => {
//html minifier
eleventyConfig.addTransform("htmlmin", function (content, outputPath) {
// Eleventy 1.0+: use this.inputPath and this.outputPath instead
if (outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
/// Markdown Component from Markdown-it
let markdownIt = require("markdown-it");
let markdownItFootnote = require("markdown-it-footnote");
let markdownItContainer = require("markdown-it-container");
let options = {
html: true,
breaks: true,
linkify: true,
typographer: true
};
eleventyConfig.setLibrary("md", markdownIt(options)
.use(markdownItFootnote)
.use(markdownItContainer, 'callout')
.use(markdownItContainer, 'callout-yellow')
.use(markdownItContainer, 'callout-blue')
.use(markdownItContainer, 'callout-pink')
.use(markdownItContainer, 'callout-purple')
.use(markdownItContainer, 'callout-green')
.use(markdownItContainer, 'warning')
);
//shortcodes
eleventyConfig.addNunjucksAsyncShortcode("Image", async (src, alt) => {
if (!alt) {
throw new Error(`Missing \`alt\` on myImage from: ${src}`);
}
let stats = await Image(src, {
widths: [25, 320, 640, 960, 1200, 1800, 2400],
formats: ["jpeg", "webp"],
urlPath: "/images/",
outputDir: "./dist/images/",
});
let lowestSrc = stats["jpeg"][0];
const placeholder = await sharp(lowestSrc.outputPath)
.resize({ fit: sharp.fit.inside })
.blur()
.toBuffer();
const base64Placeholder = `data:image/png;base64,${placeholder.toString(
"base64"
)}`;
const srcset = Object.keys(stats).reduce(
(acc, format) => ({
...acc,
[format]: stats[format].reduce(
(_acc, curr) => `${_acc} ${curr.srcset} ,`,
""
),
})
);
const source = `<source type="image/webp" data-srcset="${srcset["webp"]}" >`;
const img = `<img
class="lazy"
alt="${alt}"
src="${base64Placeholder}"
data-src="${lowestSrc.url}"
data-sizes='(min-width: 1024px) 1024px, 100vw'
data-srcset="${srcset["jpeg"]}"
width="${lowestSrc.width}"
height="${lowestSrc.height}">`;
return `<div class="image-wrapper"><picture> ${source} ${img} </picture></div>`;
});
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
excerpt_separator: "<!-- excerpt -->",
});
// Add filters
eleventyConfig.addFilter('dateFilter', dateFilter);
eleventyConfig.addFilter('w3DateFilter', w3DateFilter);
// Plugins
eleventyConfig.addPlugin(rssPlugin);
eleventyConfig.addPlugin(pluginNavigation);
// Returns work items, sorted by display order
eleventyConfig.addCollection('work', collection => {
return sortByDisplayOrder(
collection.getFilteredByGlob('./src/work/*.md'));
});
// Returns a collection of blog posts in reverse date order
eleventyConfig.addCollection('blog', collection => {
return [...collection.getFilteredByGlob('./src/posts/*.md')].reverse();
});
// Tell 11ty to use the .eleventyignore and ignore our .gitignore file
eleventyConfig.setUseGitIgnore(false);
eleventyConfig.addPassthroughCopy("src/images");
eleventyConfig.addPassthroughCopy("src/css");
eleventyConfig.addPassthroughCopy('src/admin');
eleventyConfig.setTemplateFormats(["jpg", "png", "webp", "md", "njk", "html"]);
return {
markdownTemplateEngine: 'njk',
dataTemplateEngine: 'njk',
htmlTemplateEngine: 'njk',
dir: {
input: 'src',
output: 'dist'
}
};
};