-
Notifications
You must be signed in to change notification settings - Fork 3
/
.eleventy.js
129 lines (105 loc) · 3.72 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
/* eslint-disable no-empty */
import { promises as fs } from 'node:fs';
import path from 'node:path';
import { TemplatePath } from '@11ty/eleventy-utils';
import { mergeOptions } from './src/utils/index.js';
import { OgImage } from './src/OgImage.js';
/**
* @param {import('@11ty/eleventy/src/UserConfig').default} eleventyConfig
* @param {EleventyPluginOgImageOptions} pluginOptions
*/
export default async function (eleventyConfig, pluginOptions) {
/** @type {DirectoriesConfig} */
let directoriesConfig;
/** @type {EleventyPluginOgImageMergedOptions} */
let mergedOptions;
// Until https://github.com/11ty/eleventy/issues/2729 is fixed
eleventyConfig.on('eleventy.directories', (dir) => {
directoriesConfig = dir;
mergedOptions = mergeOptions({ directoriesConfig, pluginOptions });
});
/** @type {import('@11ty/eleventy/src/TemplateConfig').default} */
let templateConfig;
eleventyConfig.on('eleventy.config', (newTemplateConfig) => {
templateConfig = newTemplateConfig;
});
/** @type {boolean} */
let previewMode;
eleventyConfig.on('eleventy.before', async ({ runMode }) => {
try {
await fs.mkdir(mergedOptions.outputDir, { recursive: true });
} catch {}
previewMode = ['watch', 'serve'].includes(runMode);
if (previewMode) {
try {
await fs.mkdir(mergedOptions.previewDir, { recursive: true });
} catch {}
} else {
try {
await fs.rm(mergedOptions.previewDir, { recursive: true, force: true });
} catch {}
}
});
eleventyConfig.ignores.add(mergeOptions({ pluginOptions }).inputFileGlob);
eleventyConfig.addAsyncShortcode(
'ogImage',
/**
* @param {string} shortcodeInputPath
* @param {Record<string, any>} [data]
* @returns {Promise<string>}
*/
async function ogImageShortcode(shortcodeInputPath, data) {
if (this.page.url === false) {
return null;
}
const { satoriOptions, sharpOptions, ...options } = mergedOptions;
const joinedInputPath = TemplatePath.standardizeFilePath(path.join(directoriesConfig.input, shortcodeInputPath));
try {
await fs.access(joinedInputPath);
} catch {
throw new Error(`Could not find file for the \`ogImage\` shortcode, looking for: ${joinedInputPath}`);
}
const ogImage = new (pluginOptions.OgImage || OgImage)({
inputPath: joinedInputPath,
data: {
page: this.page,
eleventy: this.eleventy,
eleventyPluginOgImage: {
inputPath: joinedInputPath,
width: satoriOptions.width,
height: satoriOptions.height,
outputFileExtension: options.outputFileExtension,
},
...data,
},
options: mergedOptions,
templateConfig,
});
const outputFilePath = await ogImage.outputFilePath();
const cacheFilePath = await ogImage.cacheFilePath();
if (cacheFilePath !== outputFilePath) {
try {
await fs.copyFile(cacheFilePath, outputFilePath);
} catch {}
}
try {
await fs.access(outputFilePath);
} catch {
const image = await ogImage.render();
await image.toFile(outputFilePath);
eleventyConfig.logger.log(
`Writing ${TemplatePath.stripLeadingDotSlash(outputFilePath)} from ${joinedInputPath}`,
);
}
if (previewMode) {
const previewFilePath = ogImage.previewFilePath();
try {
await fs.mkdir(path.dirname(previewFilePath), { recursive: true });
} catch {}
await fs.copyFile(outputFilePath, previewFilePath);
await fs.writeFile(`${previewFilePath}.html`, await ogImage.previewHtml());
}
return ogImage.shortcodeOutput();
},
);
}