-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
171 lines (141 loc) · 4.53 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
const { DateTime } = require('luxon');
const readingTime = require('eleventy-plugin-reading-time');
const pluginRss = require('@11ty/eleventy-plugin-rss');
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const fs = require('fs');
const path = require('path');
const isDev = process.env.ELEVENTY_ENV === 'development';
const isProd = process.env.ELEVENTY_ENV === 'production'
const manifestPath = path.resolve(
__dirname,
'public',
'assets',
'manifest.json'
);
const manifest = isDev
? {
'main.js': '/assets/main.js',
'main.css': '/assets/main.css',
}
: JSON.parse(fs.readFileSync(manifestPath, { encoding: 'utf8' }));
const { execSync } = require('child_process')
const embedYouTube = require("eleventy-plugin-youtube-embed");
const brokenLinksPlugin = require("eleventy-plugin-broken-links");
// Anchor links
const markdownIt = require('markdown-it');
const markdownItAnchor = require('markdown-it-anchor');
const slugify = require("slugify");
//Eleventy Configurations
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(readingTime);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(syntaxHighlight);
//Slugify (add strict mode)
const markdownItAnchorOptions = {
level: [1, 2, 3],
slugify: (str) =>
slugify(str, {
lower: true,
strict: true,
remove: /["]/g,
}),
};
//Replace Markdown with custom configurations
eleventyConfig.setLibrary("md", markdownIt().use(markdownItAnchor, markdownItAnchorOptions))
//Search: pageFind
eleventyConfig.on('eleventy.after', () => {
execSync(`npx pagefind --site public --glob \"**/*.html\"`, { encoding: 'utf-8' })
})
//YouTube embedded plugin
eleventyConfig.addPlugin(embedYouTube);
//Broken links plugins
eleventyConfig.addPlugin(brokenLinksPlugin);
// setup mermaid markdown highlighter
const highlighter = eleventyConfig.markdownHighlighter;
eleventyConfig.addMarkdownHighlighter((str, language) => {
if (language === 'mermaid') {
return `<pre class="mermaid">${str}</pre>`;
}
return highlighter(str, language);
});
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addPassthroughCopy({ 'src/images': 'images' });
eleventyConfig.setBrowserSyncConfig({ files: [manifestPath] });
eleventyConfig.addShortcode('bundledcss', function () {
return manifest['main.css']
? `<link href="${manifest['main.css']}" rel="stylesheet" />`
: '';
});
eleventyConfig.addShortcode('bundledjs', function () {
return manifest['main.js']
? `<script src="${manifest['main.js']}"></script>`
: '';
});
eleventyConfig.addFilter('excerpt', (post) => {
const content = post.replace(/(<([^>]+)>)/gi, '');
return content.substr(0, content.lastIndexOf(' ', 200)) + '...';
});
eleventyConfig.addFilter('readableDate', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat(
'dd LLL yyyy'
);
});
eleventyConfig.addFilter('htmlDateString', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy-LL-dd');
});
eleventyConfig.addFilter('dateToIso', (dateString) => {
return new Date(dateString).toISOString()
});
eleventyConfig.addFilter('head', (array, n) => {
if (n < 0) {
return array.slice(n);
}
return array.slice(0, n);
});
eleventyConfig.addCollection('tagList', function (collection) {
let tagSet = new Set();
collection.getAll().forEach(function (item) {
if ('tags' in item.data) {
let tags = item.data.tags;
tags = tags.filter(function (item) {
switch (item) {
case 'all':
case 'nav':
case 'post':
case 'posts':
return false;
}
return true;
});
for (const tag of tags) {
tagSet.add(tag);
}
}
});
return [...tagSet];
});
eleventyConfig.addFilter('pageTags', (tags) => {
const generalTags = ['all', 'nav', 'post', 'posts'];
return tags
.toString()
.split(',')
.filter((tag) => {
return !generalTags.includes(tag);
});
});
// Add talks.json
eleventyConfig.addPassthroughCopy({ 'src/data/talks.json': 'assets/talks.json' });
return {
dir: {
input: 'src',
output: 'public',
includes: 'includes',
data: 'data',
layouts: 'layouts'
},
passthroughFileCopy: true,
templateFormats: ['html', 'njk', 'md'],
htmlTemplateEngine: 'njk',
markdownTemplateEngine: 'njk',
};
};