Skip to content

Commit 75cdf26

Browse files
committed
Add intrinsicsize script from #144
For now, this remains a manual step. We don’t want to slow down all builds.
1 parent af03b34 commit 75cdf26

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

add-intrinsicsize.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const fs = require('fs');
2+
3+
const glob = require('glob');
4+
const imageSize = require('image-size');
5+
require('string.prototype.matchall').shim();
6+
7+
const files = glob.sync('src/**/*.{html,md}');
8+
for (const file of files) {
9+
const contents = fs.readFileSync(file, 'utf8').toString();
10+
let updatedContents = contents;
11+
const results = contents.matchAll(/^\s*<img.*$/gm);
12+
if (!results) continue;
13+
for (const result of results) {
14+
const oldLine = result[0];
15+
if (oldLine.includes('intrinsicsize')) continue;
16+
const fileName = 'src/' + oldLine.match(/src="\/([^"]+)"/)[1];
17+
const { width, height } = imageSize(fileName);
18+
const updatedLine = oldLine.replace(' alt="', ` intrinsicsize="${width}x${height}" alt="`);
19+
console.log(oldLine);
20+
console.log('>>');
21+
console.log(updatedLine);
22+
console.log('------------');
23+
updatedContents = updatedContents.replace(oldLine, updatedLine);
24+
}
25+
fs.writeFileSync(file, updatedContents);
26+
}

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"private": true,
33
"scripts": {
44
"lint": "markdownlint src/**/*.md",
5+
"intrinsicsize": "node add-intrinsicsize.js",
56
"eleventy": "eleventy",
67
"html": "html-minifier --config-file=.html-minifier.json --input-dir=dist --output-dir=dist --file-ext=html",
78
"css": "postcss src/_css/main.css --output dist/_css/main.css",
@@ -32,9 +33,11 @@
3233
"babel-minify": "^0.5.0",
3334
"cssnano": "^4.1.10",
3435
"firebase-tools": "^6.5.2",
36+
"glob": "^7.1.3",
3537
"he": "^1.2.0",
3638
"html-minifier": "^4.0.0",
3739
"husky": "^1.3.1",
40+
"image-size": "^0.7.3",
3841
"json-minify": "^1.0.0",
3942
"luxon": "^1.12.0",
4043
"markdown-it": "^8.4.2",
@@ -46,6 +49,7 @@
4649
"post-npm-install": "^1.0.0",
4750
"postcss-cli": "^6.1.2",
4851
"postcss-custom-properties": "^8.0.10",
52+
"string.prototype.matchall": "^3.0.1",
4953
"superstatic": "^6.0.4",
5054
"workbox-cli": "^4.2.0"
5155
}

0 commit comments

Comments
 (0)