-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
212 lines (175 loc) · 6.84 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
const { DateTime } = require("luxon");
const CleanCSS = require("clean-css");
const UglifyJS = require("uglify-js");
const htmlmin = require("html-minifier");
const { EleventyServerlessBundlerPlugin } = require("@11ty/eleventy");
// const lodashChunk = require("lodash.chunk");
const fs = require("fs");
const fg = require('fast-glob');
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(EleventyServerlessBundlerPlugin, {
name: "serverless", // The serverless function name from your permalink object
functionsDir: "./netlify/functions",
});
// eleventyConfig.addPlugin(EleventyServerlessBundlerPlugin, {
// name: "shipping", // The serverless function name from your permalink object
// functionsDir: "./netlify/functions",
// });
eleventyConfig.addFilter("priceFormatter", function(value) { return `€ ${value.toFixed(2)}` });
// Configuration API: use eleventyConfig.addLayoutAlias(from, to) to add
// layout aliases! Say you have a bunch of existing content using
// layout: post. If you don’t want to rewrite all of those values, just map
// post to a new file like this:
// eleventyConfig.addLayoutAlias("post", "layouts/my_new_post_layout.njk");
// Merge data instead of overriding
// https://www.11ty.dev/docs/data-deep-merge/
eleventyConfig.setDataDeepMerge(true);
// Add support for maintenance-free post authors
// Adds an authors collection using the author key in our post frontmatter
// Thanks to @pdehaan: https://github.com/pdehaan
eleventyConfig.addCollection("authors", collection => {
const blogs = collection.getFilteredByGlob("posts/*.md");
return blogs.reduce((coll, post) => {
const author = post.data.author;
if (!author) {
return coll;
}
if (!coll.hasOwnProperty(author)) {
coll[author] = [];
}
coll[author].push(post.data);
return coll;
}, {});
});
// eleventyConfig.addCollection("speciminas", collectionApi => {
// console.log('!!@@');
// console.log('addCollection speciminas');
// console.log(collectionApi.getAll());
// console.log('@@@###');
// const posts = collectionApi.getAll().filter(item => {
// return "specimina" in item;
// })
// console.log(posts);
// const tags = posts
// .map(item => item.tags)
// const uniqueTags = [...new Set(tags)]
// const pageSize = 5
// console.log(uniqueTags);
// return uniqueTags.map(tag => {
// const postsWithTag = posts.filter(post => post.tags.includes(tag))
// return lodashChunk(postsWithTag, pageSize)
// .map((item, index) => ({
// tagName: tag,
// pageNumber: index,
// pageData: item
// }))
// }).flat()
// })
// Date formatting (human readable)
eleventyConfig.addFilter("readableDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("dd LLL yyyy");
});
// Date formatting (machine readable)
eleventyConfig.addFilter("machineDate", dateObj => {
return DateTime.fromJSDate(dateObj).toFormat("yyyy-MM-dd");
});
// Minify CSS
eleventyConfig.addFilter("cssmin", function(code) {
return new CleanCSS({}).minify(code).styles;
});
// Minify JS
eleventyConfig.addFilter("jsmin", function(code) {
let minified = UglifyJS.minify(code);
if (minified.error) {
console.log("UglifyJS error: ", minified.error);
return code;
}
return minified.code;
});
// @TODO: refactor this.
// @FIXME: ugly
eleventyConfig.addNunjucksFilter("where", function(obj, key, value) {
obj.pages = obj.pages.filter(item => item[key] === value);
obj.items = obj.items.filter(item => item[key] === value);
return obj;
});
// find obj by key containting value
eleventyConfig.addNunjucksFilter("findBy", (arr, key, value) => arr.filter(item => item[key] === value)[0]);
// find obj by key containting value
eleventyConfig.addNunjucksFilter("renderContent", (snippets, snippetName) => {
return snippets.filter(item => item["name"] === snippetName)[0]["content"];
});
// Minify HTML output
eleventyConfig.addTransform("htmlmin", function(content, outputPath) {
if (outputPath && outputPath.indexOf(".html") > -1) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
eleventyConfig.addShortcode("includeAll", function(path) {
const entries = fg.sync(path, { dot: true });
let output = '';
entries.forEach(entry => { output += fs.readFileSync(entry, 'utf8').toString() });
return output;
});
if (!process.env.ELEVENTY_SERVERLESS) {
eleventyConfig.on('afterBuild', () => {
fs.copyFileSync('./netlify-override.toml', './netlify.toml');
console.log('afterBuild hook done. netlify toml overwritten.');
// Run me after the build ends
});
}
eleventyConfig.addPassthroughCopy("favicon.ico");
eleventyConfig.addPassthroughCopy("static/img");
eleventyConfig.addPassthroughCopy("robots.txt");
// eleventyConfig.addPassthroughCopy("admin");
// eleventyConfig.addPassthroughCopy("_includes/assets/css/inline.css");
eleventyConfig.addPassthroughCopy("netlify-tryout.toml");
/* Markdown Plugins */
let markdownIt = require("markdown-it");
let markdownItAnchor = require("markdown-it-anchor");
let options = {
html: true,
breaks: true,
linkify: true
};
let opts = {
permalink: false
};
eleventyConfig.setLibrary("md", markdownIt(options)
.use(markdownItAnchor, opts)
);
eleventyConfig.setBrowserSyncConfig({
notify: true,
ghostMode: {
clicks: true,
forms: true,
scroll: true
},
ui: {
port: 8081
}
});
return {
templateFormats: ["md", "njk", "html", "liquid"],
// If your site lives in a different subdirectory, change this.
// Leading or trailing slashes are all normalized away, so don’t worry about it.
// If you don’t have a subdirectory, use "" or "/" (they do the same thing)
// This is only used for URLs (it does not affect your file structure)
pathPrefix: "/",
markdownTemplateEngine: "liquid",
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
dir: {
input: ".",
includes: "_includes",
data: "_data",
output: "_site"
}
};
};