-
Notifications
You must be signed in to change notification settings - Fork 3
/
.eleventy.js
164 lines (142 loc) · 5.27 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
155
156
157
158
159
160
161
162
163
164
const fs = require('fs');
const CleanCSS = require("clean-css");
const embedYouTube = require("eleventy-plugin-youtube-embed");
const pluginSEO = require("eleventy-plugin-seo-tag");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const blogTools = require("eleventy-plugin-blog-tools");
const readingTime = require('eleventy-plugin-reading-time');
const lodashChunk = require('lodash.chunk');
const moment = require('moment');
moment.locale('en');
module.exports = function(eleventyConfig) {
eleventyConfig.setTemplateFormats([
"html",
"md",
"njk",
"css"
]);
eleventyConfig.addPassthroughCopy("assets");
eleventyConfig.addPassthroughCopy("util");
eleventyConfig.addPassthroughCopy("admin/github-markdown.css");
eleventyConfig.addPassthroughCopy("admin/index.html");
eleventyConfig.addPassthroughCopy("favicon.ico");
eleventyConfig.addPassthroughCopy("robots.txt");
eleventyConfig.addPassthroughCopy("google87579b2b505115de.html");
eleventyConfig.addPlugin(pluginSEO, require("./_data/seo.json"));
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(blogTools);
eleventyConfig.addPlugin(readingTime);
eleventyConfig.addPlugin(embedYouTube);
eleventyConfig.addFilter("cssmin", function(code) {
return new CleanCSS({}).minify(code).styles;
});
eleventyConfig.addFilter('dateReadable', date => {
return moment(date).format('LL'); // E.g. May 31, 2019
});
eleventyConfig.addFilter('categoryFilter', function(collection, category) {
if (!category) return collection;
return collection.filter(function(item) {
return item.data.category == category;
});
});
eleventyConfig.addFilter('authorFilter', function(collection, author) {
if (!author) return collection;
const filtered = collection.filter(function(item) {
return item.data.author == author;
});
if (filtered.length == 0) {
return collection;
} else {
return filtered;
}
});
eleventyConfig.addFilter("utf8_xml", (inputStr) => {
return inputStr.replace(/[^\x09\x0A\x0D\x20-\xFF\x85\xA0-\uD7FF\uE000-\uFDCF\uFDE0-\uFFFD]/gm, '');
});
const getTags = function(collection) {
let tagSet = new Set();
collection.getAllSorted().map(function(item) {
if( "tags" in item.data ) {
let tags = item.data.tags;
for (let tag of tags) {
tagSet.add(tag);
}
}
});
return [...tagSet];
};
const getCategories = function() {
return Object.keys(JSON.parse(fs.readFileSync("_data/categories.json")));
};
const getAuthors = function() {
return Object.keys(JSON.parse(fs.readFileSync("_data/authors.json")));
};
eleventyConfig.addCollection("tagList", getTags);
eleventyConfig.addCollection("categoryList", getCategories);
eleventyConfig.addCollection("authorList", getAuthors);
eleventyConfig.addCollection("pages", function(collection) {
return collection.getFilteredByGlob("*.html").reverse();
});
eleventyConfig.addCollection("articles", function(collection) {
return collection.getFilteredByGlob("blog/*.md").reverse();
});
eleventyConfig.addCollection('featuredArticles', function(collection) {
return collection.getAllSorted().reverse().filter(function(item) {
return item.data.featured == true;
});
});
// flatten two level pagination (tags -> pages of the same tag) into one
eleventyConfig.addCollection("tagPagination", function(collection) {
// Get each item that matches the tag
let paginationSize = 9;
let tagMap = [];
for( let tagName of getTags(collection) ) {
let tagItems = collection.getFilteredByTag(tagName);
let pagedItems = lodashChunk(tagItems, paginationSize);
for( let pageNumber = 0, max = pagedItems.length; pageNumber < max; pageNumber++) {
tagMap.push({
tagName: tagName,
pageNumber: pageNumber,
pageData: pagedItems[pageNumber]
});
}
}
return tagMap;
});
eleventyConfig.addCollection("catPagination", function(collection) {
let paginationSize = 9;
let catMap = [];
let catArray = getCategories();
for( let cat of catArray) {
let catItems = collection.getAllSorted().filter(item => item.data.category == cat);
let pagedItems = lodashChunk(catItems, paginationSize);
for( let pageNumber = 0, max = pagedItems.length; pageNumber < max; pageNumber++) {
catMap.push({
tagName: cat,
pageNumber: pageNumber,
pageData: pagedItems[pageNumber]
});
}
}
return catMap;
});
eleventyConfig.addCollection("authPagination", function(collection) {
let paginationSize = 9;
let authMap = [];
let authArray = Object.keys(JSON.parse(fs.readFileSync("_data/authors.json")));
for( let auth of authArray) {
let authItems = collection.getAllSorted().filter(item => item.data.author == auth);
let pagedItems = lodashChunk(authItems, paginationSize);
for( let pageNumber = 0, max = pagedItems.length; pageNumber < max; pageNumber++) {
authMap.push({
tagName: auth,
pageNumber: pageNumber,
pageData: pagedItems[pageNumber]
});
}
}
return authMap;
});
};