-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.eleventy.js
212 lines (178 loc) · 6.42 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
const htmlmin = require('html-minifier');
//const inclusiveLangPlugin = require('@11ty/eleventy-plugin-inclusive-language');
const navigationPlugin = require('@11ty/eleventy-navigation');
const syntaxHighlightPlugin = require('@11ty/eleventy-plugin-syntaxhighlight');
const fs = require('fs');
const markdownItAnchor = require('markdown-it-anchor');
const tocPlugin = require('eleventy-plugin-nesting-toc');
const pkg = require('./package.json');
/*
// Configure use of Katex for rendering math equations.
const markdownIt = require('markdown-it');
const markdownItKatex = require('markdown-it-katex');
const options = {breaks: false, html: true, linkify: true};
//const markdownLib = markdownIt(options).use(markdownItKatex);
const markdownLib = markdownIt(options);
markdownLib.use(markdownItKatex);
*/
// Configure use of MathJAX for rendering math equations.
const markdownIt = require('markdown-it');
const options = {breaks: false, html: true, linkify: true};
const mathJax = require('markdown-it-mathjax');
const sups = require('markdown-it-sup');
const subs = require('markdown-it-sub');
const footnotes = require('markdown-it-footnote');
const markdownLib = markdownIt(options)
.use(mathJax()) // https://github.com/classeur/markdown-it-mathjax shows need for ()
.use(subs)
.use(sups)
.use(footnotes)
.use(markdownItAnchor);
/**
This recursively sorts an array of documents.
The primary sort is on the "order" property.
The secondary sort is on title.
*/
function sortDocuments(arr) {
if (!arr) return;
arr.sort((doc1, doc2) => {
const order1 = doc1.order;
const order2 = doc2.order;
if (order1) {
return order2 ? order1 - order2 : -1;
} else if (order2) {
return 1;
} else {
return doc1.title.localeCompare(doc2.title);
}
});
arr.forEach(doc => sortDocuments(doc.children));
}
module.exports = eleventyConfig => {
eleventyConfig.setLibrary('md', markdownLib);
eleventyConfig.addCollection('nav', collection => {
const keyMap = {};
const navMap = {};
// Create an array of item objects that contain
// "eleventyNavigation" front matter.
const navItems = collection.items.filter(
item => item.data.eleventyNavigation
);
// Create a mapping from item keys to objects that describe them.
for (const item of navItems) {
const {data, url} = item;
const {eleventyNavigation: nav} = data;
const {key, title} = nav;
if (!title) nav.title = key;
//TODO: pkg is not defined!
if (url) nav.url = `/blog${url}?v=${pkg.version}`;
keyMap[key] = nav;
}
// Turn the items into a tree structure based on parent relationships.
for (const obj of Object.values(keyMap)) {
const {parent} = obj;
if (parent) {
parentObj = keyMap[parent];
if (parentObj) {
let {children} = parentObj;
if (!children) children = parentObj.children = [];
children.push(obj);
}
} else {
navMap[obj.key] = obj;
}
}
// Get an array of the top-level items.
const navColl = Object.values(navMap);
// Sort the items within their parent.
sortDocuments(navColl);
return navColl;
});
// This filters page objects based on a data property value.
eleventyConfig.addFilter('filter', (arr, property, value) => {
return arr.filter(obj => obj.data[property] === value);
});
// This filters page objects based on a data property value.
eleventyConfig.addFilter('filterNot', (arr, property, value) => {
return arr.filter(obj => obj.data[property] !== value);
});
eleventyConfig.addFilter('hasOrder', arr => {
return arr.filter(obj => Boolean(obj.data.eleventyNavigation.order));
});
// This filter is being added in v0.11.0.
eleventyConfig.addFilter('log', value => {
//console.log('.eleventy.js log: value =', value);
return value;
});
eleventyConfig.addFilter('noOrder', arr => {
return arr.filter(obj => !obj.data.eleventyNavigation.order);
});
eleventyConfig.addFilter('navSort', arr => {
sortDocuments(arr);
return arr;
});
//eleventyConfig.addPlugin(inclusiveLangPlugin);
// Copies files in a given directory to output directory
// without performing any processing on them.
eleventyConfig.addPassthroughCopy('src/assets');
eleventyConfig.addPassthroughCopy('src/js');
eleventyConfig.addPlugin(navigationPlugin);
eleventyConfig.addPlugin(tocPlugin);
eleventyConfig.addPlugin(syntaxHighlightPlugin);
eleventyConfig.addShortcode(
'aInternal',
(url, text) => `<a href="${url}" target="frame">${text}</a>`
);
eleventyConfig.addShortcode(
'aTargetBlank',
(url, text) =>
`<a href="${url}?v=${pkg.version}" rel="noopener" target="_blank">${text}</a>`
);
eleventyConfig.addShortcode(
'aTargetBlankNoVersion',
(url, text) => `<a href="${url}" rel="noopener" target="_blank">${text}</a>`
);
// Minify generated HTML.
eleventyConfig.addTransform('htmlmin', (content, outputPath) => {
if (!outputPath || !outputPath.endsWith('.html')) return content;
return htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
});
eleventyConfig.addWatchTarget('_includes/*.js');
eleventyConfig.addWatchTarget('_site/assets/*.css');
eleventyConfig.setBrowserSyncConfig({
files: ['_site/assets/*.css']
});
// Suppresses output of the paths of all generated files.
eleventyConfig.setQuietMode(true);
// Watches all JavaScript templates and data files
// and rebuilds the site if they change.
//eleventyConfig.setWatchJavaScriptDependencies(true);
// Create JSON file that is read by service-worker.js.
/*
try {
let files = fs.readdirSync('_site/assets');
files = files.map(file => '/blog/assets/' + file);
files.push('/blog/'); // cache the start URL (Lighthouse wants this)
const timestamp = Date.now();
const serviceWorkerData = {files, timestamp};
fs.writeFileSync(
'_site/service-worker-data.json',
JSON.stringify(serviceWorkerData)
);
console.log('wrote service-worker-data.json with timestamp', timestamp);
} catch (e) {
// Don't treat it as an error if the _site/assets directory doesn't exist.
if (e.code !== 'ENOENT') throw e;
}
*/
return {
dir: {input: 'src'},
markdownTemplateEngine: 'njk', // used in Markdown files
pathPrefix: '/blog/', // prepended to all URL paths
templateFormats: ['11ty.js', 'html', 'md', 'njk']
};
};