-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
.eleventy.js
210 lines (185 loc) · 6.71 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
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// <https://apache.org/licenses/LICENSE-2.0>.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
const { DateTime } = require('luxon');
const he = require('he');
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
const markdownItAttrs = require('markdown-it-attrs');
const markdownItContainer = require('markdown-it-container');
const markdownItEmbedImage = require('./md-embed-image.js');
const markdownItImplicitFigures = require('markdown-it-implicit-figures');
const markdownItFootnote = require('markdown-it-footnote');
const markdownItMultiMdTable = require('markdown-it-multimd-table');
const pluginRss = require('@11ty/eleventy-plugin-rss');
const pluginSyntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const installPrismLanguages = require('./prism-languages.js');
const expandFeatureSupport = require('./feature-support.js');
const markdownItConfig = {
html: true,
breaks: true,
linkify: false,
};
const markdownItAnchorConfig = {
permalink: true,
permalinkClass: 'bookmark',
permalinkSymbol: '#',
};
const md = markdownIt(markdownItConfig)
.use(markdownItFootnote)
.use(markdownItAttrs)
.use(markdownItContainer, 'note')
.use(markdownItContainer, 'table-wrapper')
.use(markdownItContainer, 'ecmascript-algorithm')
.use(markdownItContainer, 'figure', {
validate(params) {
return params.trim().match(/^figure\s+(.*)$/);
},
render(tokens, idx) {
const m = tokens[idx].info.trim().match(/^figure\s+(.*)$/);
if (tokens[idx].nesting === 1) {
// Opening tag; save caption for the closing tag.
this.saved_caption_ = m[1];
return '<figure>\n';
} else {
// Closing tag.
return '<figcaption>' + md.utils.escapeHtml(this.saved_caption_) + '</figcaption></figure>\n';
}
},
saved_caption_: null
})
.use(markdownItMultiMdTable, {
rowspan: true,
multiline: true,
})
.use(markdownItAnchor, markdownItAnchorConfig)
.use(markdownItImplicitFigures, {
figcaption: true
})
.use(markdownItEmbedImage);
// Simulating `td:has(>pre:only-child)` with a markdown-it render rule.
// Can be removed when (if?) CSS4 is actually implemented in browsers.
const prevTdRenderer = md.renderer.rules.table_column_open;
md.renderer.rules.table_column_open = (tokens, idx, options, env, self) => {
if (tokens[idx + 1].type === 'fence' && tokens[idx + 2].type === 'table_column_close') {
tokens[idx].attrJoin('class', 'td-with-just-pre');
}
if (prevTdRenderer) {
return prevTdRenderer(tokens, idx, options, env, self);
} else {
return self.renderToken(tokens, idx, options);
}
};
module.exports = (eleventyConfig) => {
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginSyntaxHighlight, {
init({ Prism }) {
installPrismLanguages(Prism);
},
});
eleventyConfig.addLayoutAlias('post', 'layouts/post.njk');
eleventyConfig.addFilter('readableDate', (dateObj) => {
return DateTime.fromJSDate(dateObj).toFormat('yyyy-LL-dd');
});
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
eleventyConfig.addFilter('htmlDateString', (dateObj) => {
return DateTime.fromJSDate(dateObj).toFormat('yyyy-LL-dd HH:mm:ss');
});
eleventyConfig.addFilter('markdown', (string) => {
return md.renderInline(string);
});
eleventyConfig.addFilter('decodeHtmlEntities', (string) => {
return he.decode(string);
});
// Match Firebase’s `cleanUrls` setting.
eleventyConfig.addFilter('clean', (path) => {
if (path === '/') return path;
if (path === 'https://v8.js.cn/') return path;
if (path.endsWith('/')) return path.slice(0, -1);
return path;
});
// Get the first `n` elements of a collection.
eleventyConfig.addFilter('head', (array, n) => {
return array.slice(0, n);
});
eleventyConfig.addFilter('filterBlogPosts', (array) => {
return array.filter(post =>
post.inputPath.startsWith('./src/blog/'));
});
eleventyConfig.addFilter('filterFeaturePosts', (array) => {
return array.filter(post =>
post.inputPath.startsWith('./src/features/'));
});
// Create a collection for blog posts only.
eleventyConfig.addCollection('posts', (collection) => {
return collection.getFilteredByGlob('src/blog/*.md')
.sort((a, b) => b.date - a.date);
});
// Create a collection for feature explainers only.
eleventyConfig.addCollection('features', (collection) => {
return collection.getFilteredByGlob('src/features/*.md')
.sort((a, b) => b.date - a.date);
});
// Create a collection with merged blogs and feature explainers.
eleventyConfig.addCollection('allPosts', (collection) => {
return collection.getFilteredByGlob('src/{blog,features}/*.md')
.sort((a, b) => b.date - a.date);
});
// Patch the Markdown renderer to recognize <feature-support>.
const oldRender = md.render.bind(md);
md.render = (input) => {
const preprocessed = expandFeatureSupport(input);
return oldRender(preprocessed);
};
// Treat `*.md` files as Markdown.
eleventyConfig.setLibrary('md', md);
eleventyConfig.addCollection('tagList', (collection) => {
const set = new Set();
for (const item of collection.getAllSorted()) {
if ('tags' in item.data) {
const tags = item.data.tags;
if (typeof tags === 'string') {
tags = [tags];
}
for (const tag of tags) {
set.add(tag);
}
}
}
return [...set].sort();
});
// Copy assets that don’t require a build step.
eleventyConfig.addPassthroughCopy('src/favicon.ico');
eleventyConfig.addPassthroughCopy('src/robots.txt');
eleventyConfig.addPassthroughCopy('src/CNAME');
eleventyConfig.addPassthroughCopy('src/.nojekyll');
eleventyConfig.addPassthroughCopy('src/_img');
eleventyConfig.addPassthroughCopy('src/_css/img');
return {
templateFormats: [
'md',
'njk',
'html',
],
pathPrefix: '/',
markdownTemplateEngine: 'liquid',
htmlTemplateEngine: 'njk',
dataTemplateEngine: 'njk',
passthroughFileCopy: true,
dir: {
input: 'src',
includes: '_includes',
data: '_data',
output: 'dist',
},
};
};