deno.land / x / lume@v2.1.4 / plugins / transform_images.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272import { getPathAndExtension } from "../core/utils/path.ts";import { log } from "../core/utils/log.ts";import { merge } from "../core/utils/object.ts";import { concurrent } from "../core/utils/concurrent.ts";import binaryLoader from "../core/loaders/binary.ts";import sharp, { create } from "../deps/sharp.ts";import Cache from "../core/cache.ts";
import type Site from "../core/site.ts";import type { Page } from "../core/file.ts";
export interface Options { /** The list extensions this plugin applies to */ extensions: string[];
/** The key name for the transformations definitions */ name: string;
/** The cache folder */ cache: string | boolean;
/** Custom transform functions */ functions: Record<string, TransformationFunction>;}
export type TransformationFunction = ( image: sharp.Sharp, // deno-lint-ignore no-explicit-any ...args: any[]) => void;
// Default optionsexport const defaults: Options = { extensions: [".jpg", ".jpeg", ".png", ".webp"], name: "transformImages", cache: true, functions: { resize( image: sharp.Sharp, width: number, height?: number, options: sharp.ResizeOptions = { withoutEnlargement: true }, ): void { image.resize(width, height, options); }, blur(image: sharp.Sharp, sigma?: number | boolean): void { image.blur(sigma); }, rotate(image: sharp.Sharp, degrees: number): void { image.rotate(degrees); }, },};
export type Format = | "jpeg" | "jp2" | "jxl" | "png" | "webp" | "gif" | "avif" | "heif" | "tiff";export interface FormatOptions { format: Format; [key: string]: unknown;}
export interface Transformation { suffix?: string; format?: Format | Format[] | FormatOptions | FormatOptions[]; matches?: RegExp | string; // deno-lint-ignore no-explicit-any [key: string]: any;}interface SingleTransformation extends Transformation { format?: Format | FormatOptions;}
/** A plugin to transform images in Lume */export default function (userOptions?: Partial<Options>) { const options = merge(defaults, userOptions);
return (site: Site) => { site.loadAssets(options.extensions, binaryLoader); site.process(options.extensions, process);
// Configure the cache folder const cacheFolder = options.cache === true ? "_cache" : options.cache; const cache = cacheFolder ? new Cache({ folder: site.src(cacheFolder) }) : undefined;
if (cacheFolder) { site.ignore(cacheFolder); site.options.watcher.ignore.push(cacheFolder); }
async function process(pages: Page[], allPages: Page[]) { await concurrent( pages, (page) => processPage(page, allPages), ); } async function processPage(page: Page, allPages: Page[]) { const transData = page.data[options.name] as | Transformation | Transformation[] | undefined;
if (!transData) { return; }
const content = page.content as Uint8Array | string; const transformations = removeDuplicatedTransformations( getTransformations(transData), ); let transformed = false; let index = 0; for (const transformation of transformations) { if (transformation.matches) { const regex = new RegExp(transformation.matches); if (!regex.test(page.data.url as string)) { continue; } }
const output = page.duplicate(index++, { ...page.data, [options.name]: undefined, });
rename(output, transformation);
if (cache) { const result = await cache.get(content, transformation);
if (result) { output.content = result; } else { await transform(content, output, transformation, options); transformed = true; await cache.set(content, transformation, output.content!); } } else { await transform(content, output, transformation, options); transformed = true; }
if (output !== page) { allPages.push(output); } }
if (transformed) { log.info(`[transform_images plugin] Processed ${page.sourcePath}`); }
// Remove the original page allPages.splice(allPages.indexOf(page), 1); } };}
async function transform( content: Uint8Array | string, page: Page, transformation: Transformation, options: Options,): Promise<void> { const image = await create(content);
for (const [name, args] of Object.entries(transformation)) { switch (name) { case "suffix": case "matches": break;
case "format": if (typeof args === "string") { image.toFormat(args as Format); } else { const { format, ...options } = args as Record<string, unknown>; image.toFormat(format as Format, options); } break;
default: if (!options.functions[name]) { throw new Error(`Unknown transformation: ${name}`); }
if (Array.isArray(args)) { options.functions[name](image, ...args); } else { options.functions[name](image, args); } } }
page.content = new Uint8Array(await image.toBuffer());}
function rename(page: Page, transformation: SingleTransformation): void { const { format, suffix } = transformation; let [path, ext] = getPathAndExtension(page.data.url);
if (format) { ext = typeof format === "string" ? `.${format}` : `.${format.format}`; }
if (suffix) { path += suffix; }
page.data.url = path + ext;}
function getTransformations( input: Transformation | Transformation[],): SingleTransformation[] { if (Array.isArray(input)) { const singles: SingleTransformation[] = [];
for (const transformation of input) { if (Array.isArray(transformation.format)) { transformation.format.forEach((format) => { singles.push({ ...transformation, format }); }); } else { singles.push(transformation as SingleTransformation); } } return singles; }
if (Array.isArray(input.format)) { return input.format.map((format) => ({ ...input, format })); }
return [input as SingleTransformation];}
function removeDuplicatedTransformations( transformations: SingleTransformation[],): SingleTransformation[] { const result = new Map<string, SingleTransformation>();
for (const transformation of transformations) { const { format, suffix, matches } = transformation; const key = `${format}:${suffix ?? ""}${matches ?? ""}`; result.set(key, transformation); }
return [...result.values()];}
/** Extends Data interface */declare global { namespace Lume { export interface Data { /** * Image transformations * @see https://lume.land/plugins/transform_images/ */ transformImages?: Transformation | Transformation[]; } }}
Version Info