forked from LeaVerou/lea.verou.me
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.cjs
152 lines (124 loc) · 3.21 KB
/
.eleventy.cjs
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
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItAttrs = require("markdown-it-attrs");
const markdownItFootnote = require("markdown-it-footnote");
const markdownItMathjax3 = require("markdown-it-mathjax3");
const embeds = require("eleventy-plugin-embed-everything");
const pluginTOC = require("eleventy-plugin-toc");
const filters = require("./assets/filters.cjs");
const tag_aliases = require("./data/tag_aliases.json");
const pluginRss = require("@11ty/eleventy-plugin-rss");
let foo = false;
module.exports = config => {
let data = {
layout: "page.njk",
permalink: "{{ page.filePathStem }}.html",
currentYear: new Date().getFullYear(),
};
for (let p in data) {
config.addGlobalData(p, data[p]);
}
config.setDataDeepMerge(true);
config.setFrontMatterParsingOptions({
excerpt: true,
// Optional, default is "---"
excerpt_separator: "<!-- more -->"
});
let md = markdownIt({
html: true,
linkify: true,
typographer: true,
})
.disable("code")
.use(markdownItMathjax3)
.use(markdownItAttrs)
.use(markdownItFootnote)
.use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.headerLink(),
level: 2,
})
;
config.setLibrary("md", md);
config.addFilter("md", (value, o = {}) => {
if (typeof value !== "string") {
if (value instanceof String) {
value = value + "";
}
else {
return value;
}
}
let ret = md.render(value, o);
if (o.url) {
ret = filters.relativize_urls(ret, o.url);
}
return ret;
});
config.addFilter("md_inline", (value) => {
if (typeof value !== "string") {
return value;
}
return md.renderInline(value, o);
});
for (let name in filters) {
config.addFilter(name, filters[name]);
}
config.addPlugin(embeds);
config.addPlugin(pluginRss);
config.addCollection("postsByTag", (collectionApi) => {
const posts = collectionApi.getFilteredByTag("blog");
let ret = {};
for (let post of posts) {
for (let tag of post.data.tags) {
if (filters.is_real_tag(tag)) {
ret[tag] ??= [];
ret[tag].push(post);
}
}
}
// Now sort, and reconstruct the object
ret = Object.fromEntries(Object.entries(ret).sort((a, b) => b[1].length - a[1].length));
// Now add aliases
for (let alias in tag_aliases) {
let aliasOf = tag_aliases[alias];
if (ret[aliasOf]) {
ret[aliasOf].aliases ??= [];
ret[aliasOf].aliases.push(alias);
}
}
return ret;
});
config.addCollection("postsByMonth", (collectionApi) => {
const posts = collectionApi.getFilteredByTag("blog").reverse();
const ret = {};
for (let post of posts) {
let key = filters.format_date(post.date, "iso").substring(0, 7); // YYYY-MM
ret[key] ??= [];
ret[key].push(post);
}
return ret;
});
config.addCollection("postsByYear", (collectionApi) => {
const posts = collectionApi.getFilteredByTag("blog").reverse();
const ret = {};
for (let post of posts) {
let key = post.date.getFullYear();
ret[key] ??= [];
ret[key].push(post);
}
return ret;
});
config.addPlugin(pluginTOC, {
tags: ["h2", "h3"],
ul: true,
wrapper: "",
});
return {
markdownTemplateEngine: "njk",
templateFormats: ["md", "njk"],
dir: {
data: "data",
output: "."
},
};
};