-
Notifications
You must be signed in to change notification settings - Fork 4
/
.eleventy.js
176 lines (151 loc) · 5.1 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
const { URL } = require('url')
const { DateTime } = require('luxon')
const pluginTOC = require('eleventy-plugin-toc')
const pluginDropcap = require('eleventy-plugin-dropcap')
const markdownIt = require('markdown-it')
const markdownItAnchor = require('markdown-it-anchor')
const markdownItHighlightJS = require('markdown-it-highlightjs')
const typogr = require('typogr')
const mdOptions = {
html: true,
breaks: true,
linkify: true,
typographer: true
}
const mdAnchorOpts = {
permalink: markdownItAnchor.permalink.linkInsideHeader({
symbol: '#',
class: 'anchor-link',
}),
level: [1, 2, 3, 4]
}
const formatDate = date => DateTime.fromJSDate(new Date(date)).toISO({includeOffset: true, suppressMilliseconds: true})
const formatDateYear = date => DateTime.fromJSDate(new Date(date)).get('year')
module.exports = eleventyConfig => {
// Markdown
eleventyConfig.setLibrary(
'md',
markdownIt(mdOptions)
.use(markdownItAnchor, mdAnchorOpts)
.use(markdownItHighlightJS)
)
// Plugins
eleventyConfig.addPlugin(pluginTOC)
eleventyConfig.addPlugin(
pluginDropcap,
{skipFirstParagraphClass: 'precursor'}
)
// Filters
eleventyConfig.addFilter('random', max => {
return Math.round(Math.random() * max)
})
eleventyConfig.addFilter('xml_friendly', string => {
return string
.replace(/&\s/g, '& ')
.replace(/<br>/g, '<br />')
})
eleventyConfig.addFilter('paginate_legacy', id => (id > 0) ? `page${id + 1}` : '')
eleventyConfig.addFilter('paginate_better', id => (id > 0) ? `${id + 1}/` : '')
eleventyConfig.addFilter('fix_links', url => url.replace('/index.html', ''))
eleventyConfig.addFilter('rss_last_updated_date', posts => {
const latest = posts.sort((a, b) => {
return new Date(a.date) < new Date(b.date)
})[0].date
return formatDate(latest)
})
eleventyConfig.addFilter('copyright_years', posts => {
const first = posts[(posts.length - 1)].date
return `${formatDateYear(first)}–${new Date().getFullYear()}`
})
eleventyConfig.addFilter('abs_url', (href, base) => new URL(href, base).toString())
eleventyConfig.addFilter('rss_date', date => formatDate(date))
eleventyConfig.addFilter('title_class', string => string.length > 30 ? ' is-long' : '')
eleventyConfig.addFilter('no_orphan', content => typogr(String(content)).chain().widont().value())
// Create Posts Collection
eleventyConfig.addCollection('posts', collection => {
return collection
.getAllSorted()
.reverse()
.filter(item => {
return item.inputPath.match(/^\.\/src\/posts\//) !== null
})
})
// Create Posts Index Collection
eleventyConfig.addCollection('postsIndex', collection => {
return collection
.getAllSorted()
.reverse()
.filter(item => {
return item.inputPath.match(/^\.\/src\/posts\//) !== null
})
.slice(0, 8)
})
// Create Posts Feed Collection
eleventyConfig.addCollection('postsFeed', collection => {
return collection
.getAllSorted()
.reverse()
.filter(item => {
return item.inputPath.match(/^\.\/src\/posts\//) !== null
})
.slice(0, 10)
})
// Create Category Collections
Array.from(['CSS', 'Sass', 'Misc', 'WordPress', 'Performance']).map(cat => {
eleventyConfig.addCollection(cat, collection => {
return collection
.getAllSorted()
.filter(item => {
if ('categories' in item.data) {
return item.data.categories.filter(category => {
return category.toLowerCase() === cat.toLowerCase()
}).length > 0
}
return false
})
.reverse()
})
})
// Create Nav Collection
eleventyConfig.addCollection('nav', collection => {
return collection
.getAll()
.filter(item => {
return 'menu_order' in item.data
})
.sort((a, b) => {
return a.data.menu_order > b.data.menu_order
})
})
// Pass through directories
eleventyConfig.addPassthroughCopy('src/images')
eleventyConfig.addPassthroughCopy('src/assets/fonts')
eleventyConfig.addPassthroughCopy({'src/assets/js/site/site.js': 'js/site.js'})
eleventyConfig.addPassthroughCopy('src/site.webmanifest')
eleventyConfig.addPassthroughCopy('src/android-chrome-192x192.png')
eleventyConfig.addPassthroughCopy('src/android-chrome-512x512.png')
eleventyConfig.addPassthroughCopy('src/apple-touch-icon.png')
eleventyConfig.addPassthroughCopy('src/safari-pinned-tab.svg')
eleventyConfig.addPassthroughCopy('src/favicon.ico')
eleventyConfig.addPassthroughCopy('src/favicon.svg')
eleventyConfig.addPassthroughCopy('src/A')
eleventyConfig.addPassthroughCopy('src/CNAME')
eleventyConfig.addPassthroughCopy('src/.nojekyll')
eleventyConfig.addPassthroughCopy('src/robots.txt')
// More watched files
eleventyConfig.addWatchTarget('./src/assets/js/**/*.js')
eleventyConfig.addWatchTarget('./src/assets/scss/**/*.scss')
return {
templateFormats: [
'liquid',
'md',
'11ty.js'
],
dir: {
input: './src',
includes: '_includes',
data: '_data',
output: '_site'
}
}
}