-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.eleventy.js
181 lines (149 loc) · 4.34 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
const R = require("ramda");
const fs = require("fs");
const { parse } = require("date-fns");
const { format, utcToZonedTime } = require("date-fns-tz");
const authors = require("./src/authors.json");
const leftArrow = fs.readFileSync("./src/assets/icon-arrow-left.svg");
const rightArrow = fs.readFileSync("./src/assets/icon-arrow-right.svg");
const pluginRss = require("@11ty/eleventy-plugin-rss");
function titleCase(str) {
return str
.toLowerCase()
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
}
function paginationItem(direction, link) {
let directionIndicator;
if (direction === "prev") {
directionIndicator = `
<div class="direction">
<div class="icon">${leftArrow}</div>Prev
</div>
`;
} else {
directionIndicator = `
<div class="direction">
Next<div class="icon">${rightArrow}</div>
</div>
`;
}
return `
<a href="${link.href}" class="${direction}">
${directionIndicator}
<div class="article-title">${link.label}</div>
</a>
`;
}
function prev(link) {
return paginationItem("prev", link);
}
function next(link) {
return paginationItem("next", link);
}
module.exports = function(config) {
// Exclusively use .eleventyignore, to make sure src/docs are used as source
config.setUseGitIgnore(false);
// Layouts
config.addLayoutAlias("base", "base.njk");
// Static files passthrough
config.addPassthroughCopy("src/css");
config.addPassthroughCopy("src/js");
config.addPassthroughCopy("src/assets");
config.addPassthroughCopy("src/video");
config.addPassthroughCopy("src/fonts");
config.addPassthroughCopy("src/favicon.svg");
config.addPassthroughCopy("src/favicon.ico");
config.addPassthroughCopy("src/robots.txt");
config.addPassthroughCopy("src/_redirects");
config.addShortcode("isFutureDate", (date) => {
return true;
});
// Shortcodes
config.addShortcode("currentYear", () => `${new Date().getFullYear()}`);
config.addShortcode("articlePagination", (sidebar, current) => {
const first = 0;
const last = sidebar.length - 1;
const c = R.findIndex((l) => l.href + "/" === current, sidebar);
if (c === -1) return "";
let links = [];
if (c > first) {
const p = sidebar[c - 1];
links.push(prev(p));
}
if (c < last) {
const n = sidebar[c + 1];
links.push(next(n));
}
if (links.length > 0) {
return `<footer class="article-pagination">${links.join("")}</footer>`;
} else {
return "";
}
});
config.addFilter("algoliafy", (text) => {
// Remove code
text = text.replaceAll(/<code>.*?<\/code>/gs, "");
// Remove html tags
text = text.replace(/<.*?>/g, " "); // Extra space for code blocks
// Remove backslashes
text = text.replace(/\\/g, "");
// Remove tabs
text = text.replace(/\t/g, "");
// Remove big spaces and punctuation
text = text.replace(/\n/g, " ");
// Remove repeated spaces
text = text.replace(/[ ]{2,}/g, " ");
// Remove quotes
text = text.replace(/"/g, "");
// Support -> and <-
text = text.replaceAll("->", "->");
text = text.replaceAll("<-", "<-");
//now limit to 8k
return text.substring(0, 8000);
});
config.addFilter("jsonify", (text) => {
return JSON.stringify(text);
});
// Parsing and formatting in UTC basically means we retain the date of a non
// timezoned date.
config.addFilter("formatDate", (date) =>
format(utcToZonedTime(date, "UTC"), "MMM dd, yyyy", "UTC")
);
config.addFilter("isoDate", (date) => formatISO(date));
config.addFilter("month", (month) =>
format(parse(month, "M", new Date()), "MMM")
);
config.addFilter("author", (authorTag) => {
const author = authors[authorTag];
if (author) {
return author.name;
} else {
return titleCase(authorTag.replaceAll("-", " "));
}
});
const markdown = require("markdown-it")({
html: true,
breaks: true,
linkify: true,
});
config.addFilter("markdown", (raw) => {
if (raw && raw.length) {
return markdown.renderInline(raw);
}
else {
return raw;
}
});
config.addPlugin(pluginRss);
return {
dir: {
input: "src",
output: "build/site",
includes: "_includes",
layouts: "_layouts",
data: "_data",
},
passthroughFileCopy: true,
};
};