-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.eleventy.js
248 lines (209 loc) · 6.92 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
const yaml = require("js-yaml");
const _ = require('lodash');
const { DateTime } = require("luxon");
const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const htmlmin = require("html-minifier");
const crypto = require('crypto');
const markdownIt = require('markdown-it')
const markdownItAnchor = require('markdown-it-anchor')
const pluginTOC = require('eleventy-plugin-nesting-toc')
const pluginSrcsetImg = require('eleventy-plugin-srcset')
const mdOptions = {
html: true,
breaks: true,
linkify: true,
typographer: true
}
const mdAnchorOpts = {
permalink: true,
permalinkClass: 'anchor',
permalinkSymbol: '#',
level: [1, 2, 3, 4]
}
module.exports = function (eleventyConfig) {
// Disable automatic use of your .gitignore
eleventyConfig.setUseGitIgnore(false);
// Merge data instead of overriding
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addCollection("posts_de", function (collection) {
return collection.getFilteredByGlob(["./src/de/blog/posts/*.md","./src/de/blog/posts/*.adoc","./src/de/blog/posts/*.html"]);
});
eleventyConfig.addCollection("posts_en", function (collection) {
return collection.getFilteredByGlob(["./src/en/blog/posts/*.md","./src/en/blog/posts/*.adoc","./src/blog/en/posts/*.html"]);
});
eleventyConfig.addFilter("tagFilter", function(tags, tagname) {
return _.find(tags, { 'name': tagname });;
});
eleventyConfig.addFilter("sortPostsByDate", function(posts, order) {
if(order === "desc") {
return [...posts].reverse();
}
return posts;
});
eleventyConfig.addFilter("filterPostsByAuthor", function(posts, author) {
return _.filter(posts, function(post) { return post.data.author == author; });
});
eleventyConfig.addFilter("filterPostsByTag", function(posts, tag) {
return _.filter(posts, function(post) { return _.includes(post.data.tags, tag)});
});
eleventyConfig.addFilter("defaultIfEmpty", function(value, defaultValue) {
if(typeof value !== 'undefined') {
return value;
} else {
return defaultValue;
}
});
eleventyConfig.addFilter("debug", function(value) {
console.log(JSON.stringify(value));
return value;
});
eleventyConfig.addFilter("sortByName", function(authors) {
return _.sortBy(authors, ['name']);
});
eleventyConfig.addFilter("toPostHash", function(data) {
if(data) {
if(data instanceof Array)
data = data.join("-");
if(data instanceof Date)
data = data.toISOString();
return crypto.createHash("sha256").update(data).digest("hex").substring(0, 12);
}
return "";
});
eleventyConfig.addFilter("calculatePriority", function(path) {
if(path.includes("blog/post"))
return 0.7;
return Math.round((1 / Math.max(1, path.length / 3)) * 10) / 10 ;
});
//Lesezeit = Anzahl Wörter / 180 Wörter pro Min, aber min 2 Minuten
eleventyConfig.addFilter("readingtime", function(text) {
return Math.max(Math.ceil(text.split(" ").length / 180),2);
});
eleventyConfig.addFilter("dateToPath", function(date) {
formatDate = function(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('/');
}
if(date)
return formatDate(date);
return null;
});
eleventyConfig.addFilter("getLanguageMatch", function(items, page_url, language_code, page_postHash) {
let result = "/" + language_code + "/";
for (let item of items) {
let data = item.data;
if(data && data.page && data.page.url && data.locale) {
if(data.locale == language_code) {
if(data.page.url.length >= 4) {
if (data.page.url.substring(3) == page_url.substring(3)) {
result = data.page.url;
break;
}
}
if(page_postHash && data.postHash) {
if(data.postHash == page_postHash) {
result = data.page.url;
break;
}
}
}
}
}
return result;
});
eleventyConfig.setLibrary(
'md',
markdownIt(mdOptions)
.use(markdownItAnchor, mdAnchorOpts)
// .use(markdownItHighlightJS)
);
eleventyConfig.addPlugin(pluginTOC, {
tags: ['h2', 'h3', 'h4'],
wrapper: 'span',
ignoredElements: ['a'],
flat: false
});
eleventyConfig.addPlugin( pluginSrcsetImg, {
srcsetWidths: [ 320, 480, 640, 960, 1280, 1600 ],
autoselector: 'article img',
fallbackWidth: 640,
createCaptions: true,
resizeOriginal: true,
dirs: {
temp: "./.tmp/",
input: "./src/",
output: "./_site/"
}
});
// date filter (localized)
eleventyConfig.addNunjucksFilter("localizedDate", function (date, localeRegion) {
localeRegion = localeRegion ? localeRegion : "de-DE";
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
const result = new Date(date).toLocaleDateString(localeRegion, options);
// console.log('Processing Date', result);
return result;
});
eleventyConfig.addNunjucksFilter("getLocalizedTag", function (tag, categories, locale) {
const matchingCats = categories.filter(cat => {
return cat.name === tag;
});
if (matchingCats.length === 0) {
return tag;
}
return matchingCats[0][locale];
});
eleventyConfig.addFilter("filterCategory", function (postlist, name) {
return postlist.filter(post => post.data.tags.includes(name));
});
// Syntax Highlighting for Code blocks
eleventyConfig.addPlugin(syntaxHighlight);
// To Support .yaml Extension in _data
// You may remove this if you can use JSON
eleventyConfig.addDataExtension("yaml", (contents) =>
yaml.load(contents)
);
// Add Tailwind Output CSS as Watch Target
eleventyConfig.addWatchTarget("./_tmp/static/css/style.css");
// Copy Static Files to /_Site
eleventyConfig.addPassthroughCopy({
"./_tmp/static/css/style.css": "./static/css/style.css",
"./src/admin/config.yml": "./admin/config.yml",
"./node_modules/alpinejs/dist/alpine.js": "./static/js/alpine.js",
"./node_modules/prismjs/themes/prism-tomorrow.css":
"./static/css/prism-tomorrow.css",
});
// Copy Image Folder to /_site
eleventyConfig.addPassthroughCopy("./src/static/img");
// Copy favicon to route of /_site
eleventyConfig.addPassthroughCopy("./src/favicon.ico");
// Minify HTML
eleventyConfig.addTransform("htmlmin", function (content, outputPath) {
// Eleventy 1.0+: use this.inputPath and this.outputPath instead
if (outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
eleventyConfig.addTemplateFormats("adoc");
eleventyConfig.addExtension("adoc", require('./.asciidoc.gen.js')(eleventyConfig));
// Let Eleventy transform HTML files as nunjucks
// So that we can use .html instead of .njk
return {
dir: {
input: "src",
},
htmlTemplateEngine: "njk",
};
};