-
Notifications
You must be signed in to change notification settings - Fork 25
/
.eleventy.js
139 lines (121 loc) · 4.32 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
// Eleventy Plugins
import {EleventyI18nPlugin} from '@11ty/eleventy';
import rssPlugin from '@11ty/eleventy-plugin-rss';
import externalLinksPlugin from '@sardine/eleventy-plugin-external-links';
import tocPlugin from 'eleventy-plugin-toc';
// Markdown Libraries
import markdownIt from 'markdown-it';
import markdownItAnchor from 'markdown-it-anchor';
import markdownItAttrs from 'markdown-it-attrs';
// Filters
import cleanTocFilter from './src/filters/clean-toc-filter.js';
import dateFilter from './src/filters/date-filter.js';
import hostnameFilter from './src/filters/hostname-filter.js';
import englishPostsFilter from './src/filters/english-posts-filter.js';
import removeLanguageCode from './src/filters/remove-language-code-filter.js';
import language from './src/filters/language.js';
import w3DateFilter from './src/filters/w3-date-filter.js';
// Shortcodes
import imageShortcode from './src/shortcodes/image.js';
// Utils
import groupEntriesByYear from './src/utils/group-entries-by-year.js';
import {loadPageDetails} from './src/utils/page-details.js';
export default config => {
// Add filters
config.addFilter('dateFilter', dateFilter);
config.addFilter('hostnameFilter', hostnameFilter);
config.addFilter('w3DateFilter', w3DateFilter);
config.addFilter('cleanTocFilter', cleanTocFilter);
config.addFilter('englishPostsFilter', englishPostsFilter);
config.addFilter('removeLanguageCode', removeLanguageCode);
config.addFilter('language', language);
config.addFilter('excerpt', post => {
const content = post.replace(/<(style|script)\b[^>]*>[\s\S]*?<\/\1>|<[^>]*>/gi, '');
return content.substr(0, content.lastIndexOf(' ', 400)) + '...';
});
// Add shortcodes
config.addNunjucksAsyncShortcode('image', imageShortcode);
// Plugins
config.addPlugin(rssPlugin);
config.addPlugin(externalLinksPlugin);
config.addPlugin(tocPlugin, {
tags: ['h2', 'h3', 'h4'],
ul: true,
flat: false
});
config.addPlugin(EleventyI18nPlugin, {
defaultLanguage: 'en',
errorMode: 'allow-fallback'
});
// Returns a collection of blog posts in reverse date order
config.addCollection('blog', collection => {
return [...collection.getFilteredByGlob('./src/posts/*.md')].reverse();
});
// Returns list of related-links from all posts and pages,
// grouped by year (descending): [{ year, entries },...]
config.addCollection('links', async collection => {
const posts = [...collection.getFilteredByGlob('./src/posts/*.md')];
const relatedLinkData = [];
for (const post of posts) {
const relatedLinks = post?.data?.relatedLinks || [];
for (const link of relatedLinks) {
const linkDetails = await loadPageDetails(link);
relatedLinkData.push(linkDetails);
}
}
return groupEntriesByYear(relatedLinkData);
});
// Returns press-links grouped by year (descending): [{ year, entries },...]
config.addCollection('press', async collection => {
const pressData =
collection.getAll().filter(item => item.data.press)[0]?.data?.press || [];
const pressLinkData = [];
for (const link of pressData) {
const linkDetails = await loadPageDetails(link);
pressLinkData.push(linkDetails);
}
return groupEntriesByYear(pressLinkData);
});
// Tell 11ty to use the .eleventyignore and ignore our .gitignore file
config.setUseGitIgnore(false);
// Pass through images
config.addPassthroughCopy('./src/images');
// Pass through css, js
config.addPassthroughCopy('./src/css');
config.addPassthroughCopy('./src/js');
// Pass through files
config.addPassthroughCopy('./src/files');
// Set custom markdown library
config.setLibrary('md', buildMarkdownLibrary());
return {
markdownTemplateEngine: 'njk',
dataTemplateEngine: 'njk',
htmlTemplateEngine: 'njk',
dir: {
input: 'src',
output: 'dist'
}
};
};
function buildMarkdownLibrary() {
const mdParser = markdownIt({
html: true
});
mdParser
.use(markdownItAnchor, {
level: 1,
permalink: markdownItAnchor.permalink.ariaHidden({
placement: 'before'
}),
slugify(s) {
const formatted = String(s)
.trim()
.toLowerCase()
.replace(/^[\d.]+\s/g, '')
.replace(/\s+/g, '-');
return encodeURIComponent(formatted);
}
})
.use(markdownItAttrs);
return mdParser;
}