@prismicio/client
Version:
The official JavaScript + TypeScript client library for Prismic
43 lines (42 loc) • 1.82 kB
JavaScript
const require_asLink = require("./asLink.cjs");
const require_isFilled = require("./isFilled.cjs");
const require_isInternalURL = require("../lib/isInternalURL.cjs");
//#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 ? require_isFilled.link(linkFieldOrDocument) : linkFieldOrDocument)) {
const target = "target" in linkFieldOrDocument ? linkFieldOrDocument.target : void 0;
const rawHref = require_asLink.asLink(linkFieldOrDocument, config.linkResolver);
const href = rawHref == null ? void 0 : rawHref;
const isExternal = typeof href === "string" ? !require_isInternalURL.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
exports.asLinkAttrs = asLinkAttrs;
//# sourceMappingURL=asLinkAttrs.cjs.map