generated from jonheslop/static-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path.eleventy.js
257 lines (230 loc) · 8.04 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
const fs = require("fs");
const eleventyNavigationPlugin = require("@11ty/eleventy-navigation");
const pluginTOC = require("eleventy-plugin-toc");
const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const metagen = require("eleventy-plugin-metagen");
const slugify = require("slugify");
const { parse } = require("node-html-parser");
const slugifyCustom = (s) =>
slugify(s, { lower: true, remove: /[*+~.()'"!:@]/g });
const hash = (s) =>
s.split("").reduce((a, b) => {
a = (a << 5) - a + b.charCodeAt(0);
return a & a;
}, 0);
// widont is a function that takes a string and replaces the space between the last two words with a non breaking space. This stops typographic widows forming
const widont = (string) => {
return string.split(" ").length > 2
? string.replace(/\s([^\s<]+)\s*$/, "\u00A0$1")
: string;
};
module.exports = (eleventyConfig) => {
eleventyConfig.addWatchTarget("./_tmp/style.min.css");
eleventyConfig.addPassthroughCopy({
"./_tmp/style.min.css": "./docs/static/style.min.css",
});
eleventyConfig.addPassthroughCopy({ img: "/docs/static/img" });
eleventyConfig.addPassthroughCopy({ js: "/docs/static/js" });
eleventyConfig.addPassthroughCopy({ video: "/docs/static/video" });
eleventyConfig.addPlugin(pluginSyntaxHighlight, {
alwaysWrapLineHighlights: true,
trim: true,
});
eleventyConfig.addPlugin(eleventyNavigationPlugin);
eleventyConfig.addPlugin(pluginTOC, {
tags: ["h2", "h3"],
ul: true,
wrapperClass: "toc-list",
});
eleventyConfig.addPlugin(metagen);
eleventyConfig.addFilter("widont", widont);
eleventyConfig.addFilter("extractFirstPara", (content) => {
const paragraphs = content.match(/<p>(.*?)<\/p>/);
if (paragraphs) {
const text = parse(paragraphs[0]).text.substr(0, 160);
return text.slice(-1) === "."
? text
: text.substr(0, text.lastIndexOf(" ")) + "…";
}
});
eleventyConfig.addGlobalData("currentYear", new Date().getFullYear());
/* Markdown Plugins */
const markdownIt = require("markdown-it")({
html: true,
breaks: true,
linkify: true,
typographer: true,
});
const markdownItAnchor = require("markdown-it-anchor");
const opts = {
level: [2, 3, 4, 5],
permalink: true,
permalinkClass: "link bn",
permalinkSymbol: "∞",
permalinkBefore: true,
slugify: slugifyCustom,
};
markdownIt.renderer.rules.image = (tokens) => {
// Sometimes this retuns things that arn’t images so we need to filter
try {
const [filtered] = tokens.filter((token) => token.type === "image");
let src = filtered.attrs[filtered.attrIndex("src")][1];
let width = "";
let height = "";
if (src.match(/^http/) === null) {
src = filtered.attrs[filtered.attrIndex("src")][1].replace(".", "");
width = filtered.attrs[filtered.attrIndex("width")][1] || "";
height = filtered.attrs[filtered.attrIndex("height")][1] || "";
}
const alt =
filtered.attrs[filtered.attrIndex("alt")][1] ||
filtered.children[0].content;
return `<figure class="mh0 mv5 pa0 border-box bg-snow-light mw6">
<img class="db" src="/docs/static${src}" alt="${alt}" width="${width}" height="${height}" loading="lazy" />
</figure>`;
} catch (e) {
return `<span class="code radish">Failed to parse image, it could be the path to the image file is incorrect</span>`;
console.error("can’t find the image");
}
};
markdownIt.core.ruler.push("widont", (state) => {
const { tokens } = state;
tokens
.filter((token) => token.type === "heading_open")
.forEach((token) => {
const textToWidont = tokens[tokens.indexOf(token) + 1].children.filter(
(token) => token.type === "text"
);
// Aggregate the next token children text.
const title = textToWidont.reduce((acc, t) => acc + t.content, "");
// Replace content with widont applied text
textToWidont.forEach((token) => {
if (token.type === "code_inline") token.content = widont(title);
});
});
});
markdownIt
.use(markdownItAnchor, opts)
.use(require("markdown-it-imsize"), { autofill: true });
eleventyConfig.setLibrary("md", markdownIt);
// Browsersync Overrides
eleventyConfig.setBrowserSyncConfig({
callbacks: {
ready: function (err, browserSync) {
const content_404 = fs.readFileSync("_site/404.html");
browserSync.addMiddleware("*", (req, res) => {
// Provides the 404 content without redirect.
res.write(content_404);
res.end();
});
},
},
ui: false,
ghostMode: false,
});
eleventyConfig.addPairedShortcode(
"snippets",
(content, languages, method = false) => {
const languageMap = {
rb: "Ruby",
js: "JavaScript",
node: "Node.js",
php: "PHP",
laravel: "Laravel",
laravelecho: "Laravel Echo",
go: "Go",
py: "Python",
c: ".NET",
java: "Java",
bash: "Pusher CLI",
swift: "Swift",
objc: "Objective-C",
http: "http",
kotlin: "Kotlin",
};
return `<div class="bg-snow-light br2 tabbed-snippets" data-method="${method}">
<nav class="ph3 bb b--smoke overflow-auto scrollbar--light">
<ul class="flex">
${languages
.map(
(language, i) =>
`<li class="mh1"><button class="bn bg-snow-light sans-serif fw6 pt3 pb2 ph2 dragonfruit pointer pa0" data-snippet="language-${language}" data-index="${i}" aria-pressed="${
i === 0
}">${languageMap[language] || language}</button></li>`
)
.join("\n")}
</ul>
</nav>
${content}</div>`;
}
);
eleventyConfig.addPairedShortcode(
"parameter",
(
content,
name,
type = null,
required = null,
language = null,
show = true
) => {
const typeLabel = `<span class="slate fw6 ml4 f6">${type}</span>`;
const requiredLabel = `<span class="pumpkin fw6 ml4 f6">Required</span>`;
const optionalLabel = `<span class="slate fw6 ml4 f6">Optional</span>`;
const slug = slugifyCustom(`${name}-${hash(content)}`);
return `<dl class="parameter-block ${show ? "" : "dn"}" ${
language !== null ? `data-language="${language}"` : ""
} id="${slug}">
<dt class="flex items-center mb3">
<a class="link bn mr4" href="#${slug}">∞</a>
<span class="code ma0 f4 lh-title" style="font-weight: 400; margin: 0;">${name}</span>${
type !== null ? typeLabel : ""
}
${
required === null ? "" : required ? requiredLabel : optionalLabel
}</dt>
<dd class="ma0">${content}</dd></dl>`;
}
);
eleventyConfig.addPairedShortcode(
"conditionalContent",
(content, language = null, show = true) => {
return `<div class="parameter-block ${
show ? "" : "dn"
}" data-language="${language}">${content}</div>`;
}
);
eleventyConfig.addPairedShortcode("methodwrap", (content) => {
return `<div class="method-wrapper">${content}</div>`;
});
eleventyConfig.addFilter("jsonify", (text) => {
return JSON.stringify(text).replace(/(?:\\n\s*){2,}/g, "\\n");
});
eleventyConfig.addFilter("algExcerpt", (text) => {
return text
.replace(/<code class="language-.*?">.*?<\/code>/gs, "")
.replace(/<.*?>/g, "")
.substring(0, 8000);
});
// Some pages are blank and exist just to show up
// in the navigation (e.g. beams/concepts/index.md)
// and we don’t want to index them in search
eleventyConfig.addCollection("algolia", (collectionApi) => {
return collectionApi
.getFilteredByTag("docs")
.filter((item) => item.url !== false);
});
eleventyConfig.setUseGitIgnore(false);
return {
templateFormats: ["md", "html", "njk"],
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
passthroughFileCopy: true,
dir: {
input: ".",
includes: "_includes",
output: "_site",
},
};
};