{"version":3,"file":"asLinkAttrs.cjs","names":["isFilledLink","asLink","isInternalURL"],"sources":["../../src/helpers/asLinkAttrs.ts"],"sourcesContent":["import { isInternalURL } from \"../lib/isInternalURL\"\nimport type { FilledContentRelationshipField } from \"../types/value/contentRelationship\"\nimport type { PrismicDocument } from \"../types/value/document\"\nimport type { FilledLinkToWebField, LinkField } from \"../types/value/link\"\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\"\nimport type { AsLinkReturnType, LinkResolverFunction } from \"./asLink\"\nimport { asLink } from \"./asLink\"\nimport { link as isFilledLink } from \"./isFilled\"\n\ntype AsLinkAttrsConfigRelArgs<\n\tLinkResolverFunctionReturnType = ReturnType<LinkResolverFunction>,\n\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t| LinkField\n\t\t| PrismicDocument\n\t\t| null\n\t\t| undefined,\n> = {\n\thref: NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>> | undefined\n\tisExternal: boolean\n\ttarget?: string\n}\n\nexport type AsLinkAttrsConfig<\n\tLinkResolverFunctionReturnType = ReturnType<LinkResolverFunction>,\n\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t| LinkField\n\t\t| PrismicDocument\n\t\t| null\n\t\t| undefined,\n> = {\n\tlinkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType>\n\trel?: (\n\t\targs: AsLinkAttrsConfigRelArgs<LinkResolverFunctionReturnType, Field>,\n\t) => string | undefined | void\n}\n\n/** The return type of `asLinkAttrs()`. */\ntype AsLinkAttrsReturnType<\n\tLinkResolverFunctionReturnType = ReturnType<LinkResolverFunction>,\n\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t| LinkField\n\t\t| PrismicDocument\n\t\t| null\n\t\t| undefined,\n> = Field extends\n\t| FilledLinkToWebField\n\t| FilledLinkToMediaField\n\t| FilledContentRelationshipField\n\t| PrismicDocument\n\t? {\n\t\t\thref: NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>> | undefined\n\t\t\ttarget?: string\n\t\t\trel?: string\n\t\t}\n\t: {\n\t\t\thref?: undefined\n\t\t\ttarget?: undefined\n\t\t\trel?: undefined\n\t\t}\n\n/**\n * Resolves any type of link field or Prismic page to a set of link attributes. The attributes are\n * designed to be passed to link HTML elements, like `<a>`.\n *\n * If a resolved URL is external (i.e. starts with a protocol like `https://`), `rel` is returned as\n * `\"noreferrer\"`.\n *\n * @typeParam LinkResolverFunctionReturnType - Link resolver function return\n * type\n * @typeParam Field - Link field or Prismic page to resolve to link attributes\n * @param linkFieldOrDocument - Any kind of link field or a page to resolve\n * @param config - Configuration that determines the output of `asLinkAttrs()`\n * @returns Resolved set of link attributes or, if the provided link field or page is empty, and\n *   empty object\n * @see Learn about route resolvers and link resolvers: {@link https://prismic.io/docs/routes}\n */\nexport const asLinkAttrs = <\n\tLinkResolverFunctionReturnType = ReturnType<LinkResolverFunction>,\n\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t| LinkField\n\t\t| PrismicDocument\n\t\t| null\n\t\t| undefined,\n>(\n\tlinkFieldOrDocument: Field,\n\tconfig: AsLinkAttrsConfig<LinkResolverFunctionReturnType> = {},\n): AsLinkAttrsReturnType<LinkResolverFunctionReturnType> => {\n\tif (\n\t\tlinkFieldOrDocument &&\n\t\t(\"link_type\" in linkFieldOrDocument ? isFilledLink(linkFieldOrDocument) : linkFieldOrDocument)\n\t) {\n\t\tconst target = \"target\" in linkFieldOrDocument ? linkFieldOrDocument.target : undefined\n\n\t\tconst rawHref = asLink(linkFieldOrDocument, config.linkResolver)\n\t\tconst href = rawHref == null ? undefined : (rawHref as NonNullable<typeof rawHref>)\n\n\t\tconst isExternal = typeof href === \"string\" ? !isInternalURL(href) : false\n\n\t\tconst rel = config.rel\n\t\t\t? config.rel({ href, isExternal, target })\n\t\t\t: isExternal\n\t\t\t\t? \"noreferrer\"\n\t\t\t\t: undefined\n\n\t\treturn {\n\t\t\thref,\n\t\t\ttarget,\n\t\t\trel: rel == null ? undefined : rel,\n\t\t}\n\t}\n\n\treturn {}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA4EA,MAAa,eAQZ,qBACA,SAA4D,EAAE,KACH;AAC3D,KACC,wBACC,eAAe,sBAAsBA,iBAAAA,KAAa,oBAAoB,GAAG,sBACzE;EACD,MAAM,SAAS,YAAY,sBAAsB,oBAAoB,SAAS,KAAA;EAE9E,MAAM,UAAUC,eAAAA,OAAO,qBAAqB,OAAO,aAAa;EAChE,MAAM,OAAO,WAAW,OAAO,KAAA,IAAa;EAE5C,MAAM,aAAa,OAAO,SAAS,WAAW,CAACC,sBAAAA,cAAc,KAAK,GAAG;EAErE,MAAM,MAAM,OAAO,MAChB,OAAO,IAAI;GAAE;GAAM;GAAY;GAAQ,CAAC,GACxC,aACC,eACA,KAAA;AAEJ,SAAO;GACN;GACA;GACA,KAAK,OAAO,OAAO,KAAA,IAAY;GAC/B;;AAGF,QAAO,EAAE"}