Skip to content

Commit e765343

Browse files
authored
Merge pull request TeamNewPipe#10166 from TeamNewPipe/fix/image-workflow
Add support for new GitHub assets URLs in image minimizer workflow
2 parents 62ce0b0 + 8bdeed8 commit e765343

1 file changed

Lines changed: 54 additions & 49 deletions

File tree

.github/workflows/image-minimizer.js

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ module.exports = async ({github, context}) => {
3030
}
3131

3232
// Regex for finding images (simple variant) ![ALT_TEXT](https://*.githubusercontent.com/<number>/<variousHexStringsAnd->.<fileExtension>)
33-
const REGEX_IMAGE_LOOKUP = /\!\[(.*)\]\((https:\/\/[-a-z0-9]+\.githubusercontent\.com\/\d+\/[-0-9a-f]{32,512}\.(jpg|gif|png))\)/gm;
33+
const REGEX_USER_CONTENT_IMAGE_LOOKUP = /\!\[(.*)\]\((https:\/\/[-a-z0-9]+\.githubusercontent\.com\/\d+\/[-0-9a-f]{32,512}\.(jpg|gif|png))\)/gm;
34+
const REGEX_ASSETS_IMAGE_LOCKUP = /\!\[(.*)\]\((https:\/\/github\.com\/[-\w\d]+\/[-\w\d]+\/assets\/\d+\/[\-0-9a-f]{32,512})\)/gm;
3435

3536
// Check if we found something
36-
let foundSimpleImages = REGEX_IMAGE_LOOKUP.test(initialBody);
37+
let foundSimpleImages = REGEX_USER_CONTENT_IMAGE_LOOKUP.test(initialBody)
38+
|| REGEX_ASSETS_IMAGE_LOCKUP.test(initialBody);
3739
if (!foundSimpleImages) {
3840
console.log('Found no simple images to process');
3941
return;
@@ -47,53 +49,8 @@ module.exports = async ({github, context}) => {
4749
var wasMatchModified = false;
4850

4951
// Try to find and replace the images with minimized ones
50-
let newBody = await replaceAsync(initialBody, REGEX_IMAGE_LOOKUP, async (match, g1, g2) => {
51-
console.log(`Found match '${match}'`);
52-
53-
if (g1.endsWith(IGNORE_ALT_NAME_END)) {
54-
console.log(`Ignoring match '${match}': IGNORE_ALT_NAME_END`);
55-
return match;
56-
}
57-
58-
let probeAspectRatio = 0;
59-
let shouldModify = false;
60-
try {
61-
console.log(`Probing ${g2}`);
62-
let probeResult = await probe(g2);
63-
if (probeResult == null) {
64-
throw 'No probeResult';
65-
}
66-
if (probeResult.hUnits != 'px') {
67-
throw `Unexpected probeResult.hUnits (expected px but got ${probeResult.hUnits})`;
68-
}
69-
if (probeResult.height <= 0) {
70-
throw `Unexpected probeResult.height (height is invalid: ${probeResult.height})`;
71-
}
72-
if (probeResult.wUnits != 'px') {
73-
throw `Unexpected probeResult.wUnits (expected px but got ${probeResult.wUnits})`;
74-
}
75-
if (probeResult.width <= 0) {
76-
throw `Unexpected probeResult.width (width is invalid: ${probeResult.width})`;
77-
}
78-
console.log(`Probing resulted in ${probeResult.width}x${probeResult.height}px`);
79-
80-
probeAspectRatio = probeResult.width / probeResult.height;
81-
shouldModify = probeResult.height > IMG_MAX_HEIGHT_PX && probeAspectRatio < MIN_ASPECT_RATIO;
82-
} catch(e) {
83-
console.log('Probing failed:', e);
84-
// Immediately abort
85-
return match;
86-
}
87-
88-
if (shouldModify) {
89-
wasMatchModified = true;
90-
console.log(`Modifying match '${match}'`);
91-
return `<img alt="${g1}" src="${g2}" width=${Math.min(600, (IMG_MAX_HEIGHT_PX * probeAspectRatio).toFixed(0))} />`;
92-
}
93-
94-
console.log(`Match '${match}' is ok/will not be modified`);
95-
return match;
96-
});
52+
let newBody = await replaceAsync(initialBody, REGEX_USER_CONTENT_IMAGE_LOOKUP, minimizeAsync);
53+
newBody = await replaceAsync(newBody, REGEX_ASSETS_IMAGE_LOCKUP, minimizeAsync);
9754

9855
if (!wasMatchModified) {
9956
console.log('Nothing was modified. Skipping update');
@@ -129,4 +86,52 @@ module.exports = async ({github, context}) => {
12986
const data = await Promise.all(promises);
13087
return str.replace(regex, () => data.shift());
13188
}
89+
90+
async function minimizeAsync(match, g1, g2) {
91+
console.log(`Found match '${match}'`);
92+
93+
if (g1.endsWith(IGNORE_ALT_NAME_END)) {
94+
console.log(`Ignoring match '${match}': IGNORE_ALT_NAME_END`);
95+
return match;
96+
}
97+
98+
let probeAspectRatio = 0;
99+
let shouldModify = false;
100+
try {
101+
console.log(`Probing ${g2}`);
102+
let probeResult = await probe(g2);
103+
if (probeResult == null) {
104+
throw 'No probeResult';
105+
}
106+
if (probeResult.hUnits != 'px') {
107+
throw `Unexpected probeResult.hUnits (expected px but got ${probeResult.hUnits})`;
108+
}
109+
if (probeResult.height <= 0) {
110+
throw `Unexpected probeResult.height (height is invalid: ${probeResult.height})`;
111+
}
112+
if (probeResult.wUnits != 'px') {
113+
throw `Unexpected probeResult.wUnits (expected px but got ${probeResult.wUnits})`;
114+
}
115+
if (probeResult.width <= 0) {
116+
throw `Unexpected probeResult.width (width is invalid: ${probeResult.width})`;
117+
}
118+
console.log(`Probing resulted in ${probeResult.width}x${probeResult.height}px`);
119+
120+
probeAspectRatio = probeResult.width / probeResult.height;
121+
shouldModify = probeResult.height > IMG_MAX_HEIGHT_PX && probeAspectRatio < MIN_ASPECT_RATIO;
122+
} catch(e) {
123+
console.log('Probing failed:', e);
124+
// Immediately abort
125+
return match;
126+
}
127+
128+
if (shouldModify) {
129+
wasMatchModified = true;
130+
console.log(`Modifying match '${match}'`);
131+
return `<img alt="${g1}" src="${g2}" width=${Math.min(600, (IMG_MAX_HEIGHT_PX * probeAspectRatio).toFixed(0))} />`;
132+
}
133+
134+
console.log(`Match '${match}' is ok/will not be modified`);
135+
return match;
136+
}
132137
}

0 commit comments

Comments
 (0)