-
Notifications
You must be signed in to change notification settings - Fork 324
/
add-image-and-video-dimensions.js
47 lines (43 loc) · 1.7 KB
/
add-image-and-video-dimensions.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
const fs = require('fs').promises;
const glob = require('glob');
const imageSize = require('image-size');
const videoSize = require('get-video-dimensions');
(async () => {
const files = glob.sync('src/**/*.{html,md}');
for (const file of files) {
const contents = await fs.readFile(file, 'utf8');
let updatedContents = contents;
const images = contents.matchAll(/^\s*<img.*$/gm);
if (images) {
for (const result of images) {
const oldLine = result[0];
if (oldLine.includes(' width="')) continue;
const fileName = 'src/' + oldLine.match(/src="\/([^"]+)"/)[1];
const { width, height } = imageSize(fileName);
const updatedLine = oldLine.replace(' alt="', ` width="${width}" height="${height}" alt="`);
console.log(oldLine);
console.log('>>');
console.log(updatedLine);
console.log('------------');
updatedContents = updatedContents.replace(oldLine, updatedLine);
}
}
// TODO: This needs updating once we use `<source>`.
const videos = contents.matchAll(/^\s*<video.*$/gm);
if (videos) {
for (const result of videos) {
const oldLine = result[0];
if (oldLine.includes(' width="')) continue;
const fileName = 'src/' + oldLine.match(/src="\/([^"]+)"/)[1];
const { width, height } = await videoSize(fileName);
const updatedLine = oldLine.replace(' src="', ` width="${width}" height="${height}" src="`);
console.log(oldLine);
console.log('>>');
console.log(updatedLine);
console.log('------------');
updatedContents = updatedContents.replace(oldLine, updatedLine);
}
}
await fs.writeFile(file, updatedContents);
}
})();