forked from OpenWebAdvocacy/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
130 lines (111 loc) · 4.04 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
// Eleventy Plugins
const rssPlugin = require('@11ty/eleventy-plugin-rss');
const externalLinksPlugin = require('@sardine/eleventy-plugin-external-links');
const tocPlugin = require('eleventy-plugin-toc');
const { EleventyI18nPlugin } = require('@11ty/eleventy');
// Markdown Libraries
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
const markdownItAttrs = require('markdown-it-attrs');
// Filters
const dateFilter = require('./src/filters/date-filter.js');
const hostnameFilter = require('./src/filters/hostname-filter.js');
const w3DateFilter = require('./src/filters/w3-date-filter.js');
const cleanTocFilter = require('./src/filters/clean-toc-filter.js');
// Shortcodes
const imageShortcode = require('./src/shortcodes/image.js');
// Utils
const groupEntriesByYear = require('./src/utils/group-entries-by-year.js');
const { loadPageDetails } = require('./src/utils/page-details.js');
const sortByDisplayOrder = require('./src/utils/sort-by-display-order.js');
module.exports = config => {
// Add filters
config.addFilter('dateFilter', dateFilter);
config.addFilter('hostnameFilter', hostnameFilter);
config.addFilter('w3DateFilter', w3DateFilter);
config.addFilter('cleanTocFilter', cleanTocFilter);
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;
}