@prismicio/client 7.2.0 → 7.3.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.
- package/dist/createClient.cjs +9 -1
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.js +9 -1
- package/dist/createClient.js.map +1 -1
- package/dist/errors/ForbiddenError.cjs.map +1 -1
- package/dist/errors/ForbiddenError.d.ts +1 -1
- package/dist/errors/ForbiddenError.js.map +1 -1
- package/dist/errors/NotFoundError.cjs.map +1 -1
- package/dist/errors/NotFoundError.d.ts +1 -1
- package/dist/errors/NotFoundError.js.map +1 -1
- package/dist/errors/ParsingError.cjs.map +1 -1
- package/dist/errors/ParsingError.d.ts +1 -1
- package/dist/errors/ParsingError.js.map +1 -1
- package/dist/errors/RefExpiredError.cjs +7 -0
- package/dist/errors/RefExpiredError.cjs.map +1 -0
- package/dist/errors/RefExpiredError.d.ts +8 -0
- package/dist/errors/RefExpiredError.js +7 -0
- package/dist/errors/RefExpiredError.js.map +1 -0
- package/dist/errors/RefNotFoundError.cjs +7 -0
- package/dist/errors/RefNotFoundError.cjs.map +1 -0
- package/dist/errors/RefNotFoundError.d.ts +8 -0
- package/dist/errors/RefNotFoundError.js +7 -0
- package/dist/errors/RefNotFoundError.js.map +1 -0
- package/dist/helpers/asHTML.cjs +43 -50
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.d.ts +31 -6
- package/dist/helpers/asHTML.js +42 -49
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.d.ts +1 -1
- package/dist/helpers/asImageSrc.d.ts +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +1 -1
- package/dist/helpers/asImageWidthSrcSet.js +1 -1
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLinkAttrs.d.ts +1 -1
- package/dist/helpers/asText.cjs +2 -2
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.js +1 -1
- package/dist/helpers/asText.js.map +1 -1
- package/dist/helpers/isFilled.d.ts +10 -13
- package/dist/index.cjs +8 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +28 -2
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/serializerHelpers.cjs +91 -35
- package/dist/lib/serializerHelpers.cjs.map +1 -1
- package/dist/lib/serializerHelpers.d.ts +8 -8
- package/dist/lib/serializerHelpers.js +91 -35
- package/dist/lib/serializerHelpers.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/richtext/asText.cjs +13 -0
- package/dist/richtext/asText.cjs.map +1 -0
- package/dist/richtext/asText.d.ts +12 -0
- package/dist/richtext/asText.js +13 -0
- package/dist/richtext/asText.js.map +1 -0
- package/dist/richtext/asTree.cjs +123 -0
- package/dist/richtext/asTree.cjs.map +1 -0
- package/dist/richtext/asTree.d.ts +14 -0
- package/dist/richtext/asTree.js +123 -0
- package/dist/richtext/asTree.js.map +1 -0
- package/dist/richtext/composeSerializers.cjs +17 -0
- package/dist/richtext/composeSerializers.cjs.map +1 -0
- package/dist/richtext/composeSerializers.d.ts +16 -0
- package/dist/richtext/composeSerializers.js +17 -0
- package/dist/richtext/composeSerializers.js.map +1 -0
- package/dist/richtext/index.d.ts +7 -0
- package/dist/richtext/serialize.cjs +19 -0
- package/dist/richtext/serialize.cjs.map +1 -0
- package/dist/richtext/serialize.d.ts +19 -0
- package/dist/richtext/serialize.js +19 -0
- package/dist/richtext/serialize.js.map +1 -0
- package/dist/richtext/types.cjs +11 -0
- package/dist/richtext/types.cjs.map +1 -0
- package/dist/richtext/types.d.ts +71 -0
- package/dist/richtext/types.js +11 -0
- package/dist/richtext/types.js.map +1 -0
- package/dist/richtext/wrapMapSerializer.cjs +24 -0
- package/dist/richtext/wrapMapSerializer.cjs.map +1 -0
- package/dist/richtext/wrapMapSerializer.d.ts +15 -0
- package/dist/richtext/wrapMapSerializer.js +24 -0
- package/dist/richtext/wrapMapSerializer.js.map +1 -0
- package/dist/richtext.cjs +15 -0
- package/dist/richtext.cjs.map +1 -0
- package/dist/richtext.js +15 -0
- package/dist/richtext.js.map +1 -0
- package/dist/types/value/image.d.ts +9 -0
- package/dist/types/value/linkToMedia.d.ts +1 -0
- package/dist/types/value/richText.cjs.map +1 -1
- package/dist/types/value/richText.d.ts +7 -0
- package/dist/types/value/richText.js.map +1 -1
- package/dist/types/value/sharedSlice.d.ts +1 -1
- package/dist/types/value/slice.d.ts +1 -1
- package/package.json +34 -15
- package/src/createClient.ts +17 -4
- package/src/errors/ForbiddenError.ts +5 -3
- package/src/errors/NotFoundError.ts +3 -1
- package/src/errors/ParsingError.ts +3 -1
- package/src/errors/RefExpiredError.ts +14 -0
- package/src/errors/RefNotFoundError.ts +14 -0
- package/src/helpers/asHTML.ts +166 -76
- package/src/helpers/asImageWidthSrcSet.ts +2 -0
- package/src/helpers/asText.ts +2 -2
- package/src/index.ts +9 -4
- package/src/lib/serializerHelpers.ts +140 -67
- package/src/richtext/asText.ts +27 -0
- package/src/richtext/asTree.ts +211 -0
- package/src/richtext/composeSerializers.ts +36 -0
- package/src/richtext/index.ts +14 -0
- package/src/richtext/serialize.ts +54 -0
- package/src/richtext/types.ts +153 -0
- package/src/richtext/wrapMapSerializer.ts +46 -0
- package/src/types/value/image.ts +9 -0
- package/src/types/value/linkToMedia.ts +1 -0
- package/src/types/value/richText.ts +7 -0
- package/src/types/value/sharedSlice.ts +1 -1
- package/src/types/value/slice.ts +1 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { RTTextNode, RichTextField } from "../types/value/richText";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Serializes a rich text or title field to a plain text string
|
|
5
|
+
*
|
|
6
|
+
* @param richTextField - A rich text or title field from Prismic
|
|
7
|
+
* @param separator - Separator used to join each element, defaults to a space
|
|
8
|
+
*
|
|
9
|
+
* @returns Plain text equivalent of the provided rich text or title field
|
|
10
|
+
*
|
|
11
|
+
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
|
|
12
|
+
*/
|
|
13
|
+
export const asText = (
|
|
14
|
+
richTextField: RichTextField,
|
|
15
|
+
separator = " ",
|
|
16
|
+
): string => {
|
|
17
|
+
let result = "";
|
|
18
|
+
|
|
19
|
+
for (let i = 0; i < richTextField.length; i++) {
|
|
20
|
+
if ("text" in richTextField[i]) {
|
|
21
|
+
result +=
|
|
22
|
+
(result ? separator : "") + (richTextField[i] as RTTextNode).text;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RTAnyNode,
|
|
3
|
+
RTBlockNode,
|
|
4
|
+
RTInlineNode,
|
|
5
|
+
RTListItemNode,
|
|
6
|
+
RTNode,
|
|
7
|
+
RTOListItemNode,
|
|
8
|
+
RTTextNode,
|
|
9
|
+
RichTextNodeType,
|
|
10
|
+
} from "../types/value/richText";
|
|
11
|
+
import { Tree, TreeNode } from "./types";
|
|
12
|
+
|
|
13
|
+
const uuid = (): string => {
|
|
14
|
+
return (++uuid.i).toString();
|
|
15
|
+
};
|
|
16
|
+
uuid.i = 0;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Parses a rich text or title field into a tree
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* This is a low level helper mainly intended to be used by higher level
|
|
23
|
+
* packages. Most users aren't expected to this function directly.
|
|
24
|
+
*
|
|
25
|
+
* @param nodes - A rich text or title field from Prismic
|
|
26
|
+
*
|
|
27
|
+
* @returns Tree from given rich text or title field
|
|
28
|
+
*/
|
|
29
|
+
export const asTree = (nodes: RTNode[]): Tree => {
|
|
30
|
+
const preparedNodes = prepareNodes(nodes);
|
|
31
|
+
|
|
32
|
+
const children: TreeNode[] = [];
|
|
33
|
+
for (let i = 0; i < preparedNodes.length; i++) {
|
|
34
|
+
children.push(nodeToTreeNode(preparedNodes[i]));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
key: uuid(),
|
|
39
|
+
children,
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const createTreeNode = (
|
|
44
|
+
node: RTAnyNode,
|
|
45
|
+
children: TreeNode[] = [],
|
|
46
|
+
): TreeNode => {
|
|
47
|
+
return {
|
|
48
|
+
key: uuid(),
|
|
49
|
+
type: node.type,
|
|
50
|
+
text: "text" in node ? node.text : undefined,
|
|
51
|
+
node,
|
|
52
|
+
children,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const createTextTreeNode = (text: string): TreeNode => {
|
|
57
|
+
return createTreeNode({
|
|
58
|
+
type: RichTextNodeType.span,
|
|
59
|
+
text,
|
|
60
|
+
spans: [],
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const prepareNodes = (nodes: RTNode[]): RTBlockNode[] => {
|
|
65
|
+
const mutNodes: RTBlockNode[] = nodes.slice(0);
|
|
66
|
+
|
|
67
|
+
for (let i = 0; i < mutNodes.length; i++) {
|
|
68
|
+
const node = mutNodes[i];
|
|
69
|
+
|
|
70
|
+
if (
|
|
71
|
+
node.type === RichTextNodeType.listItem ||
|
|
72
|
+
node.type === RichTextNodeType.oListItem
|
|
73
|
+
) {
|
|
74
|
+
const items: (RTListItemNode | RTOListItemNode)[] = [
|
|
75
|
+
node as RTListItemNode | RTOListItemNode,
|
|
76
|
+
];
|
|
77
|
+
|
|
78
|
+
while (mutNodes[i + 1] && mutNodes[i + 1].type === node.type) {
|
|
79
|
+
items.push(mutNodes[i + 1] as RTListItemNode | RTOListItemNode);
|
|
80
|
+
mutNodes.splice(i, 1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (node.type === RichTextNodeType.listItem) {
|
|
84
|
+
mutNodes[i] = {
|
|
85
|
+
type: RichTextNodeType.list,
|
|
86
|
+
items: items as RTListItemNode[],
|
|
87
|
+
};
|
|
88
|
+
} else {
|
|
89
|
+
mutNodes[i] = {
|
|
90
|
+
type: RichTextNodeType.oList,
|
|
91
|
+
items: items as RTOListItemNode[],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return mutNodes;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const nodeToTreeNode = (node: RTBlockNode): TreeNode => {
|
|
101
|
+
if ("text" in node) {
|
|
102
|
+
return createTreeNode(
|
|
103
|
+
node,
|
|
104
|
+
textNodeSpansToTreeNodeChildren(node.spans, node),
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if ("items" in node) {
|
|
109
|
+
const children: TreeNode[] = [];
|
|
110
|
+
for (let i = 0; i < node.items.length; i++) {
|
|
111
|
+
children.push(nodeToTreeNode(node.items[i]));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return createTreeNode(node, children);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return createTreeNode(node);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const textNodeSpansToTreeNodeChildren = (
|
|
121
|
+
spans: RTInlineNode[],
|
|
122
|
+
node: RTTextNode,
|
|
123
|
+
parentSpan?: RTInlineNode,
|
|
124
|
+
): TreeNode[] => {
|
|
125
|
+
if (!spans.length) {
|
|
126
|
+
return [createTextTreeNode(node.text)];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const mutSpans: RTInlineNode[] = spans.slice(0);
|
|
130
|
+
|
|
131
|
+
// Sort spans using the following criteria:
|
|
132
|
+
//
|
|
133
|
+
// 1. By start index (ascending)
|
|
134
|
+
// 2. If start indices are equal, by end index (descending)
|
|
135
|
+
//
|
|
136
|
+
// If start and end indices of more than one span are equal, use what
|
|
137
|
+
// the API gives without modifications.
|
|
138
|
+
//
|
|
139
|
+
// Sorting using this algorithm ensures proper detection of child
|
|
140
|
+
// spans.
|
|
141
|
+
mutSpans.sort((a, b) => a.start - b.start || b.end - a.end);
|
|
142
|
+
|
|
143
|
+
const children: TreeNode[] = [];
|
|
144
|
+
|
|
145
|
+
for (let i = 0; i < mutSpans.length; i++) {
|
|
146
|
+
const span = mutSpans[i];
|
|
147
|
+
const parentSpanStart = (parentSpan && parentSpan.start) || 0;
|
|
148
|
+
const spanStart = span.start - parentSpanStart;
|
|
149
|
+
const spanEnd = span.end - parentSpanStart;
|
|
150
|
+
const text = node.text.slice(spanStart, spanEnd);
|
|
151
|
+
|
|
152
|
+
const childSpans: RTInlineNode[] = [];
|
|
153
|
+
for (let j = i; j < mutSpans.length; j++) {
|
|
154
|
+
const siblingSpan = mutSpans[j];
|
|
155
|
+
|
|
156
|
+
if (siblingSpan !== span) {
|
|
157
|
+
if (siblingSpan.start >= span.start && siblingSpan.end <= span.end) {
|
|
158
|
+
childSpans.push(siblingSpan);
|
|
159
|
+
mutSpans.splice(j, 1);
|
|
160
|
+
j--;
|
|
161
|
+
} else if (
|
|
162
|
+
siblingSpan.start < span.end &&
|
|
163
|
+
siblingSpan.end > span.start
|
|
164
|
+
) {
|
|
165
|
+
childSpans.push({
|
|
166
|
+
...siblingSpan,
|
|
167
|
+
end: span.end,
|
|
168
|
+
});
|
|
169
|
+
mutSpans[j] = {
|
|
170
|
+
...siblingSpan,
|
|
171
|
+
start: span.end,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (i === 0 && spanStart > 0) {
|
|
178
|
+
children.push(createTextTreeNode(node.text.slice(0, spanStart)));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const spanWithText = { ...span, text };
|
|
182
|
+
children.push(
|
|
183
|
+
createTreeNode(
|
|
184
|
+
spanWithText,
|
|
185
|
+
textNodeSpansToTreeNodeChildren(
|
|
186
|
+
childSpans,
|
|
187
|
+
{
|
|
188
|
+
...node,
|
|
189
|
+
text,
|
|
190
|
+
},
|
|
191
|
+
span,
|
|
192
|
+
),
|
|
193
|
+
),
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
if (spanEnd < node.text.length) {
|
|
197
|
+
children.push(
|
|
198
|
+
createTextTreeNode(
|
|
199
|
+
node.text.slice(
|
|
200
|
+
spanEnd,
|
|
201
|
+
mutSpans[i + 1]
|
|
202
|
+
? mutSpans[i + 1].start - parentSpanStart
|
|
203
|
+
: undefined,
|
|
204
|
+
),
|
|
205
|
+
),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return children;
|
|
211
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { RichTextFunctionSerializer } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Takes an array of serializers and returns a serializer applying provided
|
|
5
|
+
* serializers sequentially until a result is returned
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* This is a low level helper mainly intended to be used by higher level
|
|
9
|
+
* packages Most users aren't expected to this function directly
|
|
10
|
+
*
|
|
11
|
+
* @typeParam SerializerReturnType - Return type of serializers
|
|
12
|
+
*
|
|
13
|
+
* @param serializers - Serializers to compose
|
|
14
|
+
*
|
|
15
|
+
* @returns Composed serializer
|
|
16
|
+
*/
|
|
17
|
+
export const composeSerializers = <SerializerReturnType>(
|
|
18
|
+
...serializers: (
|
|
19
|
+
| RichTextFunctionSerializer<SerializerReturnType>
|
|
20
|
+
| undefined
|
|
21
|
+
)[]
|
|
22
|
+
): RichTextFunctionSerializer<SerializerReturnType> => {
|
|
23
|
+
return (...args) => {
|
|
24
|
+
for (let i = 0; i < serializers.length; i++) {
|
|
25
|
+
const serializer = serializers[i];
|
|
26
|
+
|
|
27
|
+
if (serializer) {
|
|
28
|
+
const res = serializer(...args);
|
|
29
|
+
|
|
30
|
+
if (res != null) {
|
|
31
|
+
return res;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { asTree } from "./asTree";
|
|
2
|
+
export { asText } from "./asText";
|
|
3
|
+
|
|
4
|
+
export { serialize } from "./serialize";
|
|
5
|
+
export { wrapMapSerializer } from "./wrapMapSerializer";
|
|
6
|
+
export { composeSerializers } from "./composeSerializers";
|
|
7
|
+
|
|
8
|
+
export { RichTextNodeType as Element } from "../types/value/richText";
|
|
9
|
+
|
|
10
|
+
export type {
|
|
11
|
+
RichTextFunctionSerializer,
|
|
12
|
+
RichTextMapSerializer,
|
|
13
|
+
RichTextMapSerializerFunction,
|
|
14
|
+
} from "./types";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { RichTextField } from "../types/value/richText";
|
|
2
|
+
import { RichTextFunctionSerializer, TreeNode } from "./types";
|
|
3
|
+
|
|
4
|
+
import { asTree } from "./asTree";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Serializes a rich text or title field with a given serializer
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* This is a low level helper mainly intended to be used by higher level
|
|
11
|
+
* packages Most users aren't expected to this function directly
|
|
12
|
+
*
|
|
13
|
+
* @typeParam SerializerReturnType - Return type of the serializer
|
|
14
|
+
*
|
|
15
|
+
* @param richTextField - A rich text or title field from Prismic
|
|
16
|
+
* @param serializer - A function serializer to apply
|
|
17
|
+
*
|
|
18
|
+
* @returns An array of serialized nodes
|
|
19
|
+
*
|
|
20
|
+
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
|
|
21
|
+
*/
|
|
22
|
+
export const serialize = <SerializerReturnType>(
|
|
23
|
+
richTextField: RichTextField,
|
|
24
|
+
serializer: RichTextFunctionSerializer<SerializerReturnType>,
|
|
25
|
+
): SerializerReturnType[] => {
|
|
26
|
+
return serializeTreeNodes<SerializerReturnType>(
|
|
27
|
+
asTree(richTextField).children,
|
|
28
|
+
serializer,
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const serializeTreeNodes = <T>(
|
|
33
|
+
nodes: TreeNode[],
|
|
34
|
+
serializer: RichTextFunctionSerializer<T>,
|
|
35
|
+
): T[] => {
|
|
36
|
+
const serializedTreeNodes: T[] = [];
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
39
|
+
const treeNode = nodes[i];
|
|
40
|
+
const serializedTreeNode = serializer(
|
|
41
|
+
treeNode.type,
|
|
42
|
+
treeNode.node,
|
|
43
|
+
treeNode.text,
|
|
44
|
+
serializeTreeNodes(treeNode.children, serializer),
|
|
45
|
+
treeNode.key,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
if (serializedTreeNode != null) {
|
|
49
|
+
serializedTreeNodes.push(serializedTreeNode);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return serializedTreeNodes;
|
|
54
|
+
};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RTAnyNode,
|
|
3
|
+
RTEmNode,
|
|
4
|
+
RTEmbedNode,
|
|
5
|
+
RTHeading1Node,
|
|
6
|
+
RTHeading2Node,
|
|
7
|
+
RTHeading3Node,
|
|
8
|
+
RTHeading4Node,
|
|
9
|
+
RTHeading5Node,
|
|
10
|
+
RTHeading6Node,
|
|
11
|
+
RTImageNode,
|
|
12
|
+
RTLabelNode,
|
|
13
|
+
RTLinkNode,
|
|
14
|
+
RTListItemNode,
|
|
15
|
+
RTListNode,
|
|
16
|
+
RTOListItemNode,
|
|
17
|
+
RTOListNode,
|
|
18
|
+
RTParagraphNode,
|
|
19
|
+
RTPreformattedNode,
|
|
20
|
+
RTSpanNode,
|
|
21
|
+
RTStrongNode,
|
|
22
|
+
RichTextNodeType,
|
|
23
|
+
} from "../types/value/richText";
|
|
24
|
+
|
|
25
|
+
// Serializers
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Serializes a node from a rich text or title field with a function
|
|
29
|
+
*
|
|
30
|
+
* @typeParam ReturnType - Return type of the function serializer
|
|
31
|
+
*
|
|
32
|
+
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
|
|
33
|
+
*/
|
|
34
|
+
export type RichTextFunctionSerializer<ReturnType> = (
|
|
35
|
+
type: (typeof RichTextNodeType)[keyof typeof RichTextNodeType],
|
|
36
|
+
node: RTAnyNode,
|
|
37
|
+
text: string | undefined,
|
|
38
|
+
children: ReturnType[],
|
|
39
|
+
key: string,
|
|
40
|
+
) => ReturnType | null | undefined;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Map serializer's tag function serializer, can be helpful for typing those
|
|
44
|
+
* handlers
|
|
45
|
+
*
|
|
46
|
+
* @typeParam ReturnType - Return type of the tag serializer
|
|
47
|
+
*/
|
|
48
|
+
export type RichTextMapSerializerFunction<
|
|
49
|
+
ReturnType,
|
|
50
|
+
Node extends RTAnyNode = RTAnyNode,
|
|
51
|
+
TextType = string | undefined,
|
|
52
|
+
> = (payload: {
|
|
53
|
+
type: Node["type"];
|
|
54
|
+
node: Node;
|
|
55
|
+
text: TextType;
|
|
56
|
+
children: ReturnType[];
|
|
57
|
+
key: string;
|
|
58
|
+
}) => ReturnType | null | undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Serializes a node from a rich text or title field with a map
|
|
62
|
+
*
|
|
63
|
+
* @remarks
|
|
64
|
+
* This type of serializer needs to be processed through
|
|
65
|
+
* {@link wrapMapSerializer} before being used with {@link serialize}
|
|
66
|
+
*
|
|
67
|
+
* @typeParam ReturnType - Return type of the map serializer
|
|
68
|
+
*
|
|
69
|
+
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
|
|
70
|
+
*/
|
|
71
|
+
export type RichTextMapSerializer<ReturnType> = {
|
|
72
|
+
heading1?: RichTextMapSerializerFunction<
|
|
73
|
+
ReturnType,
|
|
74
|
+
RTHeading1Node,
|
|
75
|
+
undefined
|
|
76
|
+
>;
|
|
77
|
+
heading2?: RichTextMapSerializerFunction<
|
|
78
|
+
ReturnType,
|
|
79
|
+
RTHeading2Node,
|
|
80
|
+
undefined
|
|
81
|
+
>;
|
|
82
|
+
heading3?: RichTextMapSerializerFunction<
|
|
83
|
+
ReturnType,
|
|
84
|
+
RTHeading3Node,
|
|
85
|
+
undefined
|
|
86
|
+
>;
|
|
87
|
+
heading4?: RichTextMapSerializerFunction<
|
|
88
|
+
ReturnType,
|
|
89
|
+
RTHeading4Node,
|
|
90
|
+
undefined
|
|
91
|
+
>;
|
|
92
|
+
heading5?: RichTextMapSerializerFunction<
|
|
93
|
+
ReturnType,
|
|
94
|
+
RTHeading5Node,
|
|
95
|
+
undefined
|
|
96
|
+
>;
|
|
97
|
+
heading6?: RichTextMapSerializerFunction<
|
|
98
|
+
ReturnType,
|
|
99
|
+
RTHeading6Node,
|
|
100
|
+
undefined
|
|
101
|
+
>;
|
|
102
|
+
paragraph?: RichTextMapSerializerFunction<
|
|
103
|
+
ReturnType,
|
|
104
|
+
RTParagraphNode,
|
|
105
|
+
undefined
|
|
106
|
+
>;
|
|
107
|
+
preformatted?: RichTextMapSerializerFunction<
|
|
108
|
+
ReturnType,
|
|
109
|
+
RTPreformattedNode,
|
|
110
|
+
undefined
|
|
111
|
+
>;
|
|
112
|
+
strong?: RichTextMapSerializerFunction<ReturnType, RTStrongNode, string>;
|
|
113
|
+
em?: RichTextMapSerializerFunction<ReturnType, RTEmNode, string>;
|
|
114
|
+
listItem?: RichTextMapSerializerFunction<
|
|
115
|
+
ReturnType,
|
|
116
|
+
RTListItemNode,
|
|
117
|
+
undefined
|
|
118
|
+
>;
|
|
119
|
+
oListItem?: RichTextMapSerializerFunction<
|
|
120
|
+
ReturnType,
|
|
121
|
+
RTOListItemNode,
|
|
122
|
+
undefined
|
|
123
|
+
>;
|
|
124
|
+
list?: RichTextMapSerializerFunction<ReturnType, RTListNode, undefined>;
|
|
125
|
+
oList?: RichTextMapSerializerFunction<ReturnType, RTOListNode, undefined>;
|
|
126
|
+
image?: RichTextMapSerializerFunction<ReturnType, RTImageNode, undefined>;
|
|
127
|
+
embed?: RichTextMapSerializerFunction<ReturnType, RTEmbedNode, undefined>;
|
|
128
|
+
hyperlink?: RichTextMapSerializerFunction<ReturnType, RTLinkNode, string>;
|
|
129
|
+
label?: RichTextMapSerializerFunction<ReturnType, RTLabelNode, string>;
|
|
130
|
+
span?: RichTextMapSerializerFunction<ReturnType, RTSpanNode, string>;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Tree
|
|
134
|
+
export interface Tree {
|
|
135
|
+
key: string;
|
|
136
|
+
children: TreeNode[];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface TreeNode {
|
|
140
|
+
key: string;
|
|
141
|
+
type: (typeof RichTextNodeType)[keyof typeof RichTextNodeType];
|
|
142
|
+
text?: string;
|
|
143
|
+
node: RTAnyNode;
|
|
144
|
+
children: TreeNode[];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Helpers
|
|
148
|
+
export const RichTextReversedNodeType = {
|
|
149
|
+
[RichTextNodeType.listItem]: "listItem",
|
|
150
|
+
[RichTextNodeType.oListItem]: "oListItem",
|
|
151
|
+
[RichTextNodeType.list]: "list",
|
|
152
|
+
[RichTextNodeType.oList]: "oList",
|
|
153
|
+
} as const;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RichTextFunctionSerializer,
|
|
3
|
+
RichTextMapSerializer,
|
|
4
|
+
RichTextReversedNodeType,
|
|
5
|
+
} from "./types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wraps a map serializer into a regular function serializer
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This is a low level helper mainly intended to be used by higher level
|
|
12
|
+
* packages Most users aren't expected to this function directly
|
|
13
|
+
*
|
|
14
|
+
* @typeParam SerializerReturnType - Return type of the map serializer
|
|
15
|
+
*
|
|
16
|
+
* @param mapSerializer - Map serializer to wrap
|
|
17
|
+
*
|
|
18
|
+
* @returns A regular function serializer
|
|
19
|
+
*/
|
|
20
|
+
export const wrapMapSerializer = <SerializerReturnType>(
|
|
21
|
+
mapSerializer: RichTextMapSerializer<SerializerReturnType>,
|
|
22
|
+
): RichTextFunctionSerializer<SerializerReturnType> => {
|
|
23
|
+
return (type, node, text, children, key) => {
|
|
24
|
+
const tagSerializer: RichTextMapSerializer<SerializerReturnType>[keyof RichTextMapSerializer<SerializerReturnType>] =
|
|
25
|
+
mapSerializer[
|
|
26
|
+
(RichTextReversedNodeType[
|
|
27
|
+
type as keyof typeof RichTextReversedNodeType
|
|
28
|
+
] || type) as keyof RichTextMapSerializer<SerializerReturnType>
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
if (tagSerializer) {
|
|
32
|
+
return tagSerializer({
|
|
33
|
+
// @ts-expect-error cannot type check here
|
|
34
|
+
type,
|
|
35
|
+
// @ts-expect-error cannot type check here
|
|
36
|
+
node,
|
|
37
|
+
// @ts-expect-error cannot type check here
|
|
38
|
+
text,
|
|
39
|
+
// @ts-expect-error cannot type check here
|
|
40
|
+
children,
|
|
41
|
+
// @ts-expect-error cannot type check here
|
|
42
|
+
key,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
package/src/types/value/image.ts
CHANGED
|
@@ -12,18 +12,27 @@ export type ImageFieldImage<State extends FieldState = FieldState> =
|
|
|
12
12
|
State extends "empty" ? EmptyImageFieldImage : FilledImageFieldImage;
|
|
13
13
|
|
|
14
14
|
export interface FilledImageFieldImage {
|
|
15
|
+
id: string;
|
|
15
16
|
url: string;
|
|
16
17
|
dimensions: {
|
|
17
18
|
width: number;
|
|
18
19
|
height: number;
|
|
19
20
|
};
|
|
21
|
+
edit: {
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
24
|
+
zoom: number;
|
|
25
|
+
background: string;
|
|
26
|
+
};
|
|
20
27
|
alt: string | null;
|
|
21
28
|
copyright: string | null;
|
|
22
29
|
}
|
|
23
30
|
|
|
24
31
|
export interface EmptyImageFieldImage {
|
|
32
|
+
id?: null;
|
|
25
33
|
url?: null;
|
|
26
34
|
dimensions?: null;
|
|
35
|
+
edit?: null;
|
|
27
36
|
alt?: null;
|
|
28
37
|
copyright?: null;
|
|
29
38
|
}
|
|
@@ -153,6 +153,7 @@ export interface RTLabelNode extends RTSpanNodeBase {
|
|
|
153
153
|
*/
|
|
154
154
|
export type RTImageNode = {
|
|
155
155
|
type: typeof RichTextNodeType.image;
|
|
156
|
+
id: string;
|
|
156
157
|
url: string;
|
|
157
158
|
alt: string | null;
|
|
158
159
|
copyright: string | null;
|
|
@@ -160,6 +161,12 @@ export type RTImageNode = {
|
|
|
160
161
|
width: number;
|
|
161
162
|
height: number;
|
|
162
163
|
};
|
|
164
|
+
edit: {
|
|
165
|
+
x: number;
|
|
166
|
+
y: number;
|
|
167
|
+
zoom: number;
|
|
168
|
+
background: string;
|
|
169
|
+
};
|
|
163
170
|
linkTo?:
|
|
164
171
|
| FilledContentRelationshipField
|
|
165
172
|
| FilledLinkToWebField
|