-
Notifications
You must be signed in to change notification settings - Fork 35
/
.eleventy.js
335 lines (277 loc) · 10.5 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* @param {import("@11ty/eleventy/src/UserConfig")} eleventyConfig
*/
const glob = require('fast-glob');
const path = require('path');
const config = require('./pack11ty.config.js');
module.exports = function (eleventyConfig) {
// ------------------------------------------------------------------------
// Collections
// ------------------------------------------------------------------------
glob
.sync(path.join(config.dir.src, '_11ty/collections/*.js'))
.forEach((file) => {
let collection = require('./' + file);
Object.keys(collection).forEach((name) => {
eleventyConfig.addCollection(name, collection[name]);
});
});
// ------------------------------------------------------------------------
// Filters
// ------------------------------------------------------------------------
glob.sync(path.join(config.dir.src, '_11ty/filters/*.js')).forEach((file) => {
let filters = require('./' + file);
Object.keys(filters).forEach((name) => {
eleventyConfig.addFilter(name, filters[name]);
});
});
// ------------------------------------------------------------------------
// Shortcodes
// ------------------------------------------------------------------------
glob
.sync(path.join(config.dir.src, '_11ty/shortcodes/*.js'))
.forEach((file) => {
let shortcodes = require('./' + file);
Object.keys(shortcodes).forEach((name) => {
eleventyConfig.addNunjucksShortcode(name, shortcodes[name]);
});
});
// glob
// .sync(path.join(config.dir.src, '_11ty/pairedShortcodes/*.js'))
// .forEach((file) => {
// let pairedShortcodes = require('./' + file);
// Object.keys(pairedShortcodes).forEach((name) => {
// eleventyConfig.addPairedShortcode(name, pairedShortcodes[name]);
// });
// });
// ------------------------------------------------------------------------
// Plugins
// ------------------------------------------------------------------------
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
eleventyConfig.addPlugin(syntaxHighlight);
const rss = require('@11ty/eleventy-plugin-rss');
eleventyConfig.addPlugin(rss);
const embedTweets = require('eleventy-plugin-embed-tweet');
eleventyConfig.addPlugin(embedTweets, {
cacheDirectory: '_cache',
useInlineStyles: false,
autoEmbed: true,
});
const embedEverythingElse = require('eleventy-plugin-embed-everything');
eleventyConfig.addPlugin(embedEverythingElse, {
youtube: {
options: {
lite: {
css: {
path: '/assets/javascript/vendors/yt-lite/lite-yt-embed.css',
},
js: {
path: '/assets/javascript/vendors/yt-lite/lite-yt-embed.js',
},
},
},
},
});
eleventyConfig.addPlugin(require('eleventy-plugin-link_to'));
// ------------------------------------------------------------------------
// Markdown plugins
// ------------------------------------------------------------------------
const markdownIt = require('markdown-it');
const markdownItOptions = {
html: true,
breaks: true,
linkify: true,
};
const markdownItFootnote = require('markdown-it-footnote');
const markdownItAnchor = require('markdown-it-anchor');
const slugify = require('@sindresorhus/slugify');
const markdownItAnchorOptions = {
level: [2, 3, 4],
slugify,
permalink: markdownItAnchor.permalink.linkAfterHeader({
class: 'deeplink',
symbol:
'<svg class="icon" role="img" focusable="false" viewBox="0 0 24 24" width="1em" height="1em"><use xlink:href="#symbol-anchor" /></svg>',
style: 'visually-hidden',
visuallyHiddenClass: 'visually-hidden',
assistiveText: (title) => `Permalink to heading ${title}`,
wrapper: ['<div class="heading-wrapper">', '</div>'],
}),
};
const markdownItAttributes = require('markdown-it-attrs');
const markdownItSpan = require('markdown-it-bracketed-spans');
const markdownItContainer = require('markdown-it-container');
const markdownItAbbr = require('markdown-it-abbr');
const markdownItPlainText = require('markdown-it-plain-text');
// taken from https://gist.github.com/rodneyrehm/4feec9af8a8635f7de7cb1754f146a39
function getHeadingLevel(tagName) {
if (tagName[0].toLowerCase() === 'h') {
tagName = tagName.slice(1);
}
return parseInt(tagName, 10);
}
function markdownItHeadingLevel(md, options) {
var firstLevel = options.firstLevel;
if (typeof firstLevel === 'string') {
firstLevel = getHeadingLevel(firstLevel);
}
if (!firstLevel || isNaN(firstLevel)) {
return;
}
var levelOffset = firstLevel - 1;
if (levelOffset < 1 || levelOffset > 6) {
return;
}
md.core.ruler.push('adjust-heading-levels', function (state) {
var tokens = state.tokens;
for (var i = 0; i < tokens.length; i++) {
if (tokens[i].type !== 'heading_close') {
continue;
}
var headingOpen = tokens[i - 2];
var headingClose = tokens[i];
var currentLevel = getHeadingLevel(headingOpen.tag);
var tagName = 'h' + Math.min(currentLevel + levelOffset, 6);
headingOpen.tag = tagName;
headingClose.tag = tagName;
}
});
}
const md = markdownIt(markdownItOptions)
// .disable('code')
.use(markdownItHeadingLevel, { firstLevel: 2 })
.use(markdownItFootnote)
.use(markdownItAnchor, markdownItAnchorOptions)
.use(markdownItAttributes)
.use(markdownItSpan)
.use(markdownItAbbr)
.use(markdownItPlainText)
.use(markdownItContainer, 'lead') // Chapô in French
.use(markdownItContainer, 'encart_photo_du_jour')
.use(markdownItContainer, 'info')
.use(markdownItContainer, 'success')
.use(markdownItContainer, 'warning')
.use(markdownItContainer, 'error');
eleventyConfig.setLibrary('md', md);
// Add markdownify filter with shared Markdown-it configuration
eleventyConfig.addFilter('markdown', (markdownString) =>
md.render(markdownString)
);
// Add markdown paired shortcode with shared Markdown-it configuration
eleventyConfig.addPairedShortcode(
'markdown',
(markdownString, inline = null) =>
inline ? md.renderInline(markdownString) : md.render(markdownString)
);
// ------------------------------------------------------------------------
// Transforms
// ------------------------------------------------------------------------
if (process.env.NODE_ENV === 'production') {
const imagesResponsiver = require('eleventy-plugin-images-responsiver');
const imagesResponsiverConfig = require(path.join(
__dirname,
config.dir.src,
'_11ty/images-responsiver-config.js'
));
eleventyConfig.addPlugin(imagesResponsiver, imagesResponsiverConfig);
}
// ------------------------------------------------------------------------
// Excerpt
// ------------------------------------------------------------------------
function grayMatterExcerpt(file, options) {
const regex = /^.*::: lead(((?!(:::)).|\n)+):::.*$/gm;
let leadFound = false;
let excerptContent = '';
if ((leadMatches = regex.exec(file.content)) !== null) {
leadFound = true;
excerptContent = leadMatches[1];
} else {
excerptContent = file.content;
}
excerptContent = excerptContent.replace(/([^`])`([^`])/g, '$1#ONEFENCE#$2'); // keep single fences
md.render(excerptContent);
let excerpt = md.plainText.trim();
// remove short codes
excerpt = excerpt.replace(/{%(((?!(%})).|\n)+)%}/gm, '');
// remove nunjucks variables
excerpt = excerpt.replace(/{{(((?!(}})).|\n)+)}}/gm, '');
// remove nunjucks comments
excerpt = excerpt.replace(/{#(((?!(#})).|\n)+)#}/gm, '');
// remove inline CSS
excerpt = excerpt.replace(/<style>(((?!(<\/style>)).|\n)+)<\/style>/gm, '');
// remove JSON+LD
excerpt = excerpt.replace(
/<script type="application\/ld\+json">(((?!(<\/script>)).|\n)+)<\/script>/gm,
''
);
// add a dot at the end of headings
excerpt = excerpt.replace(/(<\/h[1-6]>)/gm, '. $1');
// remove HTML tags
excerpt = excerpt.replace(
/<\/?([a-z][a-z0-9]*)\b[^>]*>|<!--[\s\S]*?-->/gm,
''
);
// remove Markdown footnotes
excerpt = excerpt.replace(/(\[\^[^\]]+\])/gm, '');
// remove Markdown links without URL (from {% link_to %} for example)
excerpt = excerpt.replace(/\[([^\]]+)\]\(\)/gm, '$1');
// remove space before punctuation
excerpt = excerpt.replace(/ +(\.|,)/gm, '$1');
// restore fences
excerpt = excerpt.replace(/#ONEFENCE#/g, '`');
if (!leadFound && excerpt.length > 150) {
// Keep only 145 characters and an ellipsis if there was no lead
excerpt = excerpt.replace(/^(.{145}[^\s]*).*/gm, '$1') + '…';
}
file.excerpt = excerpt;
}
eleventyConfig.setFrontMatterParsingOptions({
excerpt: grayMatterExcerpt,
});
// ------------------------------------------------------------------------
// Eleventy configuration
// ------------------------------------------------------------------------
// https://github.com/11ty/eleventy/issues/893#issuecomment-606260541
eleventyConfig.setUseGitIgnore(false);
eleventyConfig.addWatchTarget('_site/js/');
eleventyConfig.addWatchTarget('_site/css/');
if (process.env.NODE_ENV === 'production') {
eleventyConfig.addPassthroughCopy(
path.join(
config.dir.src,
'{articles,billets,drafts,links,notes,talks}/**/*.{jpg,png,gif,svg,kmz,zip,css}'
)
);
} else {
// In development, don't use content from before 2020
eleventyConfig.addPassthroughCopy(
path.join(
config.dir.src,
'{articles,billets,drafts,links,notes,talks}/202*/**/*.{jpg,png,gif,svg,kmz,zip,css}'
)
);
eleventyConfig.ignores.add('./src/articles/200*/**');
eleventyConfig.ignores.add('./src/articles/201*/**');
}
eleventyConfig
.addPassthroughCopy(path.join(config.dir.src, 'assets'))
.addPassthroughCopy({ [path.join(config.dir.src, '_root')]: '/' })
.addPassthroughCopy(path.join(config.dir.src, 'offline/fallback.svg'))
.addPassthroughCopy(path.join(config.dir.src, 'tools/bookmarklets'));
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.setQuietMode(true);
return {
templateFormats: ['md', 'njk'],
markdownTemplateEngine: 'njk',
htmlTemplateEngine: false,
dataTemplateEngine: false,
passthroughFileCopy: true,
dir: {
output: config.dir.dist,
input: config.dir.src,
includes: '_includes',
layouts: '_layouts',
data: '_data',
},
};
};