@prismicio/client 7.0.0-alpha.0 → 7.0.0-alpha.2
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/README.md +4 -4
- package/dist/buildQueryURL.cjs +26 -8
- package/dist/buildQueryURL.cjs.map +1 -1
- package/dist/buildQueryURL.d.ts +207 -130
- package/dist/buildQueryURL.js +25 -7
- package/dist/buildQueryURL.js.map +1 -1
- package/dist/cookie.cjs +1 -1
- package/dist/cookie.cjs.map +1 -1
- package/dist/cookie.d.ts +5 -5
- package/dist/cookie.js.map +1 -1
- package/dist/createClient.cjs +1142 -0
- package/dist/createClient.cjs.map +1 -0
- package/dist/{client.d.ts → createClient.d.ts} +980 -908
- package/dist/createClient.js +1142 -0
- package/dist/createClient.js.map +1 -0
- package/dist/{ForbiddenError.cjs → errors/ForbiddenError.cjs} +1 -1
- package/dist/errors/ForbiddenError.cjs.map +1 -0
- package/dist/{ForbiddenError.d.ts → errors/ForbiddenError.d.ts} +11 -11
- package/dist/errors/ForbiddenError.js.map +1 -0
- package/dist/{NotFoundError.cjs → errors/NotFoundError.cjs} +1 -1
- package/dist/errors/NotFoundError.cjs.map +1 -0
- package/dist/{NotFoundError.d.ts → errors/NotFoundError.d.ts} +3 -3
- package/dist/errors/NotFoundError.js.map +1 -0
- package/dist/{ParsingError.cjs → errors/ParsingError.cjs} +1 -1
- package/dist/errors/ParsingError.cjs.map +1 -0
- package/dist/{ParsingError.d.ts → errors/ParsingError.d.ts} +12 -12
- package/dist/errors/ParsingError.js.map +1 -0
- package/dist/{PrismicError.cjs → errors/PrismicError.cjs} +1 -1
- package/dist/errors/PrismicError.cjs.map +1 -0
- package/dist/{PrismicError.d.ts → errors/PrismicError.d.ts} +5 -5
- package/dist/errors/PrismicError.js.map +1 -0
- package/dist/filter.cjs +247 -0
- package/dist/filter.cjs.map +1 -0
- package/dist/filter.d.ts +209 -0
- package/dist/filter.js +247 -0
- package/dist/filter.js.map +1 -0
- package/dist/getGraphQLEndpoint.cjs +2 -2
- package/dist/getGraphQLEndpoint.cjs.map +1 -1
- package/dist/getGraphQLEndpoint.d.ts +9 -9
- package/dist/getGraphQLEndpoint.js +1 -1
- package/dist/getGraphQLEndpoint.js.map +1 -1
- package/dist/getRepositoryEndpoint.cjs +2 -2
- package/dist/getRepositoryEndpoint.cjs.map +1 -1
- package/dist/getRepositoryEndpoint.d.ts +10 -10
- package/dist/getRepositoryEndpoint.js +1 -1
- package/dist/getRepositoryEndpoint.js.map +1 -1
- package/dist/getRepositoryName.cjs +2 -2
- package/dist/getRepositoryName.cjs.map +1 -1
- package/dist/getRepositoryName.d.ts +13 -13
- package/dist/getRepositoryName.js +1 -1
- package/dist/getRepositoryName.js.map +1 -1
- package/dist/getToolbarSrc.cjs +13 -0
- package/dist/getToolbarSrc.cjs.map +1 -0
- package/dist/getToolbarSrc.d.ts +34 -0
- package/dist/getToolbarSrc.js +13 -0
- package/dist/getToolbarSrc.js.map +1 -0
- package/dist/helpers/asDate.cjs +1 -1
- package/dist/helpers/asDate.cjs.map +1 -1
- package/dist/helpers/asDate.d.ts +16 -16
- package/dist/helpers/asDate.js.map +1 -1
- package/dist/helpers/asHTML.cjs +7 -6
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.d.ts +77 -20
- package/dist/helpers/asHTML.js +6 -5
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.cjs +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.d.ts +52 -52
- package/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
- package/dist/helpers/asImageSrc.cjs +1 -1
- package/dist/helpers/asImageSrc.cjs.map +1 -1
- package/dist/helpers/asImageSrc.d.ts +27 -27
- package/dist/helpers/asImageSrc.js.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs +19 -14
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +60 -60
- package/dist/helpers/asImageWidthSrcSet.js +18 -13
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLink.cjs +8 -2
- package/dist/helpers/asLink.cjs.map +1 -1
- package/dist/helpers/asLink.d.ts +34 -24
- package/dist/helpers/asLink.js +7 -1
- package/dist/helpers/asLink.js.map +1 -1
- package/dist/helpers/asLinkAttrs.cjs +28 -0
- package/dist/helpers/asLinkAttrs.cjs.map +1 -0
- package/dist/helpers/asLinkAttrs.d.ts +63 -0
- package/dist/helpers/asLinkAttrs.js +28 -0
- package/dist/helpers/asLinkAttrs.js.map +1 -0
- package/dist/helpers/asText.cjs +1 -1
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.d.ts +16 -16
- package/dist/helpers/asText.js.map +1 -1
- package/dist/helpers/documentToLinkField.cjs +12 -5
- package/dist/helpers/documentToLinkField.cjs.map +1 -1
- package/dist/helpers/documentToLinkField.d.ts +15 -15
- package/dist/helpers/documentToLinkField.js +11 -4
- package/dist/helpers/documentToLinkField.js.map +1 -1
- package/dist/helpers/isFilled.cjs +1 -1
- package/dist/helpers/isFilled.cjs.map +1 -1
- package/dist/helpers/isFilled.d.ts +172 -175
- package/dist/helpers/isFilled.js.map +1 -1
- package/dist/index.cjs +20 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +139 -92
- package/dist/index.js +12 -6
- package/dist/index.js.map +1 -1
- package/dist/isRepositoryEndpoint.cjs +1 -1
- package/dist/isRepositoryEndpoint.cjs.map +1 -1
- package/dist/isRepositoryEndpoint.d.ts +10 -10
- package/dist/isRepositoryEndpoint.js.map +1 -1
- package/dist/isRepositoryName.cjs +1 -1
- package/dist/isRepositoryName.cjs.map +1 -1
- package/dist/isRepositoryName.d.ts +9 -9
- package/dist/isRepositoryName.js.map +1 -1
- package/dist/lib/appendFilters.cjs +11 -0
- package/dist/lib/appendFilters.cjs.map +1 -0
- package/dist/lib/appendFilters.d.ts +18 -0
- package/dist/lib/appendFilters.js +11 -0
- package/dist/lib/appendFilters.js.map +1 -0
- package/dist/lib/castArray.cjs +4 -2
- package/dist/lib/castArray.cjs.map +1 -1
- package/dist/lib/castArray.d.ts +11 -11
- package/dist/lib/castArray.js +3 -1
- package/dist/lib/castArray.js.map +1 -1
- package/dist/lib/castThunk.cjs +4 -2
- package/dist/lib/castThunk.cjs.map +1 -1
- package/dist/lib/castThunk.d.ts +10 -10
- package/dist/lib/castThunk.js +3 -1
- package/dist/lib/castThunk.js.map +1 -1
- package/dist/lib/devMsg.cjs +8 -0
- package/dist/lib/devMsg.cjs.map +1 -0
- package/dist/lib/devMsg.d.ts +16 -0
- package/dist/lib/devMsg.js +8 -0
- package/dist/lib/devMsg.js.map +1 -0
- package/dist/lib/escapeHTML.cjs +43 -0
- package/dist/lib/escapeHTML.cjs.map +1 -0
- package/dist/lib/escapeHTML.d.ts +1 -0
- package/dist/lib/escapeHTML.js +43 -0
- package/dist/lib/escapeHTML.js.map +1 -0
- package/dist/lib/everyTagFilter.cjs +9 -0
- package/dist/lib/everyTagFilter.cjs.map +1 -0
- package/dist/lib/everyTagFilter.d.ts +9 -0
- package/dist/lib/everyTagFilter.js +9 -0
- package/dist/lib/everyTagFilter.js.map +1 -0
- package/dist/lib/findMasterRef.cjs +1 -1
- package/dist/lib/findMasterRef.cjs.map +1 -1
- package/dist/lib/findMasterRef.d.ts +10 -10
- package/dist/lib/findMasterRef.js.map +1 -1
- package/dist/lib/findRef.cjs +4 -4
- package/dist/lib/findRef.cjs.map +1 -1
- package/dist/lib/findRef.d.ts +13 -13
- package/dist/lib/findRef.js +3 -3
- package/dist/lib/findRef.js.map +1 -1
- package/dist/lib/findRefByID.cjs +1 -1
- package/dist/lib/findRefByID.cjs.map +1 -1
- package/dist/lib/findRefByID.d.ts +11 -11
- package/dist/lib/findRefByID.js.map +1 -1
- package/dist/lib/findRefByLabel.cjs +1 -1
- package/dist/lib/findRefByLabel.cjs.map +1 -1
- package/dist/lib/findRefByLabel.d.ts +11 -11
- package/dist/lib/findRefByLabel.js.map +1 -1
- package/dist/lib/getPreviewCookie.cjs +1 -1
- package/dist/lib/getPreviewCookie.cjs.map +1 -1
- package/dist/lib/getPreviewCookie.d.ts +9 -9
- package/dist/lib/getPreviewCookie.js.map +1 -1
- package/dist/lib/isInternalURL.cjs +9 -0
- package/dist/lib/isInternalURL.cjs.map +1 -0
- package/dist/lib/isInternalURL.d.ts +8 -0
- package/dist/lib/isInternalURL.js +9 -0
- package/dist/lib/isInternalURL.js.map +1 -0
- package/dist/lib/minifyGraphQLQuery.cjs +1 -1
- package/dist/lib/minifyGraphQLQuery.cjs.map +1 -1
- package/dist/lib/minifyGraphQLQuery.d.ts +8 -8
- package/dist/lib/minifyGraphQLQuery.js.map +1 -1
- package/dist/lib/serializerHelpers.cjs +7 -9
- package/dist/lib/serializerHelpers.cjs.map +1 -1
- package/dist/lib/serializerHelpers.d.ts +9 -9
- package/dist/lib/serializerHelpers.js +6 -6
- package/dist/lib/serializerHelpers.js.map +1 -1
- package/dist/lib/someTagsFilter.cjs +9 -0
- package/dist/lib/someTagsFilter.cjs.map +1 -0
- package/dist/lib/someTagsFilter.d.ts +9 -0
- package/dist/lib/someTagsFilter.js +9 -0
- package/dist/lib/someTagsFilter.js.map +1 -0
- package/dist/lib/typeFilter.cjs +8 -0
- package/dist/lib/typeFilter.cjs.map +1 -0
- package/dist/lib/typeFilter.d.ts +8 -0
- package/dist/lib/typeFilter.js +8 -0
- package/dist/lib/typeFilter.js.map +1 -0
- package/dist/package.json.cjs +5 -0
- package/dist/package.json.cjs.map +1 -0
- package/dist/package.json.js +5 -0
- package/dist/package.json.js.map +1 -0
- package/dist/types/api/query.d.ts +42 -42
- package/dist/types/api/ref.d.ts +32 -32
- package/dist/types/api/release.d.ts +10 -10
- package/dist/types/api/repository.d.ts +100 -100
- package/dist/types/api/tags.d.ts +8 -8
- package/dist/types/model/boolean.d.ts +15 -15
- package/dist/types/model/color.d.ts +13 -13
- package/dist/types/model/contentRelationship.d.ts +18 -18
- package/dist/types/model/customType.d.ts +42 -42
- package/dist/types/model/date.d.ts +14 -14
- package/dist/types/model/embed.d.ts +13 -13
- package/dist/types/model/geoPoint.d.ts +12 -12
- package/dist/types/model/group.d.ts +15 -15
- package/dist/types/model/image.d.ts +31 -31
- package/dist/types/model/integrationFields.d.ts +14 -14
- package/dist/types/model/keyText.d.ts +13 -13
- package/dist/types/model/link.cjs +1 -1
- package/dist/types/model/link.cjs.map +1 -1
- package/dist/types/model/link.d.ts +27 -27
- package/dist/types/model/link.js.map +1 -1
- package/dist/types/model/linkToMedia.d.ts +16 -16
- package/dist/types/model/number.d.ts +15 -15
- package/dist/types/model/range.d.ts +14 -14
- package/dist/types/model/richText.d.ts +35 -35
- package/dist/types/model/select.d.ts +18 -18
- package/dist/types/model/separator.d.ts +10 -10
- package/dist/types/model/sharedSlice.d.ts +16 -19
- package/dist/types/model/sharedSliceVariation.d.ts +19 -22
- package/dist/types/model/slice.cjs +1 -1
- package/dist/types/model/slice.cjs.map +1 -1
- package/dist/types/model/slice.d.ts +33 -33
- package/dist/types/model/slice.js.map +1 -1
- package/dist/types/model/sliceZone.cjs +1 -1
- package/dist/types/model/sliceZone.cjs.map +1 -1
- package/dist/types/model/sliceZone.d.ts +41 -44
- package/dist/types/model/sliceZone.js.map +1 -1
- package/dist/types/model/timestamp.d.ts +14 -14
- package/dist/types/model/title.d.ts +7 -7
- package/dist/types/model/types.cjs +10 -1
- package/dist/types/model/types.cjs.map +1 -1
- package/dist/types/model/types.d.ts +62 -62
- package/dist/types/model/types.js +9 -0
- package/dist/types/model/types.js.map +1 -1
- package/dist/types/model/uid.d.ts +13 -13
- package/dist/types/value/boolean.d.ts +6 -6
- package/dist/types/value/color.d.ts +8 -8
- package/dist/types/value/contentRelationship.d.ts +29 -29
- package/dist/types/value/date.d.ts +8 -8
- package/dist/types/value/document.d.ts +113 -113
- package/dist/types/value/embed.cjs +1 -1
- package/dist/types/value/embed.cjs.map +1 -1
- package/dist/types/value/embed.d.ts +167 -167
- package/dist/types/value/embed.js.map +1 -1
- package/dist/types/value/geoPoint.d.ts +11 -11
- package/dist/types/value/group.d.ts +7 -7
- package/dist/types/value/image.d.ts +36 -36
- package/dist/types/value/integrationFields.d.ts +9 -9
- package/dist/types/value/keyText.d.ts +8 -8
- package/dist/types/value/link.cjs +1 -1
- package/dist/types/value/link.cjs.map +1 -1
- package/dist/types/value/link.d.ts +40 -40
- package/dist/types/value/link.js.map +1 -1
- package/dist/types/value/linkToMedia.d.ts +20 -20
- package/dist/types/value/number.d.ts +8 -8
- package/dist/types/value/richText.cjs +1 -1
- package/dist/types/value/richText.cjs.map +1 -1
- package/dist/types/value/richText.d.ts +203 -203
- package/dist/types/value/richText.js.map +1 -1
- package/dist/types/value/select.d.ts +9 -9
- package/dist/types/value/sharedSlice.d.ts +11 -11
- package/dist/types/value/sharedSliceVariation.d.ts +7 -7
- package/dist/types/value/slice.d.ts +13 -13
- package/dist/types/value/sliceZone.d.ts +12 -12
- package/dist/types/value/timestamp.d.ts +8 -8
- package/dist/types/value/title.d.ts +12 -12
- package/dist/types/value/types.d.ts +40 -40
- package/dist/types/webhook/apiUpdate.d.ts +52 -52
- package/dist/types/webhook/testTrigger.d.ts +9 -9
- package/dist/types/webhook/types.cjs +1 -1
- package/dist/types/webhook/types.cjs.map +1 -1
- package/dist/types/webhook/types.d.ts +18 -18
- package/dist/types/webhook/types.js.map +1 -1
- package/package.json +23 -24
- package/src/buildQueryURL.ts +179 -33
- package/src/{client.ts → createClient.ts} +214 -110
- package/src/filter.ts +356 -0
- package/src/getGraphQLEndpoint.ts +2 -1
- package/src/getRepositoryEndpoint.ts +2 -1
- package/src/getRepositoryName.ts +1 -1
- package/src/getToolbarSrc.ts +50 -0
- package/src/helpers/asDate.ts +3 -3
- package/src/helpers/asHTML.ts +122 -27
- package/src/helpers/asImagePixelDensitySrcSet.ts +1 -1
- package/src/helpers/asImageSrc.ts +1 -1
- package/src/helpers/asImageWidthSrcSet.ts +1 -1
- package/src/helpers/asLink.ts +37 -18
- package/src/helpers/asLinkAttrs.ts +127 -0
- package/src/helpers/asText.ts +4 -4
- package/src/helpers/documentToLinkField.ts +3 -3
- package/src/helpers/isFilled.ts +9 -11
- package/src/index.ts +60 -29
- package/src/lib/appendFilters.ts +26 -0
- package/src/lib/castArray.ts +3 -1
- package/src/lib/castThunk.ts +3 -2
- package/src/lib/devMsg.ts +20 -0
- package/src/lib/escapeHTML.ts +51 -0
- package/src/lib/everyTagFilter.ts +15 -0
- package/src/lib/findRef.ts +5 -5
- package/src/lib/isInternalURL.ts +14 -0
- package/src/lib/serializerHelpers.ts +20 -15
- package/src/lib/someTagsFilter.ts +15 -0
- package/src/lib/typeFilter.ts +12 -0
- package/src/types/api/query.ts +1 -1
- package/src/types/api/ref.ts +2 -2
- package/src/types/api/release.ts +1 -1
- package/src/types/api/repository.ts +1 -1
- package/src/types/api/tags.ts +1 -1
- package/src/types/model/boolean.ts +1 -1
- package/src/types/model/color.ts +1 -1
- package/src/types/model/contentRelationship.ts +2 -1
- package/src/types/model/date.ts +1 -1
- package/src/types/model/embed.ts +1 -1
- package/src/types/model/geoPoint.ts +1 -1
- package/src/types/model/group.ts +2 -2
- package/src/types/model/image.ts +3 -3
- package/src/types/model/integrationFields.ts +1 -1
- package/src/types/model/keyText.ts +1 -1
- package/src/types/model/link.ts +3 -3
- package/src/types/model/linkToMedia.ts +2 -1
- package/src/types/model/number.ts +1 -1
- package/src/types/model/richText.ts +3 -3
- package/src/types/model/select.ts +1 -1
- package/src/types/model/sharedSlice.ts +2 -5
- package/src/types/model/sharedSliceVariation.ts +1 -4
- package/src/types/model/slice.ts +5 -4
- package/src/types/model/sliceZone.ts +5 -7
- package/src/types/model/timestamp.ts +1 -1
- package/src/types/model/title.ts +1 -1
- package/src/types/model/types.ts +12 -16
- package/src/types/model/uid.ts +1 -1
- package/src/types/value/boolean.ts +1 -1
- package/src/types/value/color.ts +1 -1
- package/src/types/value/contentRelationship.ts +8 -9
- package/src/types/value/date.ts +1 -1
- package/src/types/value/document.ts +6 -5
- package/src/types/value/embed.ts +2 -2
- package/src/types/value/geoPoint.ts +1 -1
- package/src/types/value/group.ts +2 -2
- package/src/types/value/image.ts +1 -1
- package/src/types/value/integrationFields.ts +1 -1
- package/src/types/value/keyText.ts +1 -1
- package/src/types/value/link.ts +8 -8
- package/src/types/value/linkToMedia.ts +2 -1
- package/src/types/value/number.ts +1 -1
- package/src/types/value/richText.ts +6 -5
- package/src/types/value/select.ts +1 -1
- package/src/types/value/sharedSlice.ts +1 -1
- package/src/types/value/slice.ts +1 -1
- package/src/types/value/sliceZone.ts +2 -1
- package/src/types/value/timestamp.ts +1 -0
- package/src/types/value/title.ts +2 -1
- package/src/types/value/types.ts +9 -11
- package/src/types/webhook/apiUpdate.ts +4 -4
- package/src/types/webhook/testTrigger.ts +1 -1
- package/src/types/webhook/types.ts +2 -2
- package/dist/ForbiddenError.cjs.map +0 -1
- package/dist/ForbiddenError.js.map +0 -1
- package/dist/NotFoundError.cjs.map +0 -1
- package/dist/NotFoundError.js.map +0 -1
- package/dist/ParsingError.cjs.map +0 -1
- package/dist/ParsingError.js.map +0 -1
- package/dist/PrismicError.cjs.map +0 -1
- package/dist/PrismicError.js.map +0 -1
- package/dist/client.cjs +0 -414
- package/dist/client.cjs.map +0 -1
- package/dist/client.js +0 -414
- package/dist/client.js.map +0 -1
- package/dist/lib/appendPredicates.cjs +0 -14
- package/dist/lib/appendPredicates.cjs.map +0 -1
- package/dist/lib/appendPredicates.d.ts +0 -18
- package/dist/lib/appendPredicates.js +0 -14
- package/dist/lib/appendPredicates.js.map +0 -1
- package/dist/predicate.cjs +0 -68
- package/dist/predicate.cjs.map +0 -1
- package/dist/predicate.d.ts +0 -209
- package/dist/predicate.js +0 -68
- package/dist/predicate.js.map +0 -1
- package/dist/types/client.d.ts +0 -128
- package/dist/types/helpers.d.ts +0 -63
- package/dist/types/utils.d.ts +0 -28
- package/src/lib/appendPredicates.ts +0 -29
- package/src/predicate.ts +0 -359
- package/src/types/client.ts +0 -157
- package/src/types/helpers.ts +0 -103
- package/src/types/utils.ts +0 -34
- /package/dist/{ForbiddenError.js → errors/ForbiddenError.js} +0 -0
- /package/dist/{NotFoundError.js → errors/NotFoundError.js} +0 -0
- /package/dist/{ParsingError.js → errors/ParsingError.js} +0 -0
- /package/dist/{PrismicError.js → errors/PrismicError.js} +0 -0
- /package/src/{ForbiddenError.ts → errors/ForbiddenError.ts} +0 -0
- /package/src/{NotFoundError.ts → errors/NotFoundError.ts} +0 -0
- /package/src/{ParsingError.ts → errors/ParsingError.ts} +0 -0
- /package/src/{PrismicError.ts → errors/PrismicError.ts} +0 -0
package/src/filter.ts
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats the value of a filter element to a stringified version accepted by
|
|
3
|
+
* the Prismic REST API.
|
|
4
|
+
*
|
|
5
|
+
* @param value - Value to format.
|
|
6
|
+
*
|
|
7
|
+
* @returns `value` formatted for the Prismic REST API.
|
|
8
|
+
*/
|
|
9
|
+
const formatValue = (
|
|
10
|
+
value:
|
|
11
|
+
| string
|
|
12
|
+
| number
|
|
13
|
+
| Date
|
|
14
|
+
| unknown
|
|
15
|
+
| (string | number | Date | unknown)[],
|
|
16
|
+
): string => {
|
|
17
|
+
if (Array.isArray(value)) {
|
|
18
|
+
return `[${value.map(formatValue).join(", ")}]`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (typeof value === "string") {
|
|
22
|
+
return `"${value}"`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (value instanceof Date) {
|
|
26
|
+
return `${value.getTime()}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return `${value}`;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates a filter builder function for filters with a path and
|
|
34
|
+
* arguments.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam Args - Arguments for the filter.
|
|
37
|
+
* @param name - Name of the filter used in the resulting string.
|
|
38
|
+
*
|
|
39
|
+
* @returns Filter builder function for the given name.
|
|
40
|
+
*/
|
|
41
|
+
const pathWithArgsFilter = <Args extends unknown[]>(name: string) => {
|
|
42
|
+
/**
|
|
43
|
+
* @param path - Path to the value to be compared.
|
|
44
|
+
*/
|
|
45
|
+
const fn = (path: string, ...args: Args): string => {
|
|
46
|
+
const formattedArgs = args.map(formatValue).join(", ");
|
|
47
|
+
const joiner = path && args.length ? ", " : "";
|
|
48
|
+
|
|
49
|
+
return `[${name}(${path}${joiner}${formattedArgs})]`;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return fn;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Creates a filter builder function for filters with only a path.
|
|
57
|
+
*
|
|
58
|
+
* @param name - Name of the filter used in the resulting string.
|
|
59
|
+
*
|
|
60
|
+
* @returns Filter builder function for the given name.
|
|
61
|
+
*/
|
|
62
|
+
const pathFilter = (name: string) => {
|
|
63
|
+
const filterFn = pathWithArgsFilter(name);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param path - Path for the filter.
|
|
67
|
+
*/
|
|
68
|
+
const fn = (path: string): string => {
|
|
69
|
+
return filterFn(path);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return fn;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Creates a filter builder function for filters with only arguments and
|
|
77
|
+
* no path.
|
|
78
|
+
*
|
|
79
|
+
* @param name - Name of the filter used in the resulting string.
|
|
80
|
+
*
|
|
81
|
+
* @returns Filter builder function for the given name.
|
|
82
|
+
*/
|
|
83
|
+
const argsFilter = <Args extends unknown[]>(name: string) => {
|
|
84
|
+
const filterFn = pathWithArgsFilter<Args>(name);
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param args - Arguments for the filter.
|
|
88
|
+
*/
|
|
89
|
+
const fn = (...args: Args): string => {
|
|
90
|
+
return filterFn("", ...args);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
return fn;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const filter = {
|
|
97
|
+
/**
|
|
98
|
+
* The `at` filter checks that the path matches the described value
|
|
99
|
+
* exactly. It takes a single value for a field or an array (only for tags).
|
|
100
|
+
*
|
|
101
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#at}
|
|
102
|
+
*/
|
|
103
|
+
at: pathWithArgsFilter<[value: string | number | boolean | Date | string[]]>(
|
|
104
|
+
"at",
|
|
105
|
+
),
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The `not` filter checks that the path doesn't match the provided value
|
|
109
|
+
* exactly. It takes a single value for a field or an array (only for tags).
|
|
110
|
+
*
|
|
111
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#not}
|
|
112
|
+
*/
|
|
113
|
+
not: pathWithArgsFilter<[value: string | number | boolean | Date | string[]]>(
|
|
114
|
+
"not",
|
|
115
|
+
),
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The `any` filter takes an array of values. It works exactly the same way
|
|
119
|
+
* as the `at` operator, but checks whether the fragment matches any of the
|
|
120
|
+
* values in the array.
|
|
121
|
+
*
|
|
122
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#any}
|
|
123
|
+
*/
|
|
124
|
+
any: pathWithArgsFilter<[values: (string | number | boolean | Date)[]]>(
|
|
125
|
+
"any",
|
|
126
|
+
),
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The `in` filter is used specifically to retrieve an array of documents
|
|
130
|
+
* by their IDs or UIDs. This filter is much more efficient at this than
|
|
131
|
+
* the any filter.
|
|
132
|
+
*
|
|
133
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#in}
|
|
134
|
+
*/
|
|
135
|
+
in: pathWithArgsFilter<[values: string[]]>("in"),
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The `fulltext` filter provides two capabilities:
|
|
139
|
+
*
|
|
140
|
+
* 1. Checking if a certain string is anywhere inside a document (this is what
|
|
141
|
+
* you should use to make your project's search engine feature)
|
|
142
|
+
* 2. Checking if the string is contained inside a specific custom type’s Rich
|
|
143
|
+
* Text or Key Text fragment.
|
|
144
|
+
*
|
|
145
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#fulltext}
|
|
146
|
+
*/
|
|
147
|
+
fulltext: pathWithArgsFilter<[searchTerms: string]>("fulltext"),
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The `has` filter checks whether a fragment has a value. It will return
|
|
151
|
+
* all the documents of the specified type that contain a value for the
|
|
152
|
+
* specified field.
|
|
153
|
+
*
|
|
154
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#has}
|
|
155
|
+
*/
|
|
156
|
+
has: pathFilter("has"),
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The `missing` filter checks if a fragment doesn't have a value. It will
|
|
160
|
+
* return all the documents of the specified type that do not contain a value
|
|
161
|
+
* for the specified field.
|
|
162
|
+
*
|
|
163
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#missing}
|
|
164
|
+
*/
|
|
165
|
+
missing: pathFilter("missing"),
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The `similar` filter takes the ID of a document, and returns a list of
|
|
169
|
+
* documents with similar content. This allows you to build an automated
|
|
170
|
+
* content discovery feature (for example, a "Related posts" section).
|
|
171
|
+
*
|
|
172
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#similar}
|
|
173
|
+
*/
|
|
174
|
+
similar: argsFilter<[id: string, value: number]>("similar"),
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The `geopoint.near` filter checks that the value in the path is within
|
|
178
|
+
* the radius of the given coordinates.
|
|
179
|
+
*
|
|
180
|
+
* This filter will only work for a GeoPoint field.
|
|
181
|
+
*
|
|
182
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#geopointnear}
|
|
183
|
+
*/
|
|
184
|
+
geopointNear:
|
|
185
|
+
pathWithArgsFilter<[latitude: number, longitude: number, radius: number]>(
|
|
186
|
+
"geopoint.near",
|
|
187
|
+
),
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The `number.lt` filter checks that the value in the number field is less
|
|
191
|
+
* than the value passed into the filter.
|
|
192
|
+
*
|
|
193
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#numberlessthan}
|
|
194
|
+
*/
|
|
195
|
+
numberLessThan: pathWithArgsFilter<[value: number]>("number.lt"),
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The `number.gt` filter checks that the value in the number field is
|
|
199
|
+
* greater than the value passed into the filter.
|
|
200
|
+
*
|
|
201
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#numbergreaterthan}
|
|
202
|
+
*/
|
|
203
|
+
numberGreaterThan: pathWithArgsFilter<[value: number]>("number.gt"),
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The `number.inRange` filter checks that the value in the path is within
|
|
207
|
+
* the two values passed into the filter.
|
|
208
|
+
*
|
|
209
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#numberinrange}
|
|
210
|
+
*/
|
|
211
|
+
numberInRange:
|
|
212
|
+
pathWithArgsFilter<[lowerLimit: number, upperLimit: number]>(
|
|
213
|
+
"number.inRange",
|
|
214
|
+
),
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The `date.after` filter checks that the value in the path is after the
|
|
218
|
+
* date value passed into the filter.
|
|
219
|
+
*
|
|
220
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
221
|
+
*/
|
|
222
|
+
dateAfter: pathWithArgsFilter<[date: string | number | Date]>("date.after"),
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The `date.before` filter checks that the value in the path is before the
|
|
226
|
+
* date value passed into the filter.
|
|
227
|
+
*
|
|
228
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
229
|
+
*/
|
|
230
|
+
dateBefore: pathWithArgsFilter<[date: string | number | Date]>("date.before"),
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The `date.between` filter checks that the value in the path is within
|
|
234
|
+
* the date values passed into the filter.
|
|
235
|
+
*
|
|
236
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
237
|
+
*/
|
|
238
|
+
dateBetween:
|
|
239
|
+
pathWithArgsFilter<
|
|
240
|
+
[startDate: string | number | Date, endDate: string | number | Date]
|
|
241
|
+
>("date.between"),
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The `date.day-of-month` filter checks that the value in the path is
|
|
245
|
+
* equal to the day of the month passed into the filter.
|
|
246
|
+
*
|
|
247
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
248
|
+
*/
|
|
249
|
+
dateDayOfMonth: pathWithArgsFilter<[day: number]>("date.day-of-month"),
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The `date.day-of-month-after` filter checks that the value in the path
|
|
253
|
+
* is after the day of the month passed into the filter.
|
|
254
|
+
*
|
|
255
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
256
|
+
*/
|
|
257
|
+
dateDayOfMonthAfter: pathWithArgsFilter<[day: number]>(
|
|
258
|
+
"date.day-of-month-after",
|
|
259
|
+
),
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* The `date.day-of-month-before` filter checks that the value in the path
|
|
263
|
+
* is before the day of the month passed into the filter.
|
|
264
|
+
*
|
|
265
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
266
|
+
*/
|
|
267
|
+
dateDayOfMonthBefore: pathWithArgsFilter<[day: number]>(
|
|
268
|
+
"date.day-of-month-before",
|
|
269
|
+
),
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The `date.day-of-week` filter checks that the value in the path is equal
|
|
273
|
+
* to the day of the week passed into the filter.
|
|
274
|
+
*
|
|
275
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
276
|
+
*/
|
|
277
|
+
dateDayOfWeek: pathWithArgsFilter<[day: string | number]>("date.day-of-week"),
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The `date.day-of-week-after` filter checks that the value in the path is
|
|
281
|
+
* after the day of the week passed into the filter.
|
|
282
|
+
*
|
|
283
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
284
|
+
*/
|
|
285
|
+
dateDayOfWeekAfter: pathWithArgsFilter<[day: string | number]>(
|
|
286
|
+
"date.day-of-week-after",
|
|
287
|
+
),
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* The date.day-of-week-before filter checks that the value in the path is
|
|
291
|
+
* before the day of the week passed into the filter.
|
|
292
|
+
*
|
|
293
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
294
|
+
*/
|
|
295
|
+
dateDayOfWeekBefore: pathWithArgsFilter<[day: string | number]>(
|
|
296
|
+
"date.day-of-week-before",
|
|
297
|
+
),
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The `date.month` filter checks that the value in the path occurs in the
|
|
301
|
+
* month value passed into the filter.
|
|
302
|
+
*
|
|
303
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
304
|
+
*/
|
|
305
|
+
dateMonth: pathWithArgsFilter<[month: string | number]>("date.month"),
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* The `date.month-after` filter checks that the value in the path occurs
|
|
309
|
+
* in any month after the value passed into the filter.
|
|
310
|
+
*
|
|
311
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
312
|
+
*/
|
|
313
|
+
dateMonthAfter:
|
|
314
|
+
pathWithArgsFilter<[month: string | number]>("date.month-after"),
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The `date.month-before` filter checks that the value in the path occurs
|
|
318
|
+
* in any month before the value passed into the filter.
|
|
319
|
+
*
|
|
320
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
321
|
+
*/
|
|
322
|
+
dateMonthBefore:
|
|
323
|
+
pathWithArgsFilter<[month: string | number]>("date.month-before"),
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* The `date.year` filter checks that the value in the path occurs in the
|
|
327
|
+
* year value passed into the filter.
|
|
328
|
+
*
|
|
329
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
330
|
+
*/
|
|
331
|
+
dateYear: pathWithArgsFilter<[year: number]>("date.year"),
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* The `date.hour` filter checks that the value in the path occurs within
|
|
335
|
+
* the hour value passed into the filter.
|
|
336
|
+
*
|
|
337
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
338
|
+
*/
|
|
339
|
+
dateHour: pathWithArgsFilter<[hour: number]>("date.hour"),
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* The `date.hour-after` filter checks that the value in the path occurs
|
|
343
|
+
* after the hour value passed into the filter.
|
|
344
|
+
*
|
|
345
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
346
|
+
*/
|
|
347
|
+
dateHourAfter: pathWithArgsFilter<[hour: number]>("date.hour-after"),
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The `date.hour-before` filter checks that the value in the path occurs
|
|
351
|
+
* before the hour value passed into the filter.
|
|
352
|
+
*
|
|
353
|
+
* {@link https://prismic.io/docs/rest-api-technical-reference#date-filters}
|
|
354
|
+
*/
|
|
355
|
+
dateHourBefore: pathWithArgsFilter<[hour: number]>("date.hour-before"),
|
|
356
|
+
};
|
package/src/getRepositoryName.ts
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { PrismicError } from "./errors/PrismicError";
|
|
2
|
+
|
|
3
|
+
import { isRepositoryName } from "./isRepositoryName";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the URL for a Prismic repository's Prismic Toolbar script. Use the
|
|
7
|
+
* URL to inject the script into your app.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```typescriptreact
|
|
12
|
+
* // In Next.js apps, use `next/script` in your `app/layout.tsx` file.
|
|
13
|
+
*
|
|
14
|
+
* import Script from "next/script";
|
|
15
|
+
* import * as prismic from '@prismicio/client'
|
|
16
|
+
*
|
|
17
|
+
* export default function RootLayout({
|
|
18
|
+
* children,
|
|
19
|
+
* }: {
|
|
20
|
+
* children: React.ReactNode,
|
|
21
|
+
* }) {
|
|
22
|
+
* const toolbarSrc = prismic.getToolbarSrc("my-repo");
|
|
23
|
+
*
|
|
24
|
+
* return (
|
|
25
|
+
* <html lang="en">
|
|
26
|
+
* <body>{children}</body>
|
|
27
|
+
* <Script src={toolbarSrc} />
|
|
28
|
+
* </html>
|
|
29
|
+
* );
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param repositoryName - The name of the Prismic repository. For example,
|
|
34
|
+
* `"my-repo"` if the repository URL is `my-repo.prismic.io`.
|
|
35
|
+
*
|
|
36
|
+
* @returns The URL for the given Prismic repository's Prismic Toolbar script.
|
|
37
|
+
*/
|
|
38
|
+
export const getToolbarSrc = <TRepositoryName extends string>(
|
|
39
|
+
repositoryName: TRepositoryName,
|
|
40
|
+
): `https://static.cdn.prismic.io/prismic.js?new=true&repo=${TRepositoryName}` => {
|
|
41
|
+
if (isRepositoryName(repositoryName)) {
|
|
42
|
+
return `https://static.cdn.prismic.io/prismic.js?new=true&repo=${repositoryName}` as const;
|
|
43
|
+
} else {
|
|
44
|
+
throw new PrismicError(
|
|
45
|
+
`An invalid Prismic repository name was given: ${repositoryName}`,
|
|
46
|
+
undefined,
|
|
47
|
+
undefined,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
};
|
package/src/helpers/asDate.ts
CHANGED
|
@@ -9,12 +9,12 @@ type AsDateReturnType<
|
|
|
9
9
|
> = Field extends DateField<"filled"> | TimestampField<"filled"> ? Date : null;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Transforms a
|
|
12
|
+
* Transforms a Date or Timestamp field into a JavaScript Date object
|
|
13
13
|
*
|
|
14
|
-
* @param dateOrTimestampField - A
|
|
14
|
+
* @param dateOrTimestampField - A Date or Timestamp field from Prismic
|
|
15
15
|
*
|
|
16
16
|
* @returns A Date object, null if provided date is falsy
|
|
17
|
-
* @see Templating date field from Prismic {@link https://prismic.io/docs/
|
|
17
|
+
* @see Templating date field from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#date-and-timestamp}
|
|
18
18
|
*/
|
|
19
19
|
export const asDate = <
|
|
20
20
|
Field extends DateField | TimestampField | null | undefined,
|
package/src/helpers/asHTML.ts
CHANGED
|
@@ -1,36 +1,131 @@
|
|
|
1
1
|
import {
|
|
2
|
-
serialize,
|
|
3
2
|
Element,
|
|
4
|
-
composeSerializers,
|
|
5
3
|
RichTextFunctionSerializer,
|
|
6
4
|
RichTextMapSerializer,
|
|
5
|
+
RichTextMapSerializerFunction,
|
|
6
|
+
composeSerializers,
|
|
7
|
+
serialize,
|
|
7
8
|
wrapMapSerializer,
|
|
8
9
|
} from "@prismicio/richtext";
|
|
9
10
|
|
|
10
11
|
import {
|
|
11
|
-
serializeStandardTag,
|
|
12
|
-
serializePreFormatted,
|
|
13
|
-
serializeImage,
|
|
14
12
|
serializeEmbed,
|
|
15
13
|
serializeHyperlink,
|
|
14
|
+
serializeImage,
|
|
15
|
+
serializePreFormatted,
|
|
16
16
|
serializeSpan,
|
|
17
|
+
serializeStandardTag,
|
|
17
18
|
} from "../lib/serializerHelpers";
|
|
18
19
|
|
|
19
|
-
import type {
|
|
20
|
-
HTMLFunctionSerializer,
|
|
21
|
-
HTMLMapSerializer,
|
|
22
|
-
LinkResolverFunction,
|
|
23
|
-
} from "../types/helpers";
|
|
24
20
|
import type { RichTextField } from "../types/value/richText";
|
|
25
21
|
|
|
22
|
+
import { LinkResolverFunction } from "./asLink";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Serializes a node from a Rich Text or Title field with a function to HTML.
|
|
26
|
+
*
|
|
27
|
+
* Unlike a typical `@prismicio/richtext` function serializer, this serializer
|
|
28
|
+
* converts the `children` argument to a single string rather than an array of
|
|
29
|
+
* strings.
|
|
30
|
+
*
|
|
31
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
32
|
+
*/
|
|
33
|
+
export type HTMLRichTextFunctionSerializer = (
|
|
34
|
+
type: Parameters<RichTextFunctionSerializer<string>>[0],
|
|
35
|
+
node: Parameters<RichTextFunctionSerializer<string>>[1],
|
|
36
|
+
text: Parameters<RichTextFunctionSerializer<string>>[2],
|
|
37
|
+
children: Parameters<RichTextFunctionSerializer<string>>[3][number],
|
|
38
|
+
key: Parameters<RichTextFunctionSerializer<string>>[4],
|
|
39
|
+
) => string | null | undefined;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Serializes a node from a Rich Text or Title field with a map to HTML
|
|
43
|
+
*
|
|
44
|
+
* Unlike a typical `@prismicio/richtext` map serializer, this serializer
|
|
45
|
+
* converts the `children` property to a single string rather than an array of
|
|
46
|
+
* strings.
|
|
47
|
+
*
|
|
48
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
49
|
+
*/
|
|
50
|
+
export type HTMLRichTextMapSerializer = {
|
|
51
|
+
[P in keyof RichTextMapSerializer<string>]: (payload: {
|
|
52
|
+
type: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["type"];
|
|
53
|
+
node: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["node"];
|
|
54
|
+
text: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["text"];
|
|
55
|
+
children: Parameters<
|
|
56
|
+
HTMLRichTextMapSerializerFunction<P>
|
|
57
|
+
>[0]["children"][number];
|
|
58
|
+
key: Parameters<HTMLRichTextMapSerializerFunction<P>>[0]["key"];
|
|
59
|
+
}) => string | null | undefined;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A {@link RichTextMapSerializerFunction} type specifically for
|
|
64
|
+
* {@link HTMLRichTextMapSerializer}.
|
|
65
|
+
*
|
|
66
|
+
* @typeParam BlockName - The serializer's Rich Text block type.
|
|
67
|
+
*/
|
|
68
|
+
type HTMLRichTextMapSerializerFunction<
|
|
69
|
+
BlockType extends keyof RichTextMapSerializer<string>,
|
|
70
|
+
> = RichTextMapSerializerFunction<
|
|
71
|
+
string,
|
|
72
|
+
ExtractNodeGeneric<RichTextMapSerializer<string>[BlockType]>,
|
|
73
|
+
ExtractTextTypeGeneric<RichTextMapSerializer<string>[BlockType]>
|
|
74
|
+
>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns the `Node` generic from {@link RichTextMapSerializerFunction}.
|
|
78
|
+
*
|
|
79
|
+
* @typeParam T - The `RichTextMapSerializerFunction` containing the needed
|
|
80
|
+
* `Node` generic.
|
|
81
|
+
*/
|
|
82
|
+
type ExtractNodeGeneric<T> = T extends RichTextMapSerializerFunction<
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
any,
|
|
85
|
+
infer U,
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
+
any
|
|
88
|
+
>
|
|
89
|
+
? U
|
|
90
|
+
: never;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Returns the `TextType` generic from {@link RichTextMapSerializerFunction}.
|
|
94
|
+
*
|
|
95
|
+
* @typeParam T - The `RichTextMapSerializerFunction` containing the needed
|
|
96
|
+
* `TextType` generic.
|
|
97
|
+
*/
|
|
98
|
+
type ExtractTextTypeGeneric<T> = T extends RichTextMapSerializerFunction<
|
|
99
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
100
|
+
any,
|
|
101
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
102
|
+
any,
|
|
103
|
+
infer U
|
|
104
|
+
>
|
|
105
|
+
? U
|
|
106
|
+
: never;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Serializes a node from a Rich Text or Title field with a map or a function to HTML
|
|
110
|
+
*
|
|
111
|
+
* @see {@link HTMLRichTextMapSerializer} and {@link HTMLRichTextFunctionSerializer}
|
|
112
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
113
|
+
*/
|
|
114
|
+
export type HTMLRichTextSerializer =
|
|
115
|
+
| HTMLRichTextMapSerializer
|
|
116
|
+
| HTMLRichTextFunctionSerializer;
|
|
117
|
+
|
|
26
118
|
/**
|
|
27
|
-
* Creates a default HTML
|
|
119
|
+
* Creates a default HTML Rich Text Serializer with a given Link Resolver providing
|
|
28
120
|
* sensible and safe defaults for every node type
|
|
29
121
|
*
|
|
30
122
|
* @internal
|
|
31
123
|
*/
|
|
32
|
-
const
|
|
33
|
-
linkResolver:
|
|
124
|
+
const createDefaultHTMLRichTextSerializer = (
|
|
125
|
+
linkResolver:
|
|
126
|
+
| LinkResolverFunction<string | null | undefined>
|
|
127
|
+
| undefined
|
|
128
|
+
| null,
|
|
34
129
|
): RichTextFunctionSerializer<string> => {
|
|
35
130
|
return (_type, node, text, children, _key) => {
|
|
36
131
|
switch (node.type) {
|
|
@@ -87,7 +182,7 @@ const createDefaultHTMLSerializer = (
|
|
|
87
182
|
* @returns A regular function serializer
|
|
88
183
|
*/
|
|
89
184
|
const wrapMapSerializerWithStringChildren = (
|
|
90
|
-
mapSerializer:
|
|
185
|
+
mapSerializer: HTMLRichTextMapSerializer,
|
|
91
186
|
): RichTextFunctionSerializer<string> => {
|
|
92
187
|
const modifiedMapSerializer = {} as RichTextMapSerializer<string>;
|
|
93
188
|
|
|
@@ -115,34 +210,34 @@ type AsHTMLReturnType<Field extends RichTextField | null | undefined> =
|
|
|
115
210
|
Field extends RichTextField ? string : null;
|
|
116
211
|
|
|
117
212
|
/**
|
|
118
|
-
* Serializes a
|
|
213
|
+
* Serializes a Rich Text or Title field to an HTML string
|
|
119
214
|
*
|
|
120
|
-
* @param richTextField - A
|
|
215
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
121
216
|
* @param linkResolver - An optional link resolver function to resolve links,
|
|
122
217
|
* without it you're expected to use the `routes` options from the API
|
|
123
|
-
* @param
|
|
218
|
+
* @param htmlRichTextSerializer - An optional Rich Text Serializer, unhandled cases will fallback
|
|
124
219
|
* to the default serializer
|
|
125
220
|
*
|
|
126
|
-
* @returns HTML equivalent of the provided
|
|
127
|
-
* @see Templating
|
|
221
|
+
* @returns HTML equivalent of the provided Rich Text or Title field
|
|
222
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
128
223
|
*/
|
|
129
224
|
export const asHTML = <Field extends RichTextField | null | undefined>(
|
|
130
225
|
richTextField: Field,
|
|
131
|
-
linkResolver?: LinkResolverFunction<string> | null,
|
|
132
|
-
|
|
226
|
+
linkResolver?: LinkResolverFunction<string | null | undefined> | null,
|
|
227
|
+
htmlRichTextSerializer?: HTMLRichTextSerializer | null,
|
|
133
228
|
): AsHTMLReturnType<Field> => {
|
|
134
229
|
if (richTextField) {
|
|
135
230
|
let serializer: RichTextFunctionSerializer<string>;
|
|
136
|
-
if (
|
|
231
|
+
if (htmlRichTextSerializer) {
|
|
137
232
|
serializer = composeSerializers(
|
|
138
|
-
typeof
|
|
139
|
-
? wrapMapSerializerWithStringChildren(
|
|
233
|
+
typeof htmlRichTextSerializer === "object"
|
|
234
|
+
? wrapMapSerializerWithStringChildren(htmlRichTextSerializer)
|
|
140
235
|
: (type, node, text, children, key) =>
|
|
141
|
-
|
|
142
|
-
|
|
236
|
+
htmlRichTextSerializer(type, node, text, children.join(""), key),
|
|
237
|
+
createDefaultHTMLRichTextSerializer(linkResolver),
|
|
143
238
|
);
|
|
144
239
|
} else {
|
|
145
|
-
serializer =
|
|
240
|
+
serializer = createDefaultHTMLRichTextSerializer(linkResolver);
|
|
146
241
|
}
|
|
147
242
|
|
|
148
243
|
return serialize(richTextField, serializer).join(
|