|
| 1 | +/* |
| 2 | + * Script for minimizing big images (jpg,gif,png) when they are uploaded to GitHub and not edited otherwise |
| 3 | + */ |
| 4 | +module.exports = async ({github, context}) => { |
| 5 | + const IGNORE_KEY = '<!-- IGNORE IMAGE MINIFY -->'; |
| 6 | + const IGNORE_ALT_NAME_END = 'ignoreImageMinify'; |
| 7 | + const IMG_MAX_HEIGHT_PX = 600; |
| 8 | + |
| 9 | + // Get the body of the image |
| 10 | + let initialBody = null; |
| 11 | + if (context.eventName == 'issue_comment') { |
| 12 | + initialBody = context.payload.comment.body; |
| 13 | + } else if (context.eventName == 'issues') { |
| 14 | + initialBody = context.payload.issue.body; |
| 15 | + } else { |
| 16 | + console.log('Aborting: No body found'); |
| 17 | + return; |
| 18 | + } |
| 19 | + console.log(`Found body: \n${initialBody}\n`); |
| 20 | + |
| 21 | + // Check if we should ignore the currently processing element |
| 22 | + if (initialBody.includes(IGNORE_KEY)) { |
| 23 | + console.log('Ignoring: Body contains IGNORE_KEY'); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Regex for finding images (simple variant)  |
| 28 | + const REGEX_IMAGE_LOOKUP = /\!\[(.*)\]\((https:\/\/[-a-z0-9]+\.githubusercontent\.com\/\d+\/[-0-9a-f]{32,512}\.(jpg|gif|png))\)/gm; |
| 29 | + |
| 30 | + // Check if we found something |
| 31 | + let foundSimpleImages = REGEX_IMAGE_LOOKUP.test(initialBody); |
| 32 | + if (!foundSimpleImages) { |
| 33 | + console.log('Found no simple images to process'); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + console.log('Found at least one simple image to process'); |
| 38 | + |
| 39 | + // Require the probe lib for getting the image dimensions |
| 40 | + const probe = require('probe-image-size'); |
| 41 | + |
| 42 | + // Try to find and replace the images with minimized ones |
| 43 | + let newBody = await replaceAsync(initialBody, REGEX_IMAGE_LOOKUP, async (match, g1, g2) => { |
| 44 | + console.log(`Found match '${match}'`); |
| 45 | + |
| 46 | + if (g1.endsWith(IGNORE_ALT_NAME_END)) { |
| 47 | + console.log(`Ignoring match '${match}': IGNORE_ALT_NAME_END`); |
| 48 | + return match; |
| 49 | + } |
| 50 | + |
| 51 | + let shouldModifiy = false; |
| 52 | + try { |
| 53 | + console.log(`Probing ${g2}`); |
| 54 | + let probeResult = await probe(g2); |
| 55 | + if (probeResult == null) { |
| 56 | + throw 'No probeResult'; |
| 57 | + } |
| 58 | + if (probeResult.hUnits != 'px') { |
| 59 | + throw `Unexpected probeResult.hUnits (expected px but got ${probeResult.hUnits})`; |
| 60 | + } |
| 61 | + |
| 62 | + shouldModifiy = probeResult.height > IMG_MAX_HEIGHT_PX; |
| 63 | + } catch(e) { |
| 64 | + console.log('Probing failed:', e); |
| 65 | + // Immediately abort |
| 66 | + return match; |
| 67 | + } |
| 68 | + |
| 69 | + if (shouldModifiy) { |
| 70 | + console.log(`Modifying match '${match}'`); |
| 71 | + return `<img alt="${g1}" src="${g2}" height=${IMG_MAX_HEIGHT_PX} />`; |
| 72 | + } |
| 73 | + |
| 74 | + console.log(`Match '${match}' is ok/will not be modified`); |
| 75 | + return match; |
| 76 | + }); |
| 77 | + |
| 78 | + // Update the corresponding element |
| 79 | + if (context.eventName == 'issue_comment') { |
| 80 | + console.log('Updating comment with id', context.payload.comment.id); |
| 81 | + await github.rest.issues.updateComment({ |
| 82 | + comment_id: context.payload.comment.id, |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo, |
| 85 | + body: newBody |
| 86 | + }) |
| 87 | + } else if (context.eventName == 'issues') { |
| 88 | + console.log('Updating issue', context.payload.issue.number); |
| 89 | + await github.rest.issues.update({ |
| 90 | + issue_number: context.payload.issue.number, |
| 91 | + owner: context.repo.owner, |
| 92 | + repo: context.repo.repo, |
| 93 | + body: newBody |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + // Asnyc replace function from https://stackoverflow.com/a/48032528 |
| 98 | + async function replaceAsync(str, regex, asyncFn) { |
| 99 | + const promises = []; |
| 100 | + str.replace(regex, (match, ...args) => { |
| 101 | + const promise = asyncFn(match, ...args); |
| 102 | + promises.push(promise); |
| 103 | + }); |
| 104 | + const data = await Promise.all(promises); |
| 105 | + return str.replace(regex, () => data.shift()); |
| 106 | + } |
| 107 | +} |
0 commit comments