-
Notifications
You must be signed in to change notification settings - Fork 0
/
eleventy.config.js
152 lines (140 loc) · 4.01 KB
/
eleventy.config.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
// Plugins
const pluginRss = require('@11ty/eleventy-plugin-rss');
// Parsers
const markdownIt = require('markdown-it');
const lazy_loading = require('markdown-it-image-lazy-loading');
const implicitFigures = require('markdown-it-implicit-figures');
const replaceLink = require('markdown-it-replace-link');
const { DateTime } = require('luxon');
// minify tasks
const postcss = require('postcss');
const cssnano = require('cssnano');
const autoprefixer = require('autoprefixer');
const { minify } = require('terser');
// eleventy configuration
module.exports = function (eleventyConfig) {
// add plugins
eleventyConfig.addPlugin(pluginRss);
/* Markdown */
let markdownLibrary = markdownIt({
html: true,
breaks: false,
linkify: true,
typographer: true,
replaceLink: function (link, env) {
return link + '?nf_resize=fit&w=1200';
},
})
.use(lazy_loading)
.use(implicitFigures, {
figcaption: true,
})
.use(replaceLink);
// set library
eleventyConfig.setLibrary('md', markdownLibrary);
// add passthrough files
eleventyConfig.addPassthroughCopy('src/config.yml');
eleventyConfig.addPassthroughCopy('src/favicon.svg');
eleventyConfig.addPassthroughCopy('src/manifest.json');
eleventyConfig.addPassthroughCopy('src/sw.js');
eleventyConfig.addPassthroughCopy('src/images');
eleventyConfig.addPassthroughCopy('src/data');
eleventyConfig.addPassthroughCopy('src/assets');
eleventyConfig.addPassthroughCopy('src/posts/*/uploads/*');
// postCSS filter
eleventyConfig.addNunjucksAsyncFilter(
'postCSS',
async function (code, callback) {
try {
return await postcss([autoprefixer, cssnano])
.process(code, { from: 'undefined' })
.then(function (result) {
callback(null, result.css);
});
} catch (err) {
console.error('postCSS error: ', err);
// Fail gracefully.
callback(null, code);
}
}
);
// minify JS filter for inline injection
eleventyConfig.addNunjucksAsyncFilter(
'jsmin',
async function (code, callback) {
try {
const minified = await minify(code);
callback(null, minified.code);
} catch (err) {
console.error('Terser error: ', err);
// Fail gracefully.
callback(null, code);
}
}
);
// parse datetime to readable
eleventyConfig.addFilter('readableDate', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toLocaleString(
DateTime.DATE_FULL
);
});
// parse datetime to html
eleventyConfig.addFilter('htmlDateString', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy-LL-dd');
});
// parse datetime to month (short) and day
eleventyConfig.addFilter('monthDay', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('LLL dd');
});
// remove tags
eleventyConfig.addFilter('excludeTags', (tags) => {
const toRemove = [
'all',
'post',
'posts',
'pages',
'blog',
'tagList',
'Eden',
'Itai',
'Axelrad',
'Climbing',
'Bouldering',
'Five Ten',
'fiveten',
'Five',
'Ten',
];
return tags.filter((tag) => !toRemove.includes(tag));
});
// Get tags from all posts
eleventyConfig.addCollection('tagList', (collection) => {
let tagSet = new Set();
collection.getAll().forEach((item) => {
if ('tags' in item.data) {
let tags = item.data.tags;
for (const tag of tags) {
tagSet.add(tag);
}
}
});
return [...tagSet];
});
// Get paginated posts
eleventyConfig.addCollection('pagedPosts', (collectionApi) => {
return collectionApi.getFilteredByTag('post').reverse().slice(3);
});
// Base Config
return {
templateFormats: ['njk', 'md', 'html'],
htmlTemplateEngine: 'njk',
markdownTemplateEngine: 'njk',
dir: {
input: 'src',
output: 'dist',
includes: 'components',
layouts: 'layouts',
data: 'data',
},
};
};