-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
153 lines (132 loc) · 4.4 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
const fs = require('fs');
const { minify } = require('terser');
const CleanCSS = require('clean-css');
const dayjs = require('dayjs');
const lodashChunk = require('lodash/chunk');
const pluginSyntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
module.exports = function (eleventyConfig) {
eleventyConfig.addPassthroughCopy('./src/static');
eleventyConfig.addPassthroughCopy('./src/css/fonts');
eleventyConfig.addPassthroughCopy('./src/css/github-markdown.css');
eleventyConfig.addPassthroughCopy({ 'src/admin': 'admin' });
eleventyConfig.addCollection('blogs', function (collection) {
return collection.getFilteredByGlob('./src/blogs/**/*.md');
});
eleventyConfig.addLayoutAlias('blog', 'layouts/blog.njk');
// 11ty plugins
eleventyConfig.addPlugin(pluginSyntaxHighlight);
let markdownLibrary = markdownIt({
html: true,
breaks: true,
linkify: true,
}).use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.ariaHidden({
placement: 'after',
class: 'direct-link',
symbol: '#',
level: [1, 2, 3, 4],
}),
slugify: eleventyConfig.getFilter('slug'),
});
eleventyConfig.setLibrary('md', markdownLibrary);
// js minification filter
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);
}
}
);
// css minificataion filter
eleventyConfig.addFilter('cssmin', function (code) {
return new CleanCSS({}).minify(code).styles;
});
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
eleventyConfig.addFilter('formatDate', (dateObj) => {
return dayjs(dateObj).format('DD MMMM YYYY');
});
function filterTagList(tags) {
return (tags || []).filter(
(tag) => ['all', 'nav', 'post', 'posts'].indexOf(tag) === -1
);
}
eleventyConfig.addFilter('filterTagList', filterTagList);
// Create an array of all tags
eleventyConfig.addCollection('tagList', function (collection) {
let tagSet = new Set();
collection.getAll().forEach((item) => {
(item.data.tags || []).forEach((tag) => tagSet.add(tag));
});
return filterTagList([...tagSet]);
});
// This is used to apply tag based pagination or double pagination
// Ref: https://github.com/11ty/eleventy/issues/332
eleventyConfig.addCollection('paginatedTagBlogs', function (collection) {
// Get unique list of tags
let tagSet = new Set();
collection.getAllSorted().map(function (item) {
if ('tags' in item.data) {
let tags = item.data.tags;
const filteredTags = filterTagList(tags);
for (let tag of filteredTags) {
tagSet.add(tag);
}
}
});
// Get each item that matches the tag
let paginationSize = 9;
let tagMap = [];
let tagArray = [...tagSet];
for (let tagName of tagArray) {
let tagItems = collection.getFilteredByTag(tagName);
let pagedItems = lodashChunk(tagItems, paginationSize);
// console.log( tagName, tagItems.length, pagedItems.length );
for (
let pageNumber = 0, max = pagedItems.length;
pageNumber < max;
pageNumber++
) {
tagMap.push({
tagName: tagName,
pageNumber: pageNumber,
pageData: pagedItems[pageNumber],
});
}
}
return tagMap;
});
// 11ty --server 404 page
eleventyConfig.setBrowserSyncConfig({
callbacks: {
ready: function (err, bs) {
bs.addMiddleware('*', (req, res) => {
const content_404 = fs.readFileSync('dist/404.html');
// Add 404 http status code in request header.
res.writeHead(404, { 'Content-Type': 'text/html; charset=UTF-8' });
// Provides the 404 content without redirect.
res.write(content_404);
res.end();
});
},
},
});
return {
templateFormats: ['md', 'njk', 'html', 'liquid'],
// Pre-process *.md files with: (default: `liquid`)
markdownTemplateEngine: 'njk',
// Pre-process *.html files with: (default: `liquid`)
htmlTemplateEngine: 'njk',
dir: {
input: 'src',
output: 'dist',
},
};
};