forked from v8/v8.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-intrinsicsize.js
25 lines (23 loc) · 911 Bytes
/
add-intrinsicsize.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
const fs = require('fs');
const glob = require('glob');
const imageSize = require('image-size');
const files = glob.sync('src/**/*.{html,md}');
for (const file of files) {
const contents = fs.readFileSync(file, 'utf8').toString();
let updatedContents = contents;
const results = contents.matchAll(/^\s*<img.*$/gm);
if (!results) continue;
for (const result of results) {
const oldLine = result[0];
if (oldLine.includes('intrinsicsize')) continue;
const fileName = 'src/' + oldLine.match(/src="\/([^"]+)"/)[1];
const { width, height } = imageSize(fileName);
const updatedLine = oldLine.replace(' alt="', ` intrinsicsize="${width}x${height}" alt="`);
console.log(oldLine);
console.log('>>');
console.log(updatedLine);
console.log('------------');
updatedContents = updatedContents.replace(oldLine, updatedLine);
}
fs.writeFileSync(file, updatedContents);
}