-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
152 lines (118 loc) · 4.51 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
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const htmlmin = require('html-minifier');
const { DateTime } = require('luxon');
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
module.exports = function (eleventyConfig) {
const markdownItOptions = {
html: true,
};
const markdownItAnchorOptions = {
permalink: true,
renderPermalink,
};
const markdownLib = markdownIt(markdownItOptions).use(markdownItAnchor, markdownItAnchorOptions);
eleventyConfig.setLibrary('md', markdownLib);
const buildTimestamp = Date.now();
eleventyConfig.addPassthroughCopy('CNAME');
eleventyConfig.setBrowserSyncConfig({
files: './public/styles/**/*.css',
});
eleventyConfig.addPassthroughCopy('src/static');
eleventyConfig.addFilter('formatDate', (isoDate) => {
return DateTime.fromISO(isoDate, { zone: 'utc+2' }).toFormat('dd LLLL yyyy');
});
eleventyConfig.addFilter('sortedPosts', (posts, limit = 1) => {
return sortPosts(publishedPosts(posts));
});
eleventyConfig.addFilter('recentPosts', (posts, limit = 1, url = null) => {
if (url) {
return sortPosts(publishedPosts(posts))
.filter((post) => url.indexOf(post.url) === -1)
.slice(0, limit);
}
return sortPosts(publishedPosts(posts)).slice(0, limit);
});
eleventyConfig.addFilter('randomPost', (posts, url = null) => {
if (currentPost) {
return sortRandomly(publishedPosts(posts))
.filter((post) => url.indexOf(post.url) === -1)
.slice(0, limit);
}
return sortRandomly(posts).slice(0, 1);
});
eleventyConfig.addFilter('featuredPost', (posts) => {
const featuredPosts = publishedPosts(posts).filter((post) => !!post?.data?.featured);
featuredPosts.sort(() => 0.5 - Math.random());
return featuredPosts.slice(0, 1);
});
eleventyConfig.addFilter('publishedPosts', (posts) => {
return publishedPosts(posts);
});
eleventyConfig.addFilter('sortRandomly', (data) => {
return sortRandomly(data);
});
eleventyConfig.addFilter('buildUrl', (path, addTimestamp = false) => {
const timestampParam = addTimestamp ? `?v=${buildTimestamp}` : '';
const websiteUrl = process.env.ELEVENTY_ENV === 'production' ? `https://maarten.be${path}` : path;
return `${websiteUrl}${timestampParam}`;
});
eleventyConfig.addFilter('buildMetaImage', (image) => {
return image ? `https://maarten.be/static/images/posts/${image}?v=${buildTimestamp}` : '';
});
eleventyConfig.addFilter('formatHashtags', (tags) => {
return (tags ?? []).map((tag) => `#${tag}`).join(' ');
});
eleventyConfig.addShortcode('year', () => `${new Date().getFullYear()}`);
eleventyConfig.addShortcode('age', () => `${new Date().getFullYear() - 1984}`);
eleventyConfig.addPlugin(syntaxHighlight);
if (process.env.ELEVENTY_ENV === 'production') {
eleventyConfig.addTransform('htmlmin', (content, outputPath) => {
if (outputPath.endsWith('.html')) {
return htmlmin.minify(content, {
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
removeComments: true,
sortClassName: true,
useShortDoctype: true,
});
}
return content;
});
}
return {
dir: {
input: 'src',
output: 'public',
},
};
};
function sortPosts(posts) {
return ([...posts] ?? []).sort((a, b) => DateTime.fromISO(b.date).toUnixInteger() - DateTime.fromISO(a.date).toUnixInteger());
}
function sortRandomly(data) {
return ([...data] ?? []).sort(() => 0.5 - Math.random());
}
function renderPermalink(slug, opts, state, idx) {
const position = { false: 'push', true: 'unshift' };
const space = () => Object.assign(new state.Token('text', '', 0), { content: ' ' });
const linkTokens = [
Object.assign(new state.Token('link_open', 'a', 1), {
attrs: [
['class', opts.permalinkClass],
['href', opts.permalinkHref(slug, state)],
],
}),
Object.assign(new state.Token('html_block', '', 0), {
content: `<span aria-hidden="true" class="header-anchor__symbol" title="Direct link to this section"><img src="/static/svg/link.svg" height="20" alt="link icon"></span>`,
}),
new state.Token('link_close', 'a', -1),
];
if (opts.permalinkSpace) {
linkTokens[position[!opts.permalinkBefore]](space());
}
state.tokens[idx + 1].children[position[opts.permalinkBefore]](...linkTokens);
}
function publishedPosts(posts) {
return ([...posts] ?? []).filter((post) => !!post?.data?.published);
}