-
Notifications
You must be signed in to change notification settings - Fork 4
/
.eleventy.js
216 lines (194 loc) · 6.82 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const markdownItFootnote = require("markdown-it-footnote");
const markdownItAttrs = require("markdown-it-attrs");
const markdownItTexmath = require("markdown-it-texmath");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const katex = require("katex");
const site = require("./_data/site.json");
const { wordCount } = require("eleventy-plugin-wordcount");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const { minify } = require("terser");
module.exports = function (eleventyConfig) {
// Workaround for ES modules that don't support require()
let slugify;
eleventyConfig.on('eleventy.before', async ({}) => {
slugify = (await import("@sindresorhus/slugify")).default;
});
// Add header anchor and footnotes plugin to Markdown renderer
const markdownLib = markdownIt({ html: true, typographer: true });
const permalinkOptions = {
symbol: "<i class=\"fas fa-link\"></i>",
placement: 'after'
};
markdownLib
.use(markdownItFootnote)
.use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.ariaHidden(permalinkOptions),
slugify: s => slugify(s)
})
.use(markdownItAttrs, {
leftDelimiter: "{:",
rightDelimiter: "}",
allowedAttributes: [],
})
.use(markdownItTexmath, {
engine: katex,
delimiters: ["dollars", "beg_end"],
katexOptions: {
throwOnError: true,
strict: false,
},
});
eleventyConfig.setLibrary("md", markdownLib);
// Watch CSS files for changes
eleventyConfig.setBrowserSyncConfig({
files: "./_site/assets/**/*.css",
});
// Enable syntax highlighting
eleventyConfig.addPlugin(syntaxHighlight);
// For counting words
eleventyConfig.addPlugin(wordCount);
// Copy the assets folder to the _site folder
eleventyConfig.addPassthroughCopy("assets");
eleventyConfig.addPassthroughCopy("wp-content");
eleventyConfig.addPassthroughCopy("favicon.png");
eleventyConfig.addPassthroughCopy("ads.txt");
eleventyConfig.addPassthroughCopy("robots.txt");
eleventyConfig.addPassthroughCopy(".htaccess");
eleventyConfig.addCollection("posts", function (collectionApi) {
return collectionApi.getFilteredByGlob("_posts/**/*.md");
});
// Categories collections
eleventyConfig.addCollection("ccpp", function (collectionApi) {
return getPostsForCategory(collectionApi, "c/c++");
});
eleventyConfig.addCollection("audio_fx", function (collectionApi) {
return getPostsForCategory(collectionApi, "audio fx");
});
eleventyConfig.addCollection("dsp", function (collectionApi) {
return getPostsForCategory(collectionApi, "digital signal processing");
});
eleventyConfig.addCollection(
"programming_in_general",
function (collectionApi) {
return getPostsForCategory(collectionApi, "programming in general");
}
);
eleventyConfig.addCollection("synthesis", function (collectionApi) {
return getPostsForCategory(collectionApi, "sound synthesis");
});
eleventyConfig.addCollection("podcast", function (collectionApi) {
return getPostsForCategory(collectionApi, "podcast");
});
eleventyConfig.addCollection("python_category", function (collectionApi) {
return getPostsForCategory(collectionApi, "python");
});
eleventyConfig.addCollection("sound_in_general", function (collectionApi) {
return getPostsForCategory(collectionApi, "sound in general");
});
// Define a post_url Liquid tag for cross referencing
// https://rusingh.com/articles/2020/04/24/implement-jekyll-post-url-tag-11ty-shortcode/
eleventyConfig.addShortcode("post_url", (collection, relativePostPath) => {
try {
if (collection.length < 1) {
throw "Collection appears to be empty";
}
if (!Array.isArray(collection)) {
throw "Collection is an invalid type - it must be an array!";
}
if (typeof relativePostPath !== "string") {
throw "Relative post path is an invalid type - it must be a string!";
}
const found = collection.find((p) =>
p.inputPath.includes(relativePostPath)
);
if (found === 0 || found === undefined) {
throw `${relativePostPath} not found in specified collection.`;
} else {
return found.url;
}
} catch (e) {
console.error(
`An error occured while searching for the url to ${relativePostPath}. Details:`,
e
);
}
});
// Alias for the absolute_url filter.
eleventyConfig.addFilter("absolute_url", function (allContent, url) {
return eleventyConfig.getFilter("url")(url);
});
// Fix for Jekyll's for the relative_url filter.
eleventyConfig.addFilter("relative_url", function (url) {
return site.baseurl + url;
});
eleventyConfig.addShortcode("link", (collection, relativeFilePath) => {
try {
if (collection.length < 1) {
throw "Collection appears to be empty";
}
if (!Array.isArray(collection)) {
throw "Collection is an invalid type - it must be an array!";
}
if (typeof relativeFilePath !== "string") {
throw "Relative file path is an invalid type - it must be a string!";
}
const found = collection.find((p) =>
p.inputPath.includes(relativeFilePath)
);
if (found === 0 || found === undefined) {
throw `${relativeFilePath} not found in specified collection.`;
} else {
return found.url;
}
} catch (e) {
console.error(
`An error occured while searching for the url to ${relativeFilePath}. Details:`,
e
);
}
});
eleventyConfig.addFilter("normalize_whitespace", (string) => {
if (typeof string !== "string") {
throw "normalize_whitespace: argument must be a string!";
}
return string.replace(/\s\s+/g, " ");
});
eleventyConfig.addLiquidFilter("truncate_to_first_newline", (string) => {
return string.split("\n")[0];
});
eleventyConfig.addPlugin(pluginRss);
// Return all the tags used in a collection
eleventyConfig.addFilter("getAllTags", (collection) => {
let tagSet = new Set();
for (let item of collection) {
(item.data.tags || []).forEach((tag) => tagSet.add(tag));
}
return Array.from(tagSet);
});
eleventyConfig.addLiquidFilter("jsmin", async function (code) {
try {
const minified = await minify(code);
return minified.code;
} catch (err) {
console.error("Terser error: ", err);
// Fail gracefully.
return code;
}
});
return {
dir: {
layouts: "_layouts",
},
};
};
function getPostsForCategory(collectionApi, category) {
return collectionApi.getFilteredByGlob("_posts/**/*.md").filter((item) => {
return item.data.categories
.map((categoryName) => {
return categoryName.toLowerCase();
})
.includes(category);
});
}