-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.eleventy.js
executable file
·172 lines (143 loc) · 5.33 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
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
"use strict";
const sharp = require('sharp');
const rimraf = require('rimraf');
const path = require('path');
const fs = require('fs-extra');
const {
JSDOM
} = require('jsdom');
const crypto = require('crypto');
module.exports = function(eleventyConfig, options) {
const defaults = {
autoselector: '.page-body img',
srcsetWidths: [320, 480, 640, 960, 1280, 1600],
fallbackWidth: 640,
fallbackHeight: null,
createCaptionFromTitle: false,
resizeOriginal: true,
cropPosition: "gravity.center",
dirs: {
temp: "./.tmp/",
input: "./src/",
output: "./dist/"
}
}
let processedImageArray = [];
const config = {
...defaults,
...options
}
eleventyConfig.on('afterBuild', () => {
let tempDir = path.join(process.cwd(), config.dirs.temp);
let outputDir = path.join(process.cwd(), config.dirs.output);
fs.copy(tempDir, outputDir, function(err) {
if (err) {
console.error(err);
}
});
// let toDelete = fs.readdirSync(config.dirs.temp).filter( ( el ) => !imageArray.includes( el ) );
// console.log(toDelete);
//
// toDelete.forEach(file => {
// let pathToFile = getTempPath(file);
// console.log('maybe unlink... ', pathToFile);
// if(!fs.lstatSync(pathToFile).isDirectory()) {
// console.log('unlink... ', pathToFile);
// fs.unlink(pathToFile);
// }
// });
});
function hasSvgExtension(image) {
let imageExtension = image.split('.').pop();
return imageExtension == 'svg';
}
function createHash(values) {
return crypto.createHash('md5').update(values.join('')).digest('hex');
}
function getDirectory(image) {
return path.join(process.cwd(), config.dirs.temp, image.substring(0, image.lastIndexOf("/")));
}
function getFilename(image) {
return image.split('.').shift().replace(/\s+/g, '-');
}
function getExtension(image) {
return image.split('.').pop();
}
function getInputPath(image) {
return path.join(process.cwd(), config.dirs.input, image);
}
function getTempPath(outputName) {
return path.join(process.cwd(), config.dirs.temp, outputName);
}
function sharpCropStringToArray(string) {
if(string) {
return sharp[string.split('.')[0]][string.split('.')[1]];
}
}
eleventyConfig.addShortcode('srcset', (image, alt, className = null, width, height, sizes, cropPosition, lazy) => {
if(image) {
return `<img
srcset="${ !hasSvgExtension(image) ? generateSrcset(image, width, height, cropPosition) : ''}"
sizes="${ sizes || '100vw' }"
class="${ className || '' }"
src="${ config.resizeOriginal ? generateSrc(image, width, height, cropPosition) : image }"
alt="${ alt || '' }"
loading="${ lazy ? 'lazy' : 'eager'}"
>`;
}
});
eleventyConfig.addTransform('autoSrcset', async (content, outputPath) => {
if (config.autoselector && outputPath.endsWith(".html")) {
const dom = new JSDOM(content);
const images = [...dom.window.document.querySelectorAll(config.autoselector)];
if (images.length > 0) {
await Promise.all(images.map(updateExistingImg));
}
content = dom.serialize();
}
return content;
});
const generateSrc = function(image, width = config.fallbackWidth, height = config.fallbackHeight, cropPosition = config.cropPosition) {
config.resizeOriginal ? resizeSingleImage(image, width, height, cropPosition, false) : resizeSingleImage(image);
}
const updateExistingImg = async imgElem => {
let image = imgElem.src;
if(!hasSvgExtension(image)) {
imgElem.setAttribute('srcset', generateSrcset(image));
imgElem.setAttribute('sizes', `(min-width: ${ config.fallbackWidth }px) ${ config.fallbackWidth }px, 100vw`);
imgElem.setAttribute('src', generateSrc(image));
}
if (config.createCaptionFromTitle && imgElem.getAttribute('title')) {
imgElem.insertAdjacentHTML(
'afterend',
`<figure>
<img alt="${ imgElem.alt || null }" src="${ imgElem.src }" srcset="${ imgElem.srcset || null }" sizes="${ imgElem.sizes || null }" />
<figcaption>${ imgElem.title }</figcaption>
</figure>`);
imgElem.remove();
}
}
const generateSrcset = function(image, width = config.fallbackWidth, height = config.fallbackHeight, cropPosition = config.cropPosition) {
fs.ensureDirSync(getDirectory(image));
return config.srcsetWidths.map((w) => {
let computedHeight = height ? Math.floor(height / width * w) : null;
return `${resizeSingleImage(image, w, computedHeight, cropPosition, true)} ${ w }w`;
}).join(', ');
}
const resizeSingleImage = function(image, width = '', height, cropPosition, rename) {
var outputFilename = `${getFilename(image)}_${createHash([image, width, height, cropPosition, fs.statSync(getInputPath(image)).mtime])}.${getExtension(image)}`;
processedImageArray.push(outputFilename);
if (!fs.existsSync(getTempPath(image))) {
if(!hasSvgExtension(image)) {
sharp(getInputPath(image)).resize(width, height, {
fit: sharp.fit.cover,
position: sharpCropStringToArray(cropPosition)
}).toFile(getTempPath(outputFilename))
}
else {
fs.copyFile(image, config.dirs.temp);
}
}
return outputFilename;
}
};