generated from yinkakun/eleventy-duo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
146 lines (122 loc) · 3.79 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
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 htmlmin = require('html-minifier')
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' }));
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(readingTime);
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(syntaxHighlight);
// 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);
});
});
eleventyConfig.addTransform('htmlmin', function(content, outputPath) {
if ( outputPath && outputPath.endsWith(".html") && isProd) {
return htmlmin.minify(content, {
removeComments: true,
collapseWhitespace: true,
useShortDoctype: true,
});
}
return content;
});
return {
dir: {
input: 'src',
output: 'public',
includes: 'includes',
data: 'data',
layouts: 'layouts',
passthroughFileCopy: true,
templateFormats: ['html', 'njk', 'md'],
htmlTemplateEngine: 'njk',
markdownTemplateEngine: 'njk',
},
};
};