-
Notifications
You must be signed in to change notification settings - Fork 15
/
.eleventy.js
120 lines (103 loc) · 3.13 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
const markdownIt = require('markdown-it')
const markdownItAnchor = require('markdown-it-anchor')
const yaml = require('js-yaml')
const path = require('path')
const fs = require('fs')
const markdownLibrary = markdownIt({
html: true,
breaks: true,
linkify: true,
}).use(markdownItAnchor)
// Filter to remove “runts” (wrapping last word of text)
//
// Usage:
//
// ```njk
// {{ 'some text that should not have runts` | noRunts | safe }}
// ```
//
// Expect:
//
// ```html
// some text that should not <span style="white-space:nowrap;">have runts</span>
// ```
const noRuntsFilter = value => value.replace(
/(\S+\s+\S+\s+)(\S+\s+\S+)$/,
'$1<span style="white-space:nowrap;">$2</span>'
)
// Filter to read a file
//
// Usage:
//
// ```njk
// {{ 'path/to/file.svg' | readFile | safe }}
// ```
const readFile = (...pathSegments) => {
const filePath = path.resolve(...pathSegments)
return fs.readFileSync(filePath)
}
// Filter to add cache busting query string to URL
//
// Throws an error if the cache buster key is already in the URL.
//
// Usage:
//
// ```njk
// <link rel="stylesheet" href="{{ '/css/style.css' | cacheBuster }}">
// <link rel="stylesheet" href="{{ '/css/style.css' | cacheBuster('v') }}">
// ```
//
// Expect:
//
// ```html
// <link rel="stylesheet" href="/css/style.css?cache-buster=1234567">
// <link rel="stylesheet" href="/css/style.css?v=1234567">
// ```
const cacheBuster = (value, key = 'cache-buster') => {
const base = 'https://example.com' // Default base so that URL can parse relative URLs.
const url = new URL(value, base) // If url is an absolute URL, base will be ignored.
if (url.searchParams.has(key)) {
throw new Error(`Cache buster key conflicts with existing search parameter: ${key} in ${value}`)
}
url.searchParams.set(key, new Date().valueOf())
return url.toString().replace(base, '')
}
module.exports = function (eleventyConfig) {
eleventyConfig.addPassthroughCopy('assets')
eleventyConfig.addPassthroughCopy('content/robots.txt*')
eleventyConfig.addPassthroughCopy('content/**/*.pdf')
eleventyConfig.addPassthroughCopy({
'assets/logos/favicon.ico': 'favicon.ico',
})
const years = [2016, 2017, 2018, 2019, 2020]
years.forEach(year => {
eleventyConfig.addPassthroughCopy({
[`archive/archive-${year}`]: `archive-${year}`,
[`archive/assets-${year}`]: `assets-${year}`,
})
})
eleventyConfig.setUseGitIgnore(false)
eleventyConfig.setLibrary('md', markdownLibrary)
// Extra data formats
eleventyConfig.addDataExtension('yml', contents => yaml.load(contents))
eleventyConfig.addDataExtension('yaml', contents => yaml.load(contents))
// Filters
eleventyConfig.addFilter('noRunts', noRuntsFilter)
eleventyConfig.addFilter('readFile', readFile)
eleventyConfig.addFilter('cacheBuster', cacheBuster)
return {
// Use nunjucks for template usage (like includes) within Markdown files
markdownTemplateEngine: 'njk',
templateFormats: [
'md',
'njk',
],
dir: {
input: 'content/',
includes: '_includes',
layouts: '_layouts',
output: 'public',
},
passthroughFileCopy: true,
}
}