This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
323 lines (282 loc) · 11.2 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const fs = require("fs/promises")
const { DateTime } = require("luxon")
const htmlmin = require("html-minifier")
const ErrorOverlay = require("eleventy-plugin-error-overlay")
const pluginRss = require("@11ty/eleventy-plugin-rss")
const svgContents = require("eleventy-plugin-svg-contents")
const path = require('path')
const Image = require("@11ty/eleventy-img")
const INPUT_DIR = "src"
const OUTPUT_DIR = "_site"
// This will change both Eleventy's pathPrefix, and the one output by the
// vite-related shortcodes below. Double-check if you change this, as this is only a demo :)
const PATH_PREFIX = "/"
async function imageShortcode(src, alt) {
let sizes = "(min-width: 1024px) 100vw, 50vw"
let srcPrefix = `./src/assets/images/`
src = srcPrefix + src
console.log(`Generating image(s) from: ${src}`)
if(alt === undefined) {
// Throw an error on missing alt (alt="" works okay)
throw new Error(`Missing \`alt\` on responsive image from: ${src}`)
}
let metadataImg = await Image(src, {
widths: [600, 900, 1500],
formats: ['webp', 'jpeg'],
urlPath: "/images/",
outputDir: "./_site/images/",
filenameFormat: function (id, src, width, format, options) {
const extension = path.extname(src)
const name = path.basename(src, extension)
return `${name}-${width}w.${format}`
}
})
let lowsrc = metadataImg.jpeg[0]
let highsrc = metadataImg.jpeg[metadataImg.jpeg.length - 1]
return `<picture>
${Object.values(metadataImg).map(imageFormat => {
return ` <source type="${imageFormat[0].sourceType}" srcset="${imageFormat.map(entry => entry.srcset).join(", ")}" sizes="${sizes}">`
}).join("\n")}
<img
src="${lowsrc.url}"
width="${highsrc.width}"
height="${highsrc.height}"
alt="${alt}"
loading="lazy"
decoding="async">
</picture>`
}
module.exports = function (eleventyConfig) {
eleventyConfig.addNunjucksAsyncShortcode("image", imageShortcode)
eleventyConfig.addLiquidShortcode("image", imageShortcode)
// === Liquid needed if `markdownTemplateEngine` **isn't** changed from Eleventy default
eleventyConfig.addJavaScriptFunction("image", imageShortcode)
eleventyConfig.addPlugin(pluginRss)
eleventyConfig.addPlugin(svgContents)
eleventyConfig.addPlugin(ErrorOverlay)
eleventyConfig.setQuietMode(true)
eleventyConfig.addPassthroughCopy("browserconfig.xml")
eleventyConfig.addPassthroughCopy("favicon.ico")
eleventyConfig.addPassthroughCopy("robots.txt")
eleventyConfig.addPassthroughCopy("./src/assets/fonts")
eleventyConfig.addPassthroughCopy("./src/assets/js")
eleventyConfig.addPassthroughCopy("./src/assets/svg")
eleventyConfig.addPassthroughCopy("./src/images") // not just icons due to that one OG image
eleventyConfig.setUseGitIgnore(false) // for the sake of CSS generated just for `head`
/* --- date-handling --- */
eleventyConfig.addFilter("readableDate", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "utc" }).toFormat(
"dd LLL yyyy"
)
})
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
eleventyConfig.addFilter("htmlDateString", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: 'America/Chicago' }).toFormat("MMMM d, yyyy")
})
eleventyConfig.addFilter("dateStringISO", (dateObj) => {
return DateTime.fromJSDate(dateObj).toFormat("yyyy-MM-dd")
})
eleventyConfig.addFilter("dateFromTimestamp", (timestamp) => {
return DateTime.fromISO(timestamp, { zone: "utc" }).toJSDate()
})
eleventyConfig.addFilter("dateFromRFC2822", (timestamp) => {
return DateTime.fromJSDate(timestamp).toISO()
})
eleventyConfig.addFilter("readableDateFromISO", (dateObj) => {
return DateTime.fromISO(dateObj).toFormat("LLL d, yyyy h:mm:ss a ZZZZ")
})
eleventyConfig.addFilter("pub_lastmod", (dateObj) => {
return DateTime.fromJSDate(dateObj, { zone: "America/Chicago" }).toFormat(
"MMMM d, yyyy"
)
})
/* --- end, date-handling */
// https://www.11ty.dev/docs/layouts/
eleventyConfig.addLayoutAlias("base", "layouts/_default/base.njk")
eleventyConfig.addLayoutAlias("singlepost", "layouts/posts/singlepost.njk")
eleventyConfig.addLayoutAlias("index", "layouts/_default/index.njk")
eleventyConfig.addLayoutAlias("contact", "layouts/contact/contact.njk")
eleventyConfig.addLayoutAlias("privacy", "layouts/privacy/privacy.njk")
eleventyConfig.addLayoutAlias("sitemap", "layouts/sitemap/sitemap.njk")
/* --- Markdown handling --- */
// https://www.11ty.dev/docs/languages/markdown/
// --and-- https://github.com/11ty/eleventy-base-blog/blob/master/.eleventy.js
// --and-- https://github.com/planetoftheweb/seven/blob/master/.eleventy.js
let markdownIt = require("markdown-it")
let markdownItFootnote = require("markdown-it-footnote")
let markdownItAttrs = require("markdown-it-attrs")
let markdownItBrakSpans = require("markdown-it-bracketed-spans")
let markdownItPrism = require("markdown-it-prism")
let markdownItLinkAttrs = require("markdown-it-link-attributes")
let markdownItAnchor = require("markdown-it-anchor")
let markdownItOpts = {
html: true,
linkify: false,
typographer: true,
}
const markdownEngine = markdownIt(markdownItOpts)
markdownEngine.use(markdownItFootnote)
markdownEngine.use(markdownItAttrs)
markdownEngine.use(markdownItAnchor, {
permalink: markdownItAnchor.permalink.headerLink()
})
markdownEngine.use(markdownItBrakSpans)
markdownEngine.use(markdownItPrism)
markdownEngine.use(markdownItLinkAttrs, {
pattern: /^https:/,
attrs: {
target: "_blank",
rel: "noreferrer noopener",
},
})
// START, de-bracketing footnotes
//--- see http://dirtystylus.com/2020/06/15/eleventy-markdown-and-footnotes/
markdownEngine.renderer.rules.footnote_caption = (tokens, idx) => {
let n = Number(tokens[idx].meta.id + 1).toString()
if (tokens[idx].meta.subId > 0) {
n += ":" + tokens[idx].meta.subId
}
return n
}
// END, de-bracketing footnotes
eleventyConfig.setLibrary("md", markdownEngine)
/* --- end, Markdown handling --- */
eleventyConfig.addWatchTarget("src/**/*.js")
eleventyConfig.addWatchTarget("./src/assets/css/*.css")
// eleventyConfig.addWatchTarget("./src/assets/scss/*.scss")
eleventyConfig.addWatchTarget("./src/**/*.md")
eleventyConfig.addShortcode(
"imgc",
require("./src/assets/utils/imgc.js")
)
eleventyConfig.addShortcode(
"disclaimer",
require("./src/assets/utils/disclaimer.js")
)
eleventyConfig.addTransform("htmlmin", function (content, outputPath) {
if (outputPath && outputPath.endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
})
return minified
}
return content
})
/* === START, prev/next posts stuff === */
// https://github.com/11ty/eleventy/issues/529#issuecomment-568257426
eleventyConfig.addCollection("posts", function (collection) {
const coll = collection.getFilteredByTag("post")
for (let i = 0; i < coll.length; i++) {
const prevPost = coll[i - 1]
const nextPost = coll[i + 1]
coll[i].data["prevPost"] = prevPost
coll[i].data["nextPost"] = nextPost
}
return coll
})
/* === END, prev/next posts stuff === */
// Read Vite's manifest.json, and add script tags for the entry files
// You could decide to do more things here, such as adding preload/prefetch tags
// for dynamic segments
// NOTE: There is some hard-coding going on here, with regard to the assetDir
// and location of manifest.json
// you could probably read vite.config.js and get that information directly
// @see https://vitejs.dev/guide/backend-integration.html
eleventyConfig.addNunjucksAsyncShortcode("viteScriptTag", viteScriptTag)
eleventyConfig.addNunjucksAsyncShortcode(
"viteLinkStylesheetTags",
viteLinkStylesheetTags
)
eleventyConfig.addNunjucksAsyncShortcode(
"viteLinkModulePreloadTags",
viteLinkModulePreloadTags
)
async function viteScriptTag(entryFilename) {
const entryChunk = await getChunkInformationFor(entryFilename)
return `<script type="module" src="${PATH_PREFIX}${entryChunk.file}"></script>`
}
/* Generate link[rel=modulepreload] tags for a script's imports */
/* TODO(fpapado): Consider link[rel=prefetch] for dynamic imports, or some other signifier */
async function viteLinkModulePreloadTags(entryFilename) {
const entryChunk = await getChunkInformationFor(entryFilename)
if (!entryChunk.imports || entryChunk.imports.length === 0) {
console.log(
`The script for ${entryFilename} has no imports. Nothing to preload.`
)
return ""
}
/* There can be multiple import files per entry, so assume many by default */
/* Each entry in .imports is a filename referring to a chunk in the manifest; we must resolve it to get the output path on disk.
*/
const allPreloadTags = await Promise.all(
entryChunk.imports.map(async (importEntryFilename) => {
const chunk = await getChunkInformationFor(importEntryFilename)
return `<link rel="modulepreload" href="${PATH_PREFIX}${chunk.file}"></link>`
})
)
return allPreloadTags.join("\n")
}
async function viteLinkStylesheetTags(entryFilename) {
const entryChunk = await getChunkInformationFor(entryFilename)
if (!entryChunk.css || entryChunk.css.length === 0) {
console.warn(`No css found for ${entryFilename} entry. Is that correct?`)
return "";
}
/* There can be multiple CSS files per entry, so assume many by default */
return entryChunk.css
.map(
(cssFile) =>
`<link rel="stylesheet" href="${PATH_PREFIX}${cssFile}"></link>`
)
.join("\n")
}
async function getChunkInformationFor(entryFilename) {
// We want an entryFilename, because in practice you might have multiple entrypoints
// This is similar to how you specify an entry in development more
if (!entryFilename) {
throw new Error(
"You must specify an entryFilename, so that vite-script can find the correct file."
)
}
// TODO: Consider caching this call, to avoid going to the filesystem every time
const manifest = await fs.readFile(
path.resolve(process.cwd(), "_site", "manifest.json")
)
const parsed = JSON.parse(manifest)
let entryChunk = parsed[entryFilename]
if (!entryChunk) {
const possibleEntries = Object.values(parsed)
.filter((chunk) => chunk.isEntry === true)
.map((chunk) => `"${chunk.src}"`)
.join(`, `)
throw new Error(
`No entry for ${entryFilename} found in _site/manifest.json. Valid entries in manifest: ${possibleEntries}`
)
}
return entryChunk
}
return {
templateFormats: [
"html",
"md",
"njk",
"11ty.js"
],
pathPrefix: PATH_PREFIX,
htmlTemplateEngine: "njk",
markdownTemplateEngine: "njk",
passthroughFileCopy: true,
dataTemplateEngine: "njk",
passthroughFileCopy: true,
dir: {
input: INPUT_DIR,
output: OUTPUT_DIR,
// NOTE: These two paths are relative to dir.input
// @see https://github.com/11ty/eleventy/issues/232
includes: "_includes",
data: "../_data",
},
}
}