Skip to content

Commit

Permalink
improve imagick plugin option & any to unknown type
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ngdangtu-vn committed Nov 6, 2023
1 parent ecdafbf commit 24ad89b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions plugins/imagick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export interface Options {

/** Custom transform functions */
functions: Record<string, TransformationFunction>;

/** 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 = (
Expand All @@ -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"],
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -113,7 +120,7 @@ export default function (userOptions?: Partial<Options>) {
[options.name]: undefined,
});

rename(output, transformation);
rename(output, transformation, options.rewritePath);

if (cache) {
const result = await cache.get(content, transformation);
Expand Down Expand Up @@ -161,7 +168,7 @@ function transform(
break;

case "format":
format = args;
format = args as MagickFormat | undefined;
break;

default:
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down

0 comments on commit 24ad89b

Please sign in to comment.