-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
122 lines (99 loc) · 3.11 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
const fs = require('node:fs')
const path = require('node:path')
const gulp = require('gulp')
const EdgeJsPlugin = require('@web-alchemy/eleventy-plugin-edgejs')
const { parseHTML } = require('linkedom')
const Collection = require('./src/core/collection.js')
const { render } = require('./src/core/markdown')
const articleBodyBlocks = require('./src/transforms/article-body-blocks.js')
const config = require('./config.js')
const staticFilesExt = [
'jpeg',
'jpg',
'png',
'svg',
'webp',
'avif'
]
module.exports = function(eleventyConfig) {
eleventyConfig.setQuietMode(true)
// copy static assets
gulp.src('src/public/**/*.*')
.pipe(gulp.dest('dist'))
gulp.src(`src/data/**/*.{${staticFilesExt.join(',')}}`)
.pipe(gulp.dest('dist'))
eleventyConfig.addPlugin(EdgeJsPlugin, {
extendEdgeInstance: function (edge) {
class DevManifest {
getResource(name) {
return (new URL(name, 'http://localhost:3000')).toString()
}
}
class ProdManifest {
constructor(manifest) {
this.manifest = manifest
}
getResource(name) {
return '/' + this.manifest[name]?.file
}
}
const manifest = config.isProd
? new ProdManifest(JSON.parse(fs.readFileSync(path.join(process.cwd(), config.folders.dest, config.viteManifestFileName), { encoding: 'utf-8' })))
: new DevManifest()
edge.global('entry', (entryName) => {
return manifest.getResource(entryName)
})
},
skipRenderCondition: function (inputContent, inputPath) {
return !['entries', 'layouts'].some(pathSegment => inputPath.includes(pathSegment))
}
})
eleventyConfig.addExtension('md', {
compile: async function (inputContent, inputPath) {
const html = await render(inputContent)
return async function(data) {
return html
}
}
})
eleventyConfig.addCollection('people', (collectionAPI) => {
return new Collection({
items: collectionAPI.getFilteredByGlob('src/data/people/*/index.md')
})
})
eleventyConfig.addCollection('categories', (collectionAPI) => {
return new Collection({
items: collectionAPI.getFilteredByGlob('src/data/categories/*/index.md')
})
})
eleventyConfig.addCollection('articles', (collectionAPI) => {
const articles = collectionAPI.getFilteredByGlob('src/data/articles/*/index.md')
articles.sort((a, b) => b.data.createdAt - a.data.createdAt)
return new Collection({
items: articles
})
})
eleventyConfig.addTransform('article-body-transform', async function(content, outputPath) {
if (!outputPath || !outputPath.endsWith('.html')) {
return content
}
const DOM = parseHTML(content)
const articleBodyTransforms = [
articleBodyBlocks
]
for (const transform of articleBodyTransforms) {
await transform(DOM, content, outputPath)
}
return DOM.document.toString()
})
return {
dir: {
input: config.folders.src,
output: config.folders.dest,
layouts: 'layouts',
data: 'global-data'
},
jsDataFileSuffix: '.data',
pathPrefix: '/',
}
}