-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
186 lines (159 loc) · 4.42 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
const { format } = require("date-fns/format");
const { de } = require("date-fns/locale/de");
const markdown = require("markdown-it");
const markdownIt = require("markdown-it")();
const markdownItAnchor = require("markdown-it-anchor");
const cheerio = require("cheerio");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const htmlmin = require("html-minifier-terser");
const paths = {
input: "src",
output: process.env.npm_package_config_outdir,
};
const prod = process.env.NODE_ENV === "production";
const not =
fn =>
(...args) =>
!fn(...args);
const isDraft = post =>
post.data.draft === "" ? true : Boolean(post.data.draft);
module.exports = function (eleventyConfig) {
eleventyConfig.setTemplateFormats(["njk", "hbs", "md", "html", "txt"]);
eleventyConfig.addPassthroughCopy(
"./src/**/*.{png,jpg,jpeg,webp,avif,mp4,xml}",
);
eleventyConfig.addPassthroughCopy({ "./public/static": "static" });
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addTransform("htmlmin", function (content, outputPath) {
if (outputPath && outputPath.endsWith(".html")) {
return htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
});
}
return content;
});
eleventyConfig.setFrontMatterParsingOptions({
excerpt: true,
excerpt_alias: "excerpt",
excerpt_separator: "<!-- more -->",
});
eleventyConfig.setLibrary(
"md",
markdown({
html: true,
breaks: false,
linkify: true,
}).use(markdownItAnchor, {
slugify: str => slugify(str),
tabIndex: false,
}),
);
eleventyConfig.addHandlebarsHelper("debug", function (...args) {
console.log(...args);
});
eleventyConfig.addHandlebarsHelper("date", function (date, formatFunction) {
return format(date, formatFunction, { locale: de });
});
// check if a given value equals some of the following values
// e.g. ` {{#eq somethingToCheck "batman" "joker"}} ... {{/eq}}`
eleventyConfig.addHandlebarsHelper("eq", function (...args) {
const [first, ...rest] = args;
const [options] = rest.splice(-1);
const equalsAtLeastOne = Array.from(rest).some(value => first === value);
return equalsAtLeastOne ? options.fn(this) : null;
});
eleventyConfig.addHandlebarsHelper("markdownToHtml", function (text) {
return markdownIt.render(text);
});
eleventyConfig.addCollection("blogposts", function (collection) {
const allBlogPosts = collection
.getFilteredByGlob("./src/neuigkeiten/**/*.md")
.reverse();
return prod ? allBlogPosts.filter(not(isDraft)) : allBlogPosts;
});
eleventyConfig.addHandlebarsHelper("toc", function (data) {
function renderNode(node) {
let childrenHtml = "";
if (node.children.length > 0) {
childrenHtml = `<ul>${node.children
.map(child => renderNode(child))
.join("")}</ul>`;
}
let html = "";
html += `<li class="toc-menu-item"><a href="${node.node.link}">${node.node.text}</a>${childrenHtml}</li>`;
return html;
}
const $ = cheerio.load(data.content);
const headings = [...$("h2, h3")];
const tree = tocTree(headings, 1, $);
return tree.length === 0
? ""
: `<ul class="toc-menu">${tree
.map(node => renderNode(node))
.join("")}</ul>`;
});
eleventyConfig.addWatchTarget("src/static/js/**/*.js");
eleventyConfig.addWatchTarget("src/static/css/**/*.css");
return {
dir: {
input: paths.input,
output: paths.output,
},
};
};
function tocTree(headings, parentLevel, $) {
let nodes = [];
let cursor = 0;
while (cursor < headings.length) {
const heading = headings[cursor];
const level = headingLevel(heading.name);
if (level <= parentLevel) {
break;
}
const nextHeadings = Array.from(headings).splice(cursor + 1);
const children = tocTree(nextHeadings, level, $);
nodes.push({
node: {
// do not use slug and/or title as attribute names.
// this collides with markdown-it-anchor
link: "#" + heading.attribs.id,
text: $(heading).text(),
},
children,
});
cursor += children.length;
cursor++;
}
return nodes;
}
function headingLevel(h) {
switch (h) {
case "h1":
return 1;
case "h2":
return 2;
case "h3":
return 3;
case "h4":
return 4;
case "h5":
return 5;
case "h6":
return 6;
}
}
function slugify(str) {
return str
.toLowerCase()
.replace(/ä/g, "ae")
.replace(/ö/g, "oe")
.replace(/ü/g, "ue")
.replace(/ß/g, "ss")
.replace(/[():?!.,$§]/g, "")
.replace(/\s+/g, "-")
.replace(/--*/g, "-")
.replace(/^-/, "")
.replace(/-$/, "");
}