/
.eleventy.js
76 lines (61 loc) · 2.44 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
const fs = require('fs');
const fetch = require('node-fetch');
const sharp = require('sharp');
const { TemplatePath } = require('@11ty/eleventy-utils');
const { mergeOptions } = require('./src/mergeOptions');
const { getOutputParameters } = require('./src/getOutputParameters');
const { renderOgImage } = require('./src/renderOgImage');
globalThis.fetch = fetch;
/**
* @param { import('@11ty/eleventy/src/UserConfig') } eleventyConfig
* @param { import('eleventy-plugin-og-image').EleventyPluginOgImageOptions } pluginOptions
* */
module.exports = (eleventyConfig, pluginOptions) => {
let directoriesConfig;
eleventyConfig.on('eleventy.directories', (dir) => {
directoriesConfig = dir;
});
/**
* @type { import('@11ty/eleventy/src/TemplateConfig') }
*/
let templateConfig;
eleventyConfig.on('eleventy.config', (config) => {
templateConfig = config;
});
eleventyConfig.on('eleventy.before', () => {
const options = mergeOptions({ directoriesConfig, pluginOptions });
fs.rmSync(options.outputDir, { recursive: true, force: true });
});
eleventyConfig.ignores.add(mergeOptions({ directoriesConfig: undefined, pluginOptions }).inputFileGlob);
eleventyConfig.addAsyncShortcode(
'ogImage',
/**
*
* @param { string } inputPath
* @param { Record<string, any> } [data]
*
* @returns {Promise<string>}
*/
async function ogImage(inputPath, data) {
if (this.page.url === false) {
return null;
}
const { satoriOptions, sharpOptions, ...options } = mergeOptions({ directoriesConfig, pluginOptions });
if (!fs.existsSync(TemplatePath.normalizeOperatingSystemFilePath(inputPath))) {
throw new Error(`Could not find file for the \`ogImage\` shortcode, looking for: ${inputPath}`);
}
const { svg, pngBuffer } = await renderOgImage({ inputPath, data, satoriOptions, templateConfig });
const image = await sharp(pngBuffer).toFormat(options.outputFileExtension, sharpOptions);
const { outputFilePath, outputUrl } = getOutputParameters({
options,
fileSlug: options.getOutputFileSlug({ inputPath, data, svg, context: this }),
});
if (!fs.existsSync(options.outputDir)) {
fs.mkdirSync(options.outputDir, { recursive: true });
}
await image.toFile(outputFilePath);
eleventyConfig.logger.log(`Writing ${outputFilePath} from ${inputPath}`);
return options.generateHTML(outputUrl);
},
);
};