-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
173 lines (144 loc) · 4.83 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
const mdContainer = require("markdown-it-container");
const CleanCSS = require("clean-css");
const htmlmin = require("html-minifier");
const externalLinks = require("@aloskutov/eleventy-plugin-external-links");
const workbox = require("workbox-build");
const htmlParser = require("node-html-parser");
const qrCode = require("qrcode");
const slugify = require("slugify");
module.exports = (config) => {
config.addPassthroughCopy({ "src/static/*": "/" });
config.addPassthroughCopy({ "src/static/.well-known/*": "/.well-known/" });
config.addShortcode("code", require("./shortcodes/code"));
config.addAsyncShortcode("image", require("./shortcodes/image"));
config.addShortcode("strava", require("./shortcodes/strava"));
config.addAsyncShortcode("barChart", require("./shortcodes/bar-chart"));
config.addShortcode("tags", require("./shortcodes/tags.js"));
config.addFilter("cssmin", function (code) {
return new CleanCSS({}).minify(code).styles;
});
config.addFilter("trimSlashes", function (str) {
return str.replace(/^\/+|\/+$/g, "");
});
config.addFilter("slugifyTag", function (str) {
return slugify(str.replace(/\./g, "-"), {
remove: /[*+~()'"!:@]/g,
lower: false,
});
});
const related = require("eleventy-plugin-related").related({
serializer: ({ data: { title, excerpt, tags } }) => [
[title, excerpt].join(" "),
(tags || []).join(" "),
],
weights: [1, 1],
});
config.addFilter("relatedPosts", function (doc, docs) {
return related(doc, docs).filter(({ relative }) => relative > 0.15);
});
config.addFilter("qrcode", async function (value) {
return await qrCode.toDataURL(value);
});
config.addTransform("htmlmin", (content, outputPath) => {
if (outputPath.endsWith(".html")) {
return htmlmin.minify(content, {
collapseWhitespace: true,
removeComments: true,
useShortDoctype: true,
});
}
if (outputPath.endsWith(".xml")) {
// remove empty lines
return content.replace(/^\s*[\r\n]/gm, "");
}
return content;
});
config.addFilter("simplifyCodeHighlightingForRSS", function (value) {
const prefix = "language-";
const root = htmlParser.parse(value.replace(/ /g, " "));
root.querySelectorAll("pre").forEach((el) => {
const language = [...el.classList.values()].filter((c) =>
c.startsWith(prefix),
)[0];
el.innerHTML = `<code class="${language}">${el.innerText
.replace(/<\/*(span|code).*?>/g, "")
.replace(/<br>/g, " ")}</code>`;
});
return root.toString();
});
config.addFilter("limit", function (arr, limit) {
return arr.slice(0, limit);
});
const markdownIt = new require("markdown-it")({
typographer: true,
linkify: true,
html: true,
});
markdownIt.use(mdContainer, "note");
markdownIt.use(mdContainer, "tldr");
// https://github.com/markdown-it/markdown-it-emoji/blob/master/lib/data/full.json
markdownIt.use(require("markdown-it-emoji"));
markdownIt.use(require("markdown-it-anchor"));
config.setLibrary("md", markdownIt);
config.addFilter("markdownit", function (value) {
return markdownIt.renderInline(value);
});
config.addPlugin(require("eleventy-plugin-nesting-toc"), {
tags: ["h2", "h3", "h4"],
});
config.addPlugin(require("@11ty/eleventy-plugin-syntaxhighlight"));
config.addPlugin(require("@11ty/eleventy-plugin-rss"));
config.addPlugin(require("eleventy-plugin-time-to-read"));
config.addPlugin(externalLinks, {
url: "https://justin.poehnelt.com",
target: "_self",
overwrite: false,
});
config.addFilter("dateDisplay", require("./filters/date-display.js"));
config.setBrowserSyncConfig({
files: ["public/**/*"],
open: true,
});
config.setDataDeepMerge(true);
config.addWatchTarget("./public/assets/*");
config.addWatchTarget("./shortcodes/*");
config.on("eleventy.after", async () => {
const options = {
cacheId: "sw",
skipWaiting: true,
clientsClaim: true,
swDest: `public/sw.js`,
globDirectory: "public",
globPatterns: [
"**/*.{css,js,mjs,map,jpg,png,gif,webp,ico,svg,woff2,woff,eot,ttf,otf,ttc,json}",
],
runtimeCaching: [
{
urlPattern: /\/$/,
handler: "NetworkFirst",
},
{
urlPattern: /\.html$/,
handler: "NetworkFirst",
},
{
urlPattern:
/^.*\.(jpg|png|mp4|gif|webp|ico|svg|woff2|woff|eot|ttf|otf|ttc|json)$/,
handler: "StaleWhileRevalidate",
},
],
};
await workbox.generateSW(options);
});
return {
markdownTemplateEngine: "njk",
pathPrefix: require("./src/_data/site.json").baseUrl,
dir: {
input: "src",
output: "public",
includes: "_includes",
layouts: "_layouts",
data: "_data",
},
};
};