Skip to content

Commit

Permalink
Merge pull request #160 from divriots/cdn-includes-overrides-islocal
Browse files Browse the repository at this point in the history
feat: allow cdn.src_include to override isLocal()
  • Loading branch information
muryoh authored May 15, 2024
2 parents 0626cf6 + 2c5d5d7 commit c991799
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/config-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const default_options: Options = {
},
cdn: {
process: 'off',
src_include: /^.*$/,
src_include: null,
src_exclude: null,
},
compress: true,
Expand Down
2 changes: 1 addition & 1 deletion src/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type Options = {
process:
| 'off' //default
| 'optimize';
src_include: RegExp;
src_include: RegExp | null;
src_exclude: RegExp | null;
transformer?: UrlTransformer; // Custom 'unpic' cdn url transformer, if not present it will be determined by 'unpic' based on original url
};
Expand Down
106 changes: 56 additions & 50 deletions src/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,59 +329,65 @@ async function processImage(
/*
* Check for external images
*/
if (!isLocal(attrib_src)) {
switch (config.image.cdn.process) {
case 'off':
break;
case 'optimize':
let cdnTransformer = config.image.cdn.transformer;
if (!cdnTransformer) {
const canonical = getCanonicalCdnForUrl(attrib_src);
if (!canonical) break;
cdnTransformer = getTransformer(canonical.cdn);
}
if (!cdnTransformer) break;
if (
!isIncluded(
attrib_src,
config.image.cdn.src_include,
config.image.cdn.src_exclude
)
)
break;
let attrib_width = getIntAttr(img, 'width');
if (!attrib_width) {
$state.reportIssue(htmlfile, {
type: 'warn',
msg: `Missing or malformed'width' attribute for image ${attrib_src}, unable to perform CDN transform`,
});
return;
}
let attrib_height = getIntAttr(img, 'height');
if (!attrib_height) {
$state.reportIssue(htmlfile, {
type: 'warn',
msg: `Missing or malformed 'height' attribute for image ${attrib_src}, unable to perform CDN transform`,
});
return;
}
const new_srcset = await generateSrcSetForCdn(
attrib_src,
cdnTransformer,
attrib_width,
attrib_height
switch (config.image.cdn.process) {
case 'off':
break;
case 'optimize':
let cdnTransformer = config.image.cdn.transformer;
if (cdnTransformer && !config.image.cdn.src_include) {
throw new Error(
'config.image.cdn.src_include is required when specifying a config.image.cdn.transformer'
);
}

if (new_srcset !== null) {
img.attr('srcset', new_srcset);
}

// Add sizes attribute if not specified
if (img.attr('srcset') && !img.attr('sizes')) {
img.attr('sizes', '100vw');
}
if (!cdnTransformer) {
const canonical = getCanonicalCdnForUrl(attrib_src);
if (!canonical) break;
cdnTransformer = getTransformer(canonical.cdn);
}
if (!cdnTransformer) break;
if (
!isIncluded(
attrib_src,
config.image.cdn.src_include || /.*/,
config.image.cdn.src_exclude
)
)
break;
let attrib_width = getIntAttr(img, 'width');
if (!attrib_width) {
$state.reportIssue(htmlfile, {
type: 'warn',
msg: `Missing or malformed'width' attribute for image ${attrib_src}, unable to perform CDN transform`,
});
return;
}
}
let attrib_height = getIntAttr(img, 'height');
if (!attrib_height) {
$state.reportIssue(htmlfile, {
type: 'warn',
msg: `Missing or malformed 'height' attribute for image ${attrib_src}, unable to perform CDN transform`,
});
return;
}
const new_srcset = await generateSrcSetForCdn(
attrib_src,
cdnTransformer,
attrib_width,
attrib_height
);

if (new_srcset !== null) {
img.attr('srcset', new_srcset);
}

// Add sizes attribute if not specified
if (img.attr('srcset') && !img.attr('sizes')) {
img.attr('sizes', '100vw');
}
return;
}
if (!isLocal(attrib_src)) {
switch (config.image.external.process) {
case 'off': // Don't process external images
return;
Expand Down
1 change: 0 additions & 1 deletion src/utils/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ export class Resource {
}

export function isLocal(src: string) {
if (src.startsWith('/cdn_cgi/')) return false;
const u = url.parse(src);
return !u.host;
}
Expand Down

0 comments on commit c991799

Please sign in to comment.