-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path.eleventy.js
220 lines (191 loc) · 6.12 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
const pluginRss = require('@11ty/eleventy-plugin-rss');
const getShareImage = require('@jlengstorf/get-share-image').default;
const terser = require('terser');
const postcss = require('postcss');
const csso = require('csso');
const autoprefixer = require('autoprefixer');
const cloudinary = require('@jlengstorf/cloudinary-11ty-helpers')({
cloud_name: 'jlengstorf',
folder: 'jason.energy',
image_width: 680,
});
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPassthroughCopy({ 'site/static': '/' });
eleventyConfig.addCollection('posts', (collectionApi) => {
// I was already using tags, so this is a workaround
return collectionApi
.getAll()
.filter((item) => item.data.type === 'post')
.sort((a, b) => b.date - a.date);
});
eleventyConfig.addPlugin(require('eleventy-plugin-toc'), {
tags: ['h2'],
wrapper: 'div',
});
const markdown = require('markdown-it');
const mdAnchor = require('markdown-it-anchor');
const mdLib = markdown({ html: true })
.use(mdAnchor, {
slugify: (s) =>
s
.toLowerCase()
.replace(/['’]/i, '') // avoid "let’s" => "let-s"
.replace(/[^a-z0-9]+/g, '-') // convert "some text" => "some-text"
.replace(/^-|-$/g, ''), // avoid leading/trailing hyphens
})
.use(cloudinary.mdPlugin);
eleventyConfig.setLibrary('md', mdLib);
eleventyConfig.addTemplateFormats('site.js');
eleventyConfig.addExtension('site.js', {
outputFileExtension: 'min.js',
compile: function (contents) {
return async () => {
const ret = await terser.minify(contents);
return ret.code;
};
},
});
eleventyConfig.addTemplateFormats('css');
eleventyConfig.addExtension('css', {
outputFileExtension: 'css',
compile: function (contents, inputPath) {
return async () => {
postcss();
const css = await postcss([autoprefixer()]).process(contents, {
from: inputPath,
}).css;
const minifiedCss = csso.minify(css).css;
return minifiedCss;
};
},
});
/*
* usage: {{ relativeImagePath | cloudinary(page.inputPath) }}
*
* the image path needs to be relative to the file being processed (e.g. if
* the post is in `/posts/my-post.md` and the image is at
* `/posts/images/my-image.jpg`, you'd use "./images/my-image.jpg")
*
* as far as I can tell there's no way to get the current inputPath without
* passing it in via the filter, which feels a bit clunky but it works
*/
eleventyConfig.addNunjucksAsyncFilter('cloudinary', cloudinary.asyncFilter);
eleventyConfig.addFilter('seoImage', (content) => {
return getShareImage({
title: content.toUpperCase(),
cloudName: 'jlengstorf',
imagePublicID: 'jason.energy/og',
titleFont: 'jwf.otf',
titleExtraConfig: '_line_spacing_-25',
titleFontSize: 75,
textColor: '171321',
textLeftOffset: 354,
textAreaWidth: 920,
titleBottomOffset: 185,
});
});
eleventyConfig.addPairedShortcode('footnote', (content, { id }) => {
return `<a href="#${id}" id="${id}ref" class="footnote-phrase" aria-described-by="footnote-label-${id}">${content}</a>`;
});
eleventyConfig.addPairedShortcode('footnoteText', (content, { id }) => {
return `
<aside class="footnote" id="${id}">
<p class="sr-only" id="footnote-label-${id}">additional context</p>
<div class="footnote-content" data-visible="false">
${content} <a href="#${id}ref" class="footnote-backref" aria-label="Back to content">close</a>
</div>
</aside>
`;
});
eleventyConfig.addPairedShortcode('aside', (content, type = 'default') => {
const icons = {
default: {
src: 'https://res.cloudinary.com/jlengstorf/image/upload/w_80,f_auto,q_auto/v1593991250/jason.energy/light-mode.png',
alt: 'light bulb',
},
spicy: {
src: 'https://res.cloudinary.com/jlengstorf/image/upload/w_80,f_auto,q_auto/v1609402670/jason.energy/fire.png',
alt: 'fire',
},
};
const icon = icons[type] || icons.default;
return `
<aside class="post-aside">
<div class="post-aside-icon">
<img src="${icon.src}" alt="${icon.alt}" />
</div>
<div class="post-aside-content">
${content}
</div>
</aside>
`;
});
eleventyConfig.addPairedShortcode(
'tweet',
(content, { postUrl, retweetId = false }) => {
const url = new URL('https://twitter.com/');
if (retweetId) {
url.pathname = '/intent/retweet';
url.search = new URLSearchParams({ tweet_id: retweetId });
} else {
url.pathname = '/compose/tweet';
url.search = new URLSearchParams({
text: `“${content}” —@jlengstorf`,
url: postUrl,
related: 'jlengstorf',
});
}
return `
<div class="post-tweet-box">
<p>${content}</p>
<p class="post-tweet-link">
<a href="${url}">Tweet this</a>
</p>
</div>
`;
},
);
eleventyConfig.addPairedShortcode(
'figure',
(content, { caption, credit, creditLink, align = 'center' } = {}) => {
const captionText = caption ? `<span>${caption}</span>` : '';
const linkOpen = creditLink
? `<a class="post-figure-attribution-link" href="${creditLink}">`
: '';
const linkClose = creditLink ? `</a>` : '';
const imageCredit = credit
? `<small class="post-figure-attribution">Credit: ${linkOpen}${credit}${linkClose}</small>`
: '';
return `
<figure class="post-figure post-figure-align-${align}">
${content}
<figcaption class="post-figure-caption">
${captionText} ${imageCredit}
</figcaption>
</figure>
`;
},
);
eleventyConfig.addShortcode('uses', (item) => {
return `
<div class="uses-item">
<h3>${item.name}</h3>
<ul>${item.tags.map((tag) => `<li>${tag}</li>`).join(' ')}</ul>
<p>${item.details}</p>
<a href="${item.link}" class="uses-item-link ${
item.sponsored ? 'sponsored' : ''
}">
More <span class="sr-only">${item.name}</span> details →
</a>
</div>
`;
});
return {
dir: {
input: 'site',
output: 'dist',
},
markdownTemplateEngine: 'njk',
};
};