-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
100 lines (87 loc) · 2.99 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
const fs = require("fs");
const CleanCSS = require("clean-css");
const Image = require("@11ty/eleventy-img");
const { execSync } = require('child_process');
const pluginRss = require("@11ty/eleventy-plugin-rss");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const eleventyPluginFilesMinifier = require("@sherby/eleventy-plugin-files-minifier");
/* Navigation */
const markdownIt = require('markdown-it')
const pluginTOC = require('eleventy-plugin-toc')
const markdownItAnchor = require('markdown-it-anchor');
const mdOptions = {
html: true,
breaks: true,
linkify: true,
typographer: true
}
const mdAnchorOpts = {
permalink: true,
permalinkClass: 'anchor-link',
permalinkSymbol: '#',
level: [1, 2, 3, 4]
}
module.exports = function (eleventyConfig) {
/* Syntax Highlighting */
eleventyConfig.addPlugin(syntaxHighlight);
/* The required CSS for the PrimJS color theme */
eleventyConfig.addPassthroughCopy("assets");
eleventyConfig.addPassthroughCopy("ace");
eleventyConfig.on('beforeBuild', () => {
// Run the custom script before building the site
execSync('node moveAssets.js');
});
eleventyConfig.setLibrary(
'md',
markdownIt(mdOptions)
.use(markdownItAnchor, mdAnchorOpts)
);
eleventyConfig.addPlugin(pluginTOC);
eleventyConfig.addFilter("modifyTOC", function (tocHtml, postTitle) {
if (!tocHtml)
tocHtml = `<nav class="toc"><ul></ul></nav>`;
/* Clear whitespace before string matching */
tocHtml = tocHtml.replace(/>\s+</g, '><');
/* Header */
tocHtml = tocHtml.replace('<ul>', `<ul><li><a href="#${postTitle}">${postTitle}</a><ul>`);
/* Comments */
tocHtml = tocHtml.replace('</ul></nav>', '</ul></li><li><a href="#comments">Comments</a></li></ul></nav>');
return tocHtml;
});
/* CSS minifier as per https://www.11ty.dev/docs/quicktips/inline-css/ */
eleventyConfig.addFilter("cssmin", function (code) {
return new CleanCSS({}).minify(code).styles;
});
/* Reload on CSS changes, since 11ty doesn't see them */
eleventyConfig.addWatchTarget("style");
eleventyConfig.addWatchTarget("assets");
eleventyConfig.addWatchTarget("posts");
/* HTML minifier */
eleventyConfig.addPlugin(eleventyPluginFilesMinifier);
/* RSS Plugin */
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addShortcode("rawFile", (filename) => {
const bytesOfFile = fs.readFileSync(filename);
return bytesOfFile.toString();
});
/* Thumbnail maker */
eleventyConfig.addCollection("thumbnail", async function (collectionApi) {
// Get all posts
const posts = collectionApi.getFilteredByTag("post");
for (const post of posts) {
if (post.data.image) {
const image = await Image('./posts/' + post.url + '/' + post.data.image, {
widths: [256, "auto"],
formats: ['jpeg'],
outputDir: './_site/' + post.url
});
post.data.image = post.url + image.jpeg[0].filename;
if (image.jpeg[1])
post.data.social = post.url + image.jpeg[1].filename;
else
post.data.social = post.url + image.jpeg[0].filename;
}
}
return posts;
});
};