@yoot/sanity
Version:
Sanity adapter for @yoot/yoot
66 lines (65 loc) • 1.5 kB
JavaScript
import { defineAdapter as s } from "@yoot/yoot";
import { _hasDimensions as a, _isEmpty as f, _isKeyOf as i } from "@yoot/yoot/internal";
const A = s({
supports: (t) => t.hostname === "cdn.sanity.io",
generateUrl: m,
normalizeUrl: h,
primeState: u
});
function h(t) {
const r = new URL(t.href);
return r.search = "", r.hash = "", r.href;
}
function u(t) {
if ("aspectRatio" in t.directives && !a(t)) {
const e = P(new URL(t.src));
return { ...t, ...e };
}
return t;
}
function m(t) {
const r = Object.entries(t.directives).reduce(d, new URLSearchParams()), e = new URL(t.src);
return e.search = String(r), e.href;
}
const o = {
crop: p,
fit: R,
format: g
};
function d(t, [r, e]) {
return f(e) ? t : i(r, o) ? o[r](e, t) : (i(r, n) && t.set(n[r], String(e)), t);
}
function p(t, r) {
return r.set(n.crop, String(t)), r;
}
const c = {
cover: "crop",
contain: "clip"
};
function R(t, r) {
const e = String(t);
return i(e, c) && r.set(n.fit, c[e]), r;
}
function g(t, r) {
const e = String(t);
return e === "auto" ? (r.set("auto", "format"), r) : (r.set(n.format, e), r);
}
const l = /-(\d+)x(\d+)\.[^.]+$/;
function P(t) {
const [, r, e] = t.pathname.match(l) ?? [];
return r && e ? { width: Number(r), height: Number(e) } : void 0;
}
const n = {
crop: "crop",
dpr: "dpr",
fit: "fit",
format: "fm",
height: "h",
quality: "q",
width: "w"
};
export {
A as adapter,
P as getDimensionsFromUrl
};
//# sourceMappingURL=adapter.js.map