-
Notifications
You must be signed in to change notification settings - Fork 36
/
.eleventy.js
180 lines (153 loc) · 5.25 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
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const embedYouTube = require("eleventy-plugin-youtube-embed");
const striptags = require("striptags");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const pluginTOC = require('eleventy-plugin-toc');
require("dotenv").config();
const AUTHORS = require("./src/data/AUTHORS.json");
const EXCERPT_LENGTH = 200;
function extractImage(article, prefix = "") {
if (!article.hasOwnProperty("templateContent")) {
console.warn(
'Failed to extract excerpt: Document has no property "templateContent".'
);
return "";
}
const match = article.templateContent.match(
/<img src="([^"]+)" alt="([^"]+)"/
);
if (match) {
return `<img src="${prefix}${match[1]}" alt="${match[2]}" loading="lazy"></img>`;
}
return "";
}
function extractExcerpt(article) {
if (!article.hasOwnProperty("templateContent")) {
console.warn(
'Failed to extract excerpt: Document has no property "templateContent".'
);
return null;
}
let excerpt = null;
const content = article.templateContent;
excerpt = striptags(content)
.substring(0, EXCERPT_LENGTH)
.replace(/^\s+|\s+$|\s+(?=\s)/g, "")
.trim()
.concat("...");
return excerpt;
}
module.exports = function (eleventyConfig) {
const markdownLibrary = markdownIt({
html: true,
breaks: true,
linkify: true
}).use(markdownItAnchor, {
permalink: true,
permalinkClass: "direct-link",
permalinkSymbol: "#"
});
eleventyConfig.setLibrary("md", markdownLibrary);
eleventyConfig.addPassthroughCopy("src/assets");
eleventyConfig.addPassthroughCopy("src/guides/**/*.{png,jpg,jpeg,gif,svg}");
eleventyConfig.addPassthroughCopy("src/sw.js");
eleventyConfig.addPassthroughCopy("src/manifest.json");
eleventyConfig.addPassthroughCopy("src/.well-known");
eleventyConfig.addPassthroughCopy("CNAME");
eleventyConfig.addFilter("processBrowserTagName", function (name) {
return name.split(":")[1];
});
eleventyConfig.addFilter("onlyTags", function (tags) {
return tags.filter((tag) => !tag.startsWith("browser:") && tag !== "tip");
});
eleventyConfig.addFilter("onlyBrowsers", function (browsers) {
return browsers.filter((browser) => browser.startsWith("browser:"));
});
eleventyConfig.addShortcode("excerpt", (article) => extractExcerpt(article));
eleventyConfig.addShortcode("mainImage", (article) => extractImage(article));
eleventyConfig.addShortcode("lastTipDate", (collection) => {
let lastDate = null;
for (const post of collection) {
if (!lastDate || post.date > lastDate) {
lastDate = post.date;
}
}
return lastDate.toISOString();
});
eleventyConfig.addShortcode("formatAuthors", (authors) => {
const authorArray = Array.isArray(authors)
? authors
: authors.split(",").map((a) => a.trim());
return authorArray
.map((author) =>
AUTHORS[author] ? `<a href="${AUTHORS[author]}">${author}</a>` : author
)
.join(", ");
});
eleventyConfig.addPlugin(syntaxHighlight);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(embedYouTube);
eleventyConfig.addPlugin(pluginTOC, {
tags: ['h2'],
wrapper: 'div'
});
eleventyConfig.addTransform("fix-tip-urls", function (content) {
if (this.inputPath.includes("/src/tips/en/")) {
// Replace all relative links to other tips with their absolute links.
// This is needed because we want relative file links in dev, in order
// to benefit from markdown preview. But we want absolute links in prod.
content = content.replace(
/href="\.\/([^.]+)\.md"/g,
'href="/tips/en/$1"'
);
// Also replace all relative image links with their absolute versions.
content = content.replace(
/src="\.\.\/\.\.\/assets\/img\//g,
'src="/assets/img/'
);
}
return content;
});
// A hacky way to get all tips. We don't actually use this collection.
// We just use this callback to store the tips in an array.
const allTips = [];
eleventyConfig.addCollection("allTips", function (collectionApi) {
return collectionApi.getAll().filter(function (item) {
allTips.push(item);
return item.inputPath.includes("/src/tips/en/");
});
});
eleventyConfig.addShortcode("insertTip", (slug) => {
const tipData = allTips.find((tip) => tip.fileSlug === slug);
if (!tipData) {
return `Tip not found: ${slug}`;
}
// We're generating HTML in MD, so we need to avoid leading
// whitespaces at the beginning of each line. Otherwise, it will
// be rendered as code.
return [
`<div class="tip tip-in-guide">`,
`<span class="tip-title">See also: <a href="/tips/en/${slug}">${tipData.data.title}</a></span>`,
`<a href="/tips/en/${slug}" class="tip-image">${extractImage(
tipData,
"../"
)}</a>`,
`<div class="tip-excerpt">`,
extractExcerpt(tipData),
` <a href="/tips/en/${slug}">Read more</a>`,
`</div>`,
`</div>`,
].join("");
});
return {
dir: {
input: "src",
output: "dist",
data: "data",
layouts: "layouts",
includes: "includes",
},
};
};