-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
214 lines (197 loc) · 7 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const { EleventyServerlessBundlerPlugin } = require('@11ty/eleventy')
const { DateTime } = require('luxon')
const rssPlugin = require('@11ty/eleventy-plugin-rss')
const { getColourFromString } = require('./src/utils/getColourFromString')
const slugify = require('@alexcarpenter/slugify')
const pluginTOC = require('eleventy-plugin-nesting-toc')
const heroIcons = require('eleventy-plugin-heroicons')
const markdownIt = require('markdown-it')
const markdownItAnchor = require('markdown-it-anchor')
const markdownItCopyCode = require('markdown-it-copy')
const syntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight')
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
module.exports = function (eleventyConfig) {
// Plugins
eleventyConfig.addPlugin(rssPlugin)
// eleventyConfig.addPlugin(torchlight)
eleventyConfig.addPlugin(syntaxHighlight, {
preAttributes: {
tabindex: 0,
'data-language': function ({ language, content, options }) {
return language
},
},
})
eleventyConfig.addPlugin(pluginTOC, {
ignoredElements: ['.visually-hidden', '[aria-hidden]'],
})
eleventyConfig.addPlugin(heroIcons, {
className: 'icon',
errorOnMissing: true,
})
// Serverless
// Search
eleventyConfig.addPlugin(EleventyServerlessBundlerPlugin, {
name: 'searcher',
functionsDir: './netlify/functions/',
})
eleventyConfig.setLibrary(
'md',
markdownIt({
html: true,
breaks: true,
linkify: true,
typographer: true,
})
)
eleventyConfig.amendLibrary('md', (mdLib) =>
mdLib.use(markdownItAnchor, {
permalink: true,
safariReaderFix: true,
permalink: markdownItAnchor.permalink.linkInsideHeader({
symbol: `
<span class="visually-hidden">Jump to heading</span>
<span aria-hidden="true">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon inline-block" width="20"><path stroke-linecap="round" stroke-linejoin="round" d="M5.25 8.25h15m-16.5 7.5h15m-1.8-13.5l-3.9 19.5m-2.1-19.5l-3.9 19.5" /></svg>
</span>
`,
placement: 'after',
}),
slugify: (s) => slugify(s),
})
)
eleventyConfig.amendLibrary('md', (mdLib) =>
mdLib.use(markdownItCopyCode, {
successText: `<svg xmlns="http://www.w3.org/2000/svg" class="icon inline-block" width="20" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
`,
btnText: `<svg xmlns="http://www.w3.org/2000/svg" class="icon inline-block" width="20" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
</svg>`,
})
)
// Filters
eleventyConfig.addFilter('htmlDateString', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy-LL-dd')
})
eleventyConfig.addFilter('readableDate', (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat(
'dd LLL, yyyy'
)
})
eleventyConfig.addFilter('dateFromISO', (timestamp) => {
return DateTime.fromISO(timestamp, { zone: 'utc' }).toJSDate()
})
// Collections
// Tags
eleventyConfig.addCollection('tagList', (collection) => {
const tagsObject = {
untagged: 0,
}
collection.getFilteredByGlob('src/notes/*.md').forEach((item) => {
if (item.data.draft) {
return
}
if (!item.data.tags) {
tagsObject.untagged += 1
return
}
item.data.tags.forEach((tag) => {
if (typeof tagsObject[tag] === 'undefined') {
tagsObject[tag] = 1
} else {
tagsObject[tag] += 1
}
})
})
const tagList = []
Object.keys(tagsObject).forEach((tag) => {
const tagName = slugify(tag)
tagList.push({
name: tag,
count: tagsObject[tag],
color: getColourFromString(tagName),
path: tagName,
})
})
return tagList.sort((a, b) => b.count - a.count)
})
// Notes
eleventyConfig.addCollection('notes', (collection) => {
return collection.getFilteredByGlob('src/notes/*.md').filter((item) => {
return !item.data.draft
})
})
eleventyConfig.addCollection('untagged', (collection) => {
return collection.getFilteredByGlob('src/notes/*.md').filter((item) => {
return (
(!item.data.tags || item.data.tags.length === 0) && !item.data.draft
)
})
})
// Short codes
eleventyConfig.addShortcode('tagDisplay', function (tag) {
const tagName = slugify(tag)
const tagColor = getColourFromString(tagName)
return `<span class="badge" style="background-color: ${tagColor};">${tag}</span>`
})
eleventyConfig.addShortcode('tagLink', function (tag) {
const tagName = slugify(tag)
const tagColor = getColourFromString(tagName)
return `<a href="/tags/${tagName}" class="badge" style="background-color: ${tagColor};">${tag}</a>`
})
eleventyConfig.addShortcode('tagDot', function (tag) {
if (!tag) {
return `<span class="tagDot" style="background-color: #ddd;"></span>`
}
const tagName = slugify(tag)
const tagColor = getColourFromString(tagName)
return `<span class="tagDot" style="background-color: ${tagColor};"></span>`
})
eleventyConfig.addShortcode('algoliaIndex', function (notes) {
const algoliaIndex = notes.map((note) => {
return {
title: note.data.title,
url: note.url,
date: note.date,
emoji: note.data.emoji,
content: note.template.frontMatter.content,
tags: note.data?.tags?.length ? note.data.tags : [],
objectID: note.url,
}
})
return JSON.stringify(algoliaIndex)
})
eleventyConfig.addShortcode('oldNoteWarning', function (noteDate) {
const start = DateTime.fromJSDate(noteDate)
const end = DateTime.fromJSDate(new Date())
const diffInMonths = end.diff(start, 'months')
const { months } = diffInMonths.toObject() //=> { months: 1 }
if (months > 6) {
return `<div class="oldNoteWarning">
👋 FYI, this note is over 6 months old. Some of the content may be out of date.
</div>`
}
return ''
})
eleventyConfig.addPassthroughCopy('public')
eleventyConfig.setServerOptions({
// The starting port number
// Will increment up to (configurable) 10 times if a port is already in use.
port: 3456,
// Additional files to watch that will trigger server updates
// Accepts an Array of file paths or globs (passed to `chokidar.watch`).
// Works great with a separate bundler writing files to your output folder.
// e.g. `watch: ["_site/**/*.css"]`
watch: ['_site/css/**/*.css'],
})
return {
markdownTemplateEngine: false,
dataTemplateEngine: 'njk',
htmlTemplateEngine: 'njk',
dir: {
input: 'src',
},
}
}