-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
238 lines (201 loc) · 6.92 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
const { DateTime, Settings } = require("luxon");
const fs = require("fs");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const pluginNavigation = require("@11ty/eleventy-navigation");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const readingTime = require('eleventy-plugin-reading-time');
const Image = require('@11ty/eleventy-img');
const path = require("path");
Settings.defaultLocale = 'es-MX';
function structuredData(data) {
const HEADLINE_CHARACTER_LIMIT = 110
if (!data || !data.type) {
return ''
}
const { type, headline, datePublished, dateModified, author, keywords, image } = data
const validHeadline = headline.slice(0, HEADLINE_CHARACTER_LIMIT)
const validKeywords = keywords
.split(',')
.filter(it => it !== 'posts' && it !== 'books')
const types = { post: 'BlogPosting' }
const jsonLd = {
"@context": "https://schema.org",
"@type": types[type],
headline: validHeadline,
keywords: validKeywords,
datePublished,
dateModified,
author: {
"@type": "Person",
name: author.name,
url: author.url
},
inLanguage: "es-MX",
image
}
return `<script type="application/ld+json">${JSON.stringify(jsonLd)}</script>`
}
function imageShortCode(src, alt, altShouldBeCaption = true, caption = '', loading = 'lazy', classes = "", sizes = "(min-width: 30em) 50vw, 100vw") {
const options = {
widths: [600, 640, 800, 1200],
formats: ['webp', 'jpeg'],
sharpOptions: {
animated: true
},
filenameFormat: ((id, src, width, format, options) => {
const extension = path.extname(src);
const second_to_last_part = path.normalize(src).split("/").reverse()[1]
const name = path.basename(src, extension)
return `${second_to_last_part}-${name}-${width}w.${format}`;
}),
urlPath: "/img/",
outputDir: "./_site/img",
useCache: true
}
Image(src, options);
const imageAttributes = {
class: classes,
alt,
sizes,
loading,
decoding: 'async',
}
const metadata = Image.statsSync(src, options)
const html = Image.generateHTML(metadata, imageAttributes, { whitespaceMode: 'inline' })
const figureCaption = altShouldBeCaption ? alt : caption
return figureCaption ?
`<figure>${html}<figcaption>${figureCaption}</figcaption></figure>` :
html
}
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginNavigation);
eleventyConfig.addPlugin(readingTime);
eleventyConfig.setDataDeepMerge(true);
eleventyConfig.addLayoutAlias("post", "layouts/post.njk");
eleventyConfig.addFilter("readableDate", dateObj => {
if (dateObj instanceof String) {
dateObj = new Date(dateObj);
}
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat("dd LLLL yyyy");
});
eleventyConfig.addFilter("toISOString", dateObj => {
if (dateObj instanceof String) {
dateObj = new Date(dateObj)
}
return dateObj.toISOString();
});
eleventyConfig.addFilter("getTime", dateObj => {
if (dateObj instanceof String) {
dateObj = new Date(dateObj)
}
return dateObj.getTime();
});
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-date-string
eleventyConfig.addFilter('htmlDateString', dateObj => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy-LL-dd');
});
eleventyConfig.addFilter('slugDate', dateObj => {
return DateTime.fromJSDate(dateObj, { zone: 'utc' }).toFormat('yyyy/LL/dd');
})
// Get the first `n` elements of a collection.
eleventyConfig.addFilter("head", (array, n) => {
if (n < 0) {
return array.slice(n);
}
return array.slice(0, n);
});
const now = Date.now()
const shouldBeLive = post => post.data.published_at <= now && !post.data.draft;
eleventyConfig.addCollection("publishables", function (collectionApi) {
return collectionApi
.getFilteredByGlob(["posts/*.md", "books/*.md", "atomic_essays/*.md", "newsletters/*.md"])
.filter(shouldBeLive);
});
eleventyConfig.addCollection("rssables", function (collectionApi) {
return collectionApi
.getFilteredByGlob(["posts/*.md", "books/*.md", "atomic_essays/*.md"])
.filter(shouldBeLive);
});
eleventyConfig.addCollection("posts", function (collectionApi) {
return collectionApi
.getFilteredByGlob("posts/*.md")
.filter(shouldBeLive);
});
eleventyConfig.addCollection("books", function (collectionApi) {
return collectionApi
.getFilteredByGlob("books/*.md")
.filter(shouldBeLive);
});
eleventyConfig.addCollection("postsAndBooks", function (collectionApi) {
return collectionApi
.getFilteredByGlob(["posts/*.md", "books/*.md"])
.filter(shouldBeLive);
});
eleventyConfig.addCollection("atomic_essays", function (collectionApi) {
return collectionApi
.getFilteredByGlob('atomic_essays/*.md')
.filter(shouldBeLive);
});
eleventyConfig.addCollection("faqs", function (collectionApi) {
return collectionApi
.getFilteredByGlob('faqs/*.md')
});
eleventyConfig.addPassthroughCopy("img");
eleventyConfig.addPassthroughCopy("css");
eleventyConfig.addPassthroughCopy('robots.txt')
eleventyConfig.addPassthroughCopy('humans.txt')
eleventyConfig.addPassthroughCopy('manifest.json')
eleventyConfig.addPassthroughCopy('.well-known')
/* Markdown Overrides */
let markdownLibrary = markdownIt({
html: true,
breaks: true,
linkify: true
}).use(markdownItAnchor, {
permalink: true,
permalinkClass: "direct-link",
permalinkSymbol: "🔗"
}).use(require("markdown-it-toc-done-right"), {
level: [1, 2, 3]
}).disable('code');
eleventyConfig.setLibrary("md", markdownLibrary);
eleventyConfig.addNunjucksShortcode("image", imageShortCode);
eleventyConfig.addNunjucksShortcode("structured_data", structuredData);
eleventyConfig.on('eleventy.after', async () => {
try {
const dir = '_site/newsletters'
if(fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true });
}
} catch(error) {
console.error(error)
}
})
return {
templateFormats: [
"md",
"njk",
"html",
"liquid"
],
// If your site lives in a different subdirectory, change this.
// Leading or trailing slashes are all normalized away, so don’t worry about those.
// If you don’t have a subdirectory, use "" or "/" (they do the same thing)
// This is only used for link URLs (it does not affect your file structure)
// Best paired with the `url` filter: https://www.11ty.io/docs/filters/url/
// You can also pass this in on the command line using `--pathprefix`
// pathPrefix: "/",
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk",
dataTemplateEngine: "njk",
// These are all optional, defaults are shown:
dir: {
input: ".",
includes: "_includes",
data: "_data",
output: "_site"
}
};
};