@prismicio/migrate 0.0.1 → 0.0.2-canary.6b4d958
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/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/errors/PrismicRichTextError.cjs +5 -5
- package/dist/errors/PrismicRichTextError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextError.d.cts +5 -0
- package/dist/errors/PrismicRichTextError.d.cts.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +5 -2
- package/dist/errors/PrismicRichTextError.d.ts.map +1 -0
- package/dist/errors/PrismicRichTextError.js +6 -6
- package/dist/errors/PrismicRichTextError.js.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.cjs +6 -6
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.d.cts +7 -0
- package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +7 -3
- package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.js +6 -6
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
- package/dist/htmlAsRichText.cjs +24 -8
- package/dist/htmlAsRichText.cjs.map +1 -1
- package/dist/htmlAsRichText.d.cts +23 -0
- package/dist/htmlAsRichText.d.cts.map +1 -0
- package/dist/htmlAsRichText.d.ts +14 -69
- package/dist/htmlAsRichText.d.ts.map +1 -0
- package/dist/htmlAsRichText.js +21 -7
- package/dist/htmlAsRichText.js.map +1 -1
- package/dist/index.cjs +6 -8
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +2 -7
- package/dist/lib/RichTextFieldBuilder.cjs +82 -113
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.cts +16 -0
- package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +8 -27
- package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -0
- package/dist/lib/RichTextFieldBuilder.js +82 -113
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +77 -78
- package/dist/lib/filterRichTextField.cjs.map +1 -1
- package/dist/lib/filterRichTextField.js +77 -79
- package/dist/lib/filterRichTextField.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs +102 -86
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
- package/dist/lib/hastSerializerHelpers.js +101 -88
- package/dist/lib/hastSerializerHelpers.js.map +1 -1
- package/dist/lib/hastToRichText.cjs +152 -157
- package/dist/lib/hastToRichText.cjs.map +1 -1
- package/dist/lib/hastToRichText.d.cts +21 -0
- package/dist/lib/hastToRichText.d.cts.map +1 -0
- package/dist/lib/hastToRichText.d.ts +20 -29
- package/dist/lib/hastToRichText.d.ts.map +1 -0
- package/dist/lib/hastToRichText.js +150 -156
- package/dist/lib/hastToRichText.js.map +1 -1
- package/dist/lib/rehypeRichText.cjs +56 -57
- package/dist/lib/rehypeRichText.cjs.map +1 -1
- package/dist/lib/rehypeRichText.d.cts +57 -0
- package/dist/lib/rehypeRichText.d.cts.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +53 -61
- package/dist/lib/rehypeRichText.d.ts.map +1 -0
- package/dist/lib/rehypeRichText.js +52 -55
- package/dist/lib/rehypeRichText.js.map +1 -1
- package/dist/types.d.cts +46 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +30 -41
- package/dist/types.d.ts.map +1 -0
- package/package.json +35 -58
- package/src/htmlAsRichText.ts +22 -49
- package/src/lib/RichTextFieldBuilder.ts +26 -31
- package/src/lib/filterRichTextField.ts +6 -25
- package/src/lib/hastSerializerHelpers.ts +4 -17
- package/src/lib/hastToRichText.ts +44 -66
- package/src/lib/rehypeRichText.ts +88 -102
- package/src/types.ts +24 -33
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/filterRichTextField.d.ts +0 -10
- package/dist/lib/hastSerializerHelpers.d.ts +0 -28
|
@@ -11,20 +11,14 @@ import { toString } from "hast-util-to-string"
|
|
|
11
11
|
import { visit } from "unist-util-visit"
|
|
12
12
|
import type { VFile } from "vfile"
|
|
13
13
|
|
|
14
|
+
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError"
|
|
14
15
|
import type {
|
|
15
16
|
RichTextHTMLMapSerializer,
|
|
16
17
|
RichTextHTMLMapSerializerFunction,
|
|
17
18
|
RichTextHTMLMapSerializerShorthand,
|
|
18
19
|
} from "../types"
|
|
19
|
-
|
|
20
|
-
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError"
|
|
21
|
-
|
|
20
|
+
import { serializeEmbed, serializeImage, serializeSpan } from "./hastSerializerHelpers"
|
|
22
21
|
import { RichTextFieldBuilder } from "./RichTextFieldBuilder"
|
|
23
|
-
import {
|
|
24
|
-
serializeEmbed,
|
|
25
|
-
serializeImage,
|
|
26
|
-
serializeSpan,
|
|
27
|
-
} from "./hastSerializerHelpers"
|
|
28
22
|
|
|
29
23
|
/**
|
|
30
24
|
* Pick keys from a type, distributing the operation over a union.
|
|
@@ -33,10 +27,9 @@ import {
|
|
|
33
27
|
*
|
|
34
28
|
* @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-pick.d.ts
|
|
35
29
|
*/
|
|
36
|
-
type DistributedPick<
|
|
37
|
-
ObjectType,
|
|
38
|
-
|
|
39
|
-
> = ObjectType extends unknown ? Pick<ObjectType, KeyType> : never
|
|
30
|
+
type DistributedPick<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown
|
|
31
|
+
? Pick<ObjectType, KeyType>
|
|
32
|
+
: never
|
|
40
33
|
|
|
41
34
|
const DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {
|
|
42
35
|
h1: "heading1",
|
|
@@ -49,8 +42,7 @@ const DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {
|
|
|
49
42
|
pre: "preformatted",
|
|
50
43
|
strong: "strong",
|
|
51
44
|
em: "em",
|
|
52
|
-
li: ({ context }) =>
|
|
53
|
-
context.listType === "group-o-list-item" ? "o-list-item" : "list-item",
|
|
45
|
+
li: ({ context }) => (context.listType === "group-o-list-item" ? "o-list-item" : "list-item"),
|
|
54
46
|
ul: "group-list-item",
|
|
55
47
|
ol: "group-o-list-item",
|
|
56
48
|
img: "image",
|
|
@@ -61,13 +53,11 @@ const DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {
|
|
|
61
53
|
const VFILE_RULE = "failed-to-serialize-node"
|
|
62
54
|
const VFILE_SOURCE = "prismic"
|
|
63
55
|
|
|
64
|
-
/**
|
|
65
|
-
* Configuration that determines the output of `toRichText`.
|
|
66
|
-
*/
|
|
56
|
+
/** Configuration that determines the output of `toRichText`. */
|
|
67
57
|
export type HASTToRichTextConfig = {
|
|
68
58
|
/**
|
|
69
|
-
* An optional HTML to rich text serializer. Will be merged with the default
|
|
70
|
-
*
|
|
59
|
+
* An optional HTML to rich text serializer. Will be merged with the default HTML to rich text
|
|
60
|
+
* serializer.
|
|
71
61
|
*/
|
|
72
62
|
serializer?: RichTextHTMLMapSerializer
|
|
73
63
|
|
|
@@ -85,7 +75,6 @@ export type HASTToRichTextConfig = {
|
|
|
85
75
|
* @param tree - The hast tree to transform.
|
|
86
76
|
* @param file - The vfile to attach warnings to.
|
|
87
77
|
* @param config - Configuration that determines the output of the function.
|
|
88
|
-
*
|
|
89
78
|
* @returns The rich text field equivalent of the provided hast tree.
|
|
90
79
|
*/
|
|
91
80
|
export const hastToRichText = (
|
|
@@ -107,18 +96,17 @@ export const hastToRichText = (
|
|
|
107
96
|
|
|
108
97
|
// Keep track of the last list type to know whether we need to append
|
|
109
98
|
// `list-item` or `o-list-item` nodes.
|
|
110
|
-
let lastListType: "group-list-item" | "group-o-list-item" | undefined =
|
|
111
|
-
undefined
|
|
99
|
+
let lastListType: "group-list-item" | "group-o-list-item" | undefined = undefined
|
|
112
100
|
|
|
113
101
|
visit(tree, (node) => {
|
|
114
102
|
if (node.type === "element") {
|
|
115
103
|
// Transforms line break elements to line breaks
|
|
116
104
|
if (node.tagName === "br") {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
105
|
+
try {
|
|
106
|
+
builder.appendText("\n")
|
|
107
|
+
} catch {
|
|
108
|
+
// noop
|
|
109
|
+
}
|
|
122
110
|
}
|
|
123
111
|
|
|
124
112
|
// Resolves the serializer responsible for the current node.
|
|
@@ -145,10 +133,7 @@ export const hastToRichText = (
|
|
|
145
133
|
if (!shorthandOrNode) {
|
|
146
134
|
// Exit on unhandled node.
|
|
147
135
|
return
|
|
148
|
-
} else if (
|
|
149
|
-
typeof shorthandOrNode === "object" &&
|
|
150
|
-
"type" in shorthandOrNode
|
|
151
|
-
) {
|
|
136
|
+
} else if (typeof shorthandOrNode === "object" && "type" in shorthandOrNode) {
|
|
152
137
|
// When the serializer returns a rich text node, we append it and return.
|
|
153
138
|
switch (shorthandOrNode.type) {
|
|
154
139
|
case RichTextNodeType.strong:
|
|
@@ -157,15 +142,15 @@ export const hastToRichText = (
|
|
|
157
142
|
case RichTextNodeType.hyperlink: {
|
|
158
143
|
const length = toString(node).trimEnd().length
|
|
159
144
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
145
|
+
try {
|
|
146
|
+
builder.appendSpan(shorthandOrNode, length)
|
|
147
|
+
} catch {
|
|
148
|
+
// Happens when we extract an image/embed node inside an RTTextNode and that
|
|
149
|
+
// the next children is a span. The last RT node type is then an image/embed
|
|
150
|
+
// node, so we need to resume a new RT text node.
|
|
151
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
152
|
+
builder.appendSpan(shorthandOrNode, length)
|
|
153
|
+
}
|
|
169
154
|
|
|
170
155
|
return
|
|
171
156
|
}
|
|
@@ -191,10 +176,7 @@ export const hastToRichText = (
|
|
|
191
176
|
}
|
|
192
177
|
|
|
193
178
|
let match:
|
|
194
|
-
| DistributedPick<
|
|
195
|
-
RTBlockNode | Exclude<RTInlineNode, RTLabelNode>,
|
|
196
|
-
"type"
|
|
197
|
-
>
|
|
179
|
+
| DistributedPick<RTBlockNode | Exclude<RTInlineNode, RTLabelNode>, "type">
|
|
198
180
|
| { type: "label"; data: { label: string } }
|
|
199
181
|
if (typeof shorthand === "string") {
|
|
200
182
|
match = { type: shorthand }
|
|
@@ -238,24 +220,20 @@ export const hastToRichText = (
|
|
|
238
220
|
const span = serializeSpan(node, match)
|
|
239
221
|
const length = toString(node).trimEnd().length
|
|
240
222
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
223
|
+
try {
|
|
224
|
+
builder.appendSpan(span, length)
|
|
225
|
+
} catch {
|
|
226
|
+
// Happens when we extract an image/embed node inside an RTTextNode and that
|
|
227
|
+
// the next children is a span. The last RT node type is then an image/embed
|
|
228
|
+
// node, so we need to resume a new RT text node.
|
|
229
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
230
|
+
builder.appendSpan(span, length)
|
|
231
|
+
}
|
|
250
232
|
break
|
|
251
233
|
}
|
|
252
234
|
|
|
253
235
|
default:
|
|
254
|
-
throw new Error(
|
|
255
|
-
`Unknown rich text node type: \`${
|
|
256
|
-
(match as { type: string }).type
|
|
257
|
-
}\``,
|
|
258
|
-
)
|
|
236
|
+
throw new Error(`Unknown rich text node type: \`${(match as { type: string }).type}\``)
|
|
259
237
|
}
|
|
260
238
|
} catch (error) {
|
|
261
239
|
if (error instanceof PrismicRichTextSerializerError) {
|
|
@@ -275,14 +253,14 @@ export const hastToRichText = (
|
|
|
275
253
|
// Inspired from `hast-util-whitespace`, see:
|
|
276
254
|
// https://github.com/syntax-tree/hast-util-whitespace/blob/main/lib/index.js
|
|
277
255
|
if (!(node.value.replace(/[ \t\n\f\r]/g, "") === "")) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
256
|
+
try {
|
|
257
|
+
builder.appendText(node.value)
|
|
258
|
+
} catch {
|
|
259
|
+
// Happens when we extract an image/embed node inside an RTTextNode. The last RT
|
|
260
|
+
// node type is then an image/embed node, so we need to resume a new RT text node.
|
|
261
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
262
|
+
builder.appendText(node.value)
|
|
263
|
+
}
|
|
286
264
|
}
|
|
287
265
|
}
|
|
288
266
|
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CustomTypeModelRichTextField,
|
|
3
|
-
RichTextField,
|
|
4
|
-
} from "@prismicio/client"
|
|
1
|
+
import type { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client"
|
|
5
2
|
import type { Element, Root } from "hast"
|
|
6
3
|
import { select, selectAll } from "hast-util-select"
|
|
7
4
|
import rehypeMinifyWhitespace from "rehype-minify-whitespace"
|
|
@@ -14,43 +11,41 @@ import { filterRichTextField } from "./filterRichTextField"
|
|
|
14
11
|
import type { HASTToRichTextConfig } from "./hastToRichText"
|
|
15
12
|
import { hastToRichText } from "./hastToRichText"
|
|
16
13
|
|
|
17
|
-
/**
|
|
18
|
-
* Configuration options for {@link rehypeRichText}.
|
|
19
|
-
*/
|
|
14
|
+
/** Configuration options for {@link rehypeRichText}. */
|
|
20
15
|
export type RehypeRichTextConfig = {
|
|
21
16
|
/**
|
|
22
|
-
* A CSS selector that targets the section of the document to convert to rich
|
|
23
|
-
* text.
|
|
17
|
+
* A CSS selector that targets the section of the document to convert to rich text.
|
|
24
18
|
*
|
|
25
|
-
* @example
|
|
19
|
+
* @example
|
|
20
|
+
* ;`div.post`
|
|
26
21
|
*
|
|
27
22
|
* @defaultValue `":root"` - The top-level element of the document.
|
|
28
23
|
*/
|
|
29
24
|
container?: string
|
|
30
25
|
|
|
31
26
|
/**
|
|
32
|
-
* A list of CSS selectors to exclude matching nodes from the document to
|
|
33
|
-
* process.
|
|
27
|
+
* A list of CSS selectors to exclude matching nodes from the document to process.
|
|
34
28
|
*
|
|
35
|
-
* @example
|
|
29
|
+
* @example
|
|
30
|
+
* ;`[".hidden", "aside"]`
|
|
36
31
|
*
|
|
37
32
|
* @defaultValue `[]` - No nodes are excluded.
|
|
38
33
|
*/
|
|
39
34
|
exclude?: string[]
|
|
40
35
|
|
|
41
36
|
/**
|
|
42
|
-
* A list of CSS selectors to include only matching nodes from the document to
|
|
43
|
-
* process.
|
|
37
|
+
* A list of CSS selectors to include only matching nodes from the document to process.
|
|
44
38
|
*
|
|
45
|
-
* @example
|
|
39
|
+
* @example
|
|
40
|
+
* ;`["p", "img"]`
|
|
46
41
|
*
|
|
47
42
|
* @defaultValue `[]` - All nodes are included.
|
|
48
43
|
*/
|
|
49
44
|
include?: string[]
|
|
50
45
|
|
|
51
46
|
/**
|
|
52
|
-
* A rich text or title field model definition. When provided the serializer
|
|
53
|
-
*
|
|
47
|
+
* A rich text or title field model definition. When provided the serializer will filter out any
|
|
48
|
+
* node types not allowed by the model.
|
|
54
49
|
*
|
|
55
50
|
* @defaultValue `undefined` - No filtering is applied.
|
|
56
51
|
*/
|
|
@@ -64,107 +59,98 @@ export type RehypeRichTextConfig = {
|
|
|
64
59
|
*/
|
|
65
60
|
// unified requires the function to be typed directly with the
|
|
66
61
|
// `Plugin` type to properly infer return types on processors.
|
|
67
|
-
export const rehypeRichText: Plugin<
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// Extract container node if any is specified.
|
|
89
|
-
if (config?.container) {
|
|
90
|
-
const element = select(config.container, tree)
|
|
91
|
-
|
|
92
|
-
if (!element) {
|
|
93
|
-
throw new Error(
|
|
94
|
-
`No container matching \`${config?.container}\` could be found in the input AST.`,
|
|
95
|
-
)
|
|
96
|
-
}
|
|
62
|
+
export const rehypeRichText: Plugin<[config?: RehypeRichTextConfig], Root, RichTextField> =
|
|
63
|
+
function rehypeRichText(config) {
|
|
64
|
+
// This is a bit dirty, but it seems like that's how rehype intends
|
|
65
|
+
// it to be due to JSDocs limitations(?), see:
|
|
66
|
+
// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
68
|
+
const self = this as unknown as Processor<undefined, undefined, undefined, Root, RichTextField>
|
|
69
|
+
|
|
70
|
+
// We need to exclude nodes _before_ we minify the tree as excluding
|
|
71
|
+
// nodes could end up in more whitespaces to trim.
|
|
72
|
+
self.use(() => {
|
|
73
|
+
return (tree: Root) => {
|
|
74
|
+
// Extract container node if any is specified.
|
|
75
|
+
if (config?.container) {
|
|
76
|
+
const element = select(config.container, tree)
|
|
77
|
+
|
|
78
|
+
if (!element) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`No container matching \`${config?.container}\` could be found in the input AST.`,
|
|
81
|
+
)
|
|
82
|
+
}
|
|
97
83
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
84
|
+
// We cannot reassign the tree itself, so we instead replace
|
|
85
|
+
// its children with the found element.
|
|
86
|
+
tree.children = [element]
|
|
87
|
+
}
|
|
102
88
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
89
|
+
// Remove excluded nodes if any are specified
|
|
90
|
+
if (config?.exclude) {
|
|
91
|
+
// We join selector to only run one query
|
|
92
|
+
const nodesToExclude = selectAll(config.exclude.join(", "), tree)
|
|
107
93
|
|
|
108
|
-
|
|
109
|
-
|
|
94
|
+
remove(tree, (node) => nodesToExclude.includes(node as Element))
|
|
95
|
+
}
|
|
110
96
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
97
|
+
// Include only nodes to include
|
|
98
|
+
if (config?.include) {
|
|
99
|
+
const nodesToInclude: Element[] = []
|
|
114
100
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
101
|
+
// We join selector to only run one query
|
|
102
|
+
const selector = config.include.join(", ")
|
|
103
|
+
const rawNodesToInclude = selectAll(selector, tree)
|
|
118
104
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
105
|
+
// We walk the tree to exclude matching nodes that are children of other matching nodes.
|
|
106
|
+
visit(tree, (node) => {
|
|
107
|
+
if (rawNodesToInclude.includes(node as Element)) {
|
|
108
|
+
nodesToInclude.push(node as Element)
|
|
123
109
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
110
|
+
// Stop traversing this part of the tree since we found its matching parent node.
|
|
111
|
+
return SKIP
|
|
112
|
+
}
|
|
113
|
+
})
|
|
128
114
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
115
|
+
// We cannot reassign the tree itself, so we instead replace
|
|
116
|
+
// its children with the found element.
|
|
117
|
+
tree.children = nodesToInclude
|
|
118
|
+
}
|
|
133
119
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
120
|
+
// Mark nodes matching CSS selectors
|
|
121
|
+
if (config?.serializer) {
|
|
122
|
+
for (const key in config.serializer) {
|
|
123
|
+
// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).
|
|
124
|
+
// Here we want to match anything that's not a valid HTML tag name and treat
|
|
125
|
+
// it as a CSS selector. See: https://regex101.com/r/LILLWH/1
|
|
126
|
+
if (!/^[a-z]+[1-6]?$/.test(key)) {
|
|
127
|
+
const matches = selectAll(key, tree)
|
|
128
|
+
|
|
129
|
+
for (let i = 0; i < matches.length; i++) {
|
|
130
|
+
matches[i].matchesSerializer = key
|
|
131
|
+
}
|
|
145
132
|
}
|
|
146
133
|
}
|
|
147
134
|
}
|
|
148
135
|
}
|
|
149
|
-
}
|
|
150
|
-
})
|
|
136
|
+
})
|
|
151
137
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
138
|
+
// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's
|
|
139
|
+
// registered within the plugin rather than on the processor directly.
|
|
140
|
+
self.use(rehypeMinifyWhitespace)
|
|
155
141
|
|
|
156
|
-
|
|
142
|
+
self.compiler = compiler
|
|
157
143
|
|
|
158
|
-
|
|
159
|
-
|
|
144
|
+
function compiler(tree: Root, file: VFile): RichTextField {
|
|
145
|
+
const richTextField = hastToRichText(tree, file, config)
|
|
160
146
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
147
|
+
if (config?.model) {
|
|
148
|
+
return filterRichTextField(richTextField, config.model)
|
|
149
|
+
}
|
|
164
150
|
|
|
165
|
-
|
|
151
|
+
return richTextField
|
|
152
|
+
}
|
|
166
153
|
}
|
|
167
|
-
}
|
|
168
154
|
|
|
169
155
|
declare module "unified" {
|
|
170
156
|
// Register unified processor the result type.
|
|
@@ -177,8 +163,8 @@ declare module "hast" {
|
|
|
177
163
|
// Extend hast node with a rich text value.
|
|
178
164
|
interface Element {
|
|
179
165
|
/**
|
|
180
|
-
* A serializer this node matches to. Nodes are marked with this property
|
|
181
|
-
*
|
|
166
|
+
* A serializer this node matches to. Nodes are marked with this property when they match a CSS
|
|
167
|
+
* selector from the serializer map.
|
|
182
168
|
*/
|
|
183
169
|
matchesSerializer?: string
|
|
184
170
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,70 +1,61 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
InjectMigrationSpecificTypes,
|
|
3
|
+
RTInlineNode,
|
|
4
|
+
RTNode,
|
|
5
|
+
RichTextNodeTypes,
|
|
6
|
+
} from "@prismicio/client"
|
|
2
7
|
import type { Element } from "hast"
|
|
3
8
|
|
|
4
9
|
import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder"
|
|
5
10
|
|
|
6
11
|
/**
|
|
7
|
-
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node
|
|
8
|
-
* types.
|
|
12
|
+
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.
|
|
9
13
|
*
|
|
10
14
|
* @remarks
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* `u: { label: "underline" }`
|
|
15
|
+
* The `label` rich text node type is not available as is. Use an object containing your label
|
|
16
|
+
* name to convert to label nodes instead. For example: `u: { label: "underline" }`
|
|
14
17
|
* @remarks
|
|
15
|
-
*
|
|
16
|
-
*
|
|
18
|
+
* The `span` rich text node type is not available as it is not relevant in the context of going
|
|
19
|
+
* from HTML to Prismic rich text.
|
|
17
20
|
*/
|
|
18
21
|
export type RichTextHTMLMapSerializerShorthand =
|
|
19
22
|
| Exclude<RichTextNodeTypes, "label" | "span">
|
|
20
23
|
| { label: string }
|
|
21
24
|
|
|
22
|
-
/**
|
|
23
|
-
* The payload provided to a {@link RichTextHTMLMapSerializerFunction}.
|
|
24
|
-
*/
|
|
25
|
+
/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */
|
|
25
26
|
type RichTextHTMLMapSerializerFunctionPayload = {
|
|
26
|
-
/**
|
|
27
|
-
* The hast {@link Element} node to serialize.
|
|
28
|
-
*/
|
|
27
|
+
/** The hast {@link Element} node to serialize. */
|
|
29
28
|
node: Element
|
|
30
29
|
|
|
31
|
-
/**
|
|
32
|
-
* Additional context information to help with the serialization.
|
|
33
|
-
*/
|
|
30
|
+
/** Additional context information to help with the serialization. */
|
|
34
31
|
context: {
|
|
35
|
-
/**
|
|
36
|
-
* The list type of the last list node encountered if any.
|
|
37
|
-
*/
|
|
32
|
+
/** The list type of the last list node encountered if any. */
|
|
38
33
|
listType?: "group-list-item" | "group-o-list-item"
|
|
39
34
|
}
|
|
40
35
|
}
|
|
41
36
|
|
|
42
37
|
/**
|
|
43
|
-
* Serializes a hast {@link Element} node to a
|
|
44
|
-
*
|
|
38
|
+
* Serializes a hast {@link Element} node to a {@link RichTextHTMLMapSerializerShorthand} or a rich
|
|
39
|
+
* text node.
|
|
45
40
|
*
|
|
46
41
|
* @remarks
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* {@link RichTextHTMLMapSerializerShorthand} instead.
|
|
42
|
+
* Serializing to a rich text node directly is not recommended and is only available as an escape
|
|
43
|
+
* hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.
|
|
50
44
|
*/
|
|
51
45
|
export type RichTextHTMLMapSerializerFunction = (
|
|
52
46
|
payload: RichTextHTMLMapSerializerFunctionPayload,
|
|
53
47
|
) =>
|
|
54
48
|
| RichTextHTMLMapSerializerShorthand
|
|
55
|
-
| RTNode
|
|
56
|
-
| RTInlineNode
|
|
57
|
-
| RTPartialInlineNode
|
|
49
|
+
| InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode>
|
|
58
50
|
| undefined
|
|
59
51
|
|
|
60
52
|
/**
|
|
61
|
-
* Serializes a hast {@link Element} node matching the given HTML tag name or CSS
|
|
62
|
-
*
|
|
53
|
+
* Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a
|
|
54
|
+
* Prismic rich text node.
|
|
63
55
|
*
|
|
64
56
|
* @remarks
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* {@link HTMLRichTextMapSerializer} type instead.
|
|
57
|
+
* This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich
|
|
58
|
+
* text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.
|
|
68
59
|
*/
|
|
69
60
|
export type RichTextHTMLMapSerializer = Record<
|
|
70
61
|
string,
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { CustomTypeModelRichTextField, RichTextField, TitleField } from "@prismicio/client";
|
|
2
|
-
/**
|
|
3
|
-
* Filter a rich text field based on given model.
|
|
4
|
-
*
|
|
5
|
-
* @param richTextField - A rich text or title field from Prismic.
|
|
6
|
-
* @param model - A rich text or title field custom type model.
|
|
7
|
-
*
|
|
8
|
-
* @returns A rich text or title field filtered based on the given model.
|
|
9
|
-
*/
|
|
10
|
-
export declare const filterRichTextField: <Field extends RichTextField | TitleField>(richTextField: Field, model: CustomTypeModelRichTextField) => Field;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { RTEmbedNode, RTImageNode, RTLinkNode } from "@prismicio/client";
|
|
2
|
-
import type { Element } from "hast";
|
|
3
|
-
import type { RTPartialInlineNode } from "./RichTextFieldBuilder";
|
|
4
|
-
/**
|
|
5
|
-
* Serializes a hast {@link Element} node to a {@link RTImageNode}.
|
|
6
|
-
*
|
|
7
|
-
* @param node - A hast node to serialize.
|
|
8
|
-
*
|
|
9
|
-
* @returns Equivalent {@link RTImageNode}.
|
|
10
|
-
*/
|
|
11
|
-
export declare const serializeImage: (node: Element) => RTImageNode;
|
|
12
|
-
/**
|
|
13
|
-
* Serializes a hast {@link Element} node to a {@link RTEmbedNode}.
|
|
14
|
-
*
|
|
15
|
-
* @param node - A hast node to serialize.
|
|
16
|
-
*
|
|
17
|
-
* @returns Equivalent {@link RTEmbedNode}.
|
|
18
|
-
*/
|
|
19
|
-
export declare const serializeEmbed: (node: Element) => RTEmbedNode;
|
|
20
|
-
/**
|
|
21
|
-
* Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.
|
|
22
|
-
*
|
|
23
|
-
* @param node - A hast node to serialize.
|
|
24
|
-
* @param rtPartialInlineNode - A partial rich text node.
|
|
25
|
-
*
|
|
26
|
-
* @returns Equivalent {@link RTPartialInlineNode}.
|
|
27
|
-
*/
|
|
28
|
-
export declare const serializeSpan: (node: Element, rtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, "start" | "end" | "data">) => RTPartialInlineNode;
|