-
Notifications
You must be signed in to change notification settings - Fork 27
/
.eleventy.js
163 lines (128 loc) · 3.94 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
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const moment = require("moment");
const slugify = require("@sindresorhus/slugify");
const striptags = require('striptags');
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
function createExcerpt(value, maxLength) {
const bits = value.split('<!--more-->');
if (bits.length === 1) {
const terminator = value.length > maxLength ? '…' : '';
return value.substring(0, maxLength) + terminator;
}
return bits[0];
}
module.exports = eleventyConfig => {
eleventyConfig.addCollection("homepage", collection => {
const posts = collection
.getAllSorted()
.filter(item => {
const { data } = item;
if (!data.tags || data.queued) {
return false;
}
if (!data.tags.includes("blog")) {
return false;
}
item.data.page.metaTag = striptags(
createExcerpt(item.template.frontMatter.content, 200).replace(/\n/g, '')
);
return true;
})
.reverse();
const internalPosts = posts.filter(post => post.data.layout === "post");
internalPosts.forEach((post, index) => {
if (internalPosts[index - 1]) {
post.data.page.next = {
title: internalPosts[index - 1].data.title,
url: internalPosts[index - 1].data.page.url
};
}
if (internalPosts[index + 1]) {
post.data.page.previous = {
title: internalPosts[index + 1].data.title,
url: internalPosts[index + 1].data.page.url
};
}
});
return posts;
});
eleventyConfig.addCollection("series", collection => {
let previousItem;
return collection.getAllSorted().filter(item => {
if (item.data.series) {
if (previousItem && previousItem.data.series === item.data.series) {
previousItem.data.nextInSeries = item;
}
previousItem = item;
} else {
previousItem = undefined;
}
return item.data.series;
});
});
eleventyConfig.addLiquidFilter("feature_title", title => {
const MIN_LENGTH = 10;
const MAX_LENGTH = 20;
if (!title) return "";
let currentLine = "";
let lines = [];
let words = title.split(" ");
words.forEach(word => {
if (currentLine.length + word.length <= MAX_LENGTH) {
currentLine += word + " ";
} else {
lines.push(currentLine);
currentLine = word + " ";
}
});
if (currentLine.length < MIN_LENGTH) {
lines[lines.length - 1] += currentLine;
} else {
lines.push(currentLine);
}
return `
<span class="feature-title__full">${title}</span>
${lines
.map(
line => `
<span aria-hidden="true" class="feature-title__part">${line.slice(
0,
-1
)}</span>
`
)
.join("")}
`;
});
eleventyConfig.addLiquidFilter("fullDate", date => {
const momentDate =
typeof date === "number" ? moment(new Date(date * 1000)) : moment(date);
return momentDate.format("MMMM Do, YYYY");
});
eleventyConfig.addLiquidFilter("excerpt", value => {
const allowedTags = ['code', 'em', 'strong'];
const excerpt = createExcerpt(value, 200);
return striptags(excerpt, allowedTags);
});
const markdownItOptions = { html: true };
const markdownItAnchorOptions = {
permalinkClass: "heading-anchor",
level: 1,
permalink: true,
permalinkBefore: true,
slugify
};
const markdownItPlugin = markdownIt(markdownItOptions).use(
markdownItAnchor,
markdownItAnchorOptions
);
eleventyConfig.addLiquidFilter("markdownify", text => markdownItPlugin.render(text));
eleventyConfig.setLibrary("md", markdownItPlugin);
eleventyConfig.addPassthroughCopy("assets");
eleventyConfig.addPassthroughCopy("posts");
eleventyConfig.addPlugin(syntaxHighlight);
return {
passthroughFileCopy: true
};
};