-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
53 lines (43 loc) · 1.65 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
const Nunjucks = require("nunjucks");
const truncateHtml = require('truncate-html');
const luxon = require('luxon');
const yaml = require('js-yaml');
module.exports = function(eleventyConfig) {
const nunjucksEnvironment = new Nunjucks.Environment(
new Nunjucks.FileSystemLoader("_includes")
);
eleventyConfig.addDataExtension("yaml", contents => yaml.load(contents));
eleventyConfig.addNunjucksFilter("date", function(dateString, format = 'cccc d LLLL yyyy') {
let date;
if (dateString === 'now') {
date = luxon.DateTime.now();
} else {
date = luxon.DateTime.fromISO(dateString);
}
switch(format) {
case 'iso':
return date.toISO();
default:
return date.toFormat(format);
}
});
eleventyConfig.addNunjucksFilter("lower_headings", function(html, levels) {
if (typeof html === 'undefined') {
return;
}
for(let i = 0; i < levels; ++i) {
html = html
.replace(/<h5/g, '<h6').replace(/<\/h5/g, '</h6')
.replace(/<h4/g, '<h5').replace(/<\/h4/g, '</h5')
.replace(/<h3/g, '<h4').replace(/<\/h3/g, '</h4')
.replace(/<h2/g, '<h3').replace(/<\/h2/g, '</h3')
.replace(/<h1/g, '<h2').replace(/<\/h1/g, '</h2')
}
return html;
});
eleventyConfig.addNunjucksFilter('truncate_html', function(html, length=20) {
return truncateHtml(html, length, { byWords: true });
});
eleventyConfig.setLibrary("njk", nunjucksEnvironment);
eleventyConfig.addPassthroughCopy("assets/");
};