-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.eleventy.js
143 lines (116 loc) · 4.26 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
'use strict'
const path = require('path')
const fs = require('fs/promises')
const { pipeline } = require('stream/promises')
const { createWriteStream, createReadStream } = require('fs')
const htmlmin = require('html-minifier')
const Image = require('@11ty/eleventy-img')
const embedYouTube = require('eleventy-plugin-youtube-embed')
const axios = require('axios')
const sharp = require('sharp')
const striptags = require('striptags')
const { DateTime } = require('luxon')
const now = String(Date.now())
function extractExcerpt (content) {
let excerpt = null
excerpt = striptags(content)
.substring(0, 160)
.replace(/^\s+|\s+$|\s+(?=\s)/g, '')
.trim()
.concat('...')
return excerpt
}
async function imageShortcode (src, alt, sizes, _widths, _attrs) {
const widths = _widths || [300, 600]
const attrs = _attrs || {}
const metadata = await Image(src, {
widths,
formats: ['avif', 'jpeg'],
outputDir: './dist/img/'
})
const imageAttributes = {
alt,
sizes,
loading: 'lazy',
decoding: 'async',
...attrs
}
return Image.generateHTML(metadata, imageAttributes, {
whitespaceMode: 'inline'
})
}
module.exports = function (eleventyConfig) {
eleventyConfig.addWatchTarget('./src/_includes/styles/tailwind.css')
eleventyConfig.addPassthroughCopy({ './src/_includes/static/**': './' })
eleventyConfig.addNunjucksAsyncShortcode('image', imageShortcode)
eleventyConfig.addPlugin(embedYouTube)
eleventyConfig.addShortcode('excerpt', (article) => extractExcerpt(article))
eleventyConfig.addCollection('publishedEpisodes', function (collectionApi) {
// get episodes, sorted by publish date, descending
return collectionApi.getFilteredByTag('episode')
.filter((item) => item.data.publish_date <= item.data.settings.now)
.sort((a, b) => a.data.publish_date - b.data.publish_date)
})
// minify html pages
eleventyConfig.addTransform('htmlmin', function (content, outputPath) {
if (
process.env.ELEVENTY_PRODUCTION &&
outputPath?.endsWith('.html')
) {
const minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
})
return minified
}
return content
})
eleventyConfig.addShortcode('version', function () {
return now
})
eleventyConfig.addNunjucksFilter('youtubePreviewUrl', function (id) {
return `https://i.ytimg.com/vi/${id}/maxresdefault.jpg`
})
eleventyConfig.addNunjucksAsyncFilter('youtubePreview', function (id, episodeUrl, cb) {
(async () => {
const folderDest = path.join('dist', episodeUrl)
const dest = path.join('dist', episodeUrl, 'og_image.jpg')
const exists = await fs.stat(dest).then(() => true).catch(() => false)
if (!exists) {
const folderExists = await fs.stat(folderDest).then(() => true).catch(() => false)
if (!folderExists) {
await fs.mkdir(folderDest)
}
let imageStream = createReadStream(path.join(__dirname, 'src', '_includes', 'static', 'awsbites-og.png'))
try {
// trying to download this image from YouTube before the video is actually published will give us
// a 404. If that's the case, we don't want to break the build, so we will use a default image
const url = `https://i.ytimg.com/vi/${id}/maxresdefault.jpg`
const response = await axios.get(url, { responseType: 'stream' })
imageStream = response.data
} catch (_) { }
const transform = sharp().resize({ width: 1200, height: 630, fit: sharp.fit.cover })
const destFile = createWriteStream(dest)
await pipeline(imageStream, transform, destFile)
console.log(`Created ${dest}`)
}
return `https://awsbites.com${path.join(episodeUrl, 'og_image.jpg')}`
})().then((url) => cb(null, url)).catch(cb)
})
eleventyConfig.addFilter('youtubeLink', function (id) {
return `https://www.youtube.com/watch?v=${id}`
})
eleventyConfig.addFilter('formatDate', (dateObj, format = 'yyyy-MM-dd') => {
return DateTime.fromJSDate(dateObj).toFormat(format)
})
eleventyConfig.addFilter('formatDateISO', (dateObj) => {
return DateTime.fromJSDate(dateObj).toISO()
})
return {
dir: {
input: 'src',
output: 'dist'
}
}
}