44module . exports = async ( { github, context} ) => {
55 const IGNORE_KEY = '<!-- IGNORE IMAGE MINIFY -->' ;
66 const IGNORE_ALT_NAME_END = 'ignoreImageMinify' ;
7+ // Targeted maximum height
78 const IMG_MAX_HEIGHT_PX = 600 ;
9+ // maximum width of GitHub issues/comments
10+ const IMG_MAX_WIDTH_PX = 800 ;
11+ // all images that have a lower aspect ration (-> have a smaller width) than this will be minimized
12+ const MIN_ASPECT_RATION = IMG_MAX_WIDTH_PX / IMG_MAX_HEIGHT_PX
813
914 // Get the body of the image
1015 let initialBody = null ;
@@ -38,6 +43,8 @@ module.exports = async ({github, context}) => {
3843
3944 // Require the probe lib for getting the image dimensions
4045 const probe = require ( 'probe-image-size' ) ;
46+
47+ var wasMatchModified = false ;
4148
4249 // Try to find and replace the images with minimized ones
4350 let newBody = await replaceAsync ( initialBody , REGEX_IMAGE_LOOKUP , async ( match , g1 , g2 ) => {
@@ -48,7 +55,7 @@ module.exports = async ({github, context}) => {
4855 return match ;
4956 }
5057
51- let shouldModifiy = false ;
58+ let shouldModify = false ;
5259 try {
5360 console . log ( `Probing ${ g2 } ` ) ;
5461 let probeResult = await probe ( g2 ) ;
@@ -58,22 +65,38 @@ module.exports = async ({github, context}) => {
5865 if ( probeResult . hUnits != 'px' ) {
5966 throw `Unexpected probeResult.hUnits (expected px but got ${ probeResult . hUnits } )` ;
6067 }
68+ if ( probeResult . height <= 0 ) {
69+ throw `Unexpected probeResult.height (height is invalid: ${ probeResult . height } )` ;
70+ }
71+ if ( probeResult . wUnits != 'px' ) {
72+ throw `Unexpected probeResult.wUnits (expected px but got ${ probeResult . wUnits } )` ;
73+ }
74+ if ( probeResult . width <= 0 ) {
75+ throw `Unexpected probeResult.width (width is invalid: ${ probeResult . width } )` ;
76+ }
77+ console . log ( `Probing resulted in ${ probeResult . width } x${ probeResult . height } px` ) ;
6178
62- shouldModifiy = probeResult . height > IMG_MAX_HEIGHT_PX ;
79+ shouldModify = probeResult . height > IMG_MAX_HEIGHT_PX && ( probeResult . width / probeResult . height ) < MIN_ASPECT_RATION ;
6380 } catch ( e ) {
6481 console . log ( 'Probing failed:' , e ) ;
6582 // Immediately abort
6683 return match ;
6784 }
6885
69- if ( shouldModifiy ) {
86+ if ( shouldModify ) {
87+ wasMatchModified = true ;
7088 console . log ( `Modifying match '${ match } '` ) ;
7189 return `<img alt="${ g1 } " src="${ g2 } " height=${ IMG_MAX_HEIGHT_PX } />` ;
7290 }
7391
7492 console . log ( `Match '${ match } ' is ok/will not be modified` ) ;
7593 return match ;
7694 } ) ;
95+
96+ if ( ! wasMatchModified ) {
97+ console . log ( 'Nothing was modified. Skipping update' ) ;
98+ return ;
99+ }
77100
78101 // Update the corresponding element
79102 if ( context . eventName == 'issue_comment' ) {
0 commit comments