-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
executable file
·132 lines (112 loc) · 3.86 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
const { DateTime } = require('luxon');
const pluginRss = require('@11ty/eleventy-plugin-rss');
const pluginSyntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const embedTwitter = require('eleventy-plugin-embed-twitter');
const webmentionsFilters = require('./_11ty/filters');
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginSyntaxHighlight);
eleventyConfig.addPlugin(embedTwitter);
eleventyConfig.addPassthroughCopy("_redirects");
// https://www.alpower.com/tutorials/formatting-dates-in-eleventy/
eleventyConfig.addFilter('asPostDate', (dateObj) => {
return DateTime.fromJSDate(dateObj).toLocaleString(DateTime.DATE_MED);
});
eleventyConfig.addFilter('lcase', (str) => {
return str.toLowerCase();
});
eleventyConfig.addFilter('published', (items) => {
const now = new Date();
return items.filter((item) => {
if (!('data' in item)) return true;
if (!('date' in item.data)) return true;
if (item.data.date < now) return true;
return false;
});
});
eleventyConfig.addFilter('htmlencode', (str) => {
return str.replace(/"/g, '"');
});
eleventyConfig.addFilter('htmlDateString', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy-LL-dd');
});
eleventyConfig.addFilter('sortTagsAlpha', (tagCollection) => {
return tagCollection.sort((L, R) => (L.tag > R.tag ? 1 : -1));
});
eleventyConfig.addFilter('absoluteUrl', (url) => `https://adamtuttle.codes${url}`);
Object.keys(webmentionsFilters).forEach((filterName) => {
eleventyConfig.addFilter(filterName, webmentionsFilters[filterName]);
});
const markdownIt = require('markdown-it');
const markdownItOptions = {
html: true,
linkify: true
};
const md = markdownIt(markdownItOptions)
.use(require('markdown-it-footnote'))
.use(require('markdown-it-attrs'))
.use(require('markdown-it-anchor'))
.use(function (md) {
// Recognize Mediawiki links ([[text]])
md.linkify.add('[[', {
validate: /^\s?([^\[\]\|\n\r]+)(\|[^\[\]\|\n\r]+)?\s?\]\]/,
normalize: (match) => {
const parts = match.raw.slice(2, -2).split('|');
parts[0] = parts[0].replace(/.(md|markdown)\s?$/i, '');
match.text = (parts[1] || parts[0]).trim();
match.url = `/blog/${parts[0].trim()}/`;
}
});
});
eleventyConfig.addFilter('markdownify', (string) => {
return md.render(string);
});
eleventyConfig.setLibrary('md', md);
const published = (entry) => {
const now = new Date();
// console.log({ title: entry.data.title, future: entry.data.date > now });
return entry.data.date <= now;
};
eleventyConfig.addCollection('blog', function (collection) {
return collection.getFilteredByGlob(['blog/**/*.md', 'index.md']).filter(published);
});
eleventyConfig.addCollection('blogLatest', function (collection) {
return collection.getFilteredByGlob(['blog/**/*.md', 'index.md']).filter(published).slice(-1);
});
eleventyConfig.addCollection('tagsByCount', function (collection) {
const articles = collection.getFilteredByGlob(['blog/**/*.md']);
const now = new Date();
const tags = articles.reduce((agg, article) => {
if (article.data.date > now) {
return agg;
}
if (article.data.tags) {
const articleTags = article.data.tags;
articleTags.forEach((t) => {
let ix = agg.findIndex((i) => i.tag === t);
if (ix === -1) {
ix = agg.length;
agg[ix] = { tag: t, count: 0 };
}
agg[ix].count++;
});
}
return agg;
}, []);
const sorted = tags.sort((L, R) => (L.count === R.count ? (L.tag > R.tag ? 1 : -1) : L.count > R.count ? -1 : 1));
return sorted;
});
eleventyConfig.addPassthroughCopy('assets');
eleventyConfig.addPassthroughCopy('img');
return {
useGitIgnore: false,
dir: {
input: './',
output: '_site',
layouts: 'layouts',
includes: 'includes',
data: '_data'
},
passthroughFileCopy: true
};
};