-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
197 lines (166 loc) · 6.06 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
const path = require('path');
const fs = require('fs');
const { DateTime } = require('luxon');
const { groupBy, flatten, drop } = require('lodash');
// Plugins
const pluginRss = require('@11ty/eleventy-plugin-rss');
const pluginSyntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
// Custom tags etc.
const YoutubeThumbnail = require('./src/_includes/components/YoutubeThumbnail');
const Text = require('./src/_includes/components/Text');
const Heading = require('./src/_includes/components/Heading');
const Subheading = require('./src/_includes/components/Subheading');
const Link = require('./src/_includes/components/Link');
const MarkdownBlock = require('./src/_includes/components/MarkdownBlock');
// Globals
const INPUT_DIR = 'src';
const OUTPUT_DIR = '_site';
const HASH_MANIFEST_FILENAME = '_intermediate/hash-manifest.json';
const isProduction = process.env.NODE_ENV === 'production';
module.exports = function(eleventyConfig) {
//
// PLUGINS
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginSyntaxHighlight);
//
// LAYOUTS
eleventyConfig.addLayoutAlias('post', 'layouts/post.njk');
//
// FILTERS
eleventyConfig.addFilter('readableDate', dateObj => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat(
'dd LLLL yyyy'
);
});
// Get the first `n` elements of a collection.
eleventyConfig.addFilter('head', (array, n) => {
if (n < 0) {
return array.slice(n);
}
return array.slice(0, n);
});
// Get the nth element of a collection.
eleventyConfig.addFilter('at', (array, n) => {
return array[n];
});
// Skip/drop n elements from a list and return the rest.
eleventyConfig.addFilter('drop', (array, n) => {
return drop(array, n);
});
// Flatten a list one level.
eleventyConfig.addFilter('flatten', array => {
return flatten(array);
});
// Log the argument and return unchanged.
eleventyConfig.addFilter('log', value => {
console.log(value);
return value;
});
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
eleventyConfig.addFilter('htmlDateString', dateObj => {
return DateTime.fromJSDate(dateObj).toFormat('yyyy-LL-dd');
});
// Filter that resolves a hash from a known table
// @see join-manifests for more information on how the manifest is formed
eleventyConfig.addFilter('resolveHash', function(filename) {
// Presumably, dev has no hashes, so do not attempt to resolve
if (!isProduction) {
return filename;
}
// A file at a known location that maps filenames to hashed filenames
const hashManifest = JSON.parse(
fs.readFileSync(path.resolve(HASH_MANIFEST_FILENAME))
);
const hashedFilename = hashManifest[filename];
if (!hashedFilename) {
throw Error(
`File with basename: ${basename} not found in hash manifest. Perhaps one of the tools built out of order? Check the manifests under the intermediate/ folder.`
);
}
return hashedFilename;
});
eleventyConfig.addFilter('youtubeVideoUrl', (videoId, clipTimeSeconds) => {
return `https://youtube.com/watch?v=${videoId}${
clipTimeSeconds ? `&t=${clipTimeSeconds}` : ''
}`;
});
eleventyConfig.addFilter('youtubeEmbedUrl', (videoId, clipTimeSeconds) => {
return `https://www.youtube.com/embed/${videoId}${
clipTimeSeconds ? `&start=${clipTimeSeconds}` : ''
}`;
});
//
// COLLECTIONS
// only content in the `posts/` directory
eleventyConfig.addCollection('posts', function(collection) {
const postsGlob = path.join(INPUT_DIR, 'posts/*');
return collection.getFilteredByGlob(postsGlob).sort(function(a, b) {
return a.date - b.date;
});
});
// Group posts by date
// NOTE: You have to inspect the date in the children atm;
// returning an object {[date]: items} does not work yet.
eleventyConfig.addCollection('postsByDate', function(collection) {
const postsGlob = path.join(INPUT_DIR, 'posts/*');
const sorted = collection.getFilteredByGlob(postsGlob).sort(function(a, b) {
return a.date - b.date;
});
const grouped = groupBy(sorted, item =>
DateTime.fromJSDate(item.date).startOf('day')
);
console.log({ grouped });
return Object.values(grouped);
});
eleventyConfig.addCollection('tagList', require('./_11ty/getTagList'));
// Copy these directories and files directly
eleventyConfig.addPassthroughCopy('src/img');
eleventyConfig.addPassthroughCopy('src/js/workbox');
eleventyConfig.addPassthroughCopy('src/fonts');
eleventyConfig.addPassthroughCopy('src/manifest.json');
/* Markdown Plugins */
let markdownIt = require('markdown-it');
let markdownItAnchor = require('markdown-it-anchor');
let options = {
html: true,
breaks: true,
linkify: true,
};
let opts = {
permalink: true,
permalinkClass: 'direct-link',
permalinkSymbol: '#',
};
eleventyConfig.setLibrary(
'md',
markdownIt(options).use(markdownItAnchor, opts)
);
//
// SHORTCODES
eleventyConfig.addShortcode('YoutubeThumbnail', YoutubeThumbnail);
eleventyConfig.addPairedShortcode('Text', Text);
eleventyConfig.addPairedShortcode('Heading', Heading);
eleventyConfig.addPairedShortcode('Subheading', Subheading);
eleventyConfig.addPairedShortcode('MarkdownBlock', MarkdownBlock);
eleventyConfig.addPairedShortcode('Link', Link);
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',
passthroughFileCopy: true,
dir: {
input: INPUT_DIR,
output: OUTPUT_DIR,
// NOTE: These two paths are relative to dir.input
// @see https://github.com/11ty/eleventy/issues/232
includes: '_includes',
data: '_data',
},
};
};