-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.eleventy.js
184 lines (173 loc) · 5.62 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const pluginRss = require("@11ty/eleventy-plugin-rss");
const pluginNav = require("@11ty/eleventy-navigation");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const { DateTime } = require("luxon");
const fs = require("fs");
var getIndex = (collection, currentSlug) => {
let currentIndex = 0;
collection.filter((page, index) => {
currentIndex = page.fileSlug == currentSlug ? index : currentIndex;
});
return currentIndex;
};
module.exports = function (config) {
// Plugins
config.addPlugin(pluginRss);
config.addPlugin(pluginNav);
// Filters
config.addFilter("dateToFormat", (date, format) => {
return DateTime.fromJSDate(date, { zone: "utc" }).toFormat(String(format));
});
config.addFilter("yearRange", (date1Str, date2Str = false) => {
let date = false;
let end = false;
let start = false;
const date1 = date1Str
? DateTime.fromJSDate(date1Str, { zone: "utc" })
: false;
const date2 = DateTime.fromJSDate(date2Str, { zone: "utc" });
if (date1 && date2) {
end = date1 < date2 ? date2 : date1;
start = date1 < date2 ? date1 : date2;
} else {
end = date2;
}
const endYear = end.toFormat("yyyy");
if (start && end) {
const startYear = start.toFormat("yyyy");
if (startYear == endYear) {
date = endYear;
} else {
const startCent = startYear.slice(0, 2);
const endCent = endYear.slice(0, 2);
if (startCent == endCent) {
date = startYear + "–" + end.toFormat("yy");
} else {
date = startYear + "–" + endYear;
}
}
} else if (end) {
date = endYear;
}
return date;
});
config.addFilter("dateToISO", (date) => {
return DateTime.fromJSDate(date, { zone: "utc" }).toISO({
includeOffset: false,
suppressMilliseconds: true,
});
});
config.addFilter("nextInCollection", (collection, currentSlug) => {
const currentIndex = getIndex(collection, currentSlug);
const pages = collection.filter((page, index) => {
return index == currentIndex + 1 ? page : false;
});
return pages.length ? pages[0] : false;
});
config.addFilter("prevInCollection", (collection, currentSlug) => {
const currentIndex = getIndex(collection, currentSlug);
const pages = collection.filter((page, index) => {
return index == currentIndex - 1 ? page : false;
});
return pages.length ? pages[0] : false;
});
config.addFilter("getPagesByPaths", (collection, paths) => {
let pages = [];
paths.forEach((path) => {
let page = collection.filter((page) => {
return page.filePathStem.includes(path) ? page : false;
});
if (page.length) {
pages.push(page[0]);
}
});
return pages;
});
config.addFilter("getFeaturedImage", (blocks) => {
// Get the featured images
let images = blocks.filter((block) => {
return block.type == "image" && block.featured ? block : false;
});
// If no featured images, get all the images
if (!images.length) {
images = blocks.filter((block) => {
return block.type == "image" ? block : false;
});
}
return images.length ? images[0] : false;
});
// Collections
config.addCollection("projects", (collection) => {
const projects = collection.getFilteredByGlob("content/projects/*.md");
return projects.sort(function (a, b) {
return b.data.dateEnd - a.data.dateEnd;
});
});
config.addCollection("posts", function (collection) {
const posts = collection.getFilteredByGlob("content/posts/*.md");
return posts.sort(function (a, b) {
return b.data.date - a.data.date;
});
});
config.addCollection("pages", function (collection) {
return collection.getFilteredByGlob("content/pages/*.md");
});
// Markdown
const markdownOptions = {
html: true,
breaks: true,
linkify: true,
typographer: true,
};
const markdownLibrary = markdownIt(markdownOptions).use(markdownItAnchor, {
permalink: false,
});
config.setLibrary("md", markdownLibrary);
config.addNunjucksFilter("markdownify", (markdownString) =>
markdownIt(markdownOptions).render(markdownString)
);
config.addNunjucksFilter("markdownifyInline", (markdownString) =>
markdownIt(markdownOptions).renderInline(markdownString)
);
config.setFrontMatterParsingOptions({
excerpt: true,
excerpt_separator: "<!--more-->", // Matches WordPress style
});
// BrowserSync
config.setBrowserSyncConfig({
callbacks: {
ready: function (err, browserSync) {
const content_404 = fs.readFileSync("_site/404.html");
browserSync.addMiddleware("*", (req, res) => {
// Provides the 404 content without redirect.
res.write(content_404);
res.end();
});
},
},
});
// Pass-thru files
config.addPassthroughCopy({ "content/media": "media" });
config.addPassthroughCopy("css");
config.addPassthroughCopy("fonts");
// Layouts
config.addLayoutAlias("base", "layouts/base.njk");
config.addLayoutAlias("page", "layouts/page.njk");
config.addLayoutAlias("error", "layouts/error.njk");
config.addLayoutAlias("feed", "layouts/feed.njk");
config.addLayoutAlias("home", "layouts/home.njk");
config.addLayoutAlias("post", "layouts/post.njk");
config.addLayoutAlias("posts", "layouts/posts.njk");
config.addLayoutAlias("project", "layouts/project.njk");
// Base Config
return {
dir: {
data: "content/_data",
},
templateFormats: ["njk", "md", "html", "liquid"],
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
passthroughFileCopy: true,
};
};