UNPKG

@prismicio/client

Version:

The official JavaScript + TypeScript client library for Prismic

47 lines (45 loc) 1.73 kB
import { asLink } from "./asLink.js"; import { link } from "./isFilled.js"; import { isInternalURL } from "../lib/isInternalURL.js"; //#region src/helpers/asLinkAttrs.ts /** * Resolves any type of link field or Prismic page to a set of link attributes. * The attributes are designed to be passed to link HTML elements, like `<a>`. * * If a resolved URL is external (i.e. starts with a protocol like `https://`), * `rel` is returned as `"noreferrer"`. * * @typeParam LinkResolverFunctionReturnType - link resolver function return * type * @typeParam Field - Link field or Prismic page to resolve to link attributes * * @param linkFieldOrDocument - Any kind of link field or a page to resolve * @param config - Configuration that determines the output of `asLinkAttrs()` * * @returns Resolved set of link attributes or, if the provided link field or * page is empty, and empty object * * @see Learn about route resolvers and link resolvers: {@link https://prismic.io/docs/routes} */ const asLinkAttrs = (linkFieldOrDocument, config = {}) => { if (linkFieldOrDocument && ("link_type" in linkFieldOrDocument ? link(linkFieldOrDocument) : linkFieldOrDocument)) { const target = "target" in linkFieldOrDocument ? linkFieldOrDocument.target : void 0; const rawHref = asLink(linkFieldOrDocument, config.linkResolver); const href = rawHref == null ? void 0 : rawHref; const isExternal = typeof href === "string" ? !isInternalURL(href) : false; const rel = config.rel ? config.rel({ href, isExternal, target }) : isExternal ? "noreferrer" : void 0; return { href, target, rel: rel == null ? void 0 : rel }; } return {}; }; //#endregion export { asLinkAttrs }; //# sourceMappingURL=asLinkAttrs.js.map