-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy patheleventy.config.js
More file actions
126 lines (100 loc) · 3.88 KB
/
eleventy.config.js
File metadata and controls
126 lines (100 loc) · 3.88 KB
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
const markdownIt = require('markdown-it');
const Prism = require('prismjs');
const loadLanguages = require('prismjs/components/');
const LINEBREAK_PLACEHOLDER = '---linebreak-placeholder---';
module.exports = eleventyConfig => {
process.env.TZ = 'UTC';
eleventyConfig.addPassthroughCopy('asf.md');
eleventyConfig.addPassthroughCopy('static');
eleventyConfig.setLiquidOptions({
jekyllInclude: true,
});
// use e.g. /learn.html in preference to /learn/
eleventyConfig.addGlobalData('permalink', '/{{ page.filePathStem }}.html');
eleventyConfig.addCollection('guides', collectionApi => {
return collectionApi
.getFilteredByTag('guides')
.sort((a, b) => a.data.index - b.data.index);
});
eleventyConfig.addCollection('pages', collectionApi => {
// zero-indexed, but skip page 1, as it's served at /blog/
const pageCount = Math.ceil(collectionApi.getFilteredByTag('posts').length / 5) - 1;
const blogPages = Array.from(
{ length:pageCount },
(_, n) => ({
url: `/blog/page${n+2}/`,
}),
);
return [
...collectionApi.getAll().filter(item => !item.data.tags),
...blogPages,
];
});
eleventyConfig.addCollection('posts', collectionApi => {
return collectionApi
.getFilteredByTag('posts')
.sort((a, b) => b.date - a.date || b.inputPath.localeCompare(a.inputPath));
});
eleventyConfig.addFilter("formatInputPath", function(value) {
return value.replace("./", "").replace("_guides", "guides").replace("_posts", "post");
});
eleventyConfig.addFilter('first_paragraph', function(content) {
const marker = '</p>';
const idx = content.indexOf(marker);
if(idx === -1) return content;
return content.substring(0, idx + marker.length);
});
eleventyConfig.addFilter('liquid', function(content) {
if(!this.liquid) return content;
return this.liquid.parseAndRender(content, this.context);
});
const renderMarkdown = initMarkdown();
// Re-defined markdown-it lib to prevent eleventy messing with internals.
// See: https://github.com/11ty/eleventy/issues/2438
eleventyConfig.setLibrary('md', { render:renderMarkdown });
eleventyConfig.addFilter('markdown', renderMarkdown);
eleventyConfig.addPairedShortcode('markdown', renderMarkdown);
eleventyConfig.addPairedShortcode('highlight', wrapCode);
eleventyConfig.addTransform('revert-linebreak-markers', function(content) {
console.log('revert-linebreak-markers', this.outputPath);
if(!this.outputPath?.endsWith('.html')) return content;
return content.replaceAll(new RegExp(`^${LINEBREAK_PLACEHOLDER}$`, 'gm'), '');
});
return {
dir: {
includes: '_includes',
layouts: '_layouts',
},
};
};
// Ensure consistent code style across:
// * markdown indented code blocks
// * markdown "fenced" code blocks
// * liquid {% highlight ... %} code blocks
function initMarkdown() {
const md = markdownIt({
html: true,
});
// Indented code blocks seem to introduce parsing differences across
// markdownversions, and inconsistencies with whitespace introduced
// by liquid templates. The simplest option is to disable them, and
// require code "fences" (```) instead.
md.disable('code');
md.renderer.rules.fence = (tokens, idx, options, env, slf) => {
const { content, info } = tokens[idx];
const lang = info ? info.trim().split(/\s/)[0] : '';
return wrapCode(content, lang);
};
return md.render.bind(md);
}
function wrapCode(code, lang) {
let html = code.trim();
if(lang) {
loadLanguages([lang]);
html = Prism.highlight(html, Prism.languages[lang], lang);
}
// prevent markdown interpreter from converting multiple
// linebreaks in code examples into <p>...</p>
html = html.replaceAll(/\n(?=\n)/g, `\n${LINEBREAK_PLACEHOLDER}`);
return `<figure class="highlight"><pre data-copybutton><code class="language-${lang}">${html}</code></pre></figure>`;
}