-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path.eleventy.js
154 lines (138 loc) · 4.85 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
149
150
151
152
153
154
const { DateTime } = require("luxon");
const CleanCSS = require("clean-css");
const UglifyJS = require("uglify-es");
const htmlmin = require("html-minifier");
module.exports = function(eleventyConfig) {
eleventyConfig.addLayoutAlias("post", "layouts/post.njk");
// Date formatting (human readable)
eleventyConfig.addFilter("readableDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("dd LLL yyyy");
});
// Date formatting (machine readable)
eleventyConfig.addFilter("machineDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("yyyy-MM-dd");
});
// 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: false
});
return minified;
}
return content;
});
function compareTitles(a, b) {
const titleA = a.data.title.replace(/^the /i, '');
const titleB = b.data.title.replace(/^the /i, '');
return titleA.localeCompare(titleB);
}
// only content in the `posts/` directory
eleventyConfig.addCollection("posts", function(collection) {
return collection.getAllSorted().filter(function(item) {
return item.inputPath.match(/^\.\/posts\//) !== null;
});
});
// Number songs in the 'songs' directory
eleventyConfig.addCollection("untaggedSongs", function (collection) {
songCollection = collection.getAllSorted().filter(function (item) {
return item.inputPath.match(/^\.\/songs\//) !== null;
}).filter(function (item) {
if (item.data.tags) {
return item.data.tags.includes("song") === false;
} else {
return true;
}
}).sort(compareTitles);
return songCollection
});
// Number songs in the 'songs' directory
eleventyConfig.addCollection("numberedSongs", function (collection) {
songCollection = collection.getAllSorted().filter(function (item) {
return item.inputPath.match(/^\.\/songs\//) !== null;
}).filter(function (item) {
return item.data.published === true;
}).sort(compareTitles);
// Inject the song number so we have it for the URL and the numbering
songCollection.forEach(function (a, i) {
a.data.songNumber = i + 1;
console.log("%s: songNumber: %s ", a.inputPath, i + 1 )
})
return songCollection
});
// Create a list of songs sorted by firstline
eleventyConfig.addCollection("songsByFirstline", function(collection) {
return collection.getAll().filter(function(item) {
return item.data.songLine != null;
}).filter(function (item) {
return item.data.published === true;
}).sort(function (a,b) {
return a.data.songLine.localeCompare(b.data.songLine)
});
});
// Create a list of songs sorted by chorusline
eleventyConfig.addCollection("songsByChorus", function(collection) {
return collection.getAll().filter(function(item) {
return item.data.chorusLine != null;
}).filter(function (item) {
return item.data.published === true;
}).sort(function (a,b) {
return a.data.chorusLine.localeCompare(b.data.chorusLine)
});
});
// Don't process folders with static assets e.g. images
eleventyConfig.addPassthroughCopy("static/img");
eleventyConfig.addPassthroughCopy("admin");
eleventyConfig.addPassthroughCopy("_includes/assets/");
/* Markdown Plugins */
let markdownIt = require("markdown-it");
let options = {
html: true,
breaks: true,
linkify: true,
typographer: true,
};
eleventyConfig.setLibrary("md", markdownIt(options));
const slugify = require("slugify");
eleventyConfig.addFilter("slug", (input) => {
const options = {
replacement: "-",
remove: /[&,+()$~%.'":*?<>{}]/g,
lower: true
};
return slugify(input, options);
});
return {
templateFormats: ["md", "njk", "html"],
// 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",
passthroughFileCopy: true,
dir: {
input: ".",
includes: "_includes",
data: "_data",
output: "_site"
}
};
};