@prismicio/client 7.7.4 → 7.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/index.cjs +0 -8
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.ts +0 -5
  4. package/dist/index.js +1 -9
  5. package/dist/index.js.map +1 -1
  6. package/dist/package.json.cjs +1 -1
  7. package/dist/package.json.js +1 -1
  8. package/package.json +3 -15
  9. package/src/index.ts +0 -8
  10. package/dist/errors/PrismicRichTextError.cjs +0 -6
  11. package/dist/errors/PrismicRichTextError.cjs.map +0 -1
  12. package/dist/errors/PrismicRichTextError.d.ts +0 -2
  13. package/dist/errors/PrismicRichTextError.js +0 -6
  14. package/dist/errors/PrismicRichTextError.js.map +0 -1
  15. package/dist/errors/PrismicRichTextSerializerError.cjs +0 -7
  16. package/dist/errors/PrismicRichTextSerializerError.cjs.map +0 -1
  17. package/dist/errors/PrismicRichTextSerializerError.d.ts +0 -3
  18. package/dist/errors/PrismicRichTextSerializerError.js +0 -7
  19. package/dist/errors/PrismicRichTextSerializerError.js.map +0 -1
  20. package/dist/helpers/unstable_htmlAsRichText.cjs +0 -11
  21. package/dist/helpers/unstable_htmlAsRichText.cjs.map +0 -1
  22. package/dist/helpers/unstable_htmlAsRichText.d.ts +0 -81
  23. package/dist/helpers/unstable_htmlAsRichText.js +0 -11
  24. package/dist/helpers/unstable_htmlAsRichText.js.map +0 -1
  25. package/dist/helpers/unstable_markdownAsRichText.cjs +0 -12
  26. package/dist/helpers/unstable_markdownAsRichText.cjs.map +0 -1
  27. package/dist/helpers/unstable_markdownAsRichText.d.ts +0 -30
  28. package/dist/helpers/unstable_markdownAsRichText.js +0 -12
  29. package/dist/helpers/unstable_markdownAsRichText.js.map +0 -1
  30. package/dist/lib/RichTextFieldBuilder.cjs +0 -115
  31. package/dist/lib/RichTextFieldBuilder.cjs.map +0 -1
  32. package/dist/lib/RichTextFieldBuilder.d.ts +0 -35
  33. package/dist/lib/RichTextFieldBuilder.js +0 -115
  34. package/dist/lib/RichTextFieldBuilder.js.map +0 -1
  35. package/dist/lib/filterRichTextField.cjs +0 -88
  36. package/dist/lib/filterRichTextField.cjs.map +0 -1
  37. package/dist/lib/filterRichTextField.d.ts +0 -12
  38. package/dist/lib/filterRichTextField.js +0 -88
  39. package/dist/lib/filterRichTextField.js.map +0 -1
  40. package/dist/lib/hastSerializerHelpers.cjs +0 -96
  41. package/dist/lib/hastSerializerHelpers.cjs.map +0 -1
  42. package/dist/lib/hastSerializerHelpers.d.ts +0 -28
  43. package/dist/lib/hastSerializerHelpers.js +0 -96
  44. package/dist/lib/hastSerializerHelpers.js.map +0 -1
  45. package/dist/lib/hastToRichText.cjs +0 -164
  46. package/dist/lib/hastToRichText.cjs.map +0 -1
  47. package/dist/lib/hastToRichText.d.ts +0 -30
  48. package/dist/lib/hastToRichText.js +0 -164
  49. package/dist/lib/hastToRichText.js.map +0 -1
  50. package/dist/lib/rehypeRichText.cjs +0 -59
  51. package/dist/lib/rehypeRichText.cjs.map +0 -1
  52. package/dist/lib/rehypeRichText.d.ts +0 -66
  53. package/dist/lib/rehypeRichText.js +0 -59
  54. package/dist/lib/rehypeRichText.js.map +0 -1
  55. package/src/errors/PrismicRichTextError.ts +0 -1
  56. package/src/errors/PrismicRichTextSerializerError.ts +0 -3
  57. package/src/helpers/unstable_htmlAsRichText.ts +0 -122
  58. package/src/helpers/unstable_markdownAsRichText.ts +0 -53
  59. package/src/lib/RichTextFieldBuilder.ts +0 -164
  60. package/src/lib/filterRichTextField.ts +0 -159
  61. package/src/lib/hastSerializerHelpers.ts +0 -157
  62. package/src/lib/hastToRichText.ts +0 -295
  63. package/src/lib/rehypeRichText.ts +0 -184
