@prismicio/client 7.7.2 → 7.7.4
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 +3 -3
- package/dist/buildQueryURL.cjs.map +1 -1
- package/dist/buildQueryURL.d.ts +1 -1
- package/dist/buildQueryURL.js.map +1 -1
- package/dist/cookie.cjs.map +1 -1
- package/dist/cookie.js.map +1 -1
- package/dist/createClient.cjs +31 -31
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +35 -35
- package/dist/createClient.js +31 -31
- package/dist/createClient.js.map +1 -1
- package/dist/errors/ForbiddenError.cjs.map +1 -1
- package/dist/errors/ForbiddenError.js.map +1 -1
- package/dist/errors/NotFoundError.cjs.map +1 -1
- package/dist/errors/NotFoundError.js.map +1 -1
- package/dist/errors/ParsingError.cjs.map +1 -1
- package/dist/errors/ParsingError.js.map +1 -1
- package/dist/errors/PreviewTokenExpired.cjs.map +1 -1
- package/dist/errors/PreviewTokenExpired.js.map +1 -1
- package/dist/errors/PrismicError.cjs.map +1 -1
- package/dist/errors/PrismicError.js.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
- package/dist/errors/RefExpiredError.cjs.map +1 -1
- package/dist/errors/RefExpiredError.js.map +1 -1
- package/dist/errors/RefNotFoundError.cjs.map +1 -1
- package/dist/errors/RefNotFoundError.js.map +1 -1
- package/dist/errors/RepositoryNotFoundError.cjs.map +1 -1
- package/dist/errors/RepositoryNotFoundError.js.map +1 -1
- package/dist/filter.cjs.map +1 -1
- package/dist/filter.js.map +1 -1
- package/dist/getGraphQLEndpoint.cjs.map +1 -1
- package/dist/getGraphQLEndpoint.js.map +1 -1
- package/dist/getRepositoryEndpoint.cjs.map +1 -1
- package/dist/getRepositoryEndpoint.js.map +1 -1
- package/dist/getRepositoryName.cjs.map +1 -1
- package/dist/getRepositoryName.js.map +1 -1
- package/dist/getToolbarSrc.cjs.map +1 -1
- package/dist/getToolbarSrc.js.map +1 -1
- package/dist/helpers/asDate.cjs.map +1 -1
- package/dist/helpers/asDate.js.map +1 -1
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.d.ts +2 -2
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.d.ts +2 -2
- package/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
- package/dist/helpers/asImageSrc.cjs.map +1 -1
- package/dist/helpers/asImageSrc.d.ts +2 -2
- package/dist/helpers/asImageSrc.js.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +2 -2
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLink.cjs.map +1 -1
- package/dist/helpers/asLink.d.ts +2 -4
- package/dist/helpers/asLink.js.map +1 -1
- package/dist/helpers/asLinkAttrs.cjs.map +1 -1
- package/dist/helpers/asLinkAttrs.d.ts +2 -2
- package/dist/helpers/asLinkAttrs.js.map +1 -1
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.d.ts +1 -1
- package/dist/helpers/asText.js.map +1 -1
- package/dist/helpers/documentToLinkField.cjs.map +1 -1
- package/dist/helpers/documentToLinkField.js.map +1 -1
- package/dist/helpers/isFilled.cjs.map +1 -1
- package/dist/helpers/isFilled.js.map +1 -1
- package/dist/helpers/mapSliceZone.cjs.map +1 -1
- package/dist/helpers/mapSliceZone.d.ts +1 -1
- package/dist/helpers/mapSliceZone.js.map +1 -1
- package/dist/helpers/unstable_htmlAsRichText.cjs.map +1 -1
- package/dist/helpers/unstable_htmlAsRichText.d.ts +4 -4
- package/dist/helpers/unstable_htmlAsRichText.js.map +1 -1
- package/dist/helpers/unstable_markdownAsRichText.cjs.map +1 -1
- package/dist/helpers/unstable_markdownAsRichText.d.ts +2 -2
- package/dist/helpers/unstable_markdownAsRichText.js.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/isRepositoryEndpoint.cjs.map +1 -1
- package/dist/isRepositoryEndpoint.js.map +1 -1
- package/dist/isRepositoryName.cjs.map +1 -1
- package/dist/isRepositoryName.js.map +1 -1
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.ts +1 -1
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/lib/appendFilters.cjs.map +1 -1
- package/dist/lib/appendFilters.js.map +1 -1
- package/dist/lib/castArray.cjs.map +1 -1
- package/dist/lib/castArray.js.map +1 -1
- package/dist/lib/castThunk.cjs.map +1 -1
- package/dist/lib/castThunk.js.map +1 -1
- package/dist/lib/devMsg.cjs.map +1 -1
- package/dist/lib/devMsg.d.ts +1 -1
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/lib/escapeHTML.cjs.map +1 -1
- package/dist/lib/escapeHTML.js.map +1 -1
- package/dist/lib/everyTagFilter.cjs.map +1 -1
- package/dist/lib/everyTagFilter.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs.map +1 -1
- package/dist/lib/filterRichTextField.d.ts +3 -3
- package/dist/lib/filterRichTextField.js.map +1 -1
- package/dist/lib/findMasterRef.cjs.map +1 -1
- package/dist/lib/findMasterRef.d.ts +1 -1
- package/dist/lib/findMasterRef.js.map +1 -1
- package/dist/lib/findRef.cjs.map +1 -1
- package/dist/lib/findRef.d.ts +1 -1
- package/dist/lib/findRef.js.map +1 -1
- package/dist/lib/findRefByID.cjs.map +1 -1
- package/dist/lib/findRefByID.d.ts +1 -1
- package/dist/lib/findRefByID.js.map +1 -1
- package/dist/lib/findRefByLabel.cjs.map +1 -1
- package/dist/lib/findRefByLabel.d.ts +1 -1
- package/dist/lib/findRefByLabel.js.map +1 -1
- package/dist/lib/getPreviewCookie.cjs.map +1 -1
- package/dist/lib/getPreviewCookie.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
- package/dist/lib/hastSerializerHelpers.d.ts +3 -3
- package/dist/lib/hastSerializerHelpers.js.map +1 -1
- package/dist/lib/hastToRichText.cjs +3 -4
- package/dist/lib/hastToRichText.cjs.map +1 -1
- package/dist/lib/hastToRichText.d.ts +4 -4
- package/dist/lib/hastToRichText.js +3 -4
- package/dist/lib/hastToRichText.js.map +1 -1
- package/dist/lib/isInternalURL.cjs.map +1 -1
- package/dist/lib/isInternalURL.js.map +1 -1
- package/dist/lib/minifyGraphQLQuery.cjs.map +1 -1
- package/dist/lib/minifyGraphQLQuery.js.map +1 -1
- package/dist/lib/rehypeRichText.cjs.map +1 -1
- package/dist/lib/rehypeRichText.d.ts +5 -5
- package/dist/lib/rehypeRichText.js.map +1 -1
- package/dist/lib/serializerHelpers.cjs.map +1 -1
- package/dist/lib/serializerHelpers.d.ts +2 -2
- package/dist/lib/serializerHelpers.js.map +1 -1
- package/dist/lib/someTagsFilter.cjs.map +1 -1
- package/dist/lib/someTagsFilter.js.map +1 -1
- package/dist/lib/typeFilter.cjs.map +1 -1
- package/dist/lib/typeFilter.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/richtext/asText.cjs.map +1 -1
- package/dist/richtext/asText.d.ts +1 -1
- package/dist/richtext/asText.js.map +1 -1
- package/dist/richtext/asTree.cjs.map +1 -1
- package/dist/richtext/asTree.d.ts +2 -2
- package/dist/richtext/asTree.js.map +1 -1
- package/dist/richtext/composeSerializers.cjs.map +1 -1
- package/dist/richtext/composeSerializers.d.ts +1 -1
- package/dist/richtext/composeSerializers.js.map +1 -1
- package/dist/richtext/serialize.cjs.map +1 -1
- package/dist/richtext/serialize.d.ts +1 -1
- package/dist/richtext/serialize.js.map +1 -1
- package/dist/richtext/types.cjs.map +1 -1
- package/dist/richtext/types.d.ts +1 -1
- package/dist/richtext/types.js.map +1 -1
- package/dist/richtext/wrapMapSerializer.cjs.map +1 -1
- package/dist/richtext/wrapMapSerializer.d.ts +1 -1
- package/dist/richtext/wrapMapSerializer.js.map +1 -1
- package/dist/types/api/query.d.ts +1 -1
- package/dist/types/api/release.d.ts +1 -1
- package/dist/types/api/repository.d.ts +1 -1
- package/dist/types/model/customType.d.ts +1 -1
- package/dist/types/model/link.cjs.map +1 -1
- package/dist/types/model/link.js.map +1 -1
- package/dist/types/model/slice.cjs.map +1 -1
- package/dist/types/model/slice.js.map +1 -1
- package/dist/types/model/sliceZone.cjs.map +1 -1
- package/dist/types/model/sliceZone.js.map +1 -1
- package/dist/types/model/title.d.ts +1 -1
- package/dist/types/model/types.cjs.map +1 -1
- package/dist/types/model/types.js.map +1 -1
- package/dist/types/value/document.d.ts +4 -4
- package/dist/types/value/embed.cjs.map +1 -1
- package/dist/types/value/embed.js.map +1 -1
- package/dist/types/value/link.cjs.map +1 -1
- package/dist/types/value/link.js.map +1 -1
- package/dist/types/value/richText.cjs.map +1 -1
- package/dist/types/value/richText.js.map +1 -1
- package/dist/types/webhook/apiUpdate.d.ts +1 -1
- package/dist/types/webhook/testTrigger.d.ts +1 -1
- package/dist/types/webhook/types.cjs.map +1 -1
- package/dist/types/webhook/types.d.ts +2 -2
- package/dist/types/webhook/types.js.map +1 -1
- package/package.json +1 -1
- package/src/buildQueryURL.ts +51 -51
- package/src/cookie.ts +1 -1
- package/src/createClient.ts +293 -293
- package/src/errors/ForbiddenError.ts +6 -6
- package/src/errors/NotFoundError.ts +1 -1
- package/src/errors/ParsingError.ts +8 -8
- package/src/errors/PreviewTokenExpired.ts +4 -4
- package/src/errors/PrismicError.ts +5 -5
- package/src/errors/PrismicRichTextSerializerError.ts +1 -1
- package/src/errors/RefExpiredError.ts +4 -4
- package/src/errors/RefNotFoundError.ts +4 -4
- package/src/errors/RepositoryNotFoundError.ts +1 -1
- package/src/filter.ts +22 -22
- package/src/getGraphQLEndpoint.ts +5 -5
- package/src/getRepositoryEndpoint.ts +5 -5
- package/src/getRepositoryName.ts +4 -4
- package/src/getToolbarSrc.ts +5 -5
- package/src/helpers/asDate.ts +7 -7
- package/src/helpers/asHTML.ts +54 -56
- package/src/helpers/asImagePixelDensitySrcSet.ts +14 -17
- package/src/helpers/asImageSrc.ts +9 -8
- package/src/helpers/asImageWidthSrcSet.ts +19 -22
- package/src/helpers/asLink.ts +26 -27
- package/src/helpers/asLinkAttrs.ts +30 -29
- package/src/helpers/asText.ts +15 -15
- package/src/helpers/documentToLinkField.ts +6 -6
- package/src/helpers/isFilled.ts +53 -53
- package/src/helpers/mapSliceZone.ts +34 -34
- package/src/helpers/unstable_htmlAsRichText.ts +23 -22
- package/src/helpers/unstable_markdownAsRichText.ts +14 -13
- package/src/index.ts +119 -119
- package/src/isRepositoryEndpoint.ts +4 -4
- package/src/isRepositoryName.ts +2 -2
- package/src/lib/RichTextFieldBuilder.ts +36 -41
- package/src/lib/appendFilters.ts +4 -4
- package/src/lib/castArray.ts +2 -2
- package/src/lib/castThunk.ts +2 -2
- package/src/lib/devMsg.ts +4 -4
- package/src/lib/escapeHTML.ts +24 -24
- package/src/lib/everyTagFilter.ts +4 -4
- package/src/lib/filterRichTextField.ts +31 -31
- package/src/lib/findMasterRef.ts +4 -4
- package/src/lib/findRef.ts +6 -6
- package/src/lib/findRefByID.ts +4 -4
- package/src/lib/findRefByLabel.ts +4 -4
- package/src/lib/getPreviewCookie.ts +11 -11
- package/src/lib/hastSerializerHelpers.ts +41 -37
- package/src/lib/hastToRichText.ts +74 -73
- package/src/lib/isInternalURL.ts +4 -4
- package/src/lib/minifyGraphQLQuery.ts +2 -2
- package/src/lib/rehypeRichText.ts +42 -41
- package/src/lib/serializerHelpers.ts +53 -54
- package/src/lib/someTagsFilter.ts +4 -4
- package/src/lib/typeFilter.ts +3 -3
- package/src/richtext/asText.ts +5 -5
- package/src/richtext/asTree.ts +53 -53
- package/src/richtext/composeSerializers.ts +6 -6
- package/src/richtext/index.ts +7 -7
- package/src/richtext/serialize.ts +11 -11
- package/src/richtext/types.ts +38 -38
- package/src/richtext/wrapMapSerializer.ts +6 -9
- package/src/types/api/query.ts +9 -9
- package/src/types/api/ref.ts +5 -5
- package/src/types/api/release.ts +2 -2
- package/src/types/api/repository.ts +24 -24
- package/src/types/api/tags.ts +1 -1
- package/src/types/model/boolean.ts +7 -7
- package/src/types/model/color.ts +5 -5
- package/src/types/model/contentRelationship.ts +9 -9
- package/src/types/model/customType.ts +9 -9
- package/src/types/model/date.ts +6 -6
- package/src/types/model/embed.ts +5 -5
- package/src/types/model/geoPoint.ts +4 -4
- package/src/types/model/group.ts +6 -6
- package/src/types/model/image.ts +9 -9
- package/src/types/model/integration.ts +6 -6
- package/src/types/model/keyText.ts +5 -5
- package/src/types/model/link.ts +8 -8
- package/src/types/model/linkToMedia.ts +7 -7
- package/src/types/model/number.ts +7 -7
- package/src/types/model/range.ts +8 -8
- package/src/types/model/richText.ts +16 -16
- package/src/types/model/select.ts +7 -7
- package/src/types/model/separator.ts +4 -4
- package/src/types/model/sharedSlice.ts +7 -7
- package/src/types/model/sharedSliceVariation.ts +9 -9
- package/src/types/model/slice.ts +12 -12
- package/src/types/model/sliceZone.ts +11 -11
- package/src/types/model/timestamp.ts +6 -6
- package/src/types/model/title.ts +2 -2
- package/src/types/model/types.ts +26 -26
- package/src/types/model/uid.ts +5 -5
- package/src/types/value/boolean.ts +1 -1
- package/src/types/value/color.ts +2 -2
- package/src/types/value/contentRelationship.ts +15 -15
- package/src/types/value/date.ts +2 -2
- package/src/types/value/document.ts +23 -23
- package/src/types/value/embed.ts +33 -33
- package/src/types/value/geoPoint.ts +4 -4
- package/src/types/value/group.ts +3 -3
- package/src/types/value/image.ts +21 -21
- package/src/types/value/integration.ts +2 -2
- package/src/types/value/keyText.ts +2 -2
- package/src/types/value/link.ts +12 -12
- package/src/types/value/linkToMedia.ts +11 -11
- package/src/types/value/number.ts +2 -2
- package/src/types/value/richText.ts +58 -58
- package/src/types/value/select.ts +2 -2
- package/src/types/value/sharedSlice.ts +5 -5
- package/src/types/value/sharedSliceVariation.ts +5 -5
- package/src/types/value/slice.ts +6 -6
- package/src/types/value/sliceZone.ts +4 -4
- package/src/types/value/timestamp.ts +3 -3
- package/src/types/value/title.ts +4 -4
- package/src/types/value/types.ts +22 -22
- package/src/types/webhook/apiUpdate.ts +18 -18
- package/src/types/webhook/testTrigger.ts +2 -2
- package/src/types/webhook/types.ts +8 -8
- package/dist/_node_modules/hast-util-whitespace/lib/index.cjs +0 -11
- package/dist/_node_modules/hast-util-whitespace/lib/index.cjs.map +0 -1
- package/dist/_node_modules/hast-util-whitespace/lib/index.js +0 -11
- package/dist/_node_modules/hast-util-whitespace/lib/index.js.map +0 -1
|
@@ -1,32 +1,31 @@
|
|
|
1
|
-
import { Element, Root } from "hast"
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { VFile } from "vfile";
|
|
1
|
+
import type { Element, Root } from "hast"
|
|
2
|
+
import { toString } from "mdast-util-to-string"
|
|
3
|
+
import { visit } from "unist-util-visit"
|
|
4
|
+
import type { VFile } from "vfile"
|
|
6
5
|
|
|
7
|
-
import {
|
|
6
|
+
import type {
|
|
8
7
|
RTBlockNode,
|
|
9
8
|
RTInlineNode,
|
|
10
9
|
RTLabelNode,
|
|
11
10
|
RTTextNode,
|
|
12
11
|
RichTextField,
|
|
13
|
-
|
|
14
|
-
} from "../types/value/richText"
|
|
12
|
+
} from "../types/value/richText"
|
|
13
|
+
import { RichTextNodeType } from "../types/value/richText"
|
|
15
14
|
|
|
16
|
-
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError"
|
|
15
|
+
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError"
|
|
17
16
|
|
|
18
|
-
import {
|
|
17
|
+
import type {
|
|
19
18
|
RichTextHTMLMapSerializer,
|
|
20
19
|
RichTextHTMLMapSerializerFunction,
|
|
21
20
|
RichTextHTMLMapSerializerShorthand,
|
|
22
|
-
} from "../helpers/unstable_htmlAsRichText"
|
|
21
|
+
} from "../helpers/unstable_htmlAsRichText"
|
|
23
22
|
|
|
24
|
-
import { RichTextFieldBuilder } from "./RichTextFieldBuilder"
|
|
23
|
+
import { RichTextFieldBuilder } from "./RichTextFieldBuilder"
|
|
25
24
|
import {
|
|
26
25
|
serializeEmbed,
|
|
27
26
|
serializeImage,
|
|
28
27
|
serializeSpan,
|
|
29
|
-
} from "./hastSerializerHelpers"
|
|
28
|
+
} from "./hastSerializerHelpers"
|
|
30
29
|
|
|
31
30
|
/**
|
|
32
31
|
* Pick keys from a type, distributing the operation over a union.
|
|
@@ -38,7 +37,7 @@ import {
|
|
|
38
37
|
type DistributedPick<
|
|
39
38
|
ObjectType,
|
|
40
39
|
KeyType extends keyof ObjectType,
|
|
41
|
-
> = ObjectType extends unknown ? Pick<ObjectType, KeyType> : never
|
|
40
|
+
> = ObjectType extends unknown ? Pick<ObjectType, KeyType> : never
|
|
42
41
|
|
|
43
42
|
const DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {
|
|
44
43
|
h1: "heading1",
|
|
@@ -58,10 +57,10 @@ const DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {
|
|
|
58
57
|
img: "image",
|
|
59
58
|
iframe: "embed",
|
|
60
59
|
a: "hyperlink",
|
|
61
|
-
}
|
|
60
|
+
}
|
|
62
61
|
|
|
63
|
-
const VFILE_RULE = "failed-to-serialize-node"
|
|
64
|
-
const VFILE_SOURCE = "prismic"
|
|
62
|
+
const VFILE_RULE = "failed-to-serialize-node"
|
|
63
|
+
const VFILE_SOURCE = "prismic"
|
|
65
64
|
|
|
66
65
|
/**
|
|
67
66
|
* Configuration that determines the output of `toRichText`.
|
|
@@ -71,15 +70,15 @@ export type HASTToRichTextConfig = {
|
|
|
71
70
|
* An optional HTML to rich text serializer. Will be merged with the default
|
|
72
71
|
* HTML to rich text serializer.
|
|
73
72
|
*/
|
|
74
|
-
serializer?: RichTextHTMLMapSerializer
|
|
73
|
+
serializer?: RichTextHTMLMapSerializer
|
|
75
74
|
|
|
76
75
|
/**
|
|
77
76
|
* Whether or not the text processed should be marked as right-to-left.
|
|
78
77
|
*
|
|
79
78
|
* @defaultValue `false`
|
|
80
79
|
*/
|
|
81
|
-
direction?: "ltr" | "rtl"
|
|
82
|
-
}
|
|
80
|
+
direction?: "ltr" | "rtl"
|
|
81
|
+
}
|
|
83
82
|
|
|
84
83
|
/**
|
|
85
84
|
* Transfor a hast tree to a rich text field.
|
|
@@ -95,29 +94,29 @@ export const hastToRichText = (
|
|
|
95
94
|
file: VFile,
|
|
96
95
|
config?: HASTToRichTextConfig,
|
|
97
96
|
): RichTextField => {
|
|
98
|
-
const builder = new RichTextFieldBuilder()
|
|
97
|
+
const builder = new RichTextFieldBuilder()
|
|
99
98
|
|
|
100
99
|
// Merge the default serializer with the user-provided one.
|
|
101
100
|
const serializer = {
|
|
102
101
|
...DEFAULT_SERIALIZER,
|
|
103
102
|
...config?.serializer,
|
|
104
|
-
}
|
|
103
|
+
}
|
|
105
104
|
|
|
106
105
|
// Keep track of the last text node type to append text nodes to in
|
|
107
106
|
// case of an image or an embed node is present inside a paragraph.
|
|
108
|
-
let lastRTTextNodeType: RTTextNode["type"] = RichTextNodeType.paragraph
|
|
107
|
+
let lastRTTextNodeType: RTTextNode["type"] = RichTextNodeType.paragraph
|
|
109
108
|
|
|
110
109
|
// Keep track of the last list type to know whether we need to append
|
|
111
110
|
// `list-item` or `o-list-item` nodes.
|
|
112
111
|
let lastListType: "group-list-item" | "group-o-list-item" | undefined =
|
|
113
|
-
undefined
|
|
112
|
+
undefined
|
|
114
113
|
|
|
115
114
|
visit(tree, (node) => {
|
|
116
115
|
if (node.type === "element") {
|
|
117
116
|
// Transforms line break elements to line breaks
|
|
118
117
|
if (node.tagName === "br") {
|
|
119
118
|
try {
|
|
120
|
-
builder.appendText("\n")
|
|
119
|
+
builder.appendText("\n")
|
|
121
120
|
} catch (error) {
|
|
122
121
|
// noop
|
|
123
122
|
}
|
|
@@ -126,27 +125,27 @@ export const hastToRichText = (
|
|
|
126
125
|
// Resolves the serializer responsible for the current node.
|
|
127
126
|
let serializerOrShorthand:
|
|
128
127
|
| RichTextHTMLMapSerializerShorthand
|
|
129
|
-
| RichTextHTMLMapSerializerFunction
|
|
128
|
+
| RichTextHTMLMapSerializerFunction
|
|
130
129
|
|
|
131
130
|
// We give priority to CSS selectors over tag names.
|
|
132
131
|
if (node.matchesSerializer && node.matchesSerializer in serializer) {
|
|
133
|
-
serializerOrShorthand = serializer[node.matchesSerializer]
|
|
132
|
+
serializerOrShorthand = serializer[node.matchesSerializer]
|
|
134
133
|
} else if (node.tagName in serializer) {
|
|
135
|
-
serializerOrShorthand = serializer[node.tagName]
|
|
134
|
+
serializerOrShorthand = serializer[node.tagName]
|
|
136
135
|
} else {
|
|
137
|
-
return
|
|
136
|
+
return
|
|
138
137
|
}
|
|
139
138
|
|
|
140
|
-
let shorthand: RichTextHTMLMapSerializerShorthand
|
|
139
|
+
let shorthand: RichTextHTMLMapSerializerShorthand
|
|
141
140
|
if (typeof serializerOrShorthand === "function") {
|
|
142
141
|
const shorthandOrNode = serializerOrShorthand({
|
|
143
142
|
node,
|
|
144
143
|
context: { listType: lastListType },
|
|
145
|
-
})
|
|
144
|
+
})
|
|
146
145
|
|
|
147
146
|
if (!shorthandOrNode) {
|
|
148
147
|
// Exit on unhandled node.
|
|
149
|
-
return
|
|
148
|
+
return
|
|
150
149
|
} else if (
|
|
151
150
|
typeof shorthandOrNode === "object" &&
|
|
152
151
|
"type" in shorthandOrNode
|
|
@@ -157,39 +156,39 @@ export const hastToRichText = (
|
|
|
157
156
|
case RichTextNodeType.em:
|
|
158
157
|
case RichTextNodeType.label:
|
|
159
158
|
case RichTextNodeType.hyperlink: {
|
|
160
|
-
const length = toString(node).length
|
|
159
|
+
const length = toString(node).trimEnd().length
|
|
161
160
|
|
|
162
161
|
try {
|
|
163
|
-
builder.appendSpan(shorthandOrNode, length)
|
|
162
|
+
builder.appendSpan(shorthandOrNode, length)
|
|
164
163
|
} catch (error) {
|
|
165
164
|
// Happens when we extract an image/embed node inside an RTTextNode and that
|
|
166
165
|
// the next children is a span. The last RT node type is then an image/embed
|
|
167
166
|
// node, so we need to resume a new RT text node.
|
|
168
|
-
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
169
|
-
builder.appendSpan(shorthandOrNode, length)
|
|
167
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
168
|
+
builder.appendSpan(shorthandOrNode, length)
|
|
170
169
|
}
|
|
171
170
|
|
|
172
|
-
return
|
|
171
|
+
return
|
|
173
172
|
}
|
|
174
173
|
|
|
175
174
|
case RichTextNodeType.image:
|
|
176
175
|
case RichTextNodeType.embed:
|
|
177
|
-
builder.appendNode(shorthandOrNode)
|
|
176
|
+
builder.appendNode(shorthandOrNode)
|
|
178
177
|
|
|
179
|
-
return
|
|
178
|
+
return
|
|
180
179
|
|
|
181
180
|
default:
|
|
182
|
-
lastRTTextNodeType = shorthandOrNode.type
|
|
183
|
-
builder.appendNode(shorthandOrNode)
|
|
181
|
+
lastRTTextNodeType = shorthandOrNode.type
|
|
182
|
+
builder.appendNode(shorthandOrNode)
|
|
184
183
|
|
|
185
|
-
return
|
|
184
|
+
return
|
|
186
185
|
}
|
|
187
186
|
}
|
|
188
187
|
|
|
189
188
|
// Else it's a shorthand.
|
|
190
|
-
shorthand = shorthandOrNode
|
|
189
|
+
shorthand = shorthandOrNode
|
|
191
190
|
} else {
|
|
192
|
-
shorthand = serializerOrShorthand
|
|
191
|
+
shorthand = serializerOrShorthand
|
|
193
192
|
}
|
|
194
193
|
|
|
195
194
|
let match:
|
|
@@ -197,11 +196,11 @@ export const hastToRichText = (
|
|
|
197
196
|
RTBlockNode | Exclude<RTInlineNode, RTLabelNode>,
|
|
198
197
|
"type"
|
|
199
198
|
>
|
|
200
|
-
| { type: "label"; data: { label: string } }
|
|
199
|
+
| { type: "label"; data: { label: string } }
|
|
201
200
|
if (typeof shorthand === "string") {
|
|
202
|
-
match = { type: shorthand }
|
|
201
|
+
match = { type: shorthand }
|
|
203
202
|
} else {
|
|
204
|
-
match = { type: RichTextNodeType.label, data: shorthand }
|
|
203
|
+
match = { type: RichTextNodeType.label, data: shorthand }
|
|
205
204
|
}
|
|
206
205
|
|
|
207
206
|
try {
|
|
@@ -216,40 +215,40 @@ export const hastToRichText = (
|
|
|
216
215
|
case RichTextNodeType.preformatted:
|
|
217
216
|
case RichTextNodeType.listItem:
|
|
218
217
|
case RichTextNodeType.oListItem:
|
|
219
|
-
lastRTTextNodeType = match.type
|
|
220
|
-
builder.appendTextNode(match.type, config?.direction)
|
|
221
|
-
break
|
|
218
|
+
lastRTTextNodeType = match.type
|
|
219
|
+
builder.appendTextNode(match.type, config?.direction)
|
|
220
|
+
break
|
|
222
221
|
|
|
223
222
|
case RichTextNodeType.list:
|
|
224
223
|
case RichTextNodeType.oList:
|
|
225
|
-
lastListType = match.type
|
|
226
|
-
break
|
|
224
|
+
lastListType = match.type
|
|
225
|
+
break
|
|
227
226
|
|
|
228
227
|
case RichTextNodeType.image:
|
|
229
|
-
builder.appendNode(serializeImage(node))
|
|
230
|
-
break
|
|
228
|
+
builder.appendNode(serializeImage(node))
|
|
229
|
+
break
|
|
231
230
|
|
|
232
231
|
case RichTextNodeType.embed:
|
|
233
|
-
builder.appendNode(serializeEmbed(node))
|
|
234
|
-
break
|
|
232
|
+
builder.appendNode(serializeEmbed(node))
|
|
233
|
+
break
|
|
235
234
|
|
|
236
235
|
case RichTextNodeType.strong:
|
|
237
236
|
case RichTextNodeType.em:
|
|
238
237
|
case RichTextNodeType.label:
|
|
239
238
|
case RichTextNodeType.hyperlink: {
|
|
240
|
-
const span = serializeSpan(node, match)
|
|
241
|
-
const length = toString(node).length
|
|
239
|
+
const span = serializeSpan(node, match)
|
|
240
|
+
const length = toString(node).trimEnd().length
|
|
242
241
|
|
|
243
242
|
try {
|
|
244
|
-
builder.appendSpan(span, length)
|
|
243
|
+
builder.appendSpan(span, length)
|
|
245
244
|
} catch (error) {
|
|
246
245
|
// Happens when we extract an image/embed node inside an RTTextNode and that
|
|
247
246
|
// the next children is a span. The last RT node type is then an image/embed
|
|
248
247
|
// node, so we need to resume a new RT text node.
|
|
249
|
-
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
250
|
-
builder.appendSpan(span, length)
|
|
248
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
249
|
+
builder.appendSpan(span, length)
|
|
251
250
|
}
|
|
252
|
-
break
|
|
251
|
+
break
|
|
253
252
|
}
|
|
254
253
|
|
|
255
254
|
default:
|
|
@@ -257,7 +256,7 @@ export const hastToRichText = (
|
|
|
257
256
|
`Unknown rich text node type: \`${
|
|
258
257
|
(match as { type: string }).type
|
|
259
258
|
}\``,
|
|
260
|
-
)
|
|
259
|
+
)
|
|
261
260
|
}
|
|
262
261
|
} catch (error) {
|
|
263
262
|
if (error instanceof PrismicRichTextSerializerError) {
|
|
@@ -266,29 +265,31 @@ export const hastToRichText = (
|
|
|
266
265
|
place: node.position,
|
|
267
266
|
ruleId: VFILE_RULE,
|
|
268
267
|
source: VFILE_SOURCE,
|
|
269
|
-
})
|
|
268
|
+
})
|
|
270
269
|
|
|
271
|
-
return
|
|
270
|
+
return
|
|
272
271
|
}
|
|
273
272
|
|
|
274
|
-
throw error
|
|
273
|
+
throw error
|
|
275
274
|
}
|
|
276
275
|
} else if (node.type === "text") {
|
|
277
|
-
|
|
276
|
+
// Inspired from `hast-util-whitespace`, see:
|
|
277
|
+
// https://github.com/syntax-tree/hast-util-whitespace/blob/main/lib/index.js
|
|
278
|
+
if (!(node.value.replace(/[ \t\n\f\r]/g, "") === "")) {
|
|
278
279
|
try {
|
|
279
|
-
builder.appendText(node.value)
|
|
280
|
+
builder.appendText(node.value)
|
|
280
281
|
} catch (error) {
|
|
281
282
|
// Happens when we extract an image/embed node inside an RTTextNode. The last RT
|
|
282
283
|
// node type is then an image/embed node, so we need to resume a new RT text node.
|
|
283
|
-
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
284
|
-
builder.appendText(node.value)
|
|
284
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
285
|
+
builder.appendText(node.value)
|
|
285
286
|
}
|
|
286
287
|
}
|
|
287
288
|
}
|
|
288
289
|
|
|
289
290
|
// We ignore the following node types:
|
|
290
291
|
// root, doctype, comment, raw
|
|
291
|
-
})
|
|
292
|
+
})
|
|
292
293
|
|
|
293
|
-
return builder.build()
|
|
294
|
-
}
|
|
294
|
+
return builder.build()
|
|
295
|
+
}
|
package/src/lib/isInternalURL.ts
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
// TODO: This does not detect all relative URLs as internal such as `about` or `./about`. This function assumes relative URLs start with a "/" or "#"`.
|
|
9
9
|
export const isInternalURL = (url: string): boolean => {
|
|
10
|
-
const isInternal = /^(\/(?!\/)|#)/.test(url)
|
|
11
|
-
const isSpecialLink = !isInternal && !/^https?:\/\//.test(url)
|
|
10
|
+
const isInternal = /^(\/(?!\/)|#)/.test(url)
|
|
11
|
+
const isSpecialLink = !isInternal && !/^https?:\/\//.test(url)
|
|
12
12
|
|
|
13
|
-
return isInternal && !isSpecialLink
|
|
14
|
-
}
|
|
13
|
+
return isInternal && !isSpecialLink
|
|
14
|
+
}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { Element, Root } from "hast"
|
|
2
|
-
import { select, selectAll } from "hast-util-select"
|
|
3
|
-
import rehypeMinifyWhitespace from "rehype-minify-whitespace"
|
|
4
|
-
import { Plugin, Processor } from "unified"
|
|
5
|
-
import { remove } from "unist-util-remove"
|
|
6
|
-
import { SKIP, visit } from "unist-util-visit"
|
|
7
|
-
import { VFile } from "vfile"
|
|
1
|
+
import type { Element, Root } from "hast"
|
|
2
|
+
import { select, selectAll } from "hast-util-select"
|
|
3
|
+
import rehypeMinifyWhitespace from "rehype-minify-whitespace"
|
|
4
|
+
import type { Plugin, Processor } from "unified"
|
|
5
|
+
import { remove } from "unist-util-remove"
|
|
6
|
+
import { SKIP, visit } from "unist-util-visit"
|
|
7
|
+
import type { VFile } from "vfile"
|
|
8
8
|
|
|
9
|
-
import { CustomTypeModelRichTextField } from "../types/model/richText"
|
|
10
|
-
import { RichTextField } from "../types/value/richText"
|
|
9
|
+
import type { CustomTypeModelRichTextField } from "../types/model/richText"
|
|
10
|
+
import type { RichTextField } from "../types/value/richText"
|
|
11
11
|
|
|
12
|
-
import { filterRichTextField } from "./filterRichTextField"
|
|
13
|
-
import { HASTToRichTextConfig
|
|
12
|
+
import { filterRichTextField } from "./filterRichTextField"
|
|
13
|
+
import type { HASTToRichTextConfig } from "./hastToRichText"
|
|
14
|
+
import { hastToRichText } from "./hastToRichText"
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Configuration options for {@link rehypeRichText}.
|
|
@@ -24,7 +25,7 @@ export type RehypeRichTextConfig = {
|
|
|
24
25
|
*
|
|
25
26
|
* @defaultValue `":root"` - The top-level element of the document.
|
|
26
27
|
*/
|
|
27
|
-
container?: string
|
|
28
|
+
container?: string
|
|
28
29
|
|
|
29
30
|
/**
|
|
30
31
|
* A list of CSS selectors to exclude matching nodes from the document to
|
|
@@ -34,7 +35,7 @@ export type RehypeRichTextConfig = {
|
|
|
34
35
|
*
|
|
35
36
|
* @defaultValue `[]` - No nodes are excluded.
|
|
36
37
|
*/
|
|
37
|
-
exclude?: string[]
|
|
38
|
+
exclude?: string[]
|
|
38
39
|
|
|
39
40
|
/**
|
|
40
41
|
* A list of CSS selectors to include only matching nodes from the document to
|
|
@@ -44,7 +45,7 @@ export type RehypeRichTextConfig = {
|
|
|
44
45
|
*
|
|
45
46
|
* @defaultValue `[]` - All nodes are included.
|
|
46
47
|
*/
|
|
47
|
-
include?: string[]
|
|
48
|
+
include?: string[]
|
|
48
49
|
|
|
49
50
|
/**
|
|
50
51
|
* A rich text or title field model definition. When provided the serializer
|
|
@@ -52,8 +53,8 @@ export type RehypeRichTextConfig = {
|
|
|
52
53
|
*
|
|
53
54
|
* @defaultValue `undefined` - No filtering is applied.
|
|
54
55
|
*/
|
|
55
|
-
model?: CustomTypeModelRichTextField
|
|
56
|
-
} & HASTToRichTextConfig
|
|
56
|
+
model?: CustomTypeModelRichTextField
|
|
57
|
+
} & HASTToRichTextConfig
|
|
57
58
|
|
|
58
59
|
/**
|
|
59
60
|
* A unified plugin that compiles a hast tree to a Prismic rich text field.
|
|
@@ -77,7 +78,7 @@ export const rehypeRichText: Plugin<
|
|
|
77
78
|
undefined,
|
|
78
79
|
Root,
|
|
79
80
|
RichTextField
|
|
80
|
-
|
|
81
|
+
>
|
|
81
82
|
|
|
82
83
|
// We need to exclude nodes _before_ we minify the tree as excluding
|
|
83
84
|
// nodes could end up in more whitespaces to trim.
|
|
@@ -85,48 +86,48 @@ export const rehypeRichText: Plugin<
|
|
|
85
86
|
return (tree: Root) => {
|
|
86
87
|
// Extract container node if any is specified.
|
|
87
88
|
if (config?.container) {
|
|
88
|
-
const element = select(config.container, tree)
|
|
89
|
+
const element = select(config.container, tree)
|
|
89
90
|
|
|
90
91
|
if (!element) {
|
|
91
92
|
throw new Error(
|
|
92
93
|
`No container matching \`${config?.container}\` could be found in the input AST.`,
|
|
93
|
-
)
|
|
94
|
+
)
|
|
94
95
|
}
|
|
95
96
|
|
|
96
97
|
// We cannot reassign the tree itself, so we instead replace
|
|
97
98
|
// its children with the found element.
|
|
98
|
-
tree.children = [element]
|
|
99
|
+
tree.children = [element]
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
// Remove excluded nodes if any are specified
|
|
102
103
|
if (config?.exclude) {
|
|
103
104
|
// We join selector to only run one query
|
|
104
|
-
const nodesToExclude = selectAll(config.exclude.join(", "), tree)
|
|
105
|
+
const nodesToExclude = selectAll(config.exclude.join(", "), tree)
|
|
105
106
|
|
|
106
|
-
remove(tree, (node) => nodesToExclude.includes(node as Element))
|
|
107
|
+
remove(tree, (node) => nodesToExclude.includes(node as Element))
|
|
107
108
|
}
|
|
108
109
|
|
|
109
110
|
// Include only nodes to include
|
|
110
111
|
if (config?.include) {
|
|
111
|
-
const nodesToInclude: Element[] = []
|
|
112
|
+
const nodesToInclude: Element[] = []
|
|
112
113
|
|
|
113
114
|
// We join selector to only run one query
|
|
114
|
-
const selector = config.include.join(", ")
|
|
115
|
-
const rawNodesToInclude = selectAll(selector, tree)
|
|
115
|
+
const selector = config.include.join(", ")
|
|
116
|
+
const rawNodesToInclude = selectAll(selector, tree)
|
|
116
117
|
|
|
117
118
|
// We walk the tree to exclude matching nodes that are children of other matching nodes.
|
|
118
119
|
visit(tree, (node) => {
|
|
119
120
|
if (rawNodesToInclude.includes(node as Element)) {
|
|
120
|
-
nodesToInclude.push(node as Element)
|
|
121
|
+
nodesToInclude.push(node as Element)
|
|
121
122
|
|
|
122
123
|
// Stop traversing this part of the tree since we found its matching parent node.
|
|
123
|
-
return SKIP
|
|
124
|
+
return SKIP
|
|
124
125
|
}
|
|
125
|
-
})
|
|
126
|
+
})
|
|
126
127
|
|
|
127
128
|
// We cannot reassign the tree itself, so we instead replace
|
|
128
129
|
// its children with the found element.
|
|
129
|
-
tree.children = nodesToInclude
|
|
130
|
+
tree.children = nodesToInclude
|
|
130
131
|
}
|
|
131
132
|
|
|
132
133
|
// Mark nodes matching CSS selectors
|
|
@@ -136,38 +137,38 @@ export const rehypeRichText: Plugin<
|
|
|
136
137
|
// Here we want to match anything that's not a valid HTML tag name and treat
|
|
137
138
|
// it as a CSS selector. See: https://regex101.com/r/LILLWH/1
|
|
138
139
|
if (!/^[a-z]+[1-6]?$/.test(key)) {
|
|
139
|
-
const matches = selectAll(key, tree)
|
|
140
|
+
const matches = selectAll(key, tree)
|
|
140
141
|
|
|
141
142
|
for (let i = 0; i < matches.length; i++) {
|
|
142
|
-
matches[i].matchesSerializer = key
|
|
143
|
+
matches[i].matchesSerializer = key
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
146
|
}
|
|
146
147
|
}
|
|
147
|
-
}
|
|
148
|
-
})
|
|
148
|
+
}
|
|
149
|
+
})
|
|
149
150
|
|
|
150
151
|
// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's
|
|
151
152
|
// registered within the plugin rather than on the processor directly.
|
|
152
|
-
self.use(rehypeMinifyWhitespace)
|
|
153
|
+
self.use(rehypeMinifyWhitespace)
|
|
153
154
|
|
|
154
|
-
self.compiler = compiler
|
|
155
|
+
self.compiler = compiler
|
|
155
156
|
|
|
156
157
|
function compiler(tree: Root, file: VFile): RichTextField {
|
|
157
|
-
const richTextField = hastToRichText(tree, file, config)
|
|
158
|
+
const richTextField = hastToRichText(tree, file, config)
|
|
158
159
|
|
|
159
160
|
if (config?.model) {
|
|
160
|
-
return filterRichTextField(richTextField, config.model)
|
|
161
|
+
return filterRichTextField(richTextField, config.model)
|
|
161
162
|
}
|
|
162
163
|
|
|
163
|
-
return richTextField
|
|
164
|
+
return richTextField
|
|
164
165
|
}
|
|
165
|
-
}
|
|
166
|
+
}
|
|
166
167
|
|
|
167
168
|
declare module "unified" {
|
|
168
169
|
// Register unified processor the result type.
|
|
169
170
|
interface CompileResultMap {
|
|
170
|
-
RichTextField: RichTextField
|
|
171
|
+
RichTextField: RichTextField
|
|
171
172
|
}
|
|
172
173
|
}
|
|
173
174
|
|
|
@@ -178,6 +179,6 @@ declare module "hast" {
|
|
|
178
179
|
* A serializer this node matches to. Nodes are marked with this property
|
|
179
180
|
* when they match a CSS selector from the serializer map.
|
|
180
181
|
*/
|
|
181
|
-
matchesSerializer?: string
|
|
182
|
+
matchesSerializer?: string
|
|
182
183
|
}
|
|
183
184
|
}
|