-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
120 lines (98 loc) · 3.67 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
const { Console } = require("console");
const moment = require('moment');
const CleanCSS = require("clean-css");
const PluginRss = require("@11ty/eleventy-plugin-rss");
const { EleventyRenderPlugin } = require("@11ty/eleventy");
const Image = require("@11ty/eleventy-img");
moment.locale('en');
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(PluginRss);
eleventyConfig.addPlugin(EleventyRenderPlugin);
eleventyConfig.addPassthroughCopy('img');
eleventyConfig.addWatchTarget('pages/_css');
eleventyConfig.addFilter("cssmin", function (code) {
return new CleanCSS({}).minify(code).styles;
});
eleventyConfig.addFilter('dateIso', date => {
return moment(date).toISOString();
});
eleventyConfig.addFilter('dateReadable', date => {
return moment(date).utc().format('LL'); // May 31, 2019
});
eleventyConfig.addFilter('dateInternational', date => {
return moment(date).utc().format('YYYY-MM-DD');
});
eleventyConfig.addFilter('replaceStrippedCharacters', function (title) {
return title
.replace('&', '&')
.replace(/</g, '‹')
.replace(/>/g, '›');
})
eleventyConfig.addShortcode('excerpt', article => extractExcerpt(article));
eleventyConfig.addNunjucksAsyncShortcode("image", imageShortcode);
eleventyConfig.addLiquidShortcode("image", imageShortcode);
eleventyConfig.addJavaScriptFunction("image", imageShortcode);
return {
dir: {
input: "pages",
includes: "_includes",
layouts: "_layouts",
output: "dist",
templateFormats: ["html", "liquid", "njk"]
},
passthroughFileCopy: true
}
}
async function imageShortcode(src, alt, sizes = "100vw") {
let path = this.page.inputPath;
let dir = path.substring(0, path.lastIndexOf("/") + 1);
src = dir + src;
if (alt === undefined) {
throw new Error(`Missing \`alt\` on responsiveimage from: ${src}`);
}
let metadata = await Image(src, {
widths: [320, null],
formats: ["webp"],
svgShortCircuit: true,
outputDir: "./dist/img/",
urlPath: "/img/",
sharpOptions: {
animated: true
}
});
let lowsrc = metadata.webp[0];
let highsrc = metadata.webp[metadata.webp.length - 1];
return `<div class="picture">
<picture>
${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}"
alt="${alt}"
loading="lazy"
decoding="async">
</picture></div>`;
}
function extractExcerpt(article) {
if (!article.hasOwnProperty('templateContent')) {
console.warn('Failed to extract excerpt: Document has no property "templateContent".');
return null;
}
let excerpt = null;
const content = article.templateContent;
// The start and end separators to try and match to extract the excerpt
const separatorsList = [
{ start: '<!-- Excerpt Start -->', end: '<!-- Excerpt End -->' },
{ start: '<p>', end: '</p>' }
];
separatorsList.some(separators => {
const startPosition = content.indexOf(separators.start);
const endPosition = content.indexOf(separators.end);
if (startPosition !== -1 && endPosition !== -1) {
excerpt = content.substring(startPosition + separators.start.length, endPosition).trim();
return true; // Exit out of array loop on first match
}
});
return excerpt;
}