From c08a4e851bc927347c26e4be8a3093fce26cd8b5 Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Tue, 15 Feb 2022 20:09:21 +0100 Subject: [PATCH] Improved image-minimizer * Don't minimize images that are too wide -> they will get stretched otherwise * Don't try to modify the issue/comment when nothing changed * Fixed typo --- .github/workflows/image-minimizer.js | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/.github/workflows/image-minimizer.js b/.github/workflows/image-minimizer.js index 8d10a1e77..273c6d328 100644 --- a/.github/workflows/image-minimizer.js +++ b/.github/workflows/image-minimizer.js @@ -4,7 +4,12 @@ module.exports = async ({github, context}) => { const IGNORE_KEY = ''; const IGNORE_ALT_NAME_END = 'ignoreImageMinify'; + // Targeted maximum height const IMG_MAX_HEIGHT_PX = 600; + // maximum width of GitHub issues/comments + const IMG_MAX_WIDTH_PX = 800; + // all images that have a lower aspect ration (-> have a smaller width) than this will be minimized + const MIN_ASPECT_RATION = IMG_MAX_WIDTH_PX / IMG_MAX_HEIGHT_PX // Get the body of the image let initialBody = null; @@ -38,6 +43,8 @@ module.exports = async ({github, context}) => { // Require the probe lib for getting the image dimensions const probe = require('probe-image-size'); + + var wasMatchModified = false; // Try to find and replace the images with minimized ones let newBody = await replaceAsync(initialBody, REGEX_IMAGE_LOOKUP, async (match, g1, g2) => { @@ -48,7 +55,7 @@ module.exports = async ({github, context}) => { return match; } - let shouldModifiy = false; + let shouldModify = false; try { console.log(`Probing ${g2}`); let probeResult = await probe(g2); @@ -58,15 +65,26 @@ module.exports = async ({github, context}) => { if (probeResult.hUnits != 'px') { throw `Unexpected probeResult.hUnits (expected px but got ${probeResult.hUnits})`; } + if (probeResult.height <= 0) { + throw `Unexpected probeResult.height (height is invalid: ${probeResult.height})`; + } + if (probeResult.wUnits != 'px') { + throw `Unexpected probeResult.wUnits (expected px but got ${probeResult.wUnits})`; + } + if (probeResult.width <= 0) { + throw `Unexpected probeResult.width (width is invalid: ${probeResult.width})`; + } + console.log(`Probing resulted in ${probeResult.width}x${probeResult.height}px`); - shouldModifiy = probeResult.height > IMG_MAX_HEIGHT_PX; + shouldModify = probeResult.height > IMG_MAX_HEIGHT_PX && (probeResult.width / probeResult.height) < MIN_ASPECT_RATION; } catch(e) { console.log('Probing failed:', e); // Immediately abort return match; } - if (shouldModifiy) { + if (shouldModify) { + wasMatchModified = true; console.log(`Modifying match '${match}'`); return `${g1}`; } @@ -74,6 +92,11 @@ module.exports = async ({github, context}) => { console.log(`Match '${match}' is ok/will not be modified`); return match; }); + + if (!wasMatchModified) { + console.log('Nothing was modified. Skipping update'); + return; + } // Update the corresponding element if (context.eventName == 'issue_comment') {