astro-ghostcms/.pnpm-store/v3/files/b2/4a47a164016a7cec360e943cb9e...

41 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

2024-02-14 14:10:47 +00:00
function createCanonicalURL(url, trailingSlash, base) {
let pathname = url.replace(/\/index.html$/, "");
if (trailingSlash === false) {
pathname = pathname.replace(/(\/+)?$/, "");
} else if (!getUrlExtension(url)) {
pathname = pathname.replace(/(\/+)?$/, "/");
}
pathname = pathname.replace(/\/+/g, "/");
return new URL(pathname, base);
}
function isValidURL(url) {
try {
new URL(url);
return true;
} catch (e) {
}
return false;
}
function getUrlExtension(url) {
const lastDot = url.lastIndexOf(".");
const lastSlash = url.lastIndexOf("/");
return lastDot > lastSlash ? url.slice(lastDot + 1) : "";
}
const flattenErrorPath = (errorPath) => errorPath.join(".");
const errorMap = (error, ctx) => {
if (error.code === "invalid_type") {
const badKeyPath = JSON.stringify(flattenErrorPath(error.path));
if (error.received === "undefined") {
return { message: `${badKeyPath} is required.` };
} else {
return { message: `${badKeyPath} should be ${error.expected}, not ${error.received}.` };
}
}
return { message: ctx.defaultError };
};
export {
createCanonicalURL,
errorMap,
isValidURL
};