-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheleventy.config.js
More file actions
193 lines (155 loc) · 5.42 KB
/
Copy patheleventy.config.js
File metadata and controls
193 lines (155 loc) · 5.42 KB
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
import "dotenv/config";
import * as fs from "fs";
import path from "path";
import { hash } from "crypto";
import { HtmlBasePlugin } from "@11ty/eleventy";
import { eleventyImageTransformPlugin } from "@11ty/eleventy-img";
import EleventyPluginOgImage from 'eleventy-plugin-og-image';
import { eleventyUmamiTransformPlugin } from "eleventy-plugin-umami-analytics";
const isProduction = process.env.ELEVENTY_ENV === "production";
const siteUrl = isProduction ? process.env.PROD_SITE_URL : process.env.DEV_SITE_URL
const siteHost = new URL(siteUrl).host;
const config = {
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk",
templateFormats: [
"html",
"njk",
"md",
"11ty.js"
],
dir: {
input: "src",
output: "_site",
includes: "_includes",
layouts: "_includes/layout",
data: "_data"
}
};
// site is custom config
const site = {
isProduction: isProduction,
url: siteUrl,
host: siteHost,
dir: {
apps: "apps",
config: "_config",
...config.dir // save relative paths
},
umami: {
scriptUrl: process.env.UMAMI_SCRIPT_URL,
websiteId: process.env.UMAMI_WEBSITE_ID
}
}
// ignore these files from apps
const ignored = [
".11tydata.js",
"README",
"LICENSE",
"CHANGELOG",
"node_modules",
"package.json",
"package-lock.json",
"pnpm-lock.yaml",
"yarn.lock",
".html",
]
// dynamic path, so dynamic importing
const configDir = `./${site.dir.input}/${site.dir.config}`;
const { default: pluginAddHtmlHead } = await import(`${configDir}/add-html-head.js`);
const { default: pluginBase64Image } = await import(`${configDir}/base64-image.js`);
const { default: pluginGenFavicons } = await import(`${configDir}/gen-favicons.js`);
const { default: CustomOgImage } = await import(`${configDir}/custom-og-image.js`);
function normalizeSlashes(path) {
return path.replaceAll("\\", "/");
}
// convert an url-safe base64 hash to hex
function base64HashToHex(hash) {
// replace urlsafe characters
let base64 = hash
.replace(/-/g, "+")
.replace(/_/g, "/");
// restore padding
while (base64.length % 4 !== 0) {
base64 += "=";
}
const binary = atob(base64);
// convert to hex
return Array.from(binary, c =>
c.charCodeAt(0).toString(16).padStart(2, "0")
).join("");
}
// main config function
async function eleventyConfiguration(eleventyConfig) {
// refresh when .env changes
if (!site.isProduction) eleventyConfig.addWatchTarget(".env");
// process files in .gitignore
eleventyConfig.setUseGitIgnore(false);
// expose "site" to templates
eleventyConfig.addGlobalData("site", site);
const appPaths = new Set();
eleventyConfig.addCollection("apps", (collectionApi) => {
const items = collectionApi.getFilteredByTag("app");
// save urls
for (const item of items) appPaths.add(item.inputPath);
return items;
});
// pass assets
const assetsPath = normalizeSlashes(path.join(config.dir.input, "assets"));
eleventyConfig.addPassthroughCopy(assetsPath);
// pass app assets (source/apps/abc -> output/abc)
const appsPath = normalizeSlashes(path.join(config.dir.input, site.dir.apps));
eleventyConfig.addPassthroughCopy({ [appsPath]: "./" }, {
filter: (path) => {
const normalized = normalizeSlashes(path);
const parts = normalized.split("/");
// ignore hidden files and directories
if (parts.some((part) => part.startsWith("."))) {
return false;
}
return ignored.every((s) => !normalized.includes(s));
}
});
// custom plugins
eleventyConfig.addPlugin(pluginBase64Image);
eleventyConfig.addPlugin(pluginAddHtmlHead);
eleventyConfig.addPlugin(pluginGenFavicons);
// @11ty plugins
eleventyConfig.addPlugin(HtmlBasePlugin);
eleventyConfig.addPlugin(eleventyImageTransformPlugin, {
formats: ["svg", "webp", "png"],
urlPath: "/assets/img",
hashLength: 43, // always 43 characters long: 32 (sha256) -> 44 (base64 encoded) -> 43 (stripped "=")
filenameFormat: function (id, src, width, format, options) {
// reverse the sha256 hash and turn it into hex
const hashHex = base64HashToHex(id);
const truncated = hashHex.substring(0, 8);
return `${truncated}.${width}.${format}`
},
transformOnRequest: false
});
// eleventy-plugin-og-image plugin
const fontPath = normalizeSlashes(path.join(config.dir.input, config.dir.includes, "/og-image/BitstreamVera/VeraMono.ttf"));
eleventyConfig.addPlugin(EleventyPluginOgImage, {
OgImage: CustomOgImage,
satoriOptions: {
fonts: [{
name: "Vera Mono",
data: fs.readFileSync(fontPath),
weight: 400,
style: "normal"
}]
},
shortcodeOutput: async (ogImage) => await ogImage.outputUrl(),
outputDir: "/assets/img/social",
previewMode: false
});
// eleventy-plugin-umami-analytics plugin
site.isProduction && eleventyConfig.addPlugin(eleventyUmamiTransformPlugin, {
umami: {
url: site.umami.scriptUrl,
websiteId: site.umami.websiteId,
},
});
}
export { config, site, eleventyConfiguration as default };