This repository has been archived on 2025-12-24. You can view files and clone it, but cannot push or open issues or pull requests.
site-template/packages/imagetoolsOSR/plugin/utils/shared.js
2025-03-07 14:59:06 +01:00

48 lines
1.2 KiB
JavaScript

// @ts-check
export function getConfigOptions(config, ext, imageWidth) {
const { w, width = w, format = ext, base64, raw, inline, ...rest } = config;
const imageFormat = format === "jpeg" ? "jpg" : format;
const widths = width
? width.split(";").map((w) => parseInt(w))
: [imageWidth];
const extension = format === "jpg" ? "jpeg" : format;
const type = `image/${extension}`;
const options = {
format: imageFormat,
...rest,
};
return {
type,
widths,
options,
extension,
raw: typeof raw === "string",
inline: typeof base64 === "string" || typeof inline === "string",
};
}
export function getAssetPath(base, assetFileNames, ext, width, hash) {
const regexExecArray = /(?<=\[hash:)\d+(?=\])/g.exec(assetFileNames),
hashLength = regexExecArray ? regexExecArray[0] : 8,
extname = `.${ext}`,
name = base;
width = width + "w";
hash = hash.slice(0, hashLength);
const assetPath = assetFileNames
.replace("[name]", name)
.replace("[width]", width)
.replace(regexExecArray ? `[hash:${hashLength}]` : "[hash]", hash)
.replace("[ext]", ext)
.replace("[extname]", extname);
return assetPath;
}