@@ -1,157 +0,0 @@
1
- import type { Element } from "hast"
2
- import { toHtml } from "hast-util-to-html"
3
-
4
- import { OEmbedType } from "../types/value/embed"
5
- import { LinkType } from "../types/value/link"
6
- import type {
7
- RTEmbedNode,
8
- RTImageNode,
9
- RTLinkNode,
10
- } from "../types/value/richText"
11
-
12
- import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError"
13
-
14
- import type { RTPartialInlineNode } from "./RichTextFieldBuilder"
15
-
16
- /**
17
- * Serializes a hast {@link Element} node to a {@link RTImageNode}.
18
- *
19
- * @param node - A hast node to serialize.
20
- *
21
- * @returns Equivalent {@link RTImageNode}.
22
- */
23
- export const serializeImage = (node: Element): RTImageNode => {
24
- const src = node.properties?.src as string | undefined
25
-
26
- if (!src) {
27
- throw new PrismicRichTextSerializerError(
28
- "Element of type `img` is missing an `src` attribute",
29
- )
30
- }
31
-
32
- const url = new URL(src, "https://noop.com")
33
-
34
- let width = node.properties?.width as number | undefined
35
- let height = node.properties?.height as number | undefined
36
- let x = 0
37
- let y = 0
38
- let zoom = 1
39
-
40
- // Attempt to infer the image dimensions from the URL imgix parameters.
41
- if (url.hostname === "images.prismic.io") {
42
- if (url.searchParams.has("w")) {
43
- width = Number(url.searchParams.get("w"))
44
- }
45
-
46
- if (url.searchParams.has("h")) {
47
- height = Number(url.searchParams.get("h"))
48
- }
49
-
50
- if (url.searchParams.has("rect")) {
51
- const [rectX, rextY, rectW, _rectH] = url.searchParams
52
- .get("rect")!
53
- .split(",")
54
-
55
- x = Number(rectX)
56
- y = Number(rextY)
57
-
58
- // This is not perfect but it's supposed to work on images without constraints.
59
- if (width) {
60
- zoom = Math.max(1, width / Number(rectW))
61
- }
62
- }
63
- }
64
-
65
- return {
66
- type: "image",
67
- id: "",
68
- url: src,
69
- alt: (node.properties?.alt as string) ?? "",
70
- copyright: (node.properties?.copyright as string) ?? null,
71
- // This is not accurate. We're doing the casting because it seems the migration API
72
- // accepts `undefined` values for those fields which is more convenient to us here.
73
- // See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185
74
- dimensions: { width: width as number, height: height as number },
75
- edit: { x, y, zoom, background: "transparent" },
76
- }
77
- }
78
-
79
- /**
80
- * Serializes a hast {@link Element} node to a {@link RTEmbedNode}.
81
- *
82
- * @param node - A hast node to serialize.
83
- *
84
- * @returns Equivalent {@link RTEmbedNode}.
85
- */
86
- export const serializeEmbed = (node: Element): RTEmbedNode => {
87
- const src = node.properties?.src as string | undefined
88
-
89
- if (!src) {
90
- throw new PrismicRichTextSerializerError(
91
- "Element of type `embed` is missing an `src` attribute",
92
- )
93
- }
94
-
95
- const oembedBase = {
96
- version: "1.0",
97
- embed_url: src,
98
- html: toHtml(node),
99
- title: node.properties?.title as string | undefined,
100
- }
101
-
102
- const width = node.properties?.width as number | undefined
103
- const height = node.properties?.height as number | undefined
104
-
105
- // Remove the children of the embed node as we don't want to process them.
106
- node.children = []
107
-
108
- if (width && height) {
109
- return {
110
- type: "embed",
111
- oembed: { ...oembedBase, type: OEmbedType.Rich, width, height },
112
- }
113
- } else {
114
- return {
115
- type: "embed",
116
- oembed: { ...oembedBase, type: OEmbedType.Link },
117
- }
118
- }
119
- }
120
-
121
- /**
122
- * Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.
123
- *
124
- * @param node - A hast node to serialize.
125
- * @param rtPartialInlineNode - A partial rich text node.
126
- *
127
- * @returns Equivalent {@link RTPartialInlineNode}.
128
- */
129
- export const serializeSpan = (
130
- node: Element,
131
- rtPartialInlineNode:
132
- | RTPartialInlineNode
133
- | Omit<RTLinkNode, "start" | "end" | "data">,
134
- ): RTPartialInlineNode => {
135
- if (
136
- rtPartialInlineNode.type === "hyperlink" &&
137
- !("data" in rtPartialInlineNode)
138
- ) {
139
- const url = node.properties?.href as string | undefined
140
- if (!url) {
141
- throw new PrismicRichTextSerializerError(
142
- "Element of type `hyperlink` is missing an `href` attribute",
143
- )
144
- }
145
-
146
- return {
147
- type: "hyperlink",
148
- data: {
149
- link_type: LinkType.Web,
150
- url,
151
- target: node.properties?.target as string | undefined,
152
- },
153
- }
154
- }
155
-
156
- return rtPartialInlineNode
157
- }
@@ -1,295 +0,0 @@
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"
5
-
6
- import type {
7
- RTBlockNode,
8
- RTInlineNode,
9
- RTLabelNode,
10
- RTTextNode,
11
- RichTextField,
12
- } from "../types/value/richText"
13
- import { RichTextNodeType } from "../types/value/richText"
14
-
15
- import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError"
16
-
17
- import type {
18
- RichTextHTMLMapSerializer,
19
- RichTextHTMLMapSerializerFunction,
20
- RichTextHTMLMapSerializerShorthand,
21
- } from "../helpers/unstable_htmlAsRichText"
22
-
23
- import { RichTextFieldBuilder } from "./RichTextFieldBuilder"
24
- import {
25
- serializeEmbed,
26
- serializeImage,
27
- serializeSpan,
28
- } from "./hastSerializerHelpers"
29
-
30
- /**
31
- * Pick keys from a type, distributing the operation over a union.
32
- *
33
- * Taken from the `type-fest` package.
34
- *
35
- * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-pick.d.ts
36
- */
37
- type DistributedPick<
38
- ObjectType,
39
- KeyType extends keyof ObjectType,
40
- > = ObjectType extends unknown ? Pick<ObjectType, KeyType> : never
41
-
42
- const DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {
43
- h1: "heading1",
44
- h2: "heading2",
45
- h3: "heading3",
46
- h4: "heading4",
47
- h5: "heading5",
48
- h6: "heading6",
49
- p: "paragraph",
50
- pre: "preformatted",
51
- strong: "strong",
52
- em: "em",
53
- li: ({ context }) =>
54
- context.listType === "group-o-list-item" ? "o-list-item" : "list-item",
55
- ul: "group-list-item",
56
- ol: "group-o-list-item",
57
- img: "image",
58
- iframe: "embed",
59
- a: "hyperlink",
60
- }
61
-
62
- const VFILE_RULE = "failed-to-serialize-node"
63
- const VFILE_SOURCE = "prismic"
64
-
65
- /**
66
- * Configuration that determines the output of `toRichText`.
67
- */
68
- export type HASTToRichTextConfig = {
69
- /**
70
- * An optional HTML to rich text serializer. Will be merged with the default
71
- * HTML to rich text serializer.
72
- */
73
- serializer?: RichTextHTMLMapSerializer
74
-
75
- /**
76
- * Whether or not the text processed should be marked as right-to-left.
77
- *
78
- * @defaultValue `false`
79
- */
80
- direction?: "ltr" | "rtl"
81
- }
82
-
83
- /**
84
- * Transfor a hast tree to a rich text field.
85
- *
86
- * @param tree - The hast tree to transform.
87
- * @param file - The vfile to attach warnings to.
88
- * @param config - Configuration that determines the output of the function.
89
- *
90
- * @returns The rich text field equivalent of the provided hast tree.
91
- */
92
- export const hastToRichText = (
93
- tree: Root | Element,
94
- file: VFile,
95
- config?: HASTToRichTextConfig,
96
- ): RichTextField => {
97
- const builder = new RichTextFieldBuilder()
98
-
99
- // Merge the default serializer with the user-provided one.
100
- const serializer = {
101
- ...DEFAULT_SERIALIZER,
102
- ...config?.serializer,
103
- }
104
-
105
- // Keep track of the last text node type to append text nodes to in
106
- // case of an image or an embed node is present inside a paragraph.
107
- let lastRTTextNodeType: RTTextNode["type"] = RichTextNodeType.paragraph
108
-
109
- // Keep track of the last list type to know whether we need to append
110
- // `list-item` or `o-list-item` nodes.
111
- let lastListType: "group-list-item" | "group-o-list-item" | undefined =
112
- undefined
113
-
114
- visit(tree, (node) => {
115
- if (node.type === "element") {
116
- // Transforms line break elements to line breaks
117
- if (node.tagName === "br") {
118
- try {
119
- builder.appendText("\n")
120
- } catch (error) {
121
- // noop
122
- }
123
- }
124
-
125
- // Resolves the serializer responsible for the current node.
126
- let serializerOrShorthand:
127
- | RichTextHTMLMapSerializerShorthand
128
- | RichTextHTMLMapSerializerFunction
129
-
130
- // We give priority to CSS selectors over tag names.
131
- if (node.matchesSerializer && node.matchesSerializer in serializer) {
132
- serializerOrShorthand = serializer[node.matchesSerializer]
133
- } else if (node.tagName in serializer) {
134
- serializerOrShorthand = serializer[node.tagName]
135
- } else {
136
- return
137
- }
138
-
139
- let shorthand: RichTextHTMLMapSerializerShorthand
140
- if (typeof serializerOrShorthand === "function") {
141
- const shorthandOrNode = serializerOrShorthand({
142
- node,
143
- context: { listType: lastListType },
144
- })
145
-
146
- if (!shorthandOrNode) {
147
- // Exit on unhandled node.
148
- return
149
- } else if (
150
- typeof shorthandOrNode === "object" &&
151
- "type" in shorthandOrNode
152
- ) {
153
- // When the serializer returns a rich text node, we append it and return.
154
- switch (shorthandOrNode.type) {
155
- case RichTextNodeType.strong:
156
- case RichTextNodeType.em:
157
- case RichTextNodeType.label:
158
- case RichTextNodeType.hyperlink: {
159
- const length = toString(node).trimEnd().length
160
-
161
- try {
162
- builder.appendSpan(shorthandOrNode, length)
163
- } catch (error) {
164
- // Happens when we extract an image/embed node inside an RTTextNode and that
165
- // the next children is a span. The last RT node type is then an image/embed
166
- // node, so we need to resume a new RT text node.
167
- builder.appendTextNode(lastRTTextNodeType, config?.direction)
168
- builder.appendSpan(shorthandOrNode, length)
169
- }
170
-
171
- return
172
- }
173
-
174
- case RichTextNodeType.image:
175
- case RichTextNodeType.embed:
176
- builder.appendNode(shorthandOrNode)
177
-
178
- return
179
-
180
- default:
181
- lastRTTextNodeType = shorthandOrNode.type
182
- builder.appendNode(shorthandOrNode)
183
-
184
- return
185
- }
186
- }
187
-
188
- // Else it's a shorthand.
189
- shorthand = shorthandOrNode
190
- } else {
191
- shorthand = serializerOrShorthand
192
- }
193
-
194
- let match:
195
- | DistributedPick<
196
- RTBlockNode | Exclude<RTInlineNode, RTLabelNode>,
197
- "type"
198
- >
199
- | { type: "label"; data: { label: string } }
200
- if (typeof shorthand === "string") {
201
- match = { type: shorthand }
202
- } else {
203
- match = { type: RichTextNodeType.label, data: shorthand }
204
- }
205
-
206
- try {
207
- switch (match.type) {
208
- case RichTextNodeType.heading1:
209
- case RichTextNodeType.heading2:
210
- case RichTextNodeType.heading3:
211
- case RichTextNodeType.heading4:
212
- case RichTextNodeType.heading5:
213
- case RichTextNodeType.heading6:
214
- case RichTextNodeType.paragraph:
215
- case RichTextNodeType.preformatted:
216
- case RichTextNodeType.listItem:
217
- case RichTextNodeType.oListItem:
218
- lastRTTextNodeType = match.type
219
- builder.appendTextNode(match.type, config?.direction)
220
- break
221
-
222
- case RichTextNodeType.list:
223
- case RichTextNodeType.oList:
224
- lastListType = match.type
225
- break
226
-
227
- case RichTextNodeType.image:
228
- builder.appendNode(serializeImage(node))
229
- break
230
-
231
- case RichTextNodeType.embed:
232
- builder.appendNode(serializeEmbed(node))
233
- break
234
-
235
- case RichTextNodeType.strong:
236
- case RichTextNodeType.em:
237
- case RichTextNodeType.label:
238
- case RichTextNodeType.hyperlink: {
239
- const span = serializeSpan(node, match)
240
- const length = toString(node).trimEnd().length
241
-
242
- try {
243
- builder.appendSpan(span, length)
244
- } catch (error) {
245
- // Happens when we extract an image/embed node inside an RTTextNode and that
246
- // the next children is a span. The last RT node type is then an image/embed
247
- // node, so we need to resume a new RT text node.
248
- builder.appendTextNode(lastRTTextNodeType, config?.direction)
249
- builder.appendSpan(span, length)
250
- }
251
- break
252
- }
253
-
254
- default:
255
- throw new Error(
256
- `Unknown rich text node type: \`${
257
- (match as { type: string }).type
258
- }\``,
259
- )
260
- }
261
- } catch (error) {
262
- if (error instanceof PrismicRichTextSerializerError) {
263
- file.message(error.message, {
264
- cause: error,
265
- place: node.position,
266
- ruleId: VFILE_RULE,
267
- source: VFILE_SOURCE,
268
- })
269
-
270
- return
271
- }
272
-
273
- throw error
274
- }
275
- } else if (node.type === "text") {
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, "") === "")) {
279
- try {
280
- builder.appendText(node.value)
281
- } catch (error) {
282
- // Happens when we extract an image/embed node inside an RTTextNode. The last RT
283
- // node type is then an image/embed node, so we need to resume a new RT text node.
284
- builder.appendTextNode(lastRTTextNodeType, config?.direction)
285
- builder.appendText(node.value)
286
- }
287
- }
288
- }
289
-
290
- // We ignore the following node types:
291
- // root, doctype, comment, raw
292
- })
293
-
294
- return builder.build()
295
- }
@@ -1,184 +0,0 @@
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
-
9
- import type { CustomTypeModelRichTextField } from "../types/model/richText"
10
- import type { RichTextField } from "../types/value/richText"
11
-
12
- import { filterRichTextField } from "./filterRichTextField"
13
- import type { HASTToRichTextConfig } from "./hastToRichText"
14
- import { hastToRichText } from "./hastToRichText"
15
-
16
- /**
17
- * Configuration options for {@link rehypeRichText}.
18
- */
19
- export type RehypeRichTextConfig = {
20
- /**
21
- * A CSS selector that targets the section of the document to convert to rich
22
- * text.
23
- *
24
- * @example `div.post`
25
- *
26
- * @defaultValue `":root"` - The top-level element of the document.
27
- */
28
- container?: string
29
-
30
- /**
31
- * A list of CSS selectors to exclude matching nodes from the document to
32
- * process.
33
- *
34
- * @example `[".hidden", "aside"]`
35
- *
36
- * @defaultValue `[]` - No nodes are excluded.
37
- */
38
- exclude?: string[]
39
-
40
- /**
41
- * A list of CSS selectors to include only matching nodes from the document to
42
- * process.
43
- *
44
- * @example `["p", "img"]`
45
- *
46
- * @defaultValue `[]` - All nodes are included.
47
- */
48
- include?: string[]
49
-
50
- /**
51
- * A rich text or title field model definition. When provided the serializer
52
- * will filter out any node types not allowed by the model.
53
- *
54
- * @defaultValue `undefined` - No filtering is applied.
55
- */
56
- model?: CustomTypeModelRichTextField
57
- } & HASTToRichTextConfig
58
-
59
- /**
60
- * A unified plugin that compiles a hast tree to a Prismic rich text field.
61
- *
62
- * @param config - Configuration options for the rehype rich text processor.
63
- */
64
- // unified requires the function to be typed directly with the
65
- // `Plugin` type to properly infer return types on processors.
66
- export const rehypeRichText: Plugin<
67
- [config?: RehypeRichTextConfig],
68
- Root,
69
- RichTextField
70
- > = function rehypeRichText(config) {
71
- // This is a bit dirty, but it seems like that's how rehype intends
72
- // it to be due to JSDocs limitations(?), see:
73
- // https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20
74
- // eslint-disable-next-line @typescript-eslint/no-this-alias
75
- const self = this as unknown as Processor<
76
- undefined,
77
- undefined,
78
- undefined,
79
- Root,
80
- RichTextField
81
- >
82
-
83
- // We need to exclude nodes _before_ we minify the tree as excluding
84
- // nodes could end up in more whitespaces to trim.
85
- self.use(() => {
86
- return (tree: Root) => {
87
- // Extract container node if any is specified.
88
- if (config?.container) {
89
- const element = select(config.container, tree)
90
-
91
- if (!element) {
92
- throw new Error(
93
- `No container matching \`${config?.container}\` could be found in the input AST.`,
94
- )
95
- }
96
-
97
- // We cannot reassign the tree itself, so we instead replace
98
- // its children with the found element.
99
- tree.children = [element]
100
- }
101
-
102
- // Remove excluded nodes if any are specified
103
- if (config?.exclude) {
104
- // We join selector to only run one query
105
- const nodesToExclude = selectAll(config.exclude.join(", "), tree)
106
-
107
- remove(tree, (node) => nodesToExclude.includes(node as Element))
108
- }
109
-
110
- // Include only nodes to include
111
- if (config?.include) {
112
- const nodesToInclude: Element[] = []
113
-
114
- // We join selector to only run one query
115
- const selector = config.include.join(", ")
116
- const rawNodesToInclude = selectAll(selector, tree)
117
-
118
- // We walk the tree to exclude matching nodes that are children of other matching nodes.
119
- visit(tree, (node) => {
120
- if (rawNodesToInclude.includes(node as Element)) {
121
- nodesToInclude.push(node as Element)
122
-
123
- // Stop traversing this part of the tree since we found its matching parent node.
124
- return SKIP
125
- }
126
- })
127
-
128
- // We cannot reassign the tree itself, so we instead replace
129
- // its children with the found element.
130
- tree.children = nodesToInclude
131
- }
132
-
133
- // Mark nodes matching CSS selectors
134
- if (config?.serializer) {
135
- for (const key in config.serializer) {
136
- // HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).
137
- // Here we want to match anything that's not a valid HTML tag name and treat
138
- // it as a CSS selector. See: https://regex101.com/r/LILLWH/1
139
- if (!/^[a-z]+[1-6]?$/.test(key)) {
140
- const matches = selectAll(key, tree)
141
-
142
- for (let i = 0; i < matches.length; i++) {
143
- matches[i].matchesSerializer = key
144
- }
145
- }
146
- }
147
- }
148
- }
149
- })
150
-
151
- // `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's
152
- // registered within the plugin rather than on the processor directly.
153
- self.use(rehypeMinifyWhitespace)
154
-
155
- self.compiler = compiler
156
-
157
- function compiler(tree: Root, file: VFile): RichTextField {
158
- const richTextField = hastToRichText(tree, file, config)
159
-
160
- if (config?.model) {
161
- return filterRichTextField(richTextField, config.model)
162
- }
163
-
164
- return richTextField
165
- }
166
- }
167
-
168
- declare module "unified" {
169
- // Register unified processor the result type.
170
- interface CompileResultMap {
171
- RichTextField: RichTextField
172
- }
173
- }
174
-
175
- declare module "hast" {
176
- // Extend hast node with a rich text value.
177
- interface Element {
178
- /**
179
- * A serializer this node matches to. Nodes are marked with this property
180
- * when they match a CSS selector from the serializer map.
181
- */
182
- matchesSerializer?: string
183
- }
184
- }