-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
135 lines (119 loc) · 3 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
import markdownIt from 'markdown-it';
import { EleventyRenderPlugin } from '@11ty/eleventy';
export default (eleventy) => {
// Add render plugin
eleventy.addPlugin(EleventyRenderPlugin);
// Add static files
eleventy.addPassthroughCopy('public/**');
// Render links in Markdown
eleventy.setLibrary('md', markdownIt({
linkify: true,
}));
// Custom sorting for pages (nav) collection
eleventy.addCollection('nav', sortByOrderField('nav'));
// Custom sorting for questions (faq) collection
eleventy.addCollection('faq', sortByOrderField('faq'));
// Add shortcodes & filters
eleventy.addFilter('emoji-number', emojiNumber);
eleventy.addFilter('prompt-amount', promptsAmount);
eleventy.addFilter('prompt-recent-three', recentThree);
eleventy.addFilter('prompt-visitable', filterVisitable);
eleventy.addFilter('prompt-calendar-group', groupPromptsForCalendar);
eleventy.addFilter('prompt-table-group', groupPromptsForTable);
}
function sortByOrderField(tagName) {
return function (collection) {
return collection
.getFilteredByTag(tagName)
.sort((a, b) => a.data.order - b.data.order);
};
}
function emojiNumber(number) {
function digitToEmoji(digit) {
switch (digit) {
case '0':
return '0️⃣';
case '1':
return '1️⃣';
case '2':
return '2️⃣';
case '3':
return '3️⃣';
case '4':
return '4️⃣';
case '5':
return '5️⃣';
case '6':
return '6️⃣';
case '7':
return '7️⃣';
case '8':
return '8️⃣';
case '9':
return '9️⃣';
}
}
const emoji = `${number}`
.split('')
.map(digitToEmoji)
.join('');
return `<span aria-label="${number}" title="${number}">${emoji}</span>`;
}
function promptsAmount(prompts) {
return emojiNumber(filterVisitable(prompts).length);
}
function recentThree(prompts) {
return prompts
.filter(p => p.data.permalink)
.sort((a, b) => b.date - a.date)
.slice(0, 3);
}
function filterVisitable(prompts) {
return prompts.filter(p => p.data.permalink);
}
function groupPromptsForCalendar(prompts) {
function getMonth(date) {
return date.toLocaleString('default', { month: 'long' });
}
return _groupPrompts(prompts, getMonth);
}
function groupPromptsForTable(prompts) {
function getMonth(date) {
return date
.toLocaleString('default', { month: 'long' })
.toUpperCase()
.slice(0, 3);
}
const grouped = _groupPrompts(prompts, getMonth);
for (const yearEntry of grouped) {
yearEntry.months.reverse();
}
grouped.reverse();
return grouped;
}
function _groupPrompts(prompts, getMonth) {
function getYear(date) {
return date.toLocaleString('default', { year: 'numeric' });
}
const grouped = [];
for (const prompt of prompts) {
const year = getYear(prompt.page.date);
const month = getMonth(prompt.page.date);
const yearEntry = grouped.find(entry => entry.year === year);
if (!yearEntry) {
grouped.push({
year,
months: [{
month,
prompt,
}],
});
} else {
yearEntry.months.push({
month,
prompt,
});
}
}
return grouped;
}