@prismicio/client 7.0.0-alpha.3 → 7.0.0-alpha.5
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/createClient.cjs +57 -76
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +61 -69
- package/dist/createClient.js +57 -76
- package/dist/createClient.js.map +1 -1
- package/dist/helpers/asHTML.cjs +17 -4
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.d.ts +55 -13
- package/dist/helpers/asHTML.js +17 -4
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.cjs +2 -2
- package/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.d.ts +2 -2
- package/dist/helpers/asImagePixelDensitySrcSet.js +2 -2
- package/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
- package/dist/helpers/asImageSrc.cjs +2 -2
- package/dist/helpers/asImageSrc.cjs.map +1 -1
- package/dist/helpers/asImageSrc.d.ts +2 -2
- package/dist/helpers/asImageSrc.js +2 -2
- package/dist/helpers/asImageSrc.js.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs +2 -2
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +2 -2
- package/dist/helpers/asImageWidthSrcSet.js +2 -2
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLink.cjs +12 -3
- package/dist/helpers/asLink.cjs.map +1 -1
- package/dist/helpers/asLink.d.ts +55 -15
- package/dist/helpers/asLink.js +12 -3
- package/dist/helpers/asLink.js.map +1 -1
- package/dist/helpers/asText.cjs +11 -2
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.d.ts +44 -10
- package/dist/helpers/asText.js +11 -2
- package/dist/helpers/asText.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/package.json +1 -1
- package/src/createClient.ts +176 -251
- package/src/helpers/asHTML.ts +98 -25
- package/src/helpers/asImagePixelDensitySrcSet.ts +3 -3
- package/src/helpers/asImageSrc.ts +3 -3
- package/src/helpers/asImageWidthSrcSet.ts +3 -3
- package/src/helpers/asLink.ts +102 -18
- package/src/helpers/asText.ts +71 -12
package/src/helpers/asHTML.ts
CHANGED
|
@@ -122,10 +122,7 @@ export type HTMLRichTextSerializer =
|
|
|
122
122
|
* @internal
|
|
123
123
|
*/
|
|
124
124
|
const createDefaultHTMLRichTextSerializer = (
|
|
125
|
-
linkResolver:
|
|
126
|
-
| LinkResolverFunction<string | null | undefined>
|
|
127
|
-
| undefined
|
|
128
|
-
| null,
|
|
125
|
+
linkResolver: LinkResolverFunction | undefined | null,
|
|
129
126
|
): RichTextFunctionSerializer<string> => {
|
|
130
127
|
return (_type, node, text, children, _key) => {
|
|
131
128
|
switch (node.type) {
|
|
@@ -203,41 +200,117 @@ const wrapMapSerializerWithStringChildren = (
|
|
|
203
200
|
return wrapMapSerializer(modifiedMapSerializer);
|
|
204
201
|
};
|
|
205
202
|
|
|
203
|
+
/**
|
|
204
|
+
* Configuration that determines the output of `asHTML()`.
|
|
205
|
+
*/
|
|
206
|
+
type AsHTMLConfig = {
|
|
207
|
+
/**
|
|
208
|
+
* An optional link resolver function to resolve links.
|
|
209
|
+
* Without it you're expected to use the `routes` options from the API.
|
|
210
|
+
*/
|
|
211
|
+
linkResolver?: LinkResolverFunction | null;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* An optional Rich Text Serializer, unhandled cases will fallback to the default serializer
|
|
215
|
+
*/
|
|
216
|
+
serializer?: HTMLRichTextSerializer | null;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
220
|
+
/**
|
|
221
|
+
* @deprecated Use object-style configuration instead.
|
|
222
|
+
*/
|
|
223
|
+
type AsHTMLDeprecatedTupleConfig = [
|
|
224
|
+
linkResolver?: LinkResolverFunction | null,
|
|
225
|
+
serializer?: HTMLRichTextSerializer | null,
|
|
226
|
+
];
|
|
227
|
+
|
|
206
228
|
/**
|
|
207
229
|
* The return type of `asHTML()`.
|
|
208
230
|
*/
|
|
209
231
|
type AsHTMLReturnType<Field extends RichTextField | null | undefined> =
|
|
210
232
|
Field extends RichTextField ? string : null;
|
|
211
233
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
234
|
+
// TODO: Remove overload when we remove support for deprecated tuple-style configuration.
|
|
235
|
+
export const asHTML: {
|
|
236
|
+
/**
|
|
237
|
+
* Serializes a Rich Text or Title field to an HTML string.
|
|
238
|
+
*
|
|
239
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
240
|
+
* @param config - Configuration that determines the output of `asHTML()`
|
|
241
|
+
*
|
|
242
|
+
* @returns HTML equivalent of the provided Rich Text or Title field
|
|
243
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
244
|
+
*/
|
|
245
|
+
<Field extends RichTextField | null | undefined>(
|
|
246
|
+
richTextField: Field,
|
|
247
|
+
config?: AsHTMLConfig,
|
|
248
|
+
): AsHTMLReturnType<Field>;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Serializes a Rich Text or Title field to an HTML string.
|
|
252
|
+
*
|
|
253
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
254
|
+
* @param linkResolver - An optional link resolver function to resolve links,
|
|
255
|
+
* without it you're expected to use the `routes` options from the API
|
|
256
|
+
* @param serializer - An optional Rich Text Serializer, unhandled cases will fallback
|
|
257
|
+
* to the default serializer
|
|
258
|
+
*
|
|
259
|
+
* @returns HTML equivalent of the provided Rich Text or Title field
|
|
260
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
261
|
+
*
|
|
262
|
+
* @deprecated Use object-style configuration instead.
|
|
263
|
+
*
|
|
264
|
+
* ```ts
|
|
265
|
+
* asHTML(field);
|
|
266
|
+
* asHTML(field, { linkResolver });
|
|
267
|
+
* asHTML(field, { serializer });
|
|
268
|
+
* asHTML(field, { linkResolver, serializer });
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
<Field extends RichTextField | null | undefined>(
|
|
272
|
+
richTextField: Field,
|
|
273
|
+
...config: AsHTMLDeprecatedTupleConfig
|
|
274
|
+
): AsHTMLReturnType<Field>;
|
|
275
|
+
} = <Field extends RichTextField | null | undefined>(
|
|
225
276
|
richTextField: Field,
|
|
226
|
-
|
|
227
|
-
|
|
277
|
+
// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.
|
|
278
|
+
...configObjectOrTuple: [config?: AsHTMLConfig] | AsHTMLDeprecatedTupleConfig
|
|
228
279
|
): AsHTMLReturnType<Field> => {
|
|
229
280
|
if (richTextField) {
|
|
281
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
282
|
+
const [configObjectOrLinkResolver, maybeSerializer] = configObjectOrTuple;
|
|
283
|
+
let config: AsHTMLConfig;
|
|
284
|
+
if (
|
|
285
|
+
typeof configObjectOrLinkResolver === "function" ||
|
|
286
|
+
configObjectOrLinkResolver == null
|
|
287
|
+
) {
|
|
288
|
+
config = {
|
|
289
|
+
linkResolver: configObjectOrLinkResolver,
|
|
290
|
+
serializer: maybeSerializer,
|
|
291
|
+
};
|
|
292
|
+
} else {
|
|
293
|
+
config = { ...configObjectOrLinkResolver };
|
|
294
|
+
}
|
|
295
|
+
|
|
230
296
|
let serializer: RichTextFunctionSerializer<string>;
|
|
231
|
-
if (
|
|
297
|
+
if (config.serializer) {
|
|
232
298
|
serializer = composeSerializers(
|
|
233
|
-
typeof
|
|
234
|
-
? wrapMapSerializerWithStringChildren(
|
|
299
|
+
typeof config.serializer === "object"
|
|
300
|
+
? wrapMapSerializerWithStringChildren(config.serializer)
|
|
235
301
|
: (type, node, text, children, key) =>
|
|
236
|
-
|
|
237
|
-
|
|
302
|
+
// TypeScript doesn't narrow the type correctly here since it is now in a callback function, so we have to cast it here.
|
|
303
|
+
(config.serializer as HTMLRichTextFunctionSerializer)(
|
|
304
|
+
type,
|
|
305
|
+
node,
|
|
306
|
+
text,
|
|
307
|
+
children.join(""),
|
|
308
|
+
key,
|
|
309
|
+
),
|
|
310
|
+
createDefaultHTMLRichTextSerializer(config.linkResolver),
|
|
238
311
|
);
|
|
239
312
|
} else {
|
|
240
|
-
serializer = createDefaultHTMLRichTextSerializer(linkResolver);
|
|
313
|
+
serializer = createDefaultHTMLRichTextSerializer(config.linkResolver);
|
|
241
314
|
}
|
|
242
315
|
|
|
243
316
|
return serialize(richTextField, serializer).join(
|
|
@@ -65,7 +65,7 @@ type AsImagePixelDensitySrcSetReturnType<
|
|
|
65
65
|
*
|
|
66
66
|
* @param field - Image field (or one of its responsive views) from which to get
|
|
67
67
|
* an image URL.
|
|
68
|
-
* @param
|
|
68
|
+
* @param config - An object of Imgix URL API parameters. The `pixelDensities`
|
|
69
69
|
* parameter defines the resulting `srcset` widths.
|
|
70
70
|
*
|
|
71
71
|
* @returns A `srcset` attribute value for the Image field with Imgix URL
|
|
@@ -76,12 +76,12 @@ export const asImagePixelDensitySrcSet = <
|
|
|
76
76
|
Field extends ImageFieldImage | null | undefined,
|
|
77
77
|
>(
|
|
78
78
|
field: Field,
|
|
79
|
-
|
|
79
|
+
config: AsImagePixelDensitySrcSetConfig = {},
|
|
80
80
|
): AsImagePixelDensitySrcSetReturnType<Field> => {
|
|
81
81
|
if (field && isImageThumbnailFilled(field)) {
|
|
82
82
|
// We are using destructuring to omit `pixelDensities` from the
|
|
83
83
|
// object we will pass to `buildURL()`.
|
|
84
|
-
const { pixelDensities = DEFAULT_PIXEL_DENSITIES, ...imgixParams } =
|
|
84
|
+
const { pixelDensities = DEFAULT_PIXEL_DENSITIES, ...imgixParams } = config;
|
|
85
85
|
|
|
86
86
|
return {
|
|
87
87
|
src: buildURL(field.url, imgixParams),
|
|
@@ -23,7 +23,7 @@ type AsImageSrcReturnType<Field extends ImageFieldImage | null | undefined> =
|
|
|
23
23
|
*
|
|
24
24
|
* @param field - Image field (or one of its responsive views) from which to get
|
|
25
25
|
* an image URL.
|
|
26
|
-
* @param
|
|
26
|
+
* @param config - An object of Imgix URL API parameters to transform the image.
|
|
27
27
|
*
|
|
28
28
|
* @returns The Image field's image URL with transformations applied (if given).
|
|
29
29
|
* If the Image field is empty, `null` is returned.
|
|
@@ -31,10 +31,10 @@ type AsImageSrcReturnType<Field extends ImageFieldImage | null | undefined> =
|
|
|
31
31
|
*/
|
|
32
32
|
export const asImageSrc = <Field extends ImageFieldImage | null | undefined>(
|
|
33
33
|
field: Field,
|
|
34
|
-
|
|
34
|
+
config: ImgixURLParams = {},
|
|
35
35
|
): AsImageSrcReturnType<Field> => {
|
|
36
36
|
if (field && isImageThumbnailFilled(field)) {
|
|
37
|
-
return buildURL(field.url,
|
|
37
|
+
return buildURL(field.url, config) as AsImageSrcReturnType<Field>;
|
|
38
38
|
} else {
|
|
39
39
|
return null as AsImageSrcReturnType<Field>;
|
|
40
40
|
}
|
|
@@ -68,7 +68,7 @@ type AsImageWidthSrcSetConfig = Omit<BuildWidthSrcSetParams, "widths"> & {
|
|
|
68
68
|
*
|
|
69
69
|
* @param field - Image field (or one of its responsive views) from which to get
|
|
70
70
|
* an image URL.
|
|
71
|
-
* @param
|
|
71
|
+
* @param config - An object of Imgix URL API parameters. The `widths` parameter
|
|
72
72
|
* defines the resulting `srcset` widths. Pass `"thumbnails"` to automatically
|
|
73
73
|
* use the field's responsive views.
|
|
74
74
|
*
|
|
@@ -80,7 +80,7 @@ export const asImageWidthSrcSet = <
|
|
|
80
80
|
Field extends ImageFieldImage | null | undefined,
|
|
81
81
|
>(
|
|
82
82
|
field: Field,
|
|
83
|
-
|
|
83
|
+
config: AsImageWidthSrcSetConfig = {},
|
|
84
84
|
): AsImageWidthSrcSetReturnType<Field> => {
|
|
85
85
|
if (field && isFilled.imageThumbnail(field)) {
|
|
86
86
|
// We are using destructuring to omit `widths` from the object
|
|
@@ -89,7 +89,7 @@ export const asImageWidthSrcSet = <
|
|
|
89
89
|
widths = DEFAULT_WIDTHS,
|
|
90
90
|
// eslint-disable-next-line prefer-const
|
|
91
91
|
...imgixParams
|
|
92
|
-
} =
|
|
92
|
+
} = config;
|
|
93
93
|
const {
|
|
94
94
|
url,
|
|
95
95
|
dimensions,
|
package/src/helpers/asLink.ts
CHANGED
|
@@ -19,6 +19,28 @@ export type LinkResolverFunction<ReturnType = string | null | undefined> = (
|
|
|
19
19
|
linkToDocumentField: FilledContentRelationshipField,
|
|
20
20
|
) => ReturnType;
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Configuration that determines the output of `asLink()`.
|
|
24
|
+
*/
|
|
25
|
+
type AsLinkConfig<LinkResolverFunctionReturnType = string | null | undefined> =
|
|
26
|
+
{
|
|
27
|
+
/**
|
|
28
|
+
* An optional Link Resolver function. Without it, you are
|
|
29
|
+
* expected to use the `routes` options from the API.
|
|
30
|
+
*/
|
|
31
|
+
linkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated Use object-style configuration instead.
|
|
37
|
+
*/
|
|
38
|
+
type AsLinkDeprecatedTupleConfig<
|
|
39
|
+
LinkResolverFunctionReturnType = string | null | undefined,
|
|
40
|
+
> = [
|
|
41
|
+
linkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null,
|
|
42
|
+
];
|
|
43
|
+
|
|
22
44
|
/**
|
|
23
45
|
* The return type of `asLink()`.
|
|
24
46
|
*/
|
|
@@ -37,21 +59,66 @@ export type AsLinkReturnType<
|
|
|
37
59
|
? LinkResolverFunctionReturnType | string | null
|
|
38
60
|
: null;
|
|
39
61
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
// TODO: Remove overload when we remove support for deprecated tuple-style configuration.
|
|
63
|
+
export const asLink: {
|
|
64
|
+
/**
|
|
65
|
+
* Resolves any type of Link field or Prismic document to a URL.
|
|
66
|
+
*
|
|
67
|
+
* @typeParam LinkResolverFunctionReturnType - Link Resolver function return
|
|
68
|
+
* type
|
|
69
|
+
* @typeParam Field - Link field or Prismic document to resolve to a URL
|
|
70
|
+
* @param linkFieldOrDocument - Any kind of Link field or a document to resolve
|
|
71
|
+
* @param config - Configuration that determines the output of `asLink()`
|
|
72
|
+
*
|
|
73
|
+
* @returns Resolved URL or, if the provided Link field or document is empty, `null`
|
|
74
|
+
* @see Prismic Link Resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
|
|
75
|
+
* @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}
|
|
76
|
+
*/
|
|
77
|
+
<
|
|
78
|
+
LinkResolverFunctionReturnType = string | null | undefined,
|
|
79
|
+
Field extends LinkField | PrismicDocument | null | undefined =
|
|
80
|
+
| LinkField
|
|
81
|
+
| PrismicDocument
|
|
82
|
+
| null
|
|
83
|
+
| undefined,
|
|
84
|
+
>(
|
|
85
|
+
linkFieldOrDocument: Field,
|
|
86
|
+
config?: AsLinkConfig<LinkResolverFunctionReturnType>,
|
|
87
|
+
): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Resolves any type of Link field or Prismic document to a URL.
|
|
91
|
+
*
|
|
92
|
+
* @typeParam LinkResolverFunctionReturnType - Link Resolver function return
|
|
93
|
+
* type
|
|
94
|
+
* @typeParam Field - Link field or Prismic document to resolve to a URL
|
|
95
|
+
* @param linkFieldOrDocument - Any kind of Link field or a document to resolve
|
|
96
|
+
* @param linkResolver - An optional Link Resolver function. Without it, you are
|
|
97
|
+
* expected to use the `routes` options from the API
|
|
98
|
+
*
|
|
99
|
+
* @returns Resolved URL or, if the provided Link field or document is empty, `null`
|
|
100
|
+
* @see Prismic Link Resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
|
|
101
|
+
* @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}
|
|
102
|
+
*
|
|
103
|
+
* @deprecated Use object-style configuration instead.
|
|
104
|
+
*
|
|
105
|
+
* ```ts
|
|
106
|
+
* asLink(field);
|
|
107
|
+
* asLink(field, { linkResolver });
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
<
|
|
111
|
+
LinkResolverFunctionReturnType = string | null | undefined,
|
|
112
|
+
Field extends LinkField | PrismicDocument | null | undefined =
|
|
113
|
+
| LinkField
|
|
114
|
+
| PrismicDocument
|
|
115
|
+
| null
|
|
116
|
+
| undefined,
|
|
117
|
+
>(
|
|
118
|
+
linkFieldOrDocument: Field,
|
|
119
|
+
...config: AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>
|
|
120
|
+
): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;
|
|
121
|
+
} = <
|
|
55
122
|
LinkResolverFunctionReturnType = string | null | undefined,
|
|
56
123
|
Field extends LinkField | PrismicDocument | null | undefined =
|
|
57
124
|
| LinkField
|
|
@@ -60,7 +127,10 @@ export const asLink = <
|
|
|
60
127
|
| undefined,
|
|
61
128
|
>(
|
|
62
129
|
linkFieldOrDocument: Field,
|
|
63
|
-
|
|
130
|
+
// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.
|
|
131
|
+
...configObjectOrTuple:
|
|
132
|
+
| [config?: AsLinkConfig<LinkResolverFunctionReturnType>]
|
|
133
|
+
| AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>
|
|
64
134
|
): AsLinkReturnType<LinkResolverFunctionReturnType, Field> => {
|
|
65
135
|
if (!linkFieldOrDocument) {
|
|
66
136
|
return null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;
|
|
@@ -78,6 +148,20 @@ export const asLink = <
|
|
|
78
148
|
: documentToLinkField(linkFieldOrDocument)
|
|
79
149
|
) as LinkField;
|
|
80
150
|
|
|
151
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
152
|
+
const [configObjectOrLinkResolver] = configObjectOrTuple;
|
|
153
|
+
let config: AsLinkConfig<LinkResolverFunctionReturnType>;
|
|
154
|
+
if (
|
|
155
|
+
typeof configObjectOrLinkResolver === "function" ||
|
|
156
|
+
configObjectOrLinkResolver == null
|
|
157
|
+
) {
|
|
158
|
+
config = {
|
|
159
|
+
linkResolver: configObjectOrLinkResolver,
|
|
160
|
+
};
|
|
161
|
+
} else {
|
|
162
|
+
config = { ...configObjectOrLinkResolver };
|
|
163
|
+
}
|
|
164
|
+
|
|
81
165
|
switch (linkField.link_type) {
|
|
82
166
|
case LinkType.Media:
|
|
83
167
|
case LinkType.Web:
|
|
@@ -87,9 +171,9 @@ export const asLink = <
|
|
|
87
171
|
>;
|
|
88
172
|
|
|
89
173
|
case LinkType.Document: {
|
|
90
|
-
if ("id" in linkField && linkResolver) {
|
|
174
|
+
if ("id" in linkField && config.linkResolver) {
|
|
91
175
|
// When using Link Resolver...
|
|
92
|
-
const resolvedURL = linkResolver(linkField);
|
|
176
|
+
const resolvedURL = config.linkResolver(linkField);
|
|
93
177
|
|
|
94
178
|
if (resolvedURL != null) {
|
|
95
179
|
return resolvedURL as AsLinkReturnType<
|
package/src/helpers/asText.ts
CHANGED
|
@@ -2,27 +2,86 @@ import { asText as baseAsText } from "@prismicio/richtext";
|
|
|
2
2
|
|
|
3
3
|
import { RichTextField } from "../types/value/richText";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Configuration that determines the output of `asText()`.
|
|
7
|
+
*/
|
|
8
|
+
type AsTextConfig = {
|
|
9
|
+
/**
|
|
10
|
+
* Separator used to join each element.
|
|
11
|
+
*
|
|
12
|
+
* @defaultValue ` ` (a space)
|
|
13
|
+
*/
|
|
14
|
+
separator?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated Use object-style configuration instead.
|
|
20
|
+
*/
|
|
21
|
+
type AsTextDeprecatedTupleConfig = [separator?: string];
|
|
22
|
+
|
|
5
23
|
/**
|
|
6
24
|
* The return type of `asText()`.
|
|
7
25
|
*/
|
|
8
26
|
type AsTextReturnType<Field extends RichTextField | null | undefined> =
|
|
9
27
|
Field extends RichTextField ? string : null;
|
|
10
28
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
29
|
+
export const asText: {
|
|
30
|
+
/**
|
|
31
|
+
* Serializes a Rich Text or Title field to a plain text string.
|
|
32
|
+
*
|
|
33
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
34
|
+
* @param config - Configuration that determines the output of `asText()`
|
|
35
|
+
*
|
|
36
|
+
* @returns Plain text equivalent of the provided Rich Text or Title field
|
|
37
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
38
|
+
*/
|
|
39
|
+
<Field extends RichTextField | null | undefined>(
|
|
40
|
+
richTextField: Field,
|
|
41
|
+
config?: AsTextConfig,
|
|
42
|
+
): AsTextReturnType<Field>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Serializes a Rich Text or Title field to a plain text string.
|
|
46
|
+
*
|
|
47
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
48
|
+
* @param separator - Separator used to join each element, defaults to a space
|
|
49
|
+
*
|
|
50
|
+
* @returns Plain text equivalent of the provided Rich Text or Title field
|
|
51
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
52
|
+
*
|
|
53
|
+
* @deprecated Use object-style configuration instead.
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* asText(field);
|
|
57
|
+
* asText(field, { separator });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
<Field extends RichTextField | null | undefined>(
|
|
61
|
+
richTextField: Field,
|
|
62
|
+
...config: AsTextDeprecatedTupleConfig
|
|
63
|
+
): AsTextReturnType<Field>;
|
|
64
|
+
} = <Field extends RichTextField | null | undefined>(
|
|
21
65
|
richTextField: Field,
|
|
22
|
-
|
|
66
|
+
// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.
|
|
67
|
+
...configObjectOrTuple: [config?: AsTextConfig] | AsTextDeprecatedTupleConfig
|
|
23
68
|
): AsTextReturnType<Field> => {
|
|
24
69
|
if (richTextField) {
|
|
25
|
-
|
|
70
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
71
|
+
const [configObjectOrSeparator] = configObjectOrTuple;
|
|
72
|
+
let config: AsTextConfig;
|
|
73
|
+
if (typeof configObjectOrSeparator === "string") {
|
|
74
|
+
config = {
|
|
75
|
+
separator: configObjectOrSeparator,
|
|
76
|
+
};
|
|
77
|
+
} else {
|
|
78
|
+
config = { ...configObjectOrSeparator };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return baseAsText(
|
|
82
|
+
richTextField,
|
|
83
|
+
config.separator,
|
|
84
|
+
) as AsTextReturnType<Field>;
|
|
26
85
|
} else {
|
|
27
86
|
return null as AsTextReturnType<Field>;
|
|
28
87
|
}
|