forked from parcel-bundler/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
182 lines (154 loc) Β· 5.28 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
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const pluginTOC = require('eleventy-plugin-toc');
const posthtml = require('posthtml');
const urls = require('posthtml-urls');
const {urlJoin} = require('@parcel/utils');
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(syntaxHighlight, {
templateFormats: ["*"],
alwaysWrapLineHighlights: false,
});
eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addPlugin(pluginTOC);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.setTemplateFormats(["md", "njk", "css", "png", "svg", "mp4", "jpg"]);
eleventyConfig.addWatchTarget("./api/");
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
});
let md = markdownIt({
html: true,
}).use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.linkAfterHeader({
style: 'aria-labelledby'
}),
tabIndex: false
});
md.renderer.rules.table_open = () => '<div class="table-wrapper"><table>';
md.renderer.rules.table_close = () => '</table></div>'
eleventyConfig.setLibrary("md", md);
// ---------- Macros ----------
eleventyConfig.addFilter("sortDate", function (data) {
return [...data].sort((a, b) => b.date - a.date);
});
eleventyConfig.addFilter("pageUrl", function (page) {
return page.url;
});
eleventyConfig.addFilter("toISODate", function (date) {
return date.toISOString().replace(/T.*/, "");
});
eleventyConfig.addFilter("headingID", function (tag) {
return tag.toLowerCase().replace(/[^a-z]/gi, '') + '-heading';
});
eleventyConfig.addPairedShortcode("note", function (content) {
return `<figure class="well"><div>${content}</div></figure>`;
});
eleventyConfig.addPairedShortcode("warning", function (content) {
return `<figure class="well warning"><div>${content}</div></figure>`;
});
eleventyConfig.addPairedShortcode("error", function (content) {
return `<figure class="well error"><div>${content}</div></figure>`;
});
eleventyConfig.addPairedShortcode("sample", function (content, cmd, mode) {
let data = content
.split("\n")
.filter(Boolean)
.map((s) => JSON.parse(s));
return (
`<figure class="well sample ${
mode === "column" ? "column" : "row"
}">\n\n` +
(cmd ? `<div class="cmd"><code>${cmd}</code></div>` : "") +
`<div class="assets">\n` +
`${data
.map(
({ name, content }) =>
`<div class="asset">` +
(name ? `<em>${name}:</em>` : '') +
content +
`</div>`
)
.join("\n")}\n` +
`</div>\n` +
`</figure>`
);
});
eleventyConfig.addPairedShortcode("samplefile", function (content, name) {
return JSON.stringify({ name, content });
});
eleventyConfig.addPairedShortcode("migration", function (content) {
let assets = content
.split("\n")
.filter(Boolean)
.map((s) => JSON.parse(s));
if (assets.length === 1) assets.splice(0, 0, null);
return (
`<figure class="well warning migration">\n\n` +
`<div class="assets">\n` +
assets
.map((a) =>
a
? `<div class="asset">` +
(a.name ? `<em>${a.name}</em>:` : "") +
a.content +
`</div>`
: ""
)
.join(`<div class='arrow'></div>\n`) +
`</div>\n` +
`</figure>`
);
});
eleventyConfig.addFilter('excerpt', function(content) {
let excerpt = null;
// The start and end separators to try and match to extract the excerpt
const separatorsList = [
{ start: '<!-- Excerpt Start -->', end: '<!-- Excerpt End -->' },
{ start: '<p>', end: '</p>' }
];
separatorsList.some(separators => {
const startPosition = content.indexOf(separators.start);
const endPosition = content.indexOf(separators.end);
if (startPosition !== -1 && endPosition !== -1) {
excerpt = content.substring(startPosition + separators.start.length, endPosition).trim();
return true; // Exit out of array loop on first match
}
});
return excerpt;
});
eleventyConfig.addNunjucksAsyncFilter('feedHTML', function (content, base, callback) {
posthtml()
.use(urls({
eachURL(url, attr, element) {
if (/^https?:/.test(url)) {
return url;
}
// Generate absolute urls for <a> tags - Parcel doesn't rename them anyway.
// Otherwise, prepend the base URL so we get an absolute path.
let baseUrl = element === 'a' ? 'https://parceljs.org' + base : base;
return urlJoin(baseUrl, url);
}
}))
.use(tree => {
tree.walk(node => {
if (node && node.attrs && node.attrs.class === 'header-anchor') {
return null;
}
return node;
})
})
.process(content, {closingSingleTag: "slash"})
.then(html => callback(null, html.html));
});
return {
dir: {
input: "src",
},
markdownTemplateEngine: "njk",
};
};