File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change
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 * < i m g .* $ / 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 ( / s r c = " \/ ( [ ^ " ] + ) " / ) [ 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
+ }
Original file line number Diff line number Diff line change 2
2
"private" : true ,
3
3
"scripts" : {
4
4
"lint" : " markdownlint src/**/*.md" ,
5
+ "intrinsicsize" : " node add-intrinsicsize.js" ,
5
6
"eleventy" : " eleventy" ,
6
7
"html" : " html-minifier --config-file=.html-minifier.json --input-dir=dist --output-dir=dist --file-ext=html" ,
7
8
"css" : " postcss src/_css/main.css --output dist/_css/main.css" ,
32
33
"babel-minify" : " ^0.5.0" ,
33
34
"cssnano" : " ^4.1.10" ,
34
35
"firebase-tools" : " ^6.5.2" ,
36
+ "glob" : " ^7.1.3" ,
35
37
"he" : " ^1.2.0" ,
36
38
"html-minifier" : " ^4.0.0" ,
37
39
"husky" : " ^1.3.1" ,
40
+ "image-size" : " ^0.7.3" ,
38
41
"json-minify" : " ^1.0.0" ,
39
42
"luxon" : " ^1.12.0" ,
40
43
"markdown-it" : " ^8.4.2" ,
46
49
"post-npm-install" : " ^1.0.0" ,
47
50
"postcss-cli" : " ^6.1.2" ,
48
51
"postcss-custom-properties" : " ^8.0.10" ,
52
+ "string.prototype.matchall" : " ^3.0.1" ,
49
53
"superstatic" : " ^6.0.4" ,
50
54
"workbox-cli" : " ^4.2.0"
51
55
}
You can’t perform that action at this time.
0 commit comments