/
.eleventy.js
201 lines (165 loc) · 6.06 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
const chalk = require("chalk");
const htmlmin = require("html-minifier");
const CleanCSS = require("clean-css");
const HumanReadable = require('human-readable-numbers');
const syntaxHighlightPlugin = require("@11ty/eleventy-plugin-syntaxhighlight");
const rssPlugin = require("@11ty/eleventy-plugin-rss");
const inclusiveLanguagePlugin = require("@11ty/eleventy-plugin-inclusive-language");
const cfg = require("./_data/config.json");
const avatarExceptions = require("./_data/avatarFileMap.json");
module.exports = function(eleventyConfig) {
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addPlugin(syntaxHighlightPlugin, {
templateFormats: "md"
});
eleventyConfig.addPlugin(rssPlugin);
eleventyConfig.addPlugin(inclusiveLanguagePlugin);
eleventyConfig.addPairedShortcode("callout", function(content, level = "warn") {
return `<div class="elv-callout elv-callout-${level}">${content}</div>`;
});
eleventyConfig.addShortcode("emoji", function(emoji, alt = "") {
return `<span aria-hidden="true">${emoji}</span>` +
(alt ? `<span class="sr-only">${alt}</span>` : "");
});
eleventyConfig.addShortcode("avatar", function(filename, linkUrl, text = "") {
if(!filename) {
return '<span class="avatar"></span>';
}
let twitterName;
if( filename.endsWith(".jpg") || filename.endsWith(".png") ) {
twitterName = filename.substr(0, -4);
} else {
twitterName = filename;
filename += ".jpg";
}
if( avatarExceptions[twitterName] ) {
filename = avatarExceptions[twitterName];
}
return (linkUrl ? `<a href="${linkUrl}">` : "") +
`<img src="/img/avatars/${filename}" class="avatar" alt="@${twitterName} Twitter Photo">` +
text +
(linkUrl ? `</a>` : "");
});
eleventyConfig.addShortcode("codetitle", function(title, subtitle = "Filename") {
return `<div class="codetitle codetitle-left"><b>${subtitle} </b>${title}</div>`;
});
eleventyConfig.addPairedShortcode("minilink", function(text, href) {
return `<a href="${href}" class="minilink minilink-lower">${text}</a>`;
});
eleventyConfig.addShortcode("addedin", function(version, tag, extraClass) {
if( typeof version !== "string" ) {
tag = version.tag;
version = version.version;
}
tag = tag || "span";
return `<${tag} class="minilink minilink-addedin${extraClass ? ` ${extraClass}`: ""}">New in v${version}</${tag}>`;
});
eleventyConfig.addPassthroughCopy("css/fonts");
eleventyConfig.addPassthroughCopy("img");
eleventyConfig.addPassthroughCopy("favicon.ico");
eleventyConfig.addFilter("cssmin", function(code) {
if(process.env.ELEVENTY_PRODUCTION) {
return new CleanCSS({}).minify(code).styles;
} else {
return code;
}
});
eleventyConfig.addFilter("humanReadableNum", function(num) {
return HumanReadable.toHumanString(num);
});
eleventyConfig.addFilter("sortMenu", function(collection, sortOrder) {
if(!sortOrder) {
return collection;
}
let order = sortOrder.map(path => `./docs/${path}.md`);
return collection.sort(function(a, b) {
let firstIndex = order.indexOf(a.inputPath);
let secondIndex = order.indexOf(b.inputPath);
if( firstIndex === -1 ) return 1;
if( secondIndex === -1 ) return -1;
return firstIndex - secondIndex;
});
});
eleventyConfig.addShortcode("templatelangs", function(languages, page, whitelist, anchor, isinline) {
let parentTag = isinline ? "span" : "ul";
let childTag = isinline ? "span" : "li";
return `<${parentTag} class="inlinelist">${languages.filter(lang => !whitelist ? true : whitelist.indexOf(lang.ext) > -1).map(lang => `<${childTag} class="inlinelist-item${page.url == lang.url ? ' active' : ''}"><a href="${lang.url}${anchor || ''}">${lang.name} <code>*.${lang.ext}</code></a></${childTag}>`).join(" ")}</${parentTag}>`;
});
eleventyConfig.addShortcode("latestVersion", function(versions, config) {
for( let version of versions ) {
if( version.tag === "LATEST" ) {
continue;
}
if( !config.prerelease && version.prerelease ) {
continue;
}
return version.tag;
}
});
eleventyConfig.addFilter("orphanWrap", str => {
let splitSpace = str.split(" ");
let after = "";
if( splitSpace.length > 2 ) {
after += " ";
// TODO strip HTML from this?
let lastWord = splitSpace.pop();
let secondLastWord = splitSpace.pop();
after += `${secondLastWord} ${lastWord}`;
}
return splitSpace.join(" ") + after;
});
// Thanks to https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
eleventyConfig.addFilter("shuffle", arr => {
let a = arr.slice(0);
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
});
eleventyConfig.addShortcode("addToSampleSites", function() {
return `<a href="https://github.com/11ty/11ty.io/issues/new?title=I+built+a+site+with+Eleventy!"><strong>Want to add your site to this list?</strong></a>`;
});
eleventyConfig.addCollection("quicktipssorted", function(collection) {
return collection.getFilteredByTag("quicktips").sort(function(a, b) {
return parseInt(a.data.tipindex, 10) - parseInt(b.data.tipindex, 10);
});
});
/* Markdown */
let markdownIt = require("markdown-it");
let markdownItAnchor = require("markdown-it-anchor");
let options = {
html: true,
breaks: true,
linkify: true
};
let opts = {
permalink: true,
slugify: function(s) {
return encodeURIComponent(String(s).replace(/New\ in\ v\d+\.\d+\.\d+/, '').trim().toLowerCase().replace(/\s+/g, '-'));
},
permalinkClass: "direct-link",
permalinkSymbol: "#",
level: [1,2,3,4]
};
eleventyConfig.setLibrary("md", markdownIt(options).use(markdownItAnchor, opts));
if( cfg.minifyHtml ) {
eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
if( process.env.ELEVENTY_PRODUCTION && outputPath.endsWith(".html") ) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
}
return {
templateFormats: ["html", "njk", "md"],
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk",
dataTemplateEngine: false
};
};