-
Notifications
You must be signed in to change notification settings - Fork 2
/
.eleventy.js
179 lines (163 loc) · 4.74 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
172
173
174
175
176
177
178
179
const fs = require('fs')
const moment = require('moment')
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight')
const embedYouTube = require('eleventy-plugin-youtube-embed')
const markdownIt = require('markdown-it')
const markdownItLinkAttr = require('markdown-it-link-attributes')
const markdownItAnchor = require('markdown-it-anchor')
const markdownItAttrs = require('markdown-it-attrs')
const typesetPlugin = require('eleventy-plugin-typeset')
const lazyImagesPlugin = require('eleventy-plugin-lazyimages')
const pluginRss = require('@11ty/eleventy-plugin-rss')
const pluginEmbedTweet = require('eleventy-plugin-embed-tweet')
module.exports = function (eleventyConfig) {
/**
* Opts in to a full deep merge when combining the Data Cascade.
*
* @link https://www.11ty.dev/docs/data-deep-merge/#data-deep-merge
*/
eleventyConfig.setDataDeepMerge(true)
/**
* Add custom watch targets
*
* @link https://www.11ty.dev/docs/config/#add-your-own-watch-targets
*/
eleventyConfig.addWatchTarget('./src/assets/')
/**
* Passthrough file copy
*
* @link https://www.11ty.io/docs/copy/
*/
eleventyConfig.addPassthroughCopy('./src/favicon.ico')
/**
* Add filters
*
* @link https://www.11ty.io/docs/filters/
*/
eleventyConfig.addFilter('dateReadable', (date) => {
return moment(date).format('LL')
})
eleventyConfig.addFilter('limit', function (array, limit) {
if (!limit) {
return array
}
return array.slice(0, limit)
})
/**
* Add shortcodes
*
* @link https://www.11ty.io/docs/shortcodes/
*/
eleventyConfig.addShortcode('excerpt', (article) => {
if (!article.hasOwnProperty('templateContent')) {
console.warn(
'Failed to extract excerpt: Document has no property "templateContent".'
)
return null
}
let excerpt = null
const content = article.templateContent
// The start and end separators to try and match to extract the excerpt
const separatorsList = [
{ start: '<!-- Excerpt Start -->', end: '<!-- Excerpt End -->' },
{ start: '<p>', end: '</p>' },
]
separatorsList.some((separators) => {
const startPosition = content.indexOf(separators.start)
const endPosition = content.indexOf(separators.end)
if (startPosition !== -1 && endPosition !== -1) {
excerpt = content
.substring(startPosition + separators.start.length, endPosition)
.trim()
return true // Exit out of array loop on first match
}
})
return excerpt
})
/**
* Add Transforms
*
* @link https://www.11ty.io/docs/config/#transforms
*/
if (process.env.ELEVENTY_ENV === 'production') {
eleventyConfig.addTransform('htmlmin', require('./utils/htmlmin.js'))
}
/**
* Override BrowserSync Server options
*
* @link https://www.11ty.dev/docs/config/#override-browsersync-server-options
*/
eleventyConfig.setBrowserSyncConfig({
notify: true,
snippetOptions: {
rule: {
match: /<\/head>/i,
fn: function (snippet, match) {
return snippet + match
},
},
},
// Set local server 404 fallback
callbacks: {
ready: function (err, browserSync) {
const content_404 = fs.readFileSync('dist/404/index.html')
browserSync.addMiddleware('*', (req, res) => {
// Provides the 404 content without redirect.
res.write(content_404)
res.end()
})
},
},
})
/**
* Set markdown libraries
*
* @link https://www.11ty.dev/docs/languages/markdown/#optional-set-your-own-library-instance
*/
eleventyConfig.setLibrary(
'md',
markdownIt({ html: true })
.use(markdownItAttrs)
.use(markdownItAnchor, {
permalink: true,
permalinkSymbol: '<i data-feather="link" class="link"></i>',
})
.use(markdownItLinkAttr, {
// Make external links open in a new tab.
pattern: /^https?:\/\//,
attrs: {
target: '_blank',
rel: 'noopener noreferrer',
},
})
)
/**
* Add plugins
*
* @link https://www.11ty.dev/docs/plugins/
*/
eleventyConfig.addPlugin(syntaxHighlight, {
templateFormats: ['md'],
})
eleventyConfig.addPlugin(embedYouTube, {
allowFullscreen: false,
})
eleventyConfig.addPlugin(typesetPlugin())
eleventyConfig.addPlugin(lazyImagesPlugin, {
transformImgPath: (imgPath) => `./src${imgPath}`,
})
eleventyConfig.addPlugin(pluginRss)
eleventyConfig.addPlugin(pluginEmbedTweet)
return {
dir: {
input: 'src',
output: 'dist',
layouts: 'layouts',
includes: 'includes',
},
passthroughFileCopy: true,
templateFormats: ['njk', 'md'],
htmlTemplateEngine: 'njk',
markdownTemplateEngine: 'njk',
}
}