From 24ad89b8cf271256068b74582e419ad93a7da7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=C4=83ng=20T=C3=BA?= <68631458+ngdangtu-vn@users.noreply.github.com> Date: Mon, 6 Nov 2023 22:38:15 +0700 Subject: [PATCH] improve imagick plugin option & any to unknown type This commit allow imagick to rewrite the output path. The rewrite function receives 2 params are path as string and format extension as MagickFormat. Developers can use format extension as hint for better rewritting path experience. - add rewrite option for imagick - correct param type of rename function - replace any type with unknown in Transformation interface - update CHANGELOG.md --- CHANGELOG.md | 1 + plugins/imagick.ts | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3399149..2dfd4f02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Any BREAKING CHANGE between minor versions will be documented here in upper case ### Fixed - Tailwind: Fix types for `options`. - Favicon: Better error if the source file is missing [#504]. +- Plugin `imagick`: Support rewrite output path. ## [1.19.3] - 2023-10-29 ### Changed diff --git a/plugins/imagick.ts b/plugins/imagick.ts index 9539a6e5..e51def29 100644 --- a/plugins/imagick.ts +++ b/plugins/imagick.ts @@ -19,6 +19,9 @@ export interface Options { /** Custom transform functions */ functions: Record; + + /** Rewrite path with format extension as hint. The hint can be undefined if the transformation does no reformatting. Return from the function must be a string pathname */ + rewritePath?: PathRewrittenFunction; } export type TransformationFunction = ( @@ -27,6 +30,11 @@ export type TransformationFunction = ( ...args: any[] ) => void; +export type PathRewrittenFunction = ( + path: string, + formatExtension?: MagickFormat, +) => string; + // Default options export const defaults: Options = { extensions: [".jpg", ".jpeg", ".png"], @@ -58,8 +66,7 @@ export interface Transformation { suffix?: string; format?: MagickFormat | MagickFormat[]; matches?: RegExp | string; - // deno-lint-ignore no-explicit-any - [key: string]: any; + [key: string]: unknown; } interface SingleTransformation extends Transformation { format?: MagickFormat; @@ -113,7 +120,7 @@ export default function (userOptions?: Partial) { [options.name]: undefined, }); - rename(output, transformation); + rename(output, transformation, options.rewritePath); if (cache) { const result = await cache.get(content, transformation); @@ -161,7 +168,7 @@ function transform( break; case "format": - format = args; + format = args as MagickFormat | undefined; break; default: @@ -190,7 +197,11 @@ function transform( }); } -function rename(page: Page, transformation: Transformation): void { +function rename( + page: Page, + transformation: SingleTransformation, + rewrite?: PathRewrittenFunction, +): void { const { format, suffix } = transformation; const url = page.data.url; @@ -208,6 +219,10 @@ function rename(page: Page, transformation: Transformation): void { path += suffix; } + if (typeof rewrite === "function") { + path += rewrite(path, format); + } + page.data.url = path + ext; }