-
Notifications
You must be signed in to change notification settings - Fork 2
/
eleventy-plugin-sharp.js
113 lines (94 loc) · 2.99 KB
/
eleventy-plugin-sharp.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
const path = require('path');
const sharp = require('sharp');
const debug = require('debug')('EleventyPluginSharp');
const exclude = [
'clone',
'metadata',
'stats',
'trim', // trim is a built-in filter for most templating languages
'toFile',
'toBuffer',
];
const forwardMethods = Object.keys(sharp.prototype).filter((method) => {
return !method.startsWith('_') && !exclude.includes(method);
});
const globalOptions = {
urlPath: '/images',
outputDir: 'public/images',
};
function createSharpPlugin(options) {
options = Object.assign({}, globalOptions, options);
return function sharpPlugin(eleventyConfig) {
/**
* Sharp filters
*/
eleventyConfig.addFilter('sharp', sharp);
forwardMethods.forEach((method) => {
eleventyConfig.addFilter(method, function (instance, ...args) {
if (typeof instance === 'string') instance = sharp(instance);
return instance[method](...args);
});
});
eleventyConfig.addFilter('toFile', function (instance, fileOut) {
if (typeof instance === 'string') instance = sharp(instance);
instance.fileOut = fileOut;
return instance;
});
/**
* Async shortcodes
*/
eleventyConfig.addAsyncShortcode('getUrl', async function (instance) {
if (!instance.fileOut) {
instance.fileOut = path.join(
options.outputDir,
createFileOutName(instance.options)
);
}
debug('Writing %o', instance.fileOut);
await instance.toFile(instance.fileOut);
return path.join(options.urlPath, path.basename(instance.fileOut));
});
eleventyConfig.addAsyncShortcode('getWidth', async function (instance) {
const metadata = await getMetadata(instance);
return `${metadata.width}`;
});
eleventyConfig.addAsyncShortcode('getHeight', async function (instance) {
const metadata = await getMetadata(instance);
return `${metadata.height}`;
});
eleventyConfig.addAsyncShortcode('getMetadata', getMetadata);
function getMetadata(instance) {
if (typeof instance === 'string') instance = sharp(instance);
return instance.metadata();
}
eleventyConfig.addAsyncShortcode('getStats', function (instance) {
if (typeof instance === 'string') instance = sharp(instance);
return instance.stats();
});
};
}
/**
* Adds a postfix to the file out name.
* @param {Object} options
* @return {String}
*/
function createFileOutName(opts) {
const { width, height, formatOut } = opts;
let postfix = '';
if (width > 0 || height > 0) {
if (width) {
postfix += width;
if (height) postfix += 'x';
}
if (height) {
postfix += height;
if (!width) postfix += 'h';
}
}
if (postfix) postfix = `-${postfix}`;
const input = opts.input.file;
const extname = formatOut === 'input' ? path.extname(input) : `.${formatOut}`;
const basename = path.basename(input, path.extname(input));
return `${basename}${postfix}${extname}`;
}
module.exports = createSharpPlugin;