@prismicio/client 7.0.0 → 7.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/buildQueryURL.cjs.map +1 -1
- package/dist/buildQueryURL.d.ts +11 -10
- package/dist/buildQueryURL.js.map +1 -1
- package/dist/createClient.cjs +39 -6
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +68 -21
- package/dist/createClient.js +39 -6
- package/dist/createClient.js.map +1 -1
- package/dist/filter.cjs +38 -38
- package/dist/filter.cjs.map +1 -1
- package/dist/filter.d.ts +38 -38
- package/dist/filter.js +38 -38
- package/dist/filter.js.map +1 -1
- package/dist/getGraphQLEndpoint.cjs.map +1 -1
- package/dist/getGraphQLEndpoint.d.ts +1 -0
- package/dist/getGraphQLEndpoint.js.map +1 -1
- package/dist/getRepositoryEndpoint.cjs.map +1 -1
- package/dist/getRepositoryEndpoint.d.ts +2 -0
- package/dist/getRepositoryEndpoint.js.map +1 -1
- package/dist/getRepositoryName.cjs.map +1 -1
- package/dist/getRepositoryName.d.ts +1 -0
- package/dist/getRepositoryName.js.map +1 -1
- package/dist/getToolbarSrc.cjs.map +1 -1
- package/dist/getToolbarSrc.d.ts +1 -1
- package/dist/getToolbarSrc.js.map +1 -1
- package/dist/helpers/asDate.cjs.map +1 -1
- package/dist/helpers/asDate.d.ts +1 -0
- package/dist/helpers/asDate.js.map +1 -1
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.d.ts +21 -15
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asLink.cjs.map +1 -1
- package/dist/helpers/asLink.d.ts +24 -15
- package/dist/helpers/asLink.js.map +1 -1
- package/dist/helpers/asLinkAttrs.cjs.map +1 -1
- package/dist/helpers/asLinkAttrs.d.ts +9 -4
- package/dist/helpers/asLinkAttrs.js.map +1 -1
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.d.ts +11 -7
- package/dist/helpers/asText.js.map +1 -1
- package/dist/helpers/documentToLinkField.cjs.map +1 -1
- package/dist/helpers/documentToLinkField.d.ts +2 -0
- package/dist/helpers/documentToLinkField.js.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/appendFilters.cjs.map +1 -1
- package/dist/lib/appendFilters.d.ts +4 -4
- package/dist/lib/appendFilters.js.map +1 -1
- package/dist/lib/castArray.cjs.map +1 -1
- package/dist/lib/castArray.d.ts +1 -0
- package/dist/lib/castArray.js.map +1 -1
- package/dist/lib/castThunk.cjs.map +1 -1
- package/dist/lib/castThunk.d.ts +2 -1
- package/dist/lib/castThunk.js.map +1 -1
- package/dist/lib/everyTagFilter.cjs.map +1 -1
- package/dist/lib/everyTagFilter.d.ts +2 -2
- package/dist/lib/everyTagFilter.js.map +1 -1
- package/dist/lib/findMasterRef.cjs.map +1 -1
- package/dist/lib/findMasterRef.d.ts +1 -0
- package/dist/lib/findMasterRef.js.map +1 -1
- package/dist/lib/findRef.cjs.map +1 -1
- package/dist/lib/findRef.d.ts +3 -2
- package/dist/lib/findRef.js.map +1 -1
- package/dist/lib/findRefByID.cjs.map +1 -1
- package/dist/lib/findRefByID.d.ts +1 -0
- package/dist/lib/findRefByID.js.map +1 -1
- package/dist/lib/findRefByLabel.cjs.map +1 -1
- package/dist/lib/findRefByLabel.d.ts +1 -0
- package/dist/lib/findRefByLabel.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/types/api/query.d.ts +1 -0
- package/dist/types/api/release.d.ts +1 -0
- package/dist/types/api/tags.d.ts +1 -0
- package/dist/types/model/contentRelationship.d.ts +1 -2
- package/dist/types/model/link.cjs.map +1 -1
- package/dist/types/model/link.d.ts +2 -4
- package/dist/types/model/link.js.map +1 -1
- package/dist/types/model/linkToMedia.d.ts +1 -2
- package/dist/types/value/color.d.ts +1 -0
- package/dist/types/value/date.d.ts +1 -0
- package/dist/types/value/embed.cjs.map +1 -1
- package/dist/types/value/embed.d.ts +1 -0
- package/dist/types/value/embed.js.map +1 -1
- package/dist/types/value/geoPoint.d.ts +1 -0
- package/dist/types/value/image.d.ts +2 -0
- package/dist/types/value/integration.d.ts +1 -0
- package/dist/types/value/keyText.d.ts +1 -0
- package/dist/types/value/number.d.ts +1 -0
- package/dist/types/value/select.d.ts +1 -0
- package/dist/types/value/types.d.ts +1 -0
- package/package.json +3 -3
- package/src/buildQueryURL.ts +11 -10
- package/src/createClient.ts +85 -28
- package/src/filter.ts +42 -42
- package/src/getGraphQLEndpoint.ts +1 -0
- package/src/getRepositoryEndpoint.ts +2 -0
- package/src/getRepositoryName.ts +1 -0
- package/src/getToolbarSrc.ts +1 -1
- package/src/helpers/asDate.ts +1 -0
- package/src/helpers/asHTML.ts +23 -17
- package/src/helpers/asLink.ts +24 -15
- package/src/helpers/asLinkAttrs.ts +9 -4
- package/src/helpers/asText.ts +11 -7
- package/src/helpers/documentToLinkField.ts +2 -0
- package/src/index.ts +1 -0
- package/src/lib/appendFilters.ts +4 -4
- package/src/lib/castArray.ts +1 -0
- package/src/lib/castThunk.ts +9 -2
- package/src/lib/everyTagFilter.ts +2 -2
- package/src/lib/findMasterRef.ts +1 -0
- package/src/lib/findRef.ts +3 -2
- package/src/lib/findRefByID.ts +1 -0
- package/src/lib/findRefByLabel.ts +1 -0
- package/src/types/api/query.ts +1 -0
- package/src/types/api/release.ts +1 -0
- package/src/types/api/tags.ts +1 -0
- package/src/types/model/contentRelationship.ts +1 -2
- package/src/types/model/link.ts +2 -4
- package/src/types/model/linkToMedia.ts +1 -2
- package/src/types/value/color.ts +1 -0
- package/src/types/value/date.ts +1 -0
- package/src/types/value/embed.ts +1 -0
- package/src/types/value/geoPoint.ts +1 -0
- package/src/types/value/image.ts +2 -0
- package/src/types/value/integration.ts +1 -0
- package/src/types/value/keyText.ts +1 -0
- package/src/types/value/number.ts +1 -0
- package/src/types/value/select.ts +1 -0
- package/src/types/value/types.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getToolbarSrc.js","sources":["../../src/getToolbarSrc.ts"],"sourcesContent":["import { PrismicError } from \"./errors/PrismicError\";\n\nimport { isRepositoryName } from \"./isRepositoryName\";\n\n/**\n * Returns the URL for a Prismic repository's Prismic Toolbar script. Use the\n * URL to inject the script into your app.\n *\n * @example\n *\n * ```typescriptreact\n * // In Next.js apps, use `next/script` in your `app/layout.tsx` file.\n *\n * import Script from \"next/script\";\n * import * as prismic from
|
|
1
|
+
{"version":3,"file":"getToolbarSrc.js","sources":["../../src/getToolbarSrc.ts"],"sourcesContent":["import { PrismicError } from \"./errors/PrismicError\";\n\nimport { isRepositoryName } from \"./isRepositoryName\";\n\n/**\n * Returns the URL for a Prismic repository's Prismic Toolbar script. Use the\n * URL to inject the script into your app.\n *\n * @example\n *\n * ```typescriptreact\n * // In Next.js apps, use `next/script` in your `app/layout.tsx` file.\n *\n * import Script from \"next/script\";\n * import * as prismic from \"@prismicio/client\";\n *\n * export default function RootLayout({\n * \tchildren,\n * }: {\n * \tchildren: React.ReactNode,\n * }) {\n * \tconst toolbarSrc = prismic.getToolbarSrc(\"my-repo\");\n *\n * \treturn (\n * \t\t<html lang=\"en\">\n * \t\t\t<body>{children}</body>\n * \t\t\t<Script src={toolbarSrc} />\n * \t\t</html>\n * \t);\n * }\n * ```\n *\n * @param repositoryName - The name of the Prismic repository. For example,\n * `\"my-repo\"` if the repository URL is `my-repo.prismic.io`.\n *\n * @returns The URL for the given Prismic repository's Prismic Toolbar script.\n */\nexport const getToolbarSrc = <TRepositoryName extends string>(\n\trepositoryName: TRepositoryName,\n): `https://static.cdn.prismic.io/prismic.js?new=true&repo=${TRepositoryName}` => {\n\tif (isRepositoryName(repositoryName)) {\n\t\treturn `https://static.cdn.prismic.io/prismic.js?new=true&repo=${repositoryName}` as const;\n\t} else {\n\t\tthrow new PrismicError(\n\t\t\t`An invalid Prismic repository name was given: ${repositoryName}`,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t);\n\t}\n};\n"],"names":[],"mappings":";;AAqCa,MAAA,gBAAgB,CAC5B,mBACgF;AAC5E,MAAA,iBAAiB,cAAc,GAAG;AACrC,WAAO,0DAA0D;AAAA,EAAA,OAC3D;AACN,UAAM,IAAI,aACT,iDAAiD,kBACjD,QACA,MAAS;AAAA,EAEV;AACF;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asDate.cjs","sources":["../../../src/helpers/asDate.ts"],"sourcesContent":["import type { DateField } from \"../types/value/date\";\nimport type { TimestampField } from \"../types/value/timestamp\";\n\n/**\n * The return type of `asDate()`.\n */\ntype AsDateReturnType<\n\tField extends DateField | TimestampField | null | undefined,\n> = Field extends DateField<\"filled\"> | TimestampField<\"filled\"> ? Date : null;\n\n/**\n * Transforms a date or timestamp field into a JavaScript Date object\n *\n * @param dateOrTimestampField - A date or timestamp field from Prismic\n *\n * @returns A Date object, null if provided date is falsy\n * @see Templating date field from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#date-and-timestamp}\n */\nexport const asDate = <\n\tField extends DateField | TimestampField | null | undefined,\n>(\n\tdateOrTimestampField: Field,\n): AsDateReturnType<Field> => {\n\tif (!dateOrTimestampField) {\n\t\treturn null as AsDateReturnType<Field>;\n\t}\n\n\t// If field is a timestamp field...\n\tif (dateOrTimestampField.length === 24) {\n\t\t/**\n\t\t * Converts basic ISO 8601 to ECMAScript simplified ISO 8601 format for\n\t\t * browser compatibility issues\n\t\t *\n\t\t * From: YYYY-MM-DDTHH:mm:ssZ To: YYYY-MM-DDTHH:mm:ss.sssZ\n\t\t *\n\t\t * @see MDN documentation: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#timestamp_string}\n\t\t * @see ECMAScript 2020 language specification: {@link https://262.ecma-international.org/11.0/#sec-date-time-string-format}\n\t\t * @see Related forum issue: {@link https://community.prismic.io/t/prismics-date-api/2520}\n\t\t * @see Regex101 expression: {@link https://regex101.com/r/jxyETT/1}\n\t\t */\n\t\treturn new Date(\n\t\t\tdateOrTimestampField.replace(/(\\+|-)(\\d{2})(\\d{2})$/, \".000$1$2:$3\"),\n\t\t) as AsDateReturnType<Field>;\n\t} else {\n\t\t// ...else field is a date field\n\t\treturn new Date(dateOrTimestampField) as AsDateReturnType<Field>;\n\t}\n};\n"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"asDate.cjs","sources":["../../../src/helpers/asDate.ts"],"sourcesContent":["import type { DateField } from \"../types/value/date\";\nimport type { TimestampField } from \"../types/value/timestamp\";\n\n/**\n * The return type of `asDate()`.\n */\ntype AsDateReturnType<\n\tField extends DateField | TimestampField | null | undefined,\n> = Field extends DateField<\"filled\"> | TimestampField<\"filled\"> ? Date : null;\n\n/**\n * Transforms a date or timestamp field into a JavaScript Date object\n *\n * @param dateOrTimestampField - A date or timestamp field from Prismic\n *\n * @returns A Date object, null if provided date is falsy\n *\n * @see Templating date field from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#date-and-timestamp}\n */\nexport const asDate = <\n\tField extends DateField | TimestampField | null | undefined,\n>(\n\tdateOrTimestampField: Field,\n): AsDateReturnType<Field> => {\n\tif (!dateOrTimestampField) {\n\t\treturn null as AsDateReturnType<Field>;\n\t}\n\n\t// If field is a timestamp field...\n\tif (dateOrTimestampField.length === 24) {\n\t\t/**\n\t\t * Converts basic ISO 8601 to ECMAScript simplified ISO 8601 format for\n\t\t * browser compatibility issues\n\t\t *\n\t\t * From: YYYY-MM-DDTHH:mm:ssZ To: YYYY-MM-DDTHH:mm:ss.sssZ\n\t\t *\n\t\t * @see MDN documentation: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#timestamp_string}\n\t\t * @see ECMAScript 2020 language specification: {@link https://262.ecma-international.org/11.0/#sec-date-time-string-format}\n\t\t * @see Related forum issue: {@link https://community.prismic.io/t/prismics-date-api/2520}\n\t\t * @see Regex101 expression: {@link https://regex101.com/r/jxyETT/1}\n\t\t */\n\t\treturn new Date(\n\t\t\tdateOrTimestampField.replace(/(\\+|-)(\\d{2})(\\d{2})$/, \".000$1$2:$3\"),\n\t\t) as AsDateReturnType<Field>;\n\t} else {\n\t\t// ...else field is a date field\n\t\treturn new Date(dateOrTimestampField) as AsDateReturnType<Field>;\n\t}\n};\n"],"names":[],"mappings":";;AAmBa,MAAA,SAAS,CAGrB,yBAC4B;AAC5B,MAAI,CAAC,sBAAsB;AACnB,WAAA;AAAA,EACP;AAGG,MAAA,qBAAqB,WAAW,IAAI;AAYvC,WAAO,IAAI,KACV,qBAAqB,QAAQ,yBAAyB,aAAa,CAAC;AAAA,EAAA,OAE/D;AAEC,WAAA,IAAI,KAAK,oBAAoB;AAAA,EACpC;AACF;;"}
|
package/dist/helpers/asDate.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ type AsDateReturnType<Field extends DateField | TimestampField | null | undefine
|
|
|
10
10
|
* @param dateOrTimestampField - A date or timestamp field from Prismic
|
|
11
11
|
*
|
|
12
12
|
* @returns A Date object, null if provided date is falsy
|
|
13
|
+
*
|
|
13
14
|
* @see Templating date field from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#date-and-timestamp}
|
|
14
15
|
*/
|
|
15
16
|
export declare const asDate: <Field extends `${number}-${number}-${number}` | `${number}-${number}-${number}T${number}:${number}:${number}+${number}` | null | undefined>(dateOrTimestampField: Field) => AsDateReturnType<Field>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asDate.js","sources":["../../../src/helpers/asDate.ts"],"sourcesContent":["import type { DateField } from \"../types/value/date\";\nimport type { TimestampField } from \"../types/value/timestamp\";\n\n/**\n * The return type of `asDate()`.\n */\ntype AsDateReturnType<\n\tField extends DateField | TimestampField | null | undefined,\n> = Field extends DateField<\"filled\"> | TimestampField<\"filled\"> ? Date : null;\n\n/**\n * Transforms a date or timestamp field into a JavaScript Date object\n *\n * @param dateOrTimestampField - A date or timestamp field from Prismic\n *\n * @returns A Date object, null if provided date is falsy\n * @see Templating date field from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#date-and-timestamp}\n */\nexport const asDate = <\n\tField extends DateField | TimestampField | null | undefined,\n>(\n\tdateOrTimestampField: Field,\n): AsDateReturnType<Field> => {\n\tif (!dateOrTimestampField) {\n\t\treturn null as AsDateReturnType<Field>;\n\t}\n\n\t// If field is a timestamp field...\n\tif (dateOrTimestampField.length === 24) {\n\t\t/**\n\t\t * Converts basic ISO 8601 to ECMAScript simplified ISO 8601 format for\n\t\t * browser compatibility issues\n\t\t *\n\t\t * From: YYYY-MM-DDTHH:mm:ssZ To: YYYY-MM-DDTHH:mm:ss.sssZ\n\t\t *\n\t\t * @see MDN documentation: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#timestamp_string}\n\t\t * @see ECMAScript 2020 language specification: {@link https://262.ecma-international.org/11.0/#sec-date-time-string-format}\n\t\t * @see Related forum issue: {@link https://community.prismic.io/t/prismics-date-api/2520}\n\t\t * @see Regex101 expression: {@link https://regex101.com/r/jxyETT/1}\n\t\t */\n\t\treturn new Date(\n\t\t\tdateOrTimestampField.replace(/(\\+|-)(\\d{2})(\\d{2})$/, \".000$1$2:$3\"),\n\t\t) as AsDateReturnType<Field>;\n\t} else {\n\t\t// ...else field is a date field\n\t\treturn new Date(dateOrTimestampField) as AsDateReturnType<Field>;\n\t}\n};\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"asDate.js","sources":["../../../src/helpers/asDate.ts"],"sourcesContent":["import type { DateField } from \"../types/value/date\";\nimport type { TimestampField } from \"../types/value/timestamp\";\n\n/**\n * The return type of `asDate()`.\n */\ntype AsDateReturnType<\n\tField extends DateField | TimestampField | null | undefined,\n> = Field extends DateField<\"filled\"> | TimestampField<\"filled\"> ? Date : null;\n\n/**\n * Transforms a date or timestamp field into a JavaScript Date object\n *\n * @param dateOrTimestampField - A date or timestamp field from Prismic\n *\n * @returns A Date object, null if provided date is falsy\n *\n * @see Templating date field from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#date-and-timestamp}\n */\nexport const asDate = <\n\tField extends DateField | TimestampField | null | undefined,\n>(\n\tdateOrTimestampField: Field,\n): AsDateReturnType<Field> => {\n\tif (!dateOrTimestampField) {\n\t\treturn null as AsDateReturnType<Field>;\n\t}\n\n\t// If field is a timestamp field...\n\tif (dateOrTimestampField.length === 24) {\n\t\t/**\n\t\t * Converts basic ISO 8601 to ECMAScript simplified ISO 8601 format for\n\t\t * browser compatibility issues\n\t\t *\n\t\t * From: YYYY-MM-DDTHH:mm:ssZ To: YYYY-MM-DDTHH:mm:ss.sssZ\n\t\t *\n\t\t * @see MDN documentation: {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#timestamp_string}\n\t\t * @see ECMAScript 2020 language specification: {@link https://262.ecma-international.org/11.0/#sec-date-time-string-format}\n\t\t * @see Related forum issue: {@link https://community.prismic.io/t/prismics-date-api/2520}\n\t\t * @see Regex101 expression: {@link https://regex101.com/r/jxyETT/1}\n\t\t */\n\t\treturn new Date(\n\t\t\tdateOrTimestampField.replace(/(\\+|-)(\\d{2})(\\d{2})$/, \".000$1$2:$3\"),\n\t\t) as AsDateReturnType<Field>;\n\t} else {\n\t\t// ...else field is a date field\n\t\treturn new Date(dateOrTimestampField) as AsDateReturnType<Field>;\n\t}\n};\n"],"names":[],"mappings":"AAmBa,MAAA,SAAS,CAGrB,yBAC4B;AAC5B,MAAI,CAAC,sBAAsB;AACnB,WAAA;AAAA,EACP;AAGG,MAAA,qBAAqB,WAAW,IAAI;AAYvC,WAAO,IAAI,KACV,qBAAqB,QAAQ,yBAAyB,aAAa,CAAC;AAAA,EAAA,OAE/D;AAEC,WAAA,IAAI,KAAK,oBAAoB;AAAA,EACpC;AACF;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asHTML.cjs","sources":["../../../src/helpers/asHTML.ts"],"sourcesContent":["import {\n\tElement,\n\tRichTextFunctionSerializer,\n\tRichTextMapSerializer,\n\tRichTextMapSerializerFunction,\n\tcomposeSerializers,\n\tserialize,\n\twrapMapSerializer,\n} from \"@prismicio/richtext\";\n\nimport {\n\tserializeEmbed,\n\tserializeHyperlink,\n\tserializeImage,\n\tserializePreFormatted,\n\tserializeSpan,\n\tserializeStandardTag,\n} from \"../lib/serializerHelpers\";\n\nimport type { RichTextField } from \"../types/value/richText\";\n\nimport { LinkResolverFunction } from \"./asLink\";\n\n/**\n * Serializes a node from a rich text or title field with a function to HTML.\n *\n * Unlike a typical `@prismicio/richtext` function serializer, this serializer\n * converts the `children` argument to a single string rather than an array of\n * strings.\n *\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextFunctionSerializer = (\n\ttype: Parameters<RichTextFunctionSerializer<string>>[0],\n\tnode: Parameters<RichTextFunctionSerializer<string>>[1],\n\ttext: Parameters<RichTextFunctionSerializer<string>>[2],\n\tchildren: Parameters<RichTextFunctionSerializer<string>>[3][number],\n\tkey: Parameters<RichTextFunctionSerializer<string>>[4],\n) => string | null | undefined;\n\n/**\n * Serializes a node from a rich text or title field with a map to HTML\n *\n * Unlike a typical `@prismicio/richtext` map serializer, this serializer\n * converts the `children` property to a single string rather than an array of\n * strings.\n *\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextMapSerializer = {\n\t[P in keyof RichTextMapSerializer<string>]: (payload: {\n\t\ttype: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"type\"];\n\t\tnode: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"node\"];\n\t\ttext: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"text\"];\n\t\tchildren: Parameters<\n\t\t\tHTMLRichTextMapSerializerFunction<P>\n\t\t>[0][\"children\"][number];\n\t\tkey: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"key\"];\n\t}) => string | null | undefined;\n};\n\n/**\n * A {@link RichTextMapSerializerFunction} type specifically for\n * {@link HTMLRichTextMapSerializer}.\n *\n * @typeParam BlockName - The serializer's rich text block type.\n */\ntype HTMLRichTextMapSerializerFunction<\n\tBlockType extends keyof RichTextMapSerializer<string>,\n> = RichTextMapSerializerFunction<\n\tstring,\n\tExtractNodeGeneric<RichTextMapSerializer<string>[BlockType]>,\n\tExtractTextTypeGeneric<RichTextMapSerializer<string>[BlockType]>\n>;\n\n/**\n * Returns the `Node` generic from {@link RichTextMapSerializerFunction}.\n *\n * @typeParam T - The `RichTextMapSerializerFunction` containing the needed\n * `Node` generic.\n */\ntype ExtractNodeGeneric<T> = T extends RichTextMapSerializerFunction<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tinfer U,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany\n>\n\t? U\n\t: never;\n\n/**\n * Returns the `TextType` generic from {@link RichTextMapSerializerFunction}.\n *\n * @typeParam T - The `RichTextMapSerializerFunction` containing the needed\n * `TextType` generic.\n */\ntype ExtractTextTypeGeneric<T> = T extends RichTextMapSerializerFunction<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tinfer U\n>\n\t? U\n\t: never;\n\n/**\n * Serializes a node from a rich text or title field with a map or a function to HTML\n *\n * @see {@link HTMLRichTextMapSerializer} and {@link HTMLRichTextFunctionSerializer}\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextSerializer =\n\t| HTMLRichTextMapSerializer\n\t| HTMLRichTextFunctionSerializer;\n\n/**\n * Creates a default HTML rich text serializer with a given link resolver providing\n * sensible and safe defaults for every node type\n *\n * @internal\n */\nconst createDefaultHTMLRichTextSerializer = (\n\tlinkResolver: LinkResolverFunction | undefined | null,\n): RichTextFunctionSerializer<string> => {\n\treturn (_type, node, text, children, _key) => {\n\t\tswitch (node.type) {\n\t\t\tcase Element.heading1:\n\t\t\t\treturn serializeStandardTag(\"h1\", node, children);\n\t\t\tcase Element.heading2:\n\t\t\t\treturn serializeStandardTag(\"h2\", node, children);\n\t\t\tcase Element.heading3:\n\t\t\t\treturn serializeStandardTag(\"h3\", node, children);\n\t\t\tcase Element.heading4:\n\t\t\t\treturn serializeStandardTag(\"h4\", node, children);\n\t\t\tcase Element.heading5:\n\t\t\t\treturn serializeStandardTag(\"h5\", node, children);\n\t\t\tcase Element.heading6:\n\t\t\t\treturn serializeStandardTag(\"h6\", node, children);\n\t\t\tcase Element.paragraph:\n\t\t\t\treturn serializeStandardTag(\"p\", node, children);\n\t\t\tcase Element.preformatted:\n\t\t\t\treturn serializePreFormatted(node);\n\t\t\tcase Element.strong:\n\t\t\t\treturn serializeStandardTag(\"strong\", node, children);\n\t\t\tcase Element.em:\n\t\t\t\treturn serializeStandardTag(\"em\", node, children);\n\t\t\tcase Element.listItem:\n\t\t\t\treturn serializeStandardTag(\"li\", node, children);\n\t\t\tcase Element.oListItem:\n\t\t\t\treturn serializeStandardTag(\"li\", node, children);\n\t\t\tcase Element.list:\n\t\t\t\treturn serializeStandardTag(\"ul\", node, children);\n\t\t\tcase Element.oList:\n\t\t\t\treturn serializeStandardTag(\"ol\", node, children);\n\t\t\tcase Element.image:\n\t\t\t\treturn serializeImage(linkResolver, node);\n\t\t\tcase Element.embed:\n\t\t\t\treturn serializeEmbed(node);\n\t\t\tcase Element.hyperlink:\n\t\t\t\treturn serializeHyperlink(linkResolver, node, children);\n\t\t\tcase Element.label:\n\t\t\t\treturn serializeStandardTag(\"span\", node, children);\n\t\t\tcase Element.span:\n\t\t\tdefault:\n\t\t\t\treturn serializeSpan(text);\n\t\t}\n\t};\n};\n\n/**\n * Wraps a map serializer into a regular function serializer. The given map\n * serializer should accept children as a string, not as an array of strings\n * like `@prismicio/richtext`'s `wrapMapSerializer`.\n *\n * @param mapSerializer - Map serializer to wrap\n *\n * @returns A regular function serializer\n */\nconst wrapMapSerializerWithStringChildren = (\n\tmapSerializer: HTMLRichTextMapSerializer,\n): RichTextFunctionSerializer<string> => {\n\tconst modifiedMapSerializer = {} as RichTextMapSerializer<string>;\n\n\tfor (const tag in mapSerializer) {\n\t\tconst tagSerializer = mapSerializer[tag as keyof typeof mapSerializer];\n\n\t\tif (tagSerializer) {\n\t\t\tmodifiedMapSerializer[tag as keyof typeof mapSerializer] = (payload) => {\n\t\t\t\treturn tagSerializer({\n\t\t\t\t\t...payload,\n\t\t\t\t\t// @ts-expect-error - merging blockSerializer types causes TS to bail to a never type\n\t\t\t\t\tchildren: payload.children.join(\"\"),\n\t\t\t\t});\n\t\t\t};\n\t\t}\n\t}\n\n\treturn wrapMapSerializer(modifiedMapSerializer);\n};\n\n/**\n * Configuration that determines the output of `asHTML()`.\n */\ntype AsHTMLConfig = {\n\t/**\n\t * An optional link resolver function to resolve links.\n\t * Without it you're expected to use the `routes` options from the API.\n\t */\n\tlinkResolver?: LinkResolverFunction | null;\n\n\t/**\n\t * An optional rich text serializer, unhandled cases will fallback to the default serializer\n\t */\n\tserializer?: HTMLRichTextSerializer | null;\n};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsHTMLDeprecatedTupleConfig = [\n\tlinkResolver?: LinkResolverFunction | null,\n\tserializer?: HTMLRichTextSerializer | null,\n];\n\n/**\n * The return type of `asHTML()`.\n */\ntype AsHTMLReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\n// TODO: Remove overload when we remove support for deprecated tuple-style configuration.\nexport const asHTML: {\n\t/**\n\t * Serializes a rich text or title field to an HTML string.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param config - Configuration that determines the output of `asHTML()`\n\t *\n\t * @returns HTML equivalent of the provided rich text or title field\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\tconfig?: AsHTMLConfig,\n\t): AsHTMLReturnType<Field>;\n\n\t/**\n\t * Serializes a rich text or title field to an HTML string.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param linkResolver - An optional link resolver function to resolve links,\n\t * without it you're expected to use the `routes` options from the API\n\t * @param serializer - An optional rich text serializer, unhandled cases will fallback\n\t * to the default serializer\n\t *\n\t * @returns HTML equivalent of the provided rich text or title field\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * ```ts\n\t * asHTML(field);\n\t * asHTML(field, { linkResolver });\n\t * asHTML(field, { serializer });\n\t * asHTML(field, { linkResolver, serializer });\n\t * ```\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\t...config: AsHTMLDeprecatedTupleConfig\n\t): AsHTMLReturnType<Field>;\n} = <Field extends RichTextField | null | undefined>(\n\trichTextField: Field,\n\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple: [config?: AsHTMLConfig] | AsHTMLDeprecatedTupleConfig\n): AsHTMLReturnType<Field> => {\n\tif (richTextField) {\n\t\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\t\tconst [configObjectOrLinkResolver, maybeSerializer] = configObjectOrTuple;\n\t\tlet config: AsHTMLConfig;\n\t\tif (\n\t\t\ttypeof configObjectOrLinkResolver === \"function\" ||\n\t\t\tconfigObjectOrLinkResolver == null\n\t\t) {\n\t\t\tconfig = {\n\t\t\t\tlinkResolver: configObjectOrLinkResolver,\n\t\t\t\tserializer: maybeSerializer,\n\t\t\t};\n\t\t} else {\n\t\t\tconfig = { ...configObjectOrLinkResolver };\n\t\t}\n\n\t\tlet serializer: RichTextFunctionSerializer<string>;\n\t\tif (config.serializer) {\n\t\t\tserializer = composeSerializers(\n\t\t\t\ttypeof config.serializer === \"object\"\n\t\t\t\t\t? wrapMapSerializerWithStringChildren(config.serializer)\n\t\t\t\t\t: (type, node, text, children, key) =>\n\t\t\t\t\t\t\t// TypeScript doesn't narrow the type correctly here since it is now in a callback function, so we have to cast it here.\n\t\t\t\t\t\t\t(config.serializer as HTMLRichTextFunctionSerializer)(\n\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\tnode,\n\t\t\t\t\t\t\t\ttext,\n\t\t\t\t\t\t\t\tchildren.join(\"\"),\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t),\n\t\t\t\tcreateDefaultHTMLRichTextSerializer(config.linkResolver),\n\t\t\t);\n\t\t} else {\n\t\t\tserializer = createDefaultHTMLRichTextSerializer(config.linkResolver);\n\t\t}\n\n\t\treturn serialize(richTextField, serializer).join(\n\t\t\t\"\",\n\t\t) as AsHTMLReturnType<Field>;\n\t} else {\n\t\treturn null as AsHTMLReturnType<Field>;\n\t}\n};\n"],"names":["Element","serializeStandardTag","serializePreFormatted","serializeImage","serializeEmbed","serializeHyperlink","serializeSpan","wrapMapSerializer","composeSerializers","serialize"],"mappings":";;;;AA2HA,MAAM,sCAAsC,CAC3C,iBACuC;AACvC,SAAO,CAAC,OAAO,MAAM,MAAM,UAAU,SAAQ;AAC5C,YAAQ,KAAK,MAAM;AAAA,MAClB,KAAKA,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,KAAK,MAAM,QAAQ;AAAA,MAChD,KAAKD,SAAQ,QAAA;AACZ,eAAOE,kBAAAA,sBAAsB,IAAI;AAAA,MAClC,KAAKF,SAAQ,QAAA;AACL,eAAAC,uCAAqB,UAAU,MAAM,QAAQ;AAAA,MACrD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAG,kBAAA,eAAe,cAAc,IAAI;AAAA,MACzC,KAAKH,SAAQ,QAAA;AACZ,eAAOI,kBAAAA,eAAe,IAAI;AAAA,MAC3B,KAAKJ,SAAQ,QAAA;AACL,eAAAK,qCAAmB,cAAc,MAAM,QAAQ;AAAA,MACvD,KAAKL,SAAQ,QAAA;AACL,eAAAC,uCAAqB,QAAQ,MAAM,QAAQ;AAAA,MACnD,KAAKD,SAAAA,QAAQ;AAAA,MACb;AACC,eAAOM,kBAAAA,cAAc,IAAI;AAAA,IAC1B;AAAA,EAAA;AAEH;AAWA,MAAM,sCAAsC,CAC3C,kBACuC;AACvC,QAAM,wBAAwB,CAAA;AAE9B,aAAW,OAAO,eAAe;AAC1B,UAAA,gBAAgB,cAAc,GAAiC;AAErE,QAAI,eAAe;AACI,4BAAA,GAAiC,IAAI,CAAC,YAAW;AACtE,eAAO,cAAc;AAAA,UACpB,GAAG;AAAA;AAAA,UAEH,UAAU,QAAQ,SAAS,KAAK,EAAE;AAAA,QAAA,CAClC;AAAA,MAAA;AAAA,IAEF;AAAA,EACD;AAED,SAAOC,SAAAA,kBAAkB,qBAAqB;AAC/C;AAkCa,MAAA,SAwCT,CACH,kBAEG,wBACyB;AAC5B,MAAI,eAAe;AAEZ,UAAA,CAAC,4BAA4B,eAAe,IAAI;AAClD,QAAA;AACJ,QACC,OAAO,+BAA+B,cACtC,8BAA8B,MAC7B;AACQ,eAAA;AAAA,QACR,cAAc;AAAA,QACd,YAAY;AAAA,MAAA;AAAA,WAEP;AACG,eAAA,EAAE,GAAG;IACd;AAEG,QAAA;AACJ,QAAI,OAAO,YAAY;AACtB,mBAAaC,SAAAA,mBACZ,OAAO,OAAO,eAAe,WAC1B,oCAAoC,OAAO,UAAU,IACrD,CAAC,MAAM,MAAM,MAAM,UAAU;AAAA;AAAA,QAE5B,OAAO,WACP,MACA,MACA,MACA,SAAS,KAAK,EAAE,GAChB,GAAG;AAAA,SAEP,oCAAoC,OAAO,YAAY,CAAC;AAAA,IAAA,OAEnD;AACO,mBAAA,oCAAoC,OAAO,YAAY;AAAA,IACpE;AAED,WAAOC,SAAAA,UAAU,eAAe,UAAU,EAAE,KAC3C,EAAE;AAAA,EAAA,OAEG;AACC,WAAA;AAAA,EACP;AACF;;"}
|
|
1
|
+
{"version":3,"file":"asHTML.cjs","sources":["../../../src/helpers/asHTML.ts"],"sourcesContent":["import {\n\tElement,\n\tRichTextFunctionSerializer,\n\tRichTextMapSerializer,\n\tRichTextMapSerializerFunction,\n\tcomposeSerializers,\n\tserialize,\n\twrapMapSerializer,\n} from \"@prismicio/richtext\";\n\nimport {\n\tserializeEmbed,\n\tserializeHyperlink,\n\tserializeImage,\n\tserializePreFormatted,\n\tserializeSpan,\n\tserializeStandardTag,\n} from \"../lib/serializerHelpers\";\n\nimport type { RichTextField } from \"../types/value/richText\";\n\nimport { LinkResolverFunction } from \"./asLink\";\n\n/**\n * Serializes a node from a rich text or title field with a function to HTML.\n *\n * Unlike a typical `@prismicio/richtext` function serializer, this serializer\n * converts the `children` argument to a single string rather than an array of\n * strings.\n *\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextFunctionSerializer = (\n\ttype: Parameters<RichTextFunctionSerializer<string>>[0],\n\tnode: Parameters<RichTextFunctionSerializer<string>>[1],\n\ttext: Parameters<RichTextFunctionSerializer<string>>[2],\n\tchildren: Parameters<RichTextFunctionSerializer<string>>[3][number],\n\tkey: Parameters<RichTextFunctionSerializer<string>>[4],\n) => string | null | undefined;\n\n/**\n * Serializes a node from a rich text or title field with a map to HTML\n *\n * Unlike a typical `@prismicio/richtext` map serializer, this serializer\n * converts the `children` property to a single string rather than an array of\n * strings.\n *\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextMapSerializer = {\n\t[P in keyof RichTextMapSerializer<string>]: (payload: {\n\t\ttype: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"type\"];\n\t\tnode: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"node\"];\n\t\ttext: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"text\"];\n\t\tchildren: Parameters<\n\t\t\tHTMLRichTextMapSerializerFunction<P>\n\t\t>[0][\"children\"][number];\n\t\tkey: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"key\"];\n\t}) => string | null | undefined;\n};\n\n/**\n * A {@link RichTextMapSerializerFunction} type specifically for\n * {@link HTMLRichTextMapSerializer}.\n *\n * @typeParam BlockName - The serializer's rich text block type.\n */\ntype HTMLRichTextMapSerializerFunction<\n\tBlockType extends keyof RichTextMapSerializer<string>,\n> = RichTextMapSerializerFunction<\n\tstring,\n\tExtractNodeGeneric<RichTextMapSerializer<string>[BlockType]>,\n\tExtractTextTypeGeneric<RichTextMapSerializer<string>[BlockType]>\n>;\n\n/**\n * Returns the `Node` generic from {@link RichTextMapSerializerFunction}.\n *\n * @typeParam T - The `RichTextMapSerializerFunction` containing the needed\n * `Node` generic.\n */\ntype ExtractNodeGeneric<T> = T extends RichTextMapSerializerFunction<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tinfer U,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany\n>\n\t? U\n\t: never;\n\n/**\n * Returns the `TextType` generic from {@link RichTextMapSerializerFunction}.\n *\n * @typeParam T - The `RichTextMapSerializerFunction` containing the needed\n * `TextType` generic.\n */\ntype ExtractTextTypeGeneric<T> = T extends RichTextMapSerializerFunction<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tinfer U\n>\n\t? U\n\t: never;\n\n/**\n * Serializes a node from a rich text or title field with a map or a function to\n * HTML\n *\n * @see {@link HTMLRichTextMapSerializer} and {@link HTMLRichTextFunctionSerializer}\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextSerializer =\n\t| HTMLRichTextMapSerializer\n\t| HTMLRichTextFunctionSerializer;\n\n/**\n * Creates a default HTML rich text serializer with a given link resolver\n * providing sensible and safe defaults for every node type\n *\n * @internal\n */\nconst createDefaultHTMLRichTextSerializer = (\n\tlinkResolver: LinkResolverFunction | undefined | null,\n): RichTextFunctionSerializer<string> => {\n\treturn (_type, node, text, children, _key) => {\n\t\tswitch (node.type) {\n\t\t\tcase Element.heading1:\n\t\t\t\treturn serializeStandardTag(\"h1\", node, children);\n\t\t\tcase Element.heading2:\n\t\t\t\treturn serializeStandardTag(\"h2\", node, children);\n\t\t\tcase Element.heading3:\n\t\t\t\treturn serializeStandardTag(\"h3\", node, children);\n\t\t\tcase Element.heading4:\n\t\t\t\treturn serializeStandardTag(\"h4\", node, children);\n\t\t\tcase Element.heading5:\n\t\t\t\treturn serializeStandardTag(\"h5\", node, children);\n\t\t\tcase Element.heading6:\n\t\t\t\treturn serializeStandardTag(\"h6\", node, children);\n\t\t\tcase Element.paragraph:\n\t\t\t\treturn serializeStandardTag(\"p\", node, children);\n\t\t\tcase Element.preformatted:\n\t\t\t\treturn serializePreFormatted(node);\n\t\t\tcase Element.strong:\n\t\t\t\treturn serializeStandardTag(\"strong\", node, children);\n\t\t\tcase Element.em:\n\t\t\t\treturn serializeStandardTag(\"em\", node, children);\n\t\t\tcase Element.listItem:\n\t\t\t\treturn serializeStandardTag(\"li\", node, children);\n\t\t\tcase Element.oListItem:\n\t\t\t\treturn serializeStandardTag(\"li\", node, children);\n\t\t\tcase Element.list:\n\t\t\t\treturn serializeStandardTag(\"ul\", node, children);\n\t\t\tcase Element.oList:\n\t\t\t\treturn serializeStandardTag(\"ol\", node, children);\n\t\t\tcase Element.image:\n\t\t\t\treturn serializeImage(linkResolver, node);\n\t\t\tcase Element.embed:\n\t\t\t\treturn serializeEmbed(node);\n\t\t\tcase Element.hyperlink:\n\t\t\t\treturn serializeHyperlink(linkResolver, node, children);\n\t\t\tcase Element.label:\n\t\t\t\treturn serializeStandardTag(\"span\", node, children);\n\t\t\tcase Element.span:\n\t\t\tdefault:\n\t\t\t\treturn serializeSpan(text);\n\t\t}\n\t};\n};\n\n/**\n * Wraps a map serializer into a regular function serializer. The given map\n * serializer should accept children as a string, not as an array of strings\n * like `@prismicio/richtext`'s `wrapMapSerializer`.\n *\n * @param mapSerializer - Map serializer to wrap\n *\n * @returns A regular function serializer\n */\nconst wrapMapSerializerWithStringChildren = (\n\tmapSerializer: HTMLRichTextMapSerializer,\n): RichTextFunctionSerializer<string> => {\n\tconst modifiedMapSerializer = {} as RichTextMapSerializer<string>;\n\n\tfor (const tag in mapSerializer) {\n\t\tconst tagSerializer = mapSerializer[tag as keyof typeof mapSerializer];\n\n\t\tif (tagSerializer) {\n\t\t\tmodifiedMapSerializer[tag as keyof typeof mapSerializer] = (payload) => {\n\t\t\t\treturn tagSerializer({\n\t\t\t\t\t...payload,\n\t\t\t\t\t// @ts-expect-error - merging blockSerializer types causes TS to bail to a never type\n\t\t\t\t\tchildren: payload.children.join(\"\"),\n\t\t\t\t});\n\t\t\t};\n\t\t}\n\t}\n\n\treturn wrapMapSerializer(modifiedMapSerializer);\n};\n\n/**\n * Configuration that determines the output of `asHTML()`.\n */\ntype AsHTMLConfig = {\n\t/**\n\t * An optional link resolver function to resolve links. Without it you're\n\t * expected to use the `routes` options from the API.\n\t */\n\tlinkResolver?: LinkResolverFunction | null;\n\n\t/**\n\t * An optional rich text serializer, unhandled cases will fallback to the\n\t * default serializer\n\t */\n\tserializer?: HTMLRichTextSerializer | null;\n};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsHTMLDeprecatedTupleConfig = [\n\tlinkResolver?: LinkResolverFunction | null,\n\tserializer?: HTMLRichTextSerializer | null,\n];\n\n/**\n * The return type of `asHTML()`.\n */\ntype AsHTMLReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\n// TODO: Remove overload when we remove support for deprecated tuple-style configuration.\nexport const asHTML: {\n\t/**\n\t * Serializes a rich text or title field to an HTML string.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * asHTML(field);\n\t * asHTML(field, { linkResolver });\n\t * asHTML(field, { serializer });\n\t * asHTML(field, { linkResolver, serializer });\n\t * ```\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param config - Configuration that determines the output of `asHTML()`\n\t *\n\t * @returns HTML equivalent of the provided rich text or title field\n\t *\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\tconfig?: AsHTMLConfig,\n\t): AsHTMLReturnType<Field>;\n\n\t/**\n\t * Serializes a rich text or title field to an HTML string.\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param linkResolver - An optional link resolver function to resolve links,\n\t * without it you're expected to use the `routes` options from the API\n\t * @param serializer - An optional rich text serializer, unhandled cases will\n\t * fallback to the default serializer\n\t *\n\t * @returns HTML equivalent of the provided rich text or title field\n\t *\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\t...config: AsHTMLDeprecatedTupleConfig\n\t): AsHTMLReturnType<Field>;\n} = <Field extends RichTextField | null | undefined>(\n\trichTextField: Field,\n\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple: [config?: AsHTMLConfig] | AsHTMLDeprecatedTupleConfig\n): AsHTMLReturnType<Field> => {\n\tif (richTextField) {\n\t\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\t\tconst [configObjectOrLinkResolver, maybeSerializer] = configObjectOrTuple;\n\t\tlet config: AsHTMLConfig;\n\t\tif (\n\t\t\ttypeof configObjectOrLinkResolver === \"function\" ||\n\t\t\tconfigObjectOrLinkResolver == null\n\t\t) {\n\t\t\tconfig = {\n\t\t\t\tlinkResolver: configObjectOrLinkResolver,\n\t\t\t\tserializer: maybeSerializer,\n\t\t\t};\n\t\t} else {\n\t\t\tconfig = { ...configObjectOrLinkResolver };\n\t\t}\n\n\t\tlet serializer: RichTextFunctionSerializer<string>;\n\t\tif (config.serializer) {\n\t\t\tserializer = composeSerializers(\n\t\t\t\ttypeof config.serializer === \"object\"\n\t\t\t\t\t? wrapMapSerializerWithStringChildren(config.serializer)\n\t\t\t\t\t: (type, node, text, children, key) =>\n\t\t\t\t\t\t\t// TypeScript doesn't narrow the type correctly here since it is now in a callback function, so we have to cast it here.\n\t\t\t\t\t\t\t(config.serializer as HTMLRichTextFunctionSerializer)(\n\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\tnode,\n\t\t\t\t\t\t\t\ttext,\n\t\t\t\t\t\t\t\tchildren.join(\"\"),\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t),\n\t\t\t\tcreateDefaultHTMLRichTextSerializer(config.linkResolver),\n\t\t\t);\n\t\t} else {\n\t\t\tserializer = createDefaultHTMLRichTextSerializer(config.linkResolver);\n\t\t}\n\n\t\treturn serialize(richTextField, serializer).join(\n\t\t\t\"\",\n\t\t) as AsHTMLReturnType<Field>;\n\t} else {\n\t\treturn null as AsHTMLReturnType<Field>;\n\t}\n};\n"],"names":["Element","serializeStandardTag","serializePreFormatted","serializeImage","serializeEmbed","serializeHyperlink","serializeSpan","wrapMapSerializer","composeSerializers","serialize"],"mappings":";;;;AA4HA,MAAM,sCAAsC,CAC3C,iBACuC;AACvC,SAAO,CAAC,OAAO,MAAM,MAAM,UAAU,SAAQ;AAC5C,YAAQ,KAAK,MAAM;AAAA,MAClB,KAAKA,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,KAAK,MAAM,QAAQ;AAAA,MAChD,KAAKD,SAAQ,QAAA;AACZ,eAAOE,kBAAAA,sBAAsB,IAAI;AAAA,MAClC,KAAKF,SAAQ,QAAA;AACL,eAAAC,uCAAqB,UAAU,MAAM,QAAQ;AAAA,MACrD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAC,uCAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAKD,SAAQ,QAAA;AACL,eAAAG,kBAAA,eAAe,cAAc,IAAI;AAAA,MACzC,KAAKH,SAAQ,QAAA;AACZ,eAAOI,kBAAAA,eAAe,IAAI;AAAA,MAC3B,KAAKJ,SAAQ,QAAA;AACL,eAAAK,qCAAmB,cAAc,MAAM,QAAQ;AAAA,MACvD,KAAKL,SAAQ,QAAA;AACL,eAAAC,uCAAqB,QAAQ,MAAM,QAAQ;AAAA,MACnD,KAAKD,SAAAA,QAAQ;AAAA,MACb;AACC,eAAOM,kBAAAA,cAAc,IAAI;AAAA,IAC1B;AAAA,EAAA;AAEH;AAWA,MAAM,sCAAsC,CAC3C,kBACuC;AACvC,QAAM,wBAAwB,CAAA;AAE9B,aAAW,OAAO,eAAe;AAC1B,UAAA,gBAAgB,cAAc,GAAiC;AAErE,QAAI,eAAe;AACI,4BAAA,GAAiC,IAAI,CAAC,YAAW;AACtE,eAAO,cAAc;AAAA,UACpB,GAAG;AAAA;AAAA,UAEH,UAAU,QAAQ,SAAS,KAAK,EAAE;AAAA,QAAA,CAClC;AAAA,MAAA;AAAA,IAEF;AAAA,EACD;AAED,SAAOC,SAAAA,kBAAkB,qBAAqB;AAC/C;AAmCa,MAAA,SA4CT,CACH,kBAEG,wBACyB;AAC5B,MAAI,eAAe;AAEZ,UAAA,CAAC,4BAA4B,eAAe,IAAI;AAClD,QAAA;AACJ,QACC,OAAO,+BAA+B,cACtC,8BAA8B,MAC7B;AACQ,eAAA;AAAA,QACR,cAAc;AAAA,QACd,YAAY;AAAA,MAAA;AAAA,WAEP;AACG,eAAA,EAAE,GAAG;IACd;AAEG,QAAA;AACJ,QAAI,OAAO,YAAY;AACtB,mBAAaC,SAAAA,mBACZ,OAAO,OAAO,eAAe,WAC1B,oCAAoC,OAAO,UAAU,IACrD,CAAC,MAAM,MAAM,MAAM,UAAU;AAAA;AAAA,QAE5B,OAAO,WACP,MACA,MACA,MACA,SAAS,KAAK,EAAE,GAChB,GAAG;AAAA,SAEP,oCAAoC,OAAO,YAAY,CAAC;AAAA,IAAA,OAEnD;AACO,mBAAA,oCAAoC,OAAO,YAAY;AAAA,IACpE;AAED,WAAOC,SAAAA,UAAU,eAAe,UAAU,EAAE,KAC3C,EAAE;AAAA,EAAA,OAEG;AACC,WAAA;AAAA,EACP;AACF;;"}
|
package/dist/helpers/asHTML.d.ts
CHANGED
|
@@ -51,7 +51,8 @@ type ExtractNodeGeneric<T> = T extends RichTextMapSerializerFunction<any, infer
|
|
|
51
51
|
*/
|
|
52
52
|
type ExtractTextTypeGeneric<T> = T extends RichTextMapSerializerFunction<any, any, infer U> ? U : never;
|
|
53
53
|
/**
|
|
54
|
-
* Serializes a node from a rich text or title field with a map or a function to
|
|
54
|
+
* Serializes a node from a rich text or title field with a map or a function to
|
|
55
|
+
* HTML
|
|
55
56
|
*
|
|
56
57
|
* @see {@link HTMLRichTextMapSerializer} and {@link HTMLRichTextFunctionSerializer}
|
|
57
58
|
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
@@ -62,12 +63,13 @@ export type HTMLRichTextSerializer = HTMLRichTextMapSerializer | HTMLRichTextFun
|
|
|
62
63
|
*/
|
|
63
64
|
type AsHTMLConfig = {
|
|
64
65
|
/**
|
|
65
|
-
* An optional link resolver function to resolve links.
|
|
66
|
-
*
|
|
66
|
+
* An optional link resolver function to resolve links. Without it you're
|
|
67
|
+
* expected to use the `routes` options from the API.
|
|
67
68
|
*/
|
|
68
69
|
linkResolver?: LinkResolverFunction | null;
|
|
69
70
|
/**
|
|
70
|
-
* An optional rich text serializer, unhandled cases will fallback to the
|
|
71
|
+
* An optional rich text serializer, unhandled cases will fallback to the
|
|
72
|
+
* default serializer
|
|
71
73
|
*/
|
|
72
74
|
serializer?: HTMLRichTextSerializer | null;
|
|
73
75
|
};
|
|
@@ -86,33 +88,37 @@ export declare const asHTML: {
|
|
|
86
88
|
/**
|
|
87
89
|
* Serializes a rich text or title field to an HTML string.
|
|
88
90
|
*
|
|
91
|
+
* @example
|
|
92
|
+
*
|
|
93
|
+
* ```ts
|
|
94
|
+
* asHTML(field);
|
|
95
|
+
* asHTML(field, { linkResolver });
|
|
96
|
+
* asHTML(field, { serializer });
|
|
97
|
+
* asHTML(field, { linkResolver, serializer });
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
89
100
|
* @param richTextField - A rich text or title field from Prismic
|
|
90
101
|
* @param config - Configuration that determines the output of `asHTML()`
|
|
91
102
|
*
|
|
92
103
|
* @returns HTML equivalent of the provided rich text or title field
|
|
104
|
+
*
|
|
93
105
|
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
94
106
|
*/
|
|
95
107
|
<Field extends RichTextField | null | undefined>(richTextField: Field, config?: AsHTMLConfig): AsHTMLReturnType<Field>;
|
|
96
108
|
/**
|
|
97
109
|
* Serializes a rich text or title field to an HTML string.
|
|
98
110
|
*
|
|
111
|
+
* @deprecated Use object-style configuration instead.
|
|
112
|
+
*
|
|
99
113
|
* @param richTextField - A rich text or title field from Prismic
|
|
100
114
|
* @param linkResolver - An optional link resolver function to resolve links,
|
|
101
115
|
* without it you're expected to use the `routes` options from the API
|
|
102
|
-
* @param serializer - An optional rich text serializer, unhandled cases will
|
|
103
|
-
* to the default serializer
|
|
116
|
+
* @param serializer - An optional rich text serializer, unhandled cases will
|
|
117
|
+
* fallback to the default serializer
|
|
104
118
|
*
|
|
105
119
|
* @returns HTML equivalent of the provided rich text or title field
|
|
106
|
-
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
107
|
-
*
|
|
108
|
-
* @deprecated Use object-style configuration instead.
|
|
109
120
|
*
|
|
110
|
-
*
|
|
111
|
-
* asHTML(field);
|
|
112
|
-
* asHTML(field, { linkResolver });
|
|
113
|
-
* asHTML(field, { serializer });
|
|
114
|
-
* asHTML(field, { linkResolver, serializer });
|
|
115
|
-
* ```
|
|
121
|
+
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
116
122
|
*/
|
|
117
123
|
<Field extends RichTextField | null | undefined>(richTextField: Field, ...config: AsHTMLDeprecatedTupleConfig): AsHTMLReturnType<Field>;
|
|
118
124
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asHTML.js","sources":["../../../src/helpers/asHTML.ts"],"sourcesContent":["import {\n\tElement,\n\tRichTextFunctionSerializer,\n\tRichTextMapSerializer,\n\tRichTextMapSerializerFunction,\n\tcomposeSerializers,\n\tserialize,\n\twrapMapSerializer,\n} from \"@prismicio/richtext\";\n\nimport {\n\tserializeEmbed,\n\tserializeHyperlink,\n\tserializeImage,\n\tserializePreFormatted,\n\tserializeSpan,\n\tserializeStandardTag,\n} from \"../lib/serializerHelpers\";\n\nimport type { RichTextField } from \"../types/value/richText\";\n\nimport { LinkResolverFunction } from \"./asLink\";\n\n/**\n * Serializes a node from a rich text or title field with a function to HTML.\n *\n * Unlike a typical `@prismicio/richtext` function serializer, this serializer\n * converts the `children` argument to a single string rather than an array of\n * strings.\n *\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextFunctionSerializer = (\n\ttype: Parameters<RichTextFunctionSerializer<string>>[0],\n\tnode: Parameters<RichTextFunctionSerializer<string>>[1],\n\ttext: Parameters<RichTextFunctionSerializer<string>>[2],\n\tchildren: Parameters<RichTextFunctionSerializer<string>>[3][number],\n\tkey: Parameters<RichTextFunctionSerializer<string>>[4],\n) => string | null | undefined;\n\n/**\n * Serializes a node from a rich text or title field with a map to HTML\n *\n * Unlike a typical `@prismicio/richtext` map serializer, this serializer\n * converts the `children` property to a single string rather than an array of\n * strings.\n *\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextMapSerializer = {\n\t[P in keyof RichTextMapSerializer<string>]: (payload: {\n\t\ttype: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"type\"];\n\t\tnode: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"node\"];\n\t\ttext: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"text\"];\n\t\tchildren: Parameters<\n\t\t\tHTMLRichTextMapSerializerFunction<P>\n\t\t>[0][\"children\"][number];\n\t\tkey: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"key\"];\n\t}) => string | null | undefined;\n};\n\n/**\n * A {@link RichTextMapSerializerFunction} type specifically for\n * {@link HTMLRichTextMapSerializer}.\n *\n * @typeParam BlockName - The serializer's rich text block type.\n */\ntype HTMLRichTextMapSerializerFunction<\n\tBlockType extends keyof RichTextMapSerializer<string>,\n> = RichTextMapSerializerFunction<\n\tstring,\n\tExtractNodeGeneric<RichTextMapSerializer<string>[BlockType]>,\n\tExtractTextTypeGeneric<RichTextMapSerializer<string>[BlockType]>\n>;\n\n/**\n * Returns the `Node` generic from {@link RichTextMapSerializerFunction}.\n *\n * @typeParam T - The `RichTextMapSerializerFunction` containing the needed\n * `Node` generic.\n */\ntype ExtractNodeGeneric<T> = T extends RichTextMapSerializerFunction<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tinfer U,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany\n>\n\t? U\n\t: never;\n\n/**\n * Returns the `TextType` generic from {@link RichTextMapSerializerFunction}.\n *\n * @typeParam T - The `RichTextMapSerializerFunction` containing the needed\n * `TextType` generic.\n */\ntype ExtractTextTypeGeneric<T> = T extends RichTextMapSerializerFunction<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tinfer U\n>\n\t? U\n\t: never;\n\n/**\n * Serializes a node from a rich text or title field with a map or a function to HTML\n *\n * @see {@link HTMLRichTextMapSerializer} and {@link HTMLRichTextFunctionSerializer}\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextSerializer =\n\t| HTMLRichTextMapSerializer\n\t| HTMLRichTextFunctionSerializer;\n\n/**\n * Creates a default HTML rich text serializer with a given link resolver providing\n * sensible and safe defaults for every node type\n *\n * @internal\n */\nconst createDefaultHTMLRichTextSerializer = (\n\tlinkResolver: LinkResolverFunction | undefined | null,\n): RichTextFunctionSerializer<string> => {\n\treturn (_type, node, text, children, _key) => {\n\t\tswitch (node.type) {\n\t\t\tcase Element.heading1:\n\t\t\t\treturn serializeStandardTag(\"h1\", node, children);\n\t\t\tcase Element.heading2:\n\t\t\t\treturn serializeStandardTag(\"h2\", node, children);\n\t\t\tcase Element.heading3:\n\t\t\t\treturn serializeStandardTag(\"h3\", node, children);\n\t\t\tcase Element.heading4:\n\t\t\t\treturn serializeStandardTag(\"h4\", node, children);\n\t\t\tcase Element.heading5:\n\t\t\t\treturn serializeStandardTag(\"h5\", node, children);\n\t\t\tcase Element.heading6:\n\t\t\t\treturn serializeStandardTag(\"h6\", node, children);\n\t\t\tcase Element.paragraph:\n\t\t\t\treturn serializeStandardTag(\"p\", node, children);\n\t\t\tcase Element.preformatted:\n\t\t\t\treturn serializePreFormatted(node);\n\t\t\tcase Element.strong:\n\t\t\t\treturn serializeStandardTag(\"strong\", node, children);\n\t\t\tcase Element.em:\n\t\t\t\treturn serializeStandardTag(\"em\", node, children);\n\t\t\tcase Element.listItem:\n\t\t\t\treturn serializeStandardTag(\"li\", node, children);\n\t\t\tcase Element.oListItem:\n\t\t\t\treturn serializeStandardTag(\"li\", node, children);\n\t\t\tcase Element.list:\n\t\t\t\treturn serializeStandardTag(\"ul\", node, children);\n\t\t\tcase Element.oList:\n\t\t\t\treturn serializeStandardTag(\"ol\", node, children);\n\t\t\tcase Element.image:\n\t\t\t\treturn serializeImage(linkResolver, node);\n\t\t\tcase Element.embed:\n\t\t\t\treturn serializeEmbed(node);\n\t\t\tcase Element.hyperlink:\n\t\t\t\treturn serializeHyperlink(linkResolver, node, children);\n\t\t\tcase Element.label:\n\t\t\t\treturn serializeStandardTag(\"span\", node, children);\n\t\t\tcase Element.span:\n\t\t\tdefault:\n\t\t\t\treturn serializeSpan(text);\n\t\t}\n\t};\n};\n\n/**\n * Wraps a map serializer into a regular function serializer. The given map\n * serializer should accept children as a string, not as an array of strings\n * like `@prismicio/richtext`'s `wrapMapSerializer`.\n *\n * @param mapSerializer - Map serializer to wrap\n *\n * @returns A regular function serializer\n */\nconst wrapMapSerializerWithStringChildren = (\n\tmapSerializer: HTMLRichTextMapSerializer,\n): RichTextFunctionSerializer<string> => {\n\tconst modifiedMapSerializer = {} as RichTextMapSerializer<string>;\n\n\tfor (const tag in mapSerializer) {\n\t\tconst tagSerializer = mapSerializer[tag as keyof typeof mapSerializer];\n\n\t\tif (tagSerializer) {\n\t\t\tmodifiedMapSerializer[tag as keyof typeof mapSerializer] = (payload) => {\n\t\t\t\treturn tagSerializer({\n\t\t\t\t\t...payload,\n\t\t\t\t\t// @ts-expect-error - merging blockSerializer types causes TS to bail to a never type\n\t\t\t\t\tchildren: payload.children.join(\"\"),\n\t\t\t\t});\n\t\t\t};\n\t\t}\n\t}\n\n\treturn wrapMapSerializer(modifiedMapSerializer);\n};\n\n/**\n * Configuration that determines the output of `asHTML()`.\n */\ntype AsHTMLConfig = {\n\t/**\n\t * An optional link resolver function to resolve links.\n\t * Without it you're expected to use the `routes` options from the API.\n\t */\n\tlinkResolver?: LinkResolverFunction | null;\n\n\t/**\n\t * An optional rich text serializer, unhandled cases will fallback to the default serializer\n\t */\n\tserializer?: HTMLRichTextSerializer | null;\n};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsHTMLDeprecatedTupleConfig = [\n\tlinkResolver?: LinkResolverFunction | null,\n\tserializer?: HTMLRichTextSerializer | null,\n];\n\n/**\n * The return type of `asHTML()`.\n */\ntype AsHTMLReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\n// TODO: Remove overload when we remove support for deprecated tuple-style configuration.\nexport const asHTML: {\n\t/**\n\t * Serializes a rich text or title field to an HTML string.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param config - Configuration that determines the output of `asHTML()`\n\t *\n\t * @returns HTML equivalent of the provided rich text or title field\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\tconfig?: AsHTMLConfig,\n\t): AsHTMLReturnType<Field>;\n\n\t/**\n\t * Serializes a rich text or title field to an HTML string.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param linkResolver - An optional link resolver function to resolve links,\n\t * without it you're expected to use the `routes` options from the API\n\t * @param serializer - An optional rich text serializer, unhandled cases will fallback\n\t * to the default serializer\n\t *\n\t * @returns HTML equivalent of the provided rich text or title field\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * ```ts\n\t * asHTML(field);\n\t * asHTML(field, { linkResolver });\n\t * asHTML(field, { serializer });\n\t * asHTML(field, { linkResolver, serializer });\n\t * ```\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\t...config: AsHTMLDeprecatedTupleConfig\n\t): AsHTMLReturnType<Field>;\n} = <Field extends RichTextField | null | undefined>(\n\trichTextField: Field,\n\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple: [config?: AsHTMLConfig] | AsHTMLDeprecatedTupleConfig\n): AsHTMLReturnType<Field> => {\n\tif (richTextField) {\n\t\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\t\tconst [configObjectOrLinkResolver, maybeSerializer] = configObjectOrTuple;\n\t\tlet config: AsHTMLConfig;\n\t\tif (\n\t\t\ttypeof configObjectOrLinkResolver === \"function\" ||\n\t\t\tconfigObjectOrLinkResolver == null\n\t\t) {\n\t\t\tconfig = {\n\t\t\t\tlinkResolver: configObjectOrLinkResolver,\n\t\t\t\tserializer: maybeSerializer,\n\t\t\t};\n\t\t} else {\n\t\t\tconfig = { ...configObjectOrLinkResolver };\n\t\t}\n\n\t\tlet serializer: RichTextFunctionSerializer<string>;\n\t\tif (config.serializer) {\n\t\t\tserializer = composeSerializers(\n\t\t\t\ttypeof config.serializer === \"object\"\n\t\t\t\t\t? wrapMapSerializerWithStringChildren(config.serializer)\n\t\t\t\t\t: (type, node, text, children, key) =>\n\t\t\t\t\t\t\t// TypeScript doesn't narrow the type correctly here since it is now in a callback function, so we have to cast it here.\n\t\t\t\t\t\t\t(config.serializer as HTMLRichTextFunctionSerializer)(\n\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\tnode,\n\t\t\t\t\t\t\t\ttext,\n\t\t\t\t\t\t\t\tchildren.join(\"\"),\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t),\n\t\t\t\tcreateDefaultHTMLRichTextSerializer(config.linkResolver),\n\t\t\t);\n\t\t} else {\n\t\t\tserializer = createDefaultHTMLRichTextSerializer(config.linkResolver);\n\t\t}\n\n\t\treturn serialize(richTextField, serializer).join(\n\t\t\t\"\",\n\t\t) as AsHTMLReturnType<Field>;\n\t} else {\n\t\treturn null as AsHTMLReturnType<Field>;\n\t}\n};\n"],"names":[],"mappings":";;AA2HA,MAAM,sCAAsC,CAC3C,iBACuC;AACvC,SAAO,CAAC,OAAO,MAAM,MAAM,UAAU,SAAQ;AAC5C,YAAQ,KAAK,MAAM;AAAA,MAClB,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,KAAK,MAAM,QAAQ;AAAA,MAChD,KAAK,QAAQ;AACZ,eAAO,sBAAsB,IAAI;AAAA,MAClC,KAAK,QAAQ;AACL,eAAA,qBAAqB,UAAU,MAAM,QAAQ;AAAA,MACrD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,eAAe,cAAc,IAAI;AAAA,MACzC,KAAK,QAAQ;AACZ,eAAO,eAAe,IAAI;AAAA,MAC3B,KAAK,QAAQ;AACL,eAAA,mBAAmB,cAAc,MAAM,QAAQ;AAAA,MACvD,KAAK,QAAQ;AACL,eAAA,qBAAqB,QAAQ,MAAM,QAAQ;AAAA,MACnD,KAAK,QAAQ;AAAA,MACb;AACC,eAAO,cAAc,IAAI;AAAA,IAC1B;AAAA,EAAA;AAEH;AAWA,MAAM,sCAAsC,CAC3C,kBACuC;AACvC,QAAM,wBAAwB,CAAA;AAE9B,aAAW,OAAO,eAAe;AAC1B,UAAA,gBAAgB,cAAc,GAAiC;AAErE,QAAI,eAAe;AACI,4BAAA,GAAiC,IAAI,CAAC,YAAW;AACtE,eAAO,cAAc;AAAA,UACpB,GAAG;AAAA;AAAA,UAEH,UAAU,QAAQ,SAAS,KAAK,EAAE;AAAA,QAAA,CAClC;AAAA,MAAA;AAAA,IAEF;AAAA,EACD;AAED,SAAO,kBAAkB,qBAAqB;AAC/C;AAkCa,MAAA,SAwCT,CACH,kBAEG,wBACyB;AAC5B,MAAI,eAAe;AAEZ,UAAA,CAAC,4BAA4B,eAAe,IAAI;AAClD,QAAA;AACJ,QACC,OAAO,+BAA+B,cACtC,8BAA8B,MAC7B;AACQ,eAAA;AAAA,QACR,cAAc;AAAA,QACd,YAAY;AAAA,MAAA;AAAA,WAEP;AACG,eAAA,EAAE,GAAG;IACd;AAEG,QAAA;AACJ,QAAI,OAAO,YAAY;AACtB,mBAAa,mBACZ,OAAO,OAAO,eAAe,WAC1B,oCAAoC,OAAO,UAAU,IACrD,CAAC,MAAM,MAAM,MAAM,UAAU;AAAA;AAAA,QAE5B,OAAO,WACP,MACA,MACA,MACA,SAAS,KAAK,EAAE,GAChB,GAAG;AAAA,SAEP,oCAAoC,OAAO,YAAY,CAAC;AAAA,IAAA,OAEnD;AACO,mBAAA,oCAAoC,OAAO,YAAY;AAAA,IACpE;AAED,WAAO,UAAU,eAAe,UAAU,EAAE,KAC3C,EAAE;AAAA,EAAA,OAEG;AACC,WAAA;AAAA,EACP;AACF;"}
|
|
1
|
+
{"version":3,"file":"asHTML.js","sources":["../../../src/helpers/asHTML.ts"],"sourcesContent":["import {\n\tElement,\n\tRichTextFunctionSerializer,\n\tRichTextMapSerializer,\n\tRichTextMapSerializerFunction,\n\tcomposeSerializers,\n\tserialize,\n\twrapMapSerializer,\n} from \"@prismicio/richtext\";\n\nimport {\n\tserializeEmbed,\n\tserializeHyperlink,\n\tserializeImage,\n\tserializePreFormatted,\n\tserializeSpan,\n\tserializeStandardTag,\n} from \"../lib/serializerHelpers\";\n\nimport type { RichTextField } from \"../types/value/richText\";\n\nimport { LinkResolverFunction } from \"./asLink\";\n\n/**\n * Serializes a node from a rich text or title field with a function to HTML.\n *\n * Unlike a typical `@prismicio/richtext` function serializer, this serializer\n * converts the `children` argument to a single string rather than an array of\n * strings.\n *\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextFunctionSerializer = (\n\ttype: Parameters<RichTextFunctionSerializer<string>>[0],\n\tnode: Parameters<RichTextFunctionSerializer<string>>[1],\n\ttext: Parameters<RichTextFunctionSerializer<string>>[2],\n\tchildren: Parameters<RichTextFunctionSerializer<string>>[3][number],\n\tkey: Parameters<RichTextFunctionSerializer<string>>[4],\n) => string | null | undefined;\n\n/**\n * Serializes a node from a rich text or title field with a map to HTML\n *\n * Unlike a typical `@prismicio/richtext` map serializer, this serializer\n * converts the `children` property to a single string rather than an array of\n * strings.\n *\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextMapSerializer = {\n\t[P in keyof RichTextMapSerializer<string>]: (payload: {\n\t\ttype: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"type\"];\n\t\tnode: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"node\"];\n\t\ttext: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"text\"];\n\t\tchildren: Parameters<\n\t\t\tHTMLRichTextMapSerializerFunction<P>\n\t\t>[0][\"children\"][number];\n\t\tkey: Parameters<HTMLRichTextMapSerializerFunction<P>>[0][\"key\"];\n\t}) => string | null | undefined;\n};\n\n/**\n * A {@link RichTextMapSerializerFunction} type specifically for\n * {@link HTMLRichTextMapSerializer}.\n *\n * @typeParam BlockName - The serializer's rich text block type.\n */\ntype HTMLRichTextMapSerializerFunction<\n\tBlockType extends keyof RichTextMapSerializer<string>,\n> = RichTextMapSerializerFunction<\n\tstring,\n\tExtractNodeGeneric<RichTextMapSerializer<string>[BlockType]>,\n\tExtractTextTypeGeneric<RichTextMapSerializer<string>[BlockType]>\n>;\n\n/**\n * Returns the `Node` generic from {@link RichTextMapSerializerFunction}.\n *\n * @typeParam T - The `RichTextMapSerializerFunction` containing the needed\n * `Node` generic.\n */\ntype ExtractNodeGeneric<T> = T extends RichTextMapSerializerFunction<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tinfer U,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany\n>\n\t? U\n\t: never;\n\n/**\n * Returns the `TextType` generic from {@link RichTextMapSerializerFunction}.\n *\n * @typeParam T - The `RichTextMapSerializerFunction` containing the needed\n * `TextType` generic.\n */\ntype ExtractTextTypeGeneric<T> = T extends RichTextMapSerializerFunction<\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tany,\n\tinfer U\n>\n\t? U\n\t: never;\n\n/**\n * Serializes a node from a rich text or title field with a map or a function to\n * HTML\n *\n * @see {@link HTMLRichTextMapSerializer} and {@link HTMLRichTextFunctionSerializer}\n * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n */\nexport type HTMLRichTextSerializer =\n\t| HTMLRichTextMapSerializer\n\t| HTMLRichTextFunctionSerializer;\n\n/**\n * Creates a default HTML rich text serializer with a given link resolver\n * providing sensible and safe defaults for every node type\n *\n * @internal\n */\nconst createDefaultHTMLRichTextSerializer = (\n\tlinkResolver: LinkResolverFunction | undefined | null,\n): RichTextFunctionSerializer<string> => {\n\treturn (_type, node, text, children, _key) => {\n\t\tswitch (node.type) {\n\t\t\tcase Element.heading1:\n\t\t\t\treturn serializeStandardTag(\"h1\", node, children);\n\t\t\tcase Element.heading2:\n\t\t\t\treturn serializeStandardTag(\"h2\", node, children);\n\t\t\tcase Element.heading3:\n\t\t\t\treturn serializeStandardTag(\"h3\", node, children);\n\t\t\tcase Element.heading4:\n\t\t\t\treturn serializeStandardTag(\"h4\", node, children);\n\t\t\tcase Element.heading5:\n\t\t\t\treturn serializeStandardTag(\"h5\", node, children);\n\t\t\tcase Element.heading6:\n\t\t\t\treturn serializeStandardTag(\"h6\", node, children);\n\t\t\tcase Element.paragraph:\n\t\t\t\treturn serializeStandardTag(\"p\", node, children);\n\t\t\tcase Element.preformatted:\n\t\t\t\treturn serializePreFormatted(node);\n\t\t\tcase Element.strong:\n\t\t\t\treturn serializeStandardTag(\"strong\", node, children);\n\t\t\tcase Element.em:\n\t\t\t\treturn serializeStandardTag(\"em\", node, children);\n\t\t\tcase Element.listItem:\n\t\t\t\treturn serializeStandardTag(\"li\", node, children);\n\t\t\tcase Element.oListItem:\n\t\t\t\treturn serializeStandardTag(\"li\", node, children);\n\t\t\tcase Element.list:\n\t\t\t\treturn serializeStandardTag(\"ul\", node, children);\n\t\t\tcase Element.oList:\n\t\t\t\treturn serializeStandardTag(\"ol\", node, children);\n\t\t\tcase Element.image:\n\t\t\t\treturn serializeImage(linkResolver, node);\n\t\t\tcase Element.embed:\n\t\t\t\treturn serializeEmbed(node);\n\t\t\tcase Element.hyperlink:\n\t\t\t\treturn serializeHyperlink(linkResolver, node, children);\n\t\t\tcase Element.label:\n\t\t\t\treturn serializeStandardTag(\"span\", node, children);\n\t\t\tcase Element.span:\n\t\t\tdefault:\n\t\t\t\treturn serializeSpan(text);\n\t\t}\n\t};\n};\n\n/**\n * Wraps a map serializer into a regular function serializer. The given map\n * serializer should accept children as a string, not as an array of strings\n * like `@prismicio/richtext`'s `wrapMapSerializer`.\n *\n * @param mapSerializer - Map serializer to wrap\n *\n * @returns A regular function serializer\n */\nconst wrapMapSerializerWithStringChildren = (\n\tmapSerializer: HTMLRichTextMapSerializer,\n): RichTextFunctionSerializer<string> => {\n\tconst modifiedMapSerializer = {} as RichTextMapSerializer<string>;\n\n\tfor (const tag in mapSerializer) {\n\t\tconst tagSerializer = mapSerializer[tag as keyof typeof mapSerializer];\n\n\t\tif (tagSerializer) {\n\t\t\tmodifiedMapSerializer[tag as keyof typeof mapSerializer] = (payload) => {\n\t\t\t\treturn tagSerializer({\n\t\t\t\t\t...payload,\n\t\t\t\t\t// @ts-expect-error - merging blockSerializer types causes TS to bail to a never type\n\t\t\t\t\tchildren: payload.children.join(\"\"),\n\t\t\t\t});\n\t\t\t};\n\t\t}\n\t}\n\n\treturn wrapMapSerializer(modifiedMapSerializer);\n};\n\n/**\n * Configuration that determines the output of `asHTML()`.\n */\ntype AsHTMLConfig = {\n\t/**\n\t * An optional link resolver function to resolve links. Without it you're\n\t * expected to use the `routes` options from the API.\n\t */\n\tlinkResolver?: LinkResolverFunction | null;\n\n\t/**\n\t * An optional rich text serializer, unhandled cases will fallback to the\n\t * default serializer\n\t */\n\tserializer?: HTMLRichTextSerializer | null;\n};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsHTMLDeprecatedTupleConfig = [\n\tlinkResolver?: LinkResolverFunction | null,\n\tserializer?: HTMLRichTextSerializer | null,\n];\n\n/**\n * The return type of `asHTML()`.\n */\ntype AsHTMLReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\n// TODO: Remove overload when we remove support for deprecated tuple-style configuration.\nexport const asHTML: {\n\t/**\n\t * Serializes a rich text or title field to an HTML string.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * asHTML(field);\n\t * asHTML(field, { linkResolver });\n\t * asHTML(field, { serializer });\n\t * asHTML(field, { linkResolver, serializer });\n\t * ```\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param config - Configuration that determines the output of `asHTML()`\n\t *\n\t * @returns HTML equivalent of the provided rich text or title field\n\t *\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\tconfig?: AsHTMLConfig,\n\t): AsHTMLReturnType<Field>;\n\n\t/**\n\t * Serializes a rich text or title field to an HTML string.\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param linkResolver - An optional link resolver function to resolve links,\n\t * without it you're expected to use the `routes` options from the API\n\t * @param serializer - An optional rich text serializer, unhandled cases will\n\t * fallback to the default serializer\n\t *\n\t * @returns HTML equivalent of the provided rich text or title field\n\t *\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\t...config: AsHTMLDeprecatedTupleConfig\n\t): AsHTMLReturnType<Field>;\n} = <Field extends RichTextField | null | undefined>(\n\trichTextField: Field,\n\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple: [config?: AsHTMLConfig] | AsHTMLDeprecatedTupleConfig\n): AsHTMLReturnType<Field> => {\n\tif (richTextField) {\n\t\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\t\tconst [configObjectOrLinkResolver, maybeSerializer] = configObjectOrTuple;\n\t\tlet config: AsHTMLConfig;\n\t\tif (\n\t\t\ttypeof configObjectOrLinkResolver === \"function\" ||\n\t\t\tconfigObjectOrLinkResolver == null\n\t\t) {\n\t\t\tconfig = {\n\t\t\t\tlinkResolver: configObjectOrLinkResolver,\n\t\t\t\tserializer: maybeSerializer,\n\t\t\t};\n\t\t} else {\n\t\t\tconfig = { ...configObjectOrLinkResolver };\n\t\t}\n\n\t\tlet serializer: RichTextFunctionSerializer<string>;\n\t\tif (config.serializer) {\n\t\t\tserializer = composeSerializers(\n\t\t\t\ttypeof config.serializer === \"object\"\n\t\t\t\t\t? wrapMapSerializerWithStringChildren(config.serializer)\n\t\t\t\t\t: (type, node, text, children, key) =>\n\t\t\t\t\t\t\t// TypeScript doesn't narrow the type correctly here since it is now in a callback function, so we have to cast it here.\n\t\t\t\t\t\t\t(config.serializer as HTMLRichTextFunctionSerializer)(\n\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\tnode,\n\t\t\t\t\t\t\t\ttext,\n\t\t\t\t\t\t\t\tchildren.join(\"\"),\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t),\n\t\t\t\tcreateDefaultHTMLRichTextSerializer(config.linkResolver),\n\t\t\t);\n\t\t} else {\n\t\t\tserializer = createDefaultHTMLRichTextSerializer(config.linkResolver);\n\t\t}\n\n\t\treturn serialize(richTextField, serializer).join(\n\t\t\t\"\",\n\t\t) as AsHTMLReturnType<Field>;\n\t} else {\n\t\treturn null as AsHTMLReturnType<Field>;\n\t}\n};\n"],"names":[],"mappings":";;AA4HA,MAAM,sCAAsC,CAC3C,iBACuC;AACvC,SAAO,CAAC,OAAO,MAAM,MAAM,UAAU,SAAQ;AAC5C,YAAQ,KAAK,MAAM;AAAA,MAClB,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,KAAK,MAAM,QAAQ;AAAA,MAChD,KAAK,QAAQ;AACZ,eAAO,sBAAsB,IAAI;AAAA,MAClC,KAAK,QAAQ;AACL,eAAA,qBAAqB,UAAU,MAAM,QAAQ;AAAA,MACrD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,qBAAqB,MAAM,MAAM,QAAQ;AAAA,MACjD,KAAK,QAAQ;AACL,eAAA,eAAe,cAAc,IAAI;AAAA,MACzC,KAAK,QAAQ;AACZ,eAAO,eAAe,IAAI;AAAA,MAC3B,KAAK,QAAQ;AACL,eAAA,mBAAmB,cAAc,MAAM,QAAQ;AAAA,MACvD,KAAK,QAAQ;AACL,eAAA,qBAAqB,QAAQ,MAAM,QAAQ;AAAA,MACnD,KAAK,QAAQ;AAAA,MACb;AACC,eAAO,cAAc,IAAI;AAAA,IAC1B;AAAA,EAAA;AAEH;AAWA,MAAM,sCAAsC,CAC3C,kBACuC;AACvC,QAAM,wBAAwB,CAAA;AAE9B,aAAW,OAAO,eAAe;AAC1B,UAAA,gBAAgB,cAAc,GAAiC;AAErE,QAAI,eAAe;AACI,4BAAA,GAAiC,IAAI,CAAC,YAAW;AACtE,eAAO,cAAc;AAAA,UACpB,GAAG;AAAA;AAAA,UAEH,UAAU,QAAQ,SAAS,KAAK,EAAE;AAAA,QAAA,CAClC;AAAA,MAAA;AAAA,IAEF;AAAA,EACD;AAED,SAAO,kBAAkB,qBAAqB;AAC/C;AAmCa,MAAA,SA4CT,CACH,kBAEG,wBACyB;AAC5B,MAAI,eAAe;AAEZ,UAAA,CAAC,4BAA4B,eAAe,IAAI;AAClD,QAAA;AACJ,QACC,OAAO,+BAA+B,cACtC,8BAA8B,MAC7B;AACQ,eAAA;AAAA,QACR,cAAc;AAAA,QACd,YAAY;AAAA,MAAA;AAAA,WAEP;AACG,eAAA,EAAE,GAAG;IACd;AAEG,QAAA;AACJ,QAAI,OAAO,YAAY;AACtB,mBAAa,mBACZ,OAAO,OAAO,eAAe,WAC1B,oCAAoC,OAAO,UAAU,IACrD,CAAC,MAAM,MAAM,MAAM,UAAU;AAAA;AAAA,QAE5B,OAAO,WACP,MACA,MACA,MACA,SAAS,KAAK,EAAE,GAChB,GAAG;AAAA,SAEP,oCAAoC,OAAO,YAAY,CAAC;AAAA,IAAA,OAEnD;AACO,mBAAA,oCAAoC,OAAO,YAAY;AAAA,IACpE;AAED,WAAO,UAAU,eAAe,UAAU,EAAE,KAC3C,EAAE;AAAA,EAAA,OAEG;AACC,WAAA;AAAA,EACP;AACF;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asLink.cjs","sources":["../../../src/helpers/asLink.ts"],"sourcesContent":["import type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField, LinkType } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { documentToLinkField } from \"./documentToLinkField\";\n\n/**\n * Resolves a link to a Prismic document to a URL\n *\n * @typeParam ReturnType - Return type of your link resolver function, useful if\n * you prefer to return a complex object\n * @param linkToDocumentField - A document link field to resolve\n *\n * @returns Resolved URL\n * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n */\nexport type LinkResolverFunction<ReturnType = string | null | undefined> = (\n\tlinkToDocumentField: FilledContentRelationshipField,\n) => ReturnType;\n\n/**\n * Configuration that determines the output of `asLink()`.\n */\ntype AsLinkConfig<LinkResolverFunctionReturnType = string | null | undefined> =\n\t{\n\t\t/**\n\t\t * An optional link resolver function. Without it, you are\n\t\t *
|
|
1
|
+
{"version":3,"file":"asLink.cjs","sources":["../../../src/helpers/asLink.ts"],"sourcesContent":["import type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField, LinkType } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { documentToLinkField } from \"./documentToLinkField\";\n\n/**\n * Resolves a link to a Prismic document to a URL\n *\n * @typeParam ReturnType - Return type of your link resolver function, useful if\n * you prefer to return a complex object\n * @param linkToDocumentField - A document link field to resolve\n *\n * @returns Resolved URL\n *\n * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n */\nexport type LinkResolverFunction<ReturnType = string | null | undefined> = (\n\tlinkToDocumentField: FilledContentRelationshipField,\n) => ReturnType;\n\n/**\n * Configuration that determines the output of `asLink()`.\n */\ntype AsLinkConfig<LinkResolverFunctionReturnType = string | null | undefined> =\n\t{\n\t\t/**\n\t\t * An optional link resolver function. Without it, you are expected to use\n\t\t * the `routes` options from the API.\n\t\t */\n\t\tlinkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null;\n\t};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsLinkDeprecatedTupleConfig<\n\tLinkResolverFunctionReturnType = string | null | undefined,\n> = [\n\tlinkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null,\n];\n\n/**\n * The return type of `asLink()`.\n */\nexport type AsLinkReturnType<\n\tLinkResolverFunctionReturnType = string | null | undefined,\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? LinkResolverFunctionReturnType | string | null\n\t: null;\n\n// TODO: Remove overload when we remove support for deprecated tuple-style configuration.\nexport const asLink: {\n\t/**\n\t * Resolves any type of link field or Prismic document to a URL.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * asLink(field);\n\t * asLink(field, { linkResolver });\n\t * ```\n\t *\n\t * @typeParam LinkResolverFunctionReturnType - link resolver function return\n\t * type\n\t * @typeParam Field - Link field or Prismic document to resolve to a URL\n\t *\n\t * @param linkFieldOrDocument - Any kind of link field or a document to\n\t * resolve\n\t * @param config - Configuration that determines the output of `asLink()`\n\t *\n\t * @returns Resolved URL or, if the provided link field or document is empty,\n\t * `null`\n\t * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n\t * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\n\t */\n\t<\n\t\tLinkResolverFunctionReturnType = string | null | undefined,\n\t\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t\t| LinkField\n\t\t\t| PrismicDocument\n\t\t\t| null\n\t\t\t| undefined,\n\t>(\n\t\tlinkFieldOrDocument: Field,\n\t\tconfig?: AsLinkConfig<LinkResolverFunctionReturnType>,\n\t): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\n\t/**\n\t * Resolves any type of link field or Prismic document to a URL.\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * @typeParam LinkResolverFunctionReturnType - link resolver function return\n\t * type\n\t * @typeParam Field - Link field or Prismic document to resolve to a URL\n\t *\n\t * @param linkFieldOrDocument - Any kind of link field or a document to\n\t * resolve\n\t * @param linkResolver - An optional link resolver function. Without it, you\n\t * are expected to use the `routes` options from the API\n\t *\n\t * @returns Resolved URL or, if the provided link field or document is empty,\n\t * `null`\n\t * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n\t * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\n\t */\n\t<\n\t\tLinkResolverFunctionReturnType = string | null | undefined,\n\t\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t\t| LinkField\n\t\t\t| PrismicDocument\n\t\t\t| null\n\t\t\t| undefined,\n\t>(\n\t\tlinkFieldOrDocument: Field,\n\t\t...config: AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>\n\t): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n} = <\n\tLinkResolverFunctionReturnType = string | null | undefined,\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\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple:\n\t\t| [config?: AsLinkConfig<LinkResolverFunctionReturnType>]\n\t\t| AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>\n): AsLinkReturnType<LinkResolverFunctionReturnType, Field> => {\n\tif (!linkFieldOrDocument) {\n\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t}\n\n\t// Converts document to link field if needed\n\tconst linkField =\n\t\t// prettier-ignore\n\t\t(\n\t\t\t// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t\t// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501\n\t\t\t// TODO: Remove the `prettier-ignore` comment when this bug is fixed.\n\t\t\t\"link_type\" in linkFieldOrDocument\n\t\t\t\t? linkFieldOrDocument\n\t\t\t\t: documentToLinkField(linkFieldOrDocument)\n\t\t) as LinkField;\n\n\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\tconst [configObjectOrLinkResolver] = configObjectOrTuple;\n\tlet config: AsLinkConfig<LinkResolverFunctionReturnType>;\n\tif (\n\t\ttypeof configObjectOrLinkResolver === \"function\" ||\n\t\tconfigObjectOrLinkResolver == null\n\t) {\n\t\tconfig = {\n\t\t\tlinkResolver: configObjectOrLinkResolver,\n\t\t};\n\t} else {\n\t\tconfig = { ...configObjectOrLinkResolver };\n\t}\n\n\tswitch (linkField.link_type) {\n\t\tcase LinkType.Media:\n\t\tcase LinkType.Web:\n\t\t\treturn (\"url\" in linkField ? linkField.url : null) as AsLinkReturnType<\n\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\tField\n\t\t\t>;\n\n\t\tcase LinkType.Document: {\n\t\t\tif (\"id\" in linkField && config.linkResolver) {\n\t\t\t\t// When using link resolver...\n\t\t\t\tconst resolvedURL = config.linkResolver(linkField);\n\n\t\t\t\tif (resolvedURL != null) {\n\t\t\t\t\treturn resolvedURL as AsLinkReturnType<\n\t\t\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\t\t\tField\n\t\t\t\t\t>;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (\"url\" in linkField && linkField.url) {\n\t\t\t\t// When using route resolver...\n\t\t\t\treturn linkField.url as AsLinkReturnType<\n\t\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\t\tField\n\t\t\t\t>;\n\t\t\t}\n\n\t\t\t// When empty or link resolver and route resolver are not used...\n\t\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t\t}\n\n\t\tcase LinkType.Any:\n\t\tdefault:\n\t\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t}\n};\n"],"names":["documentToLinkField","LinkType"],"mappings":";;;;AA+Da,MAAA,SAkET,CAQH,wBAEG,wBAGyD;AAC5D,MAAI,CAAC,qBAAqB;AAClB,WAAA;AAAA,EACP;AAGK,QAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMJ,eAAe,sBACZ,sBACAA,oBAAAA,oBAAoB,mBAAmB;AAAA;AAItC,QAAA,CAAC,0BAA0B,IAAI;AACjC,MAAA;AACJ,MACC,OAAO,+BAA+B,cACtC,8BAA8B,MAC7B;AACQ,aAAA;AAAA,MACR,cAAc;AAAA,IAAA;AAAA,SAET;AACG,aAAA,EAAE,GAAG;EACd;AAED,UAAQ,UAAU,WAAW;AAAA,IAC5B,KAAKC,KAAAA,SAAS;AAAA,IACd,KAAKA,KAAS,SAAA;AACL,aAAA,SAAS,YAAY,UAAU,MAAM;AAAA,IAK9C,KAAKA,KAAAA,SAAS,UAAU;AACnB,UAAA,QAAQ,aAAa,OAAO,cAAc;AAEvC,cAAA,cAAc,OAAO,aAAa,SAAS;AAEjD,YAAI,eAAe,MAAM;AACjB,iBAAA;AAAA,QAIP;AAAA,MACD;AAEG,UAAA,SAAS,aAAa,UAAU,KAAK;AAExC,eAAO,UAAU;AAAA,MAIjB;AAGM,aAAA;AAAA,IACP;AAAA,IAED,KAAKA,KAAAA,SAAS;AAAA,IACd;AACQ,aAAA;AAAA,EACR;AACF;;"}
|
package/dist/helpers/asLink.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { FilledLinkToMediaField } from "../types/value/linkToMedia";
|
|
|
10
10
|
* @param linkToDocumentField - A document link field to resolve
|
|
11
11
|
*
|
|
12
12
|
* @returns Resolved URL
|
|
13
|
+
*
|
|
13
14
|
* @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
|
|
14
15
|
*/
|
|
15
16
|
export type LinkResolverFunction<ReturnType = string | null | undefined> = (linkToDocumentField: FilledContentRelationshipField) => ReturnType;
|
|
@@ -18,8 +19,8 @@ export type LinkResolverFunction<ReturnType = string | null | undefined> = (link
|
|
|
18
19
|
*/
|
|
19
20
|
type AsLinkConfig<LinkResolverFunctionReturnType = string | null | undefined> = {
|
|
20
21
|
/**
|
|
21
|
-
* An optional link resolver function. Without it, you are
|
|
22
|
-
*
|
|
22
|
+
* An optional link resolver function. Without it, you are expected to use
|
|
23
|
+
* the `routes` options from the API.
|
|
23
24
|
*/
|
|
24
25
|
linkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null;
|
|
25
26
|
};
|
|
@@ -37,13 +38,23 @@ export declare const asLink: {
|
|
|
37
38
|
/**
|
|
38
39
|
* Resolves any type of link field or Prismic document to a URL.
|
|
39
40
|
*
|
|
41
|
+
* @example
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* asLink(field);
|
|
45
|
+
* asLink(field, { linkResolver });
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
40
48
|
* @typeParam LinkResolverFunctionReturnType - link resolver function return
|
|
41
49
|
* type
|
|
42
50
|
* @typeParam Field - Link field or Prismic document to resolve to a URL
|
|
43
|
-
*
|
|
51
|
+
*
|
|
52
|
+
* @param linkFieldOrDocument - Any kind of link field or a document to
|
|
53
|
+
* resolve
|
|
44
54
|
* @param config - Configuration that determines the output of `asLink()`
|
|
45
55
|
*
|
|
46
|
-
* @returns Resolved URL or, if the provided link field or document is empty,
|
|
56
|
+
* @returns Resolved URL or, if the provided link field or document is empty,
|
|
57
|
+
* `null`
|
|
47
58
|
* @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
|
|
48
59
|
* @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}
|
|
49
60
|
*/
|
|
@@ -51,23 +62,21 @@ export declare const asLink: {
|
|
|
51
62
|
/**
|
|
52
63
|
* Resolves any type of link field or Prismic document to a URL.
|
|
53
64
|
*
|
|
65
|
+
* @deprecated Use object-style configuration instead.
|
|
66
|
+
*
|
|
54
67
|
* @typeParam LinkResolverFunctionReturnType - link resolver function return
|
|
55
68
|
* type
|
|
56
69
|
* @typeParam Field - Link field or Prismic document to resolve to a URL
|
|
57
|
-
* @param linkFieldOrDocument - Any kind of link field or a document to resolve
|
|
58
|
-
* @param linkResolver - An optional link resolver function. Without it, you are
|
|
59
|
-
* expected to use the `routes` options from the API
|
|
60
70
|
*
|
|
61
|
-
* @
|
|
71
|
+
* @param linkFieldOrDocument - Any kind of link field or a document to
|
|
72
|
+
* resolve
|
|
73
|
+
* @param linkResolver - An optional link resolver function. Without it, you
|
|
74
|
+
* are expected to use the `routes` options from the API
|
|
75
|
+
*
|
|
76
|
+
* @returns Resolved URL or, if the provided link field or document is empty,
|
|
77
|
+
* `null`
|
|
62
78
|
* @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
|
|
63
79
|
* @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}
|
|
64
|
-
*
|
|
65
|
-
* @deprecated Use object-style configuration instead.
|
|
66
|
-
*
|
|
67
|
-
* ```ts
|
|
68
|
-
* asLink(field);
|
|
69
|
-
* asLink(field, { linkResolver });
|
|
70
|
-
* ```
|
|
71
80
|
*/
|
|
72
81
|
<LinkResolverFunctionReturnType = string | null | undefined, Field extends LinkField | PrismicDocument | null | undefined = LinkField | PrismicDocument | null | undefined>(linkFieldOrDocument: Field, ...config: AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;
|
|
73
82
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asLink.js","sources":["../../../src/helpers/asLink.ts"],"sourcesContent":["import type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField, LinkType } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { documentToLinkField } from \"./documentToLinkField\";\n\n/**\n * Resolves a link to a Prismic document to a URL\n *\n * @typeParam ReturnType - Return type of your link resolver function, useful if\n * you prefer to return a complex object\n * @param linkToDocumentField - A document link field to resolve\n *\n * @returns Resolved URL\n * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n */\nexport type LinkResolverFunction<ReturnType = string | null | undefined> = (\n\tlinkToDocumentField: FilledContentRelationshipField,\n) => ReturnType;\n\n/**\n * Configuration that determines the output of `asLink()`.\n */\ntype AsLinkConfig<LinkResolverFunctionReturnType = string | null | undefined> =\n\t{\n\t\t/**\n\t\t * An optional link resolver function. Without it, you are\n\t\t *
|
|
1
|
+
{"version":3,"file":"asLink.js","sources":["../../../src/helpers/asLink.ts"],"sourcesContent":["import type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField, LinkType } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { documentToLinkField } from \"./documentToLinkField\";\n\n/**\n * Resolves a link to a Prismic document to a URL\n *\n * @typeParam ReturnType - Return type of your link resolver function, useful if\n * you prefer to return a complex object\n * @param linkToDocumentField - A document link field to resolve\n *\n * @returns Resolved URL\n *\n * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n */\nexport type LinkResolverFunction<ReturnType = string | null | undefined> = (\n\tlinkToDocumentField: FilledContentRelationshipField,\n) => ReturnType;\n\n/**\n * Configuration that determines the output of `asLink()`.\n */\ntype AsLinkConfig<LinkResolverFunctionReturnType = string | null | undefined> =\n\t{\n\t\t/**\n\t\t * An optional link resolver function. Without it, you are expected to use\n\t\t * the `routes` options from the API.\n\t\t */\n\t\tlinkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null;\n\t};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsLinkDeprecatedTupleConfig<\n\tLinkResolverFunctionReturnType = string | null | undefined,\n> = [\n\tlinkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null,\n];\n\n/**\n * The return type of `asLink()`.\n */\nexport type AsLinkReturnType<\n\tLinkResolverFunctionReturnType = string | null | undefined,\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? LinkResolverFunctionReturnType | string | null\n\t: null;\n\n// TODO: Remove overload when we remove support for deprecated tuple-style configuration.\nexport const asLink: {\n\t/**\n\t * Resolves any type of link field or Prismic document to a URL.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * asLink(field);\n\t * asLink(field, { linkResolver });\n\t * ```\n\t *\n\t * @typeParam LinkResolverFunctionReturnType - link resolver function return\n\t * type\n\t * @typeParam Field - Link field or Prismic document to resolve to a URL\n\t *\n\t * @param linkFieldOrDocument - Any kind of link field or a document to\n\t * resolve\n\t * @param config - Configuration that determines the output of `asLink()`\n\t *\n\t * @returns Resolved URL or, if the provided link field or document is empty,\n\t * `null`\n\t * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n\t * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\n\t */\n\t<\n\t\tLinkResolverFunctionReturnType = string | null | undefined,\n\t\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t\t| LinkField\n\t\t\t| PrismicDocument\n\t\t\t| null\n\t\t\t| undefined,\n\t>(\n\t\tlinkFieldOrDocument: Field,\n\t\tconfig?: AsLinkConfig<LinkResolverFunctionReturnType>,\n\t): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\n\t/**\n\t * Resolves any type of link field or Prismic document to a URL.\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * @typeParam LinkResolverFunctionReturnType - link resolver function return\n\t * type\n\t * @typeParam Field - Link field or Prismic document to resolve to a URL\n\t *\n\t * @param linkFieldOrDocument - Any kind of link field or a document to\n\t * resolve\n\t * @param linkResolver - An optional link resolver function. Without it, you\n\t * are expected to use the `routes` options from the API\n\t *\n\t * @returns Resolved URL or, if the provided link field or document is empty,\n\t * `null`\n\t * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n\t * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\n\t */\n\t<\n\t\tLinkResolverFunctionReturnType = string | null | undefined,\n\t\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t\t| LinkField\n\t\t\t| PrismicDocument\n\t\t\t| null\n\t\t\t| undefined,\n\t>(\n\t\tlinkFieldOrDocument: Field,\n\t\t...config: AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>\n\t): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n} = <\n\tLinkResolverFunctionReturnType = string | null | undefined,\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\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple:\n\t\t| [config?: AsLinkConfig<LinkResolverFunctionReturnType>]\n\t\t| AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>\n): AsLinkReturnType<LinkResolverFunctionReturnType, Field> => {\n\tif (!linkFieldOrDocument) {\n\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t}\n\n\t// Converts document to link field if needed\n\tconst linkField =\n\t\t// prettier-ignore\n\t\t(\n\t\t\t// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t\t// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501\n\t\t\t// TODO: Remove the `prettier-ignore` comment when this bug is fixed.\n\t\t\t\"link_type\" in linkFieldOrDocument\n\t\t\t\t? linkFieldOrDocument\n\t\t\t\t: documentToLinkField(linkFieldOrDocument)\n\t\t) as LinkField;\n\n\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\tconst [configObjectOrLinkResolver] = configObjectOrTuple;\n\tlet config: AsLinkConfig<LinkResolverFunctionReturnType>;\n\tif (\n\t\ttypeof configObjectOrLinkResolver === \"function\" ||\n\t\tconfigObjectOrLinkResolver == null\n\t) {\n\t\tconfig = {\n\t\t\tlinkResolver: configObjectOrLinkResolver,\n\t\t};\n\t} else {\n\t\tconfig = { ...configObjectOrLinkResolver };\n\t}\n\n\tswitch (linkField.link_type) {\n\t\tcase LinkType.Media:\n\t\tcase LinkType.Web:\n\t\t\treturn (\"url\" in linkField ? linkField.url : null) as AsLinkReturnType<\n\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\tField\n\t\t\t>;\n\n\t\tcase LinkType.Document: {\n\t\t\tif (\"id\" in linkField && config.linkResolver) {\n\t\t\t\t// When using link resolver...\n\t\t\t\tconst resolvedURL = config.linkResolver(linkField);\n\n\t\t\t\tif (resolvedURL != null) {\n\t\t\t\t\treturn resolvedURL as AsLinkReturnType<\n\t\t\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\t\t\tField\n\t\t\t\t\t>;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (\"url\" in linkField && linkField.url) {\n\t\t\t\t// When using route resolver...\n\t\t\t\treturn linkField.url as AsLinkReturnType<\n\t\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\t\tField\n\t\t\t\t>;\n\t\t\t}\n\n\t\t\t// When empty or link resolver and route resolver are not used...\n\t\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t\t}\n\n\t\tcase LinkType.Any:\n\t\tdefault:\n\t\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t}\n};\n"],"names":[],"mappings":";;AA+Da,MAAA,SAkET,CAQH,wBAEG,wBAGyD;AAC5D,MAAI,CAAC,qBAAqB;AAClB,WAAA;AAAA,EACP;AAGK,QAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMJ,eAAe,sBACZ,sBACA,oBAAoB,mBAAmB;AAAA;AAItC,QAAA,CAAC,0BAA0B,IAAI;AACjC,MAAA;AACJ,MACC,OAAO,+BAA+B,cACtC,8BAA8B,MAC7B;AACQ,aAAA;AAAA,MACR,cAAc;AAAA,IAAA;AAAA,SAET;AACG,aAAA,EAAE,GAAG;EACd;AAED,UAAQ,UAAU,WAAW;AAAA,IAC5B,KAAK,SAAS;AAAA,IACd,KAAK,SAAS;AACL,aAAA,SAAS,YAAY,UAAU,MAAM;AAAA,IAK9C,KAAK,SAAS,UAAU;AACnB,UAAA,QAAQ,aAAa,OAAO,cAAc;AAEvC,cAAA,cAAc,OAAO,aAAa,SAAS;AAEjD,YAAI,eAAe,MAAM;AACjB,iBAAA;AAAA,QAIP;AAAA,MACD;AAEG,UAAA,SAAS,aAAa,UAAU,KAAK;AAExC,eAAO,UAAU;AAAA,MAIjB;AAGM,aAAA;AAAA,IACP;AAAA,IAED,KAAK,SAAS;AAAA,IACd;AACQ,aAAA;AAAA,EACR;AACF;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asLinkAttrs.cjs","sources":["../../../src/helpers/asLinkAttrs.ts"],"sourcesContent":["import { isInternalURL } from \"../lib/isInternalURL\";\n\nimport type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { AsLinkReturnType, LinkResolverFunction, 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:\n\t\t| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>\n\t\t| 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/**\n * The return type of `asLinkAttrs()`.\n */\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:\n\t\t\t\t| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>\n\t\t\t\t| undefined;\n\t\t\ttarget?: string;\n\t\t\trel?: string;\n\t }\n\t: {\n\t\t\thref?: undefined;\n\t\t\ttarget?: undefined;\n\t\t\trel?: undefined;\n\t };\n\n/**\n * Resolves any type of link field or Prismic document to a set of link attributes. The attributes are designed to be passed to link HTML elements
|
|
1
|
+
{"version":3,"file":"asLinkAttrs.cjs","sources":["../../../src/helpers/asLinkAttrs.ts"],"sourcesContent":["import { isInternalURL } from \"../lib/isInternalURL\";\n\nimport type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { AsLinkReturnType, LinkResolverFunction, 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:\n\t\t| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>\n\t\t| 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/**\n * The return type of `asLinkAttrs()`.\n */\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:\n\t\t\t\t| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>\n\t\t\t\t| undefined;\n\t\t\ttarget?: string;\n\t\t\trel?: string;\n\t }\n\t: {\n\t\t\thref?: undefined;\n\t\t\ttarget?: undefined;\n\t\t\trel?: undefined;\n\t };\n\n/**\n * Resolves any type of link field or Prismic document to a set of link\n * attributes. The attributes are designed to be passed to link HTML elements,\n * like `<a>`.\n *\n * If a resolved URL is external (i.e. starts with a protocol like `https://`),\n * `rel` is returned as `\"noreferrer\"`.\n *\n * @typeParam LinkResolverFunctionReturnType - link resolver function return\n * type\n * @typeParam Field - Link field or Prismic document to resolve to link\n * attributes\n * @param linkFieldOrDocument - Any kind of link field or a document to resolve\n * @param config - Configuration that determines the output of `asLinkAttrs()`\n *\n * @returns Resolved set of link attributes or, if the provided link field or\n * document is empty, and empty object\n * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\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// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501\n\t\t(\"link_type\" in linkFieldOrDocument\n\t\t\t? isFilledLink(linkFieldOrDocument)\n\t\t\t: linkFieldOrDocument)\n\t) {\n\t\tconst target =\n\t\t\t// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t\t// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501\n\t\t\t\"target\" in linkFieldOrDocument ? linkFieldOrDocument.target : undefined;\n\n\t\tconst rawHref = asLink(linkFieldOrDocument, config.linkResolver);\n\t\tconst href =\n\t\t\trawHref == 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? \"noreferrer\"\n\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"],"names":["isFilledLink","asLink","isInternalURL"],"mappings":";;;;;AAuFO,MAAM,cAAc,CAQ1B,qBACA,SAA4D,OACF;AAEzD,MAAA;AAAA;AAAA,GAGC,eAAe,sBACbA,SAAAA,KAAa,mBAAmB,IAChC,sBACF;AACK,UAAA;AAAA;AAAA;AAAA,MAGL,YAAY,sBAAsB,oBAAoB,SAAS;AAAA;AAEhE,UAAM,UAAUC,OAAA,OAAO,qBAAqB,OAAO,YAAY;AACzD,UAAA,OACL,WAAW,OAAO,SAAa;AAEhC,UAAM,aAAa,OAAO,SAAS,WAAW,CAACC,4BAAc,IAAI,IAAI;AAErE,UAAM,MAAM,OAAO,MAChB,OAAO,IAAI,EAAE,MAAM,YAAY,OAAM,CAAE,IACvC,aACA,eACA;AAEI,WAAA;AAAA,MACN;AAAA,MACA;AAAA,MACA,KAAK,OAAO,OAAO,SAAY;AAAA,IAAA;AAAA,EAEhC;AAED,SAAO;AACR;;"}
|
|
@@ -25,17 +25,22 @@ type AsLinkAttrsReturnType<LinkResolverFunctionReturnType = ReturnType<LinkResol
|
|
|
25
25
|
rel?: undefined;
|
|
26
26
|
};
|
|
27
27
|
/**
|
|
28
|
-
* Resolves any type of link field or Prismic document to a set of link
|
|
28
|
+
* Resolves any type of link field or Prismic document to a set of link
|
|
29
|
+
* attributes. The attributes are designed to be passed to link HTML elements,
|
|
30
|
+
* like `<a>`.
|
|
29
31
|
*
|
|
30
|
-
* If a resolved URL is external (i.e. starts with a protocol like `https://`),
|
|
32
|
+
* If a resolved URL is external (i.e. starts with a protocol like `https://`),
|
|
33
|
+
* `rel` is returned as `"noreferrer"`.
|
|
31
34
|
*
|
|
32
35
|
* @typeParam LinkResolverFunctionReturnType - link resolver function return
|
|
33
36
|
* type
|
|
34
|
-
* @typeParam Field - Link field or Prismic document to resolve to link
|
|
37
|
+
* @typeParam Field - Link field or Prismic document to resolve to link
|
|
38
|
+
* attributes
|
|
35
39
|
* @param linkFieldOrDocument - Any kind of link field or a document to resolve
|
|
36
40
|
* @param config - Configuration that determines the output of `asLinkAttrs()`
|
|
37
41
|
*
|
|
38
|
-
* @returns Resolved set of link attributes or, if the provided link field or
|
|
42
|
+
* @returns Resolved set of link attributes or, if the provided link field or
|
|
43
|
+
* document is empty, and empty object
|
|
39
44
|
* @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
|
|
40
45
|
* @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}
|
|
41
46
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asLinkAttrs.js","sources":["../../../src/helpers/asLinkAttrs.ts"],"sourcesContent":["import { isInternalURL } from \"../lib/isInternalURL\";\n\nimport type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { AsLinkReturnType, LinkResolverFunction, 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:\n\t\t| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>\n\t\t| 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/**\n * The return type of `asLinkAttrs()`.\n */\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:\n\t\t\t\t| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>\n\t\t\t\t| undefined;\n\t\t\ttarget?: string;\n\t\t\trel?: string;\n\t }\n\t: {\n\t\t\thref?: undefined;\n\t\t\ttarget?: undefined;\n\t\t\trel?: undefined;\n\t };\n\n/**\n * Resolves any type of link field or Prismic document to a set of link attributes. The attributes are designed to be passed to link HTML elements
|
|
1
|
+
{"version":3,"file":"asLinkAttrs.js","sources":["../../../src/helpers/asLinkAttrs.ts"],"sourcesContent":["import { isInternalURL } from \"../lib/isInternalURL\";\n\nimport type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { AsLinkReturnType, LinkResolverFunction, 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:\n\t\t| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>\n\t\t| 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/**\n * The return type of `asLinkAttrs()`.\n */\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:\n\t\t\t\t| NonNullable<AsLinkReturnType<LinkResolverFunctionReturnType, Field>>\n\t\t\t\t| undefined;\n\t\t\ttarget?: string;\n\t\t\trel?: string;\n\t }\n\t: {\n\t\t\thref?: undefined;\n\t\t\ttarget?: undefined;\n\t\t\trel?: undefined;\n\t };\n\n/**\n * Resolves any type of link field or Prismic document to a set of link\n * attributes. The attributes are designed to be passed to link HTML elements,\n * like `<a>`.\n *\n * If a resolved URL is external (i.e. starts with a protocol like `https://`),\n * `rel` is returned as `\"noreferrer\"`.\n *\n * @typeParam LinkResolverFunctionReturnType - link resolver function return\n * type\n * @typeParam Field - Link field or Prismic document to resolve to link\n * attributes\n * @param linkFieldOrDocument - Any kind of link field or a document to resolve\n * @param config - Configuration that determines the output of `asLinkAttrs()`\n *\n * @returns Resolved set of link attributes or, if the provided link field or\n * document is empty, and empty object\n * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\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// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501\n\t\t(\"link_type\" in linkFieldOrDocument\n\t\t\t? isFilledLink(linkFieldOrDocument)\n\t\t\t: linkFieldOrDocument)\n\t) {\n\t\tconst target =\n\t\t\t// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t\t// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501\n\t\t\t\"target\" in linkFieldOrDocument ? linkFieldOrDocument.target : undefined;\n\n\t\tconst rawHref = asLink(linkFieldOrDocument, config.linkResolver);\n\t\tconst href =\n\t\t\trawHref == 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? \"noreferrer\"\n\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"],"names":["isFilledLink"],"mappings":";;;AAuFO,MAAM,cAAc,CAQ1B,qBACA,SAA4D,OACF;AAEzD,MAAA;AAAA;AAAA,GAGC,eAAe,sBACbA,KAAa,mBAAmB,IAChC,sBACF;AACK,UAAA;AAAA;AAAA;AAAA,MAGL,YAAY,sBAAsB,oBAAoB,SAAS;AAAA;AAEhE,UAAM,UAAU,OAAO,qBAAqB,OAAO,YAAY;AACzD,UAAA,OACL,WAAW,OAAO,SAAa;AAEhC,UAAM,aAAa,OAAO,SAAS,WAAW,CAAC,cAAc,IAAI,IAAI;AAErE,UAAM,MAAM,OAAO,MAChB,OAAO,IAAI,EAAE,MAAM,YAAY,OAAM,CAAE,IACvC,aACA,eACA;AAEI,WAAA;AAAA,MACN;AAAA,MACA;AAAA,MACA,KAAK,OAAO,OAAO,SAAY;AAAA,IAAA;AAAA,EAEhC;AAED,SAAO;AACR;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asText.cjs","sources":["../../../src/helpers/asText.ts"],"sourcesContent":["import { asText as baseAsText } from \"@prismicio/richtext\";\n\nimport { RichTextField } from \"../types/value/richText\";\n\n/**\n * Configuration that determines the output of `asText()`.\n */\ntype AsTextConfig = {\n\t/**\n\t * Separator used to join each element.\n\t *\n\t * @defaultValue ` ` (a space)\n\t */\n\tseparator?: string;\n};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsTextDeprecatedTupleConfig = [separator?: string];\n\n/**\n * The return type of `asText()`.\n */\ntype AsTextReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\nexport const asText: {\n\t/**\n\t * Serializes a rich text or title field to a plain text string.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param config - Configuration that determines the output of `asText()`\n\t *\n\t * @returns Plain text equivalent of the provided rich text or title field\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\tconfig?: AsTextConfig,\n\t): AsTextReturnType<Field>;\n\n\t/**\n\t * Serializes a rich text or title field to a plain text string.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param separator - Separator used to join each element, defaults to a space\n\t *\n\t * @returns Plain text equivalent of the provided rich text or title field\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t
|
|
1
|
+
{"version":3,"file":"asText.cjs","sources":["../../../src/helpers/asText.ts"],"sourcesContent":["import { asText as baseAsText } from \"@prismicio/richtext\";\n\nimport { RichTextField } from \"../types/value/richText\";\n\n/**\n * Configuration that determines the output of `asText()`.\n */\ntype AsTextConfig = {\n\t/**\n\t * Separator used to join each element.\n\t *\n\t * @defaultValue ` ` (a space)\n\t */\n\tseparator?: string;\n};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsTextDeprecatedTupleConfig = [separator?: string];\n\n/**\n * The return type of `asText()`.\n */\ntype AsTextReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\nexport const asText: {\n\t/**\n\t * Serializes a rich text or title field to a plain text string.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * asText(field);\n\t * asText(field, { separator });\n\t * ```\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param config - Configuration that determines the output of `asText()`\n\t *\n\t * @returns Plain text equivalent of the provided rich text or title field\n\t *\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\tconfig?: AsTextConfig,\n\t): AsTextReturnType<Field>;\n\n\t/**\n\t * Serializes a rich text or title field to a plain text string.\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * @param richTextField - A rich text or title field from Prismic\n\t * @param separator - Separator used to join each element, defaults to a space\n\t *\n\t * @returns Plain text equivalent of the provided rich text or title field\n\t *\n\t * @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\t...config: AsTextDeprecatedTupleConfig\n\t): AsTextReturnType<Field>;\n} = <Field extends RichTextField | null | undefined>(\n\trichTextField: Field,\n\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple: [config?: AsTextConfig] | AsTextDeprecatedTupleConfig\n): AsTextReturnType<Field> => {\n\tif (richTextField) {\n\t\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\t\tconst [configObjectOrSeparator] = configObjectOrTuple;\n\t\tlet config: AsTextConfig;\n\t\tif (typeof configObjectOrSeparator === \"string\") {\n\t\t\tconfig = {\n\t\t\t\tseparator: configObjectOrSeparator,\n\t\t\t};\n\t\t} else {\n\t\t\tconfig = { ...configObjectOrSeparator };\n\t\t}\n\n\t\treturn baseAsText(\n\t\t\trichTextField,\n\t\t\tconfig.separator,\n\t\t) as AsTextReturnType<Field>;\n\t} else {\n\t\treturn null as AsTextReturnType<Field>;\n\t}\n};\n"],"names":["baseAsText"],"mappings":";;;AA4Ba,MAAA,SAuCT,CACH,kBAEG,wBACyB;AAC5B,MAAI,eAAe;AAEZ,UAAA,CAAC,uBAAuB,IAAI;AAC9B,QAAA;AACA,QAAA,OAAO,4BAA4B,UAAU;AACvC,eAAA;AAAA,QACR,WAAW;AAAA,MAAA;AAAA,WAEN;AACG,eAAA,EAAE,GAAG;IACd;AAEM,WAAAA,gBACN,eACA,OAAO,SAAS;AAAA,EAAA,OAEX;AACC,WAAA;AAAA,EACP;AACF;;"}
|
package/dist/helpers/asText.d.ts
CHANGED
|
@@ -22,28 +22,32 @@ export declare const asText: {
|
|
|
22
22
|
/**
|
|
23
23
|
* Serializes a rich text or title field to a plain text string.
|
|
24
24
|
*
|
|
25
|
+
* @example
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* asText(field);
|
|
29
|
+
* asText(field, { separator });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
25
32
|
* @param richTextField - A rich text or title field from Prismic
|
|
26
33
|
* @param config - Configuration that determines the output of `asText()`
|
|
27
34
|
*
|
|
28
35
|
* @returns Plain text equivalent of the provided rich text or title field
|
|
36
|
+
*
|
|
29
37
|
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
30
38
|
*/
|
|
31
39
|
<Field extends RichTextField | null | undefined>(richTextField: Field, config?: AsTextConfig): AsTextReturnType<Field>;
|
|
32
40
|
/**
|
|
33
41
|
* Serializes a rich text or title field to a plain text string.
|
|
34
42
|
*
|
|
43
|
+
* @deprecated Use object-style configuration instead.
|
|
44
|
+
*
|
|
35
45
|
* @param richTextField - A rich text or title field from Prismic
|
|
36
46
|
* @param separator - Separator used to join each element, defaults to a space
|
|
37
47
|
*
|
|
38
48
|
* @returns Plain text equivalent of the provided rich text or title field
|
|
39
|
-
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
40
|
-
*
|
|
41
|
-
* @deprecated Use object-style configuration instead.
|
|
42
49
|
*
|
|
43
|
-
*
|
|
44
|
-
* asText(field);
|
|
45
|
-
* asText(field, { separator });
|
|
46
|
-
* ```
|
|
50
|
+
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
47
51
|
*/
|
|
48
52
|
<Field extends RichTextField | null | undefined>(richTextField: Field, ...config: AsTextDeprecatedTupleConfig): AsTextReturnType<Field>;
|
|
49
53
|
};
|