sanity-plugin-link-field
Version:
A custom Link field for Sanity Studio
62 lines (61 loc) • 3.35 kB
JavaScript
import { jsx } from "react/jsx-runtime";
import { forwardRef } from "react";
import { isExternalLink, isEmailLink, isPhoneLink, isCustomLink, isInternalLink } from "./helpers.mjs";
const generateHref = {
internal: (link, hrefResolver) => {
var _a, _b, _c, _d, _e, _f;
const internalLink = link, resolvedHref = internalLink.internalLink && hrefResolver ? hrefResolver(internalLink) : void 0;
if (typeof resolvedHref == "object" && "pathname" in resolvedHref) {
if (resolvedHref.hash = (_a = internalLink.anchor) == null ? void 0 : _a.replace(/^#/, ""), internalLink.parameters) {
const params = new URLSearchParams(internalLink.parameters), resolvedParams = new URLSearchParams((_b = resolvedHref.query) == null ? void 0 : _b.toString());
for (const [key, value] of params.entries())
resolvedParams.set(key, value);
resolvedHref.query = resolvedParams.toString();
}
return resolvedHref;
}
let href = resolvedHref || ((_d = (_c = internalLink.internalLink) == null ? void 0 : _c.slug) != null && _d.current ? `/${internalLink.internalLink.slug.current.replace(/^\//, "")}` : void 0);
return href && typeof href == "string" && (href += (((_e = internalLink.parameters) == null ? void 0 : _e.trim()) || "") + (((_f = internalLink.anchor) == null ? void 0 : _f.trim()) || "")), href || "#";
},
external: (link) => {
var _a, _b;
return isExternalLink(link) && link.url ? link.url.trim() + (((_a = link.parameters) == null ? void 0 : _a.trim()) || "") + (((_b = link.anchor) == null ? void 0 : _b.trim()) || "") : "#";
},
email: (link) => isEmailLink(link) && link.email ? `mailto:${link.email.trim()}` : "#",
phone: (link) => {
var _a;
return isPhoneLink(link) && link.phone ? (
// Tel links cannot contain spaces
`tel:${(_a = link.phone) == null ? void 0 : _a.replace(/\s+/g, "").trim()}`
) : "#";
},
custom: (link) => {
var _a, _b;
return isCustomLink(link) && link.value ? link.value.trim() + (((_a = link.parameters) == null ? void 0 : _a.trim()) || "") + (((_b = link.anchor) == null ? void 0 : _b.trim()) || "") : "#";
}
}, getLinkText = (link) => {
var _a, _b, _c;
return link.text || (isInternalLink(link) ? (
// Naively try to get the title or slug of the internal link
((_a = link.internalLink) == null ? void 0 : _a.title) || ((_c = (_b = link.internalLink) == null ? void 0 : _b.slug) == null ? void 0 : _c.current)
) : isExternalLink(link) ? link.url : isPhoneLink(link) ? link.phone : isEmailLink(link) ? link.email : isCustomLink(link) ? link.value : void 0) || "#";
}, Link = forwardRef(
({ link, as: Component = "a", hrefResolver, children, ...props }, ref) => {
var _a, _b, _c, _d;
return link ? (children || (children = getLinkText(link)), /* @__PURE__ */ jsx(
Component,
{
href: link.type === "internal" ? (_b = (_a = generateHref)[link.type]) == null ? void 0 : _b.call(_a, link, hrefResolver) : (_d = (_c = generateHref)[isCustomLink(link) ? "custom" : link.type]) == null ? void 0 : _d.call(_c, link),
target: !isPhoneLink(link) && !isEmailLink(link) && link.blank ? "_blank" : void 0,
ref,
...props,
children
}
)) : null;
}
);
Link.displayName = "Link";
export {
Link
};
//# sourceMappingURL=component.mjs.map