@prismicio/migrate 0.0.1
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/LICENSE +202 -0
- package/README.md +96 -0
- package/dist/errors/PrismicRichTextError.cjs +6 -0
- package/dist/errors/PrismicRichTextError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +2 -0
- package/dist/errors/PrismicRichTextError.js +6 -0
- package/dist/errors/PrismicRichTextError.js.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs +7 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +3 -0
- package/dist/errors/PrismicRichTextSerializerError.js +7 -0
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -0
- package/dist/htmlAsRichText.cjs +11 -0
- package/dist/htmlAsRichText.cjs.map +1 -0
- package/dist/htmlAsRichText.d.ts +78 -0
- package/dist/htmlAsRichText.js +11 -0
- package/dist/htmlAsRichText.js.map +1 -0
- package/dist/index.cjs +9 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/RichTextFieldBuilder.cjs +115 -0
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +35 -0
- package/dist/lib/RichTextFieldBuilder.js +115 -0
- package/dist/lib/RichTextFieldBuilder.js.map +1 -0
- package/dist/lib/filterRichTextField.cjs +87 -0
- package/dist/lib/filterRichTextField.cjs.map +1 -0
- package/dist/lib/filterRichTextField.d.ts +10 -0
- package/dist/lib/filterRichTextField.js +87 -0
- package/dist/lib/filterRichTextField.js.map +1 -0
- package/dist/lib/hastSerializerHelpers.cjs +95 -0
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -0
- package/dist/lib/hastSerializerHelpers.d.ts +28 -0
- package/dist/lib/hastSerializerHelpers.js +95 -0
- package/dist/lib/hastSerializerHelpers.js.map +1 -0
- package/dist/lib/hastToRichText.cjs +164 -0
- package/dist/lib/hastToRichText.cjs.map +1 -0
- package/dist/lib/hastToRichText.d.ts +30 -0
- package/dist/lib/hastToRichText.js +164 -0
- package/dist/lib/hastToRichText.js.map +1 -0
- package/dist/lib/rehypeRichText.cjs +59 -0
- package/dist/lib/rehypeRichText.cjs.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +65 -0
- package/dist/lib/rehypeRichText.js +59 -0
- package/dist/lib/rehypeRichText.js.map +1 -0
- package/dist/types.d.ts +57 -0
- package/package.json +95 -0
- package/src/errors/PrismicRichTextError.ts +1 -0
- package/src/errors/PrismicRichTextSerializerError.ts +3 -0
- package/src/htmlAsRichText.ts +117 -0
- package/src/index.ts +7 -0
- package/src/lib/RichTextFieldBuilder.ts +164 -0
- package/src/lib/filterRichTextField.ts +158 -0
- package/src/lib/hastSerializerHelpers.ts +151 -0
- package/src/lib/hastToRichText.ts +294 -0
- package/src/lib/rehypeRichText.ts +185 -0
- package/src/types.ts +72 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const client = require("@prismicio/client");
|
|
4
|
+
const hastUtilToString = require("hast-util-to-string");
|
|
5
|
+
const unistUtilVisit = require("unist-util-visit");
|
|
6
|
+
const PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
|
|
7
|
+
const RichTextFieldBuilder = require("./RichTextFieldBuilder.cjs");
|
|
8
|
+
const hastSerializerHelpers = require("./hastSerializerHelpers.cjs");
|
|
9
|
+
const DEFAULT_SERIALIZER = {
|
|
10
|
+
h1: "heading1",
|
|
11
|
+
h2: "heading2",
|
|
12
|
+
h3: "heading3",
|
|
13
|
+
h4: "heading4",
|
|
14
|
+
h5: "heading5",
|
|
15
|
+
h6: "heading6",
|
|
16
|
+
p: "paragraph",
|
|
17
|
+
pre: "preformatted",
|
|
18
|
+
strong: "strong",
|
|
19
|
+
em: "em",
|
|
20
|
+
li: ({ context }) => context.listType === "group-o-list-item" ? "o-list-item" : "list-item",
|
|
21
|
+
ul: "group-list-item",
|
|
22
|
+
ol: "group-o-list-item",
|
|
23
|
+
img: "image",
|
|
24
|
+
iframe: "embed",
|
|
25
|
+
a: "hyperlink"
|
|
26
|
+
};
|
|
27
|
+
const VFILE_RULE = "failed-to-serialize-node";
|
|
28
|
+
const VFILE_SOURCE = "prismic";
|
|
29
|
+
const hastToRichText = (tree, file, config) => {
|
|
30
|
+
const builder = new RichTextFieldBuilder.RichTextFieldBuilder();
|
|
31
|
+
const serializer = {
|
|
32
|
+
...DEFAULT_SERIALIZER,
|
|
33
|
+
...config == null ? void 0 : config.serializer
|
|
34
|
+
};
|
|
35
|
+
let lastRTTextNodeType = client.RichTextNodeType.paragraph;
|
|
36
|
+
let lastListType = void 0;
|
|
37
|
+
unistUtilVisit.visit(tree, (node) => {
|
|
38
|
+
if (node.type === "element") {
|
|
39
|
+
if (node.tagName === "br") {
|
|
40
|
+
try {
|
|
41
|
+
builder.appendText("\n");
|
|
42
|
+
} catch (error) {
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
let serializerOrShorthand;
|
|
46
|
+
if (node.matchesSerializer && node.matchesSerializer in serializer) {
|
|
47
|
+
serializerOrShorthand = serializer[node.matchesSerializer];
|
|
48
|
+
} else if (node.tagName in serializer) {
|
|
49
|
+
serializerOrShorthand = serializer[node.tagName];
|
|
50
|
+
} else {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
let shorthand;
|
|
54
|
+
if (typeof serializerOrShorthand === "function") {
|
|
55
|
+
const shorthandOrNode = serializerOrShorthand({
|
|
56
|
+
node,
|
|
57
|
+
context: { listType: lastListType }
|
|
58
|
+
});
|
|
59
|
+
if (!shorthandOrNode) {
|
|
60
|
+
return;
|
|
61
|
+
} else if (typeof shorthandOrNode === "object" && "type" in shorthandOrNode) {
|
|
62
|
+
switch (shorthandOrNode.type) {
|
|
63
|
+
case client.RichTextNodeType.strong:
|
|
64
|
+
case client.RichTextNodeType.em:
|
|
65
|
+
case client.RichTextNodeType.label:
|
|
66
|
+
case client.RichTextNodeType.hyperlink: {
|
|
67
|
+
const length = hastUtilToString.toString(node).trimEnd().length;
|
|
68
|
+
try {
|
|
69
|
+
builder.appendSpan(shorthandOrNode, length);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
72
|
+
builder.appendSpan(shorthandOrNode, length);
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
case client.RichTextNodeType.image:
|
|
77
|
+
case client.RichTextNodeType.embed:
|
|
78
|
+
builder.appendNode(shorthandOrNode);
|
|
79
|
+
return;
|
|
80
|
+
default:
|
|
81
|
+
lastRTTextNodeType = shorthandOrNode.type;
|
|
82
|
+
builder.appendNode(shorthandOrNode);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
shorthand = shorthandOrNode;
|
|
87
|
+
} else {
|
|
88
|
+
shorthand = serializerOrShorthand;
|
|
89
|
+
}
|
|
90
|
+
let match;
|
|
91
|
+
if (typeof shorthand === "string") {
|
|
92
|
+
match = { type: shorthand };
|
|
93
|
+
} else {
|
|
94
|
+
match = { type: client.RichTextNodeType.label, data: shorthand };
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
switch (match.type) {
|
|
98
|
+
case client.RichTextNodeType.heading1:
|
|
99
|
+
case client.RichTextNodeType.heading2:
|
|
100
|
+
case client.RichTextNodeType.heading3:
|
|
101
|
+
case client.RichTextNodeType.heading4:
|
|
102
|
+
case client.RichTextNodeType.heading5:
|
|
103
|
+
case client.RichTextNodeType.heading6:
|
|
104
|
+
case client.RichTextNodeType.paragraph:
|
|
105
|
+
case client.RichTextNodeType.preformatted:
|
|
106
|
+
case client.RichTextNodeType.listItem:
|
|
107
|
+
case client.RichTextNodeType.oListItem:
|
|
108
|
+
lastRTTextNodeType = match.type;
|
|
109
|
+
builder.appendTextNode(match.type, config == null ? void 0 : config.direction);
|
|
110
|
+
break;
|
|
111
|
+
case client.RichTextNodeType.list:
|
|
112
|
+
case client.RichTextNodeType.oList:
|
|
113
|
+
lastListType = match.type;
|
|
114
|
+
break;
|
|
115
|
+
case client.RichTextNodeType.image:
|
|
116
|
+
builder.appendNode(hastSerializerHelpers.serializeImage(node));
|
|
117
|
+
break;
|
|
118
|
+
case client.RichTextNodeType.embed:
|
|
119
|
+
builder.appendNode(hastSerializerHelpers.serializeEmbed(node));
|
|
120
|
+
break;
|
|
121
|
+
case client.RichTextNodeType.strong:
|
|
122
|
+
case client.RichTextNodeType.em:
|
|
123
|
+
case client.RichTextNodeType.label:
|
|
124
|
+
case client.RichTextNodeType.hyperlink: {
|
|
125
|
+
const span = hastSerializerHelpers.serializeSpan(node, match);
|
|
126
|
+
const length = hastUtilToString.toString(node).trimEnd().length;
|
|
127
|
+
try {
|
|
128
|
+
builder.appendSpan(span, length);
|
|
129
|
+
} catch (error) {
|
|
130
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
131
|
+
builder.appendSpan(span, length);
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
default:
|
|
136
|
+
throw new Error(`Unknown rich text node type: \`${match.type}\``);
|
|
137
|
+
}
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (error instanceof PrismicRichTextSerializerError.PrismicRichTextSerializerError) {
|
|
140
|
+
file.message(error.message, {
|
|
141
|
+
cause: error,
|
|
142
|
+
place: node.position,
|
|
143
|
+
ruleId: VFILE_RULE,
|
|
144
|
+
source: VFILE_SOURCE
|
|
145
|
+
});
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
} else if (node.type === "text") {
|
|
151
|
+
if (!(node.value.replace(/[ \t\n\f\r]/g, "") === "")) {
|
|
152
|
+
try {
|
|
153
|
+
builder.appendText(node.value);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
156
|
+
builder.appendText(node.value);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return builder.build();
|
|
162
|
+
};
|
|
163
|
+
exports.hastToRichText = hastToRichText;
|
|
164
|
+
//# sourceMappingURL=hastToRichText.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hastToRichText.cjs","sources":["../../../src/lib/hastToRichText.ts"],"sourcesContent":["import type {\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTLabelNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\nimport { RichTextNodeType } from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { toString } from \"hast-util-to-string\"\nimport { visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport type {\n\tRichTextHTMLMapSerializer,\n\tRichTextHTMLMapSerializerFunction,\n\tRichTextHTMLMapSerializerShorthand,\n} from \"../types\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\n\nimport { RichTextFieldBuilder } from \"./RichTextFieldBuilder\"\nimport {\n\tserializeEmbed,\n\tserializeImage,\n\tserializeSpan,\n} from \"./hastSerializerHelpers\"\n\n/**\n * Pick keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-pick.d.ts\n */\ntype DistributedPick<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Pick<ObjectType, KeyType> : never\n\nconst DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {\n\th1: \"heading1\",\n\th2: \"heading2\",\n\th3: \"heading3\",\n\th4: \"heading4\",\n\th5: \"heading5\",\n\th6: \"heading6\",\n\tp: \"paragraph\",\n\tpre: \"preformatted\",\n\tstrong: \"strong\",\n\tem: \"em\",\n\tli: ({ context }) =>\n\t\tcontext.listType === \"group-o-list-item\" ? \"o-list-item\" : \"list-item\",\n\tul: \"group-list-item\",\n\tol: \"group-o-list-item\",\n\timg: \"image\",\n\tiframe: \"embed\",\n\ta: \"hyperlink\",\n}\n\nconst VFILE_RULE = \"failed-to-serialize-node\"\nconst VFILE_SOURCE = \"prismic\"\n\n/**\n * Configuration that determines the output of `toRichText`.\n */\nexport type HASTToRichTextConfig = {\n\t/**\n\t * An optional HTML to rich text serializer. Will be merged with the default\n\t * HTML to rich text serializer.\n\t */\n\tserializer?: RichTextHTMLMapSerializer\n\n\t/**\n\t * Whether or not the text processed should be marked as right-to-left.\n\t *\n\t * @defaultValue `false`\n\t */\n\tdirection?: \"ltr\" | \"rtl\"\n}\n\n/**\n * Transfor a hast tree to a rich text field.\n *\n * @param tree - The hast tree to transform.\n * @param file - The vfile to attach warnings to.\n * @param config - Configuration that determines the output of the function.\n *\n * @returns The rich text field equivalent of the provided hast tree.\n */\nexport const hastToRichText = (\n\ttree: Root | Element,\n\tfile: VFile,\n\tconfig?: HASTToRichTextConfig,\n): RichTextField => {\n\tconst builder = new RichTextFieldBuilder()\n\n\t// Merge the default serializer with the user-provided one.\n\tconst serializer = {\n\t\t...DEFAULT_SERIALIZER,\n\t\t...config?.serializer,\n\t}\n\n\t// Keep track of the last text node type to append text nodes to in\n\t// case of an image or an embed node is present inside a paragraph.\n\tlet lastRTTextNodeType: RTTextNode[\"type\"] = RichTextNodeType.paragraph\n\n\t// Keep track of the last list type to know whether we need to append\n\t// `list-item` or `o-list-item` nodes.\n\tlet lastListType: \"group-list-item\" | \"group-o-list-item\" | undefined =\n\t\tundefined\n\n\tvisit(tree, (node) => {\n\t\tif (node.type === \"element\") {\n\t\t\t// Transforms line break elements to line breaks\n\t\t\tif (node.tagName === \"br\") {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(\"\\n\")\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// noop\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Resolves the serializer responsible for the current node.\n\t\t\tlet serializerOrShorthand:\n\t\t\t\t| RichTextHTMLMapSerializerShorthand\n\t\t\t\t| RichTextHTMLMapSerializerFunction\n\n\t\t\t// We give priority to CSS selectors over tag names.\n\t\t\tif (node.matchesSerializer && node.matchesSerializer in serializer) {\n\t\t\t\tserializerOrShorthand = serializer[node.matchesSerializer]\n\t\t\t} else if (node.tagName in serializer) {\n\t\t\t\tserializerOrShorthand = serializer[node.tagName]\n\t\t\t} else {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tlet shorthand: RichTextHTMLMapSerializerShorthand\n\t\t\tif (typeof serializerOrShorthand === \"function\") {\n\t\t\t\tconst shorthandOrNode = serializerOrShorthand({\n\t\t\t\t\tnode,\n\t\t\t\t\tcontext: { listType: lastListType },\n\t\t\t\t})\n\n\t\t\t\tif (!shorthandOrNode) {\n\t\t\t\t\t// Exit on unhandled node.\n\t\t\t\t\treturn\n\t\t\t\t} else if (\n\t\t\t\t\ttypeof shorthandOrNode === \"object\" &&\n\t\t\t\t\t\"type\" in shorthandOrNode\n\t\t\t\t) {\n\t\t\t\t\t// When the serializer returns a rich text node, we append it and return.\n\t\t\t\t\tswitch (shorthandOrNode.type) {\n\t\t\t\t\t\tcase RichTextNodeType.strong:\n\t\t\t\t\t\tcase RichTextNodeType.em:\n\t\t\t\t\t\tcase RichTextNodeType.label:\n\t\t\t\t\t\tcase RichTextNodeType.hyperlink: {\n\t\t\t\t\t\t\tconst length = toString(node).trimEnd().length\n\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, length)\n\t\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode and that\n\t\t\t\t\t\t\t\t// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, length)\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tcase RichTextNodeType.image:\n\t\t\t\t\t\tcase RichTextNodeType.embed:\n\t\t\t\t\t\t\tbuilder.appendNode(shorthandOrNode)\n\n\t\t\t\t\t\t\treturn\n\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tlastRTTextNodeType = shorthandOrNode.type\n\t\t\t\t\t\t\tbuilder.appendNode(shorthandOrNode)\n\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Else it's a shorthand.\n\t\t\t\tshorthand = shorthandOrNode\n\t\t\t} else {\n\t\t\t\tshorthand = serializerOrShorthand\n\t\t\t}\n\n\t\t\tlet match:\n\t\t\t\t| DistributedPick<\n\t\t\t\t\t\tRTBlockNode | Exclude<RTInlineNode, RTLabelNode>,\n\t\t\t\t\t\t\"type\"\n\t\t\t\t >\n\t\t\t\t| { type: \"label\"; data: { label: string } }\n\t\t\tif (typeof shorthand === \"string\") {\n\t\t\t\tmatch = { type: shorthand }\n\t\t\t} else {\n\t\t\t\tmatch = { type: RichTextNodeType.label, data: shorthand }\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tswitch (match.type) {\n\t\t\t\t\tcase RichTextNodeType.heading1:\n\t\t\t\t\tcase RichTextNodeType.heading2:\n\t\t\t\t\tcase RichTextNodeType.heading3:\n\t\t\t\t\tcase RichTextNodeType.heading4:\n\t\t\t\t\tcase RichTextNodeType.heading5:\n\t\t\t\t\tcase RichTextNodeType.heading6:\n\t\t\t\t\tcase RichTextNodeType.paragraph:\n\t\t\t\t\tcase RichTextNodeType.preformatted:\n\t\t\t\t\tcase RichTextNodeType.listItem:\n\t\t\t\t\tcase RichTextNodeType.oListItem:\n\t\t\t\t\t\tlastRTTextNodeType = match.type\n\t\t\t\t\t\tbuilder.appendTextNode(match.type, config?.direction)\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.list:\n\t\t\t\t\tcase RichTextNodeType.oList:\n\t\t\t\t\t\tlastListType = match.type\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.image:\n\t\t\t\t\t\tbuilder.appendNode(serializeImage(node))\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.embed:\n\t\t\t\t\t\tbuilder.appendNode(serializeEmbed(node))\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.strong:\n\t\t\t\t\tcase RichTextNodeType.em:\n\t\t\t\t\tcase RichTextNodeType.label:\n\t\t\t\t\tcase RichTextNodeType.hyperlink: {\n\t\t\t\t\t\tconst span = serializeSpan(node, match)\n\t\t\t\t\t\tconst length = toString(node).trimEnd().length\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode and that\n\t\t\t\t\t\t\t// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`Unknown rich text node type: \\`${\n\t\t\t\t\t\t\t\t(match as { type: string }).type\n\t\t\t\t\t\t\t}\\``,\n\t\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof PrismicRichTextSerializerError) {\n\t\t\t\t\tfile.message(error.message, {\n\t\t\t\t\t\tcause: error,\n\t\t\t\t\t\tplace: node.position,\n\t\t\t\t\t\truleId: VFILE_RULE,\n\t\t\t\t\t\tsource: VFILE_SOURCE,\n\t\t\t\t\t})\n\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tthrow error\n\t\t\t}\n\t\t} else if (node.type === \"text\") {\n\t\t\t// Inspired from `hast-util-whitespace`, see:\n\t\t\t// https://github.com/syntax-tree/hast-util-whitespace/blob/main/lib/index.js\n\t\t\tif (!(node.value.replace(/[ \\t\\n\\f\\r]/g, \"\") === \"\")) {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(node.value)\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode. The last RT\n\t\t\t\t\t// node type is then an image/embed node, so we need to resume a new RT text node.\n\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\tbuilder.appendText(node.value)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// We ignore the following node types:\n\t\t// root, doctype, comment, raw\n\t})\n\n\treturn builder.build()\n}\n"],"names":["RichTextFieldBuilder","RichTextNodeType","visit","toString","serializeImage","serializeEmbed","serializeSpan","PrismicRichTextSerializerError"],"mappings":";;;;;;;;AAwCA,MAAM,qBAAgD;AAAA,EACrD,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,IAAI;AAAA,EACJ,IAAI,CAAC,EAAE,cACN,QAAQ,aAAa,sBAAsB,gBAAgB;AAAA,EAC5D,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,GAAG;;AAGJ,MAAM,aAAa;AACnB,MAAM,eAAe;A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M,KAAK;AACtC,kBAAM,SAASH,iBAAAA,SAAS,IAAI,EAAE,UAAU;AAEpC,gBAAA;AACK,sBAAA,WAAW,MAAM,MAAM;AAAA,qBACvB,OAAO;AAIP,sBAAA,eAAe,oBAAoB,iCAAQ,SAAS;AACpD,sBAAA,WAAW,MAAM,MAAM;AAAA,YAChC;AACA;AAAA,UACD;AAAA,UAEA;AACC,kBAAM,IAAI,MACT,kCACE,MAA2B,IAC7B,IAAI;AAAA,QAEP;AAAA,eACQ,OAAO;AACf,YAAI,iBAAiBI,+BAAAA,gCAAgC;AAC/C,eAAA,QAAQ,MAAM,SAAS;AAAA,YAC3B,OAAO;AAAA,YACP,OAAO,KAAK;AAAA,YACZ,QAAQ;AAAA,YACR,QAAQ;AAAA,UAAA,CACR;AAED;AAAA,QACD;AAEM,cAAA;AAAA,MACP;AAAA,IAAA,WACU,KAAK,SAAS,QAAQ;AAGhC,UAAI,EAAE,KAAK,MAAM,QAAQ,gBAAgB,EAAE,MAAM,KAAK;AACjD,YAAA;AACK,kBAAA,WAAW,KAAK,KAAK;AAAA,iBACrB,OAAO;AAGP,kBAAA,eAAe,oBAAoB,iCAAQ,SAAS;AACpD,kBAAA,WAAW,KAAK,KAAK;AAAA,QAC9B;AAAA,MACD;AAAA,IACD;AAAA,EAAA,CAIA;AAED,SAAO,QAAQ;AAChB;;"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { RichTextField } from "@prismicio/client";
|
|
2
|
+
import type { Element, Root } from "hast";
|
|
3
|
+
import type { VFile } from "vfile";
|
|
4
|
+
import type { RichTextHTMLMapSerializer } from "../types";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration that determines the output of `toRichText`.
|
|
7
|
+
*/
|
|
8
|
+
export type HASTToRichTextConfig = {
|
|
9
|
+
/**
|
|
10
|
+
* An optional HTML to rich text serializer. Will be merged with the default
|
|
11
|
+
* HTML to rich text serializer.
|
|
12
|
+
*/
|
|
13
|
+
serializer?: RichTextHTMLMapSerializer;
|
|
14
|
+
/**
|
|
15
|
+
* Whether or not the text processed should be marked as right-to-left.
|
|
16
|
+
*
|
|
17
|
+
* @defaultValue `false`
|
|
18
|
+
*/
|
|
19
|
+
direction?: "ltr" | "rtl";
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Transfor a hast tree to a rich text field.
|
|
23
|
+
*
|
|
24
|
+
* @param tree - The hast tree to transform.
|
|
25
|
+
* @param file - The vfile to attach warnings to.
|
|
26
|
+
* @param config - Configuration that determines the output of the function.
|
|
27
|
+
*
|
|
28
|
+
* @returns The rich text field equivalent of the provided hast tree.
|
|
29
|
+
*/
|
|
30
|
+
export declare const hastToRichText: (tree: Root | Element, file: VFile, config?: HASTToRichTextConfig) => RichTextField;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { RichTextNodeType } from "@prismicio/client";
|
|
2
|
+
import { toString } from "hast-util-to-string";
|
|
3
|
+
import { visit } from "unist-util-visit";
|
|
4
|
+
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError.js";
|
|
5
|
+
import { RichTextFieldBuilder } from "./RichTextFieldBuilder.js";
|
|
6
|
+
import { serializeSpan, serializeEmbed, serializeImage } from "./hastSerializerHelpers.js";
|
|
7
|
+
const DEFAULT_SERIALIZER = {
|
|
8
|
+
h1: "heading1",
|
|
9
|
+
h2: "heading2",
|
|
10
|
+
h3: "heading3",
|
|
11
|
+
h4: "heading4",
|
|
12
|
+
h5: "heading5",
|
|
13
|
+
h6: "heading6",
|
|
14
|
+
p: "paragraph",
|
|
15
|
+
pre: "preformatted",
|
|
16
|
+
strong: "strong",
|
|
17
|
+
em: "em",
|
|
18
|
+
li: ({ context }) => context.listType === "group-o-list-item" ? "o-list-item" : "list-item",
|
|
19
|
+
ul: "group-list-item",
|
|
20
|
+
ol: "group-o-list-item",
|
|
21
|
+
img: "image",
|
|
22
|
+
iframe: "embed",
|
|
23
|
+
a: "hyperlink"
|
|
24
|
+
};
|
|
25
|
+
const VFILE_RULE = "failed-to-serialize-node";
|
|
26
|
+
const VFILE_SOURCE = "prismic";
|
|
27
|
+
const hastToRichText = (tree, file, config) => {
|
|
28
|
+
const builder = new RichTextFieldBuilder();
|
|
29
|
+
const serializer = {
|
|
30
|
+
...DEFAULT_SERIALIZER,
|
|
31
|
+
...config == null ? void 0 : config.serializer
|
|
32
|
+
};
|
|
33
|
+
let lastRTTextNodeType = RichTextNodeType.paragraph;
|
|
34
|
+
let lastListType = void 0;
|
|
35
|
+
visit(tree, (node) => {
|
|
36
|
+
if (node.type === "element") {
|
|
37
|
+
if (node.tagName === "br") {
|
|
38
|
+
try {
|
|
39
|
+
builder.appendText("\n");
|
|
40
|
+
} catch (error) {
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
let serializerOrShorthand;
|
|
44
|
+
if (node.matchesSerializer && node.matchesSerializer in serializer) {
|
|
45
|
+
serializerOrShorthand = serializer[node.matchesSerializer];
|
|
46
|
+
} else if (node.tagName in serializer) {
|
|
47
|
+
serializerOrShorthand = serializer[node.tagName];
|
|
48
|
+
} else {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
let shorthand;
|
|
52
|
+
if (typeof serializerOrShorthand === "function") {
|
|
53
|
+
const shorthandOrNode = serializerOrShorthand({
|
|
54
|
+
node,
|
|
55
|
+
context: { listType: lastListType }
|
|
56
|
+
});
|
|
57
|
+
if (!shorthandOrNode) {
|
|
58
|
+
return;
|
|
59
|
+
} else if (typeof shorthandOrNode === "object" && "type" in shorthandOrNode) {
|
|
60
|
+
switch (shorthandOrNode.type) {
|
|
61
|
+
case RichTextNodeType.strong:
|
|
62
|
+
case RichTextNodeType.em:
|
|
63
|
+
case RichTextNodeType.label:
|
|
64
|
+
case RichTextNodeType.hyperlink: {
|
|
65
|
+
const length = toString(node).trimEnd().length;
|
|
66
|
+
try {
|
|
67
|
+
builder.appendSpan(shorthandOrNode, length);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
70
|
+
builder.appendSpan(shorthandOrNode, length);
|
|
71
|
+
}
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
case RichTextNodeType.image:
|
|
75
|
+
case RichTextNodeType.embed:
|
|
76
|
+
builder.appendNode(shorthandOrNode);
|
|
77
|
+
return;
|
|
78
|
+
default:
|
|
79
|
+
lastRTTextNodeType = shorthandOrNode.type;
|
|
80
|
+
builder.appendNode(shorthandOrNode);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
shorthand = shorthandOrNode;
|
|
85
|
+
} else {
|
|
86
|
+
shorthand = serializerOrShorthand;
|
|
87
|
+
}
|
|
88
|
+
let match;
|
|
89
|
+
if (typeof shorthand === "string") {
|
|
90
|
+
match = { type: shorthand };
|
|
91
|
+
} else {
|
|
92
|
+
match = { type: RichTextNodeType.label, data: shorthand };
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
switch (match.type) {
|
|
96
|
+
case RichTextNodeType.heading1:
|
|
97
|
+
case RichTextNodeType.heading2:
|
|
98
|
+
case RichTextNodeType.heading3:
|
|
99
|
+
case RichTextNodeType.heading4:
|
|
100
|
+
case RichTextNodeType.heading5:
|
|
101
|
+
case RichTextNodeType.heading6:
|
|
102
|
+
case RichTextNodeType.paragraph:
|
|
103
|
+
case RichTextNodeType.preformatted:
|
|
104
|
+
case RichTextNodeType.listItem:
|
|
105
|
+
case RichTextNodeType.oListItem:
|
|
106
|
+
lastRTTextNodeType = match.type;
|
|
107
|
+
builder.appendTextNode(match.type, config == null ? void 0 : config.direction);
|
|
108
|
+
break;
|
|
109
|
+
case RichTextNodeType.list:
|
|
110
|
+
case RichTextNodeType.oList:
|
|
111
|
+
lastListType = match.type;
|
|
112
|
+
break;
|
|
113
|
+
case RichTextNodeType.image:
|
|
114
|
+
builder.appendNode(serializeImage(node));
|
|
115
|
+
break;
|
|
116
|
+
case RichTextNodeType.embed:
|
|
117
|
+
builder.appendNode(serializeEmbed(node));
|
|
118
|
+
break;
|
|
119
|
+
case RichTextNodeType.strong:
|
|
120
|
+
case RichTextNodeType.em:
|
|
121
|
+
case RichTextNodeType.label:
|
|
122
|
+
case RichTextNodeType.hyperlink: {
|
|
123
|
+
const span = serializeSpan(node, match);
|
|
124
|
+
const length = toString(node).trimEnd().length;
|
|
125
|
+
try {
|
|
126
|
+
builder.appendSpan(span, length);
|
|
127
|
+
} catch (error) {
|
|
128
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
129
|
+
builder.appendSpan(span, length);
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
default:
|
|
134
|
+
throw new Error(`Unknown rich text node type: \`${match.type}\``);
|
|
135
|
+
}
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (error instanceof PrismicRichTextSerializerError) {
|
|
138
|
+
file.message(error.message, {
|
|
139
|
+
cause: error,
|
|
140
|
+
place: node.position,
|
|
141
|
+
ruleId: VFILE_RULE,
|
|
142
|
+
source: VFILE_SOURCE
|
|
143
|
+
});
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
} else if (node.type === "text") {
|
|
149
|
+
if (!(node.value.replace(/[ \t\n\f\r]/g, "") === "")) {
|
|
150
|
+
try {
|
|
151
|
+
builder.appendText(node.value);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
154
|
+
builder.appendText(node.value);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
return builder.build();
|
|
160
|
+
};
|
|
161
|
+
export {
|
|
162
|
+
hastToRichText
|
|
163
|
+
};
|
|
164
|
+
//# sourceMappingURL=hastToRichText.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hastToRichText.js","sources":["../../../src/lib/hastToRichText.ts"],"sourcesContent":["import type {\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTLabelNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\nimport { RichTextNodeType } from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { toString } from \"hast-util-to-string\"\nimport { visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport type {\n\tRichTextHTMLMapSerializer,\n\tRichTextHTMLMapSerializerFunction,\n\tRichTextHTMLMapSerializerShorthand,\n} from \"../types\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\n\nimport { RichTextFieldBuilder } from \"./RichTextFieldBuilder\"\nimport {\n\tserializeEmbed,\n\tserializeImage,\n\tserializeSpan,\n} from \"./hastSerializerHelpers\"\n\n/**\n * Pick keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-pick.d.ts\n */\ntype DistributedPick<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Pick<ObjectType, KeyType> : never\n\nconst DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {\n\th1: \"heading1\",\n\th2: \"heading2\",\n\th3: \"heading3\",\n\th4: \"heading4\",\n\th5: \"heading5\",\n\th6: \"heading6\",\n\tp: \"paragraph\",\n\tpre: \"preformatted\",\n\tstrong: \"strong\",\n\tem: \"em\",\n\tli: ({ context }) =>\n\t\tcontext.listType === \"group-o-list-item\" ? \"o-list-item\" : \"list-item\",\n\tul: \"group-list-item\",\n\tol: \"group-o-list-item\",\n\timg: \"image\",\n\tiframe: \"embed\",\n\ta: \"hyperlink\",\n}\n\nconst VFILE_RULE = \"failed-to-serialize-node\"\nconst VFILE_SOURCE = \"prismic\"\n\n/**\n * Configuration that determines the output of `toRichText`.\n */\nexport type HASTToRichTextConfig = {\n\t/**\n\t * An optional HTML to rich text serializer. Will be merged with the default\n\t * HTML to rich text serializer.\n\t */\n\tserializer?: RichTextHTMLMapSerializer\n\n\t/**\n\t * Whether or not the text processed should be marked as right-to-left.\n\t *\n\t * @defaultValue `false`\n\t */\n\tdirection?: \"ltr\" | \"rtl\"\n}\n\n/**\n * Transfor a hast tree to a rich text field.\n *\n * @param tree - The hast tree to transform.\n * @param file - The vfile to attach warnings to.\n * @param config - Configuration that determines the output of the function.\n *\n * @returns The rich text field equivalent of the provided hast tree.\n */\nexport const hastToRichText = (\n\ttree: Root | Element,\n\tfile: VFile,\n\tconfig?: HASTToRichTextConfig,\n): RichTextField => {\n\tconst builder = new RichTextFieldBuilder()\n\n\t// Merge the default serializer with the user-provided one.\n\tconst serializer = {\n\t\t...DEFAULT_SERIALIZER,\n\t\t...config?.serializer,\n\t}\n\n\t// Keep track of the last text node type to append text nodes to in\n\t// case of an image or an embed node is present inside a paragraph.\n\tlet lastRTTextNodeType: RTTextNode[\"type\"] = RichTextNodeType.paragraph\n\n\t// Keep track of the last list type to know whether we need to append\n\t// `list-item` or `o-list-item` nodes.\n\tlet lastListType: \"group-list-item\" | \"group-o-list-item\" | undefined =\n\t\tundefined\n\n\tvisit(tree, (node) => {\n\t\tif (node.type === \"element\") {\n\t\t\t// Transforms line break elements to line breaks\n\t\t\tif (node.tagName === \"br\") {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(\"\\n\")\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// noop\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Resolves the serializer responsible for the current node.\n\t\t\tlet serializerOrShorthand:\n\t\t\t\t| RichTextHTMLMapSerializerShorthand\n\t\t\t\t| RichTextHTMLMapSerializerFunction\n\n\t\t\t// We give priority to CSS selectors over tag names.\n\t\t\tif (node.matchesSerializer && node.matchesSerializer in serializer) {\n\t\t\t\tserializerOrShorthand = serializer[node.matchesSerializer]\n\t\t\t} else if (node.tagName in serializer) {\n\t\t\t\tserializerOrShorthand = serializer[node.tagName]\n\t\t\t} else {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tlet shorthand: RichTextHTMLMapSerializerShorthand\n\t\t\tif (typeof serializerOrShorthand === \"function\") {\n\t\t\t\tconst shorthandOrNode = serializerOrShorthand({\n\t\t\t\t\tnode,\n\t\t\t\t\tcontext: { listType: lastListType },\n\t\t\t\t})\n\n\t\t\t\tif (!shorthandOrNode) {\n\t\t\t\t\t// Exit on unhandled node.\n\t\t\t\t\treturn\n\t\t\t\t} else if (\n\t\t\t\t\ttypeof shorthandOrNode === \"object\" &&\n\t\t\t\t\t\"type\" in shorthandOrNode\n\t\t\t\t) {\n\t\t\t\t\t// When the serializer returns a rich text node, we append it and return.\n\t\t\t\t\tswitch (shorthandOrNode.type) {\n\t\t\t\t\t\tcase RichTextNodeType.strong:\n\t\t\t\t\t\tcase RichTextNodeType.em:\n\t\t\t\t\t\tcase RichTextNodeType.label:\n\t\t\t\t\t\tcase RichTextNodeType.hyperlink: {\n\t\t\t\t\t\t\tconst length = toString(node).trimEnd().length\n\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, length)\n\t\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode and that\n\t\t\t\t\t\t\t\t// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, length)\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tcase RichTextNodeType.image:\n\t\t\t\t\t\tcase RichTextNodeType.embed:\n\t\t\t\t\t\t\tbuilder.appendNode(shorthandOrNode)\n\n\t\t\t\t\t\t\treturn\n\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tlastRTTextNodeType = shorthandOrNode.type\n\t\t\t\t\t\t\tbuilder.appendNode(shorthandOrNode)\n\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Else it's a shorthand.\n\t\t\t\tshorthand = shorthandOrNode\n\t\t\t} else {\n\t\t\t\tshorthand = serializerOrShorthand\n\t\t\t}\n\n\t\t\tlet match:\n\t\t\t\t| DistributedPick<\n\t\t\t\t\t\tRTBlockNode | Exclude<RTInlineNode, RTLabelNode>,\n\t\t\t\t\t\t\"type\"\n\t\t\t\t >\n\t\t\t\t| { type: \"label\"; data: { label: string } }\n\t\t\tif (typeof shorthand === \"string\") {\n\t\t\t\tmatch = { type: shorthand }\n\t\t\t} else {\n\t\t\t\tmatch = { type: RichTextNodeType.label, data: shorthand }\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tswitch (match.type) {\n\t\t\t\t\tcase RichTextNodeType.heading1:\n\t\t\t\t\tcase RichTextNodeType.heading2:\n\t\t\t\t\tcase RichTextNodeType.heading3:\n\t\t\t\t\tcase RichTextNodeType.heading4:\n\t\t\t\t\tcase RichTextNodeType.heading5:\n\t\t\t\t\tcase RichTextNodeType.heading6:\n\t\t\t\t\tcase RichTextNodeType.paragraph:\n\t\t\t\t\tcase RichTextNodeType.preformatted:\n\t\t\t\t\tcase RichTextNodeType.listItem:\n\t\t\t\t\tcase RichTextNodeType.oListItem:\n\t\t\t\t\t\tlastRTTextNodeType = match.type\n\t\t\t\t\t\tbuilder.appendTextNode(match.type, config?.direction)\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.list:\n\t\t\t\t\tcase RichTextNodeType.oList:\n\t\t\t\t\t\tlastListType = match.type\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.image:\n\t\t\t\t\t\tbuilder.appendNode(serializeImage(node))\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.embed:\n\t\t\t\t\t\tbuilder.appendNode(serializeEmbed(node))\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.strong:\n\t\t\t\t\tcase RichTextNodeType.em:\n\t\t\t\t\tcase RichTextNodeType.label:\n\t\t\t\t\tcase RichTextNodeType.hyperlink: {\n\t\t\t\t\t\tconst span = serializeSpan(node, match)\n\t\t\t\t\t\tconst length = toString(node).trimEnd().length\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode and that\n\t\t\t\t\t\t\t// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`Unknown rich text node type: \\`${\n\t\t\t\t\t\t\t\t(match as { type: string }).type\n\t\t\t\t\t\t\t}\\``,\n\t\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof PrismicRichTextSerializerError) {\n\t\t\t\t\tfile.message(error.message, {\n\t\t\t\t\t\tcause: error,\n\t\t\t\t\t\tplace: node.position,\n\t\t\t\t\t\truleId: VFILE_RULE,\n\t\t\t\t\t\tsource: VFILE_SOURCE,\n\t\t\t\t\t})\n\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tthrow error\n\t\t\t}\n\t\t} else if (node.type === \"text\") {\n\t\t\t// Inspired from `hast-util-whitespace`, see:\n\t\t\t// https://github.com/syntax-tree/hast-util-whitespace/blob/main/lib/index.js\n\t\t\tif (!(node.value.replace(/[ \\t\\n\\f\\r]/g, \"\") === \"\")) {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(node.value)\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode. The last RT\n\t\t\t\t\t// node type is then an image/embed node, so we need to resume a new RT text node.\n\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\tbuilder.appendText(node.value)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// We ignore the following node types:\n\t\t// root, doctype, comment, raw\n\t})\n\n\treturn builder.build()\n}\n"],"names":[],"mappings":";;;;;;AAwCA,MAAM,qBAAgD;AAAA,EACrD,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,IAAI;AAAA,EACJ,IAAI,CAAC,EAAE,cACN,QAAQ,aAAa,sBAAsB,gBAAgB;AAAA,EAC5D,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,GAAG;;AAGJ,MAAM,aAAa;AACnB,MAAM,eAAe;AA6Bd,MAAM,iBAAiB,CAC7B,MACA,MACA,WACkB;AACZ,QAAA,UAAU,IAAI;AAGpB,QAAM,aAAa;AAAA,IAClB,GAAG;AAAA,IACH,GAAG,iCAAQ;AAAA,EAAA;AAKZ,MAAI,qBAAyC,iBAAiB;AAI9D,MAAI,eACH;AAEK,QAAA,MAAM,CAAC,SAAQ;AAChB,QAAA,KAAK,SAAS,WAAW;AAExB,UAAA,KAAK,YAAY,MAAM;AACtB,YAAA;AACH,kBAAQ,WAAW,IAAI;AAAA,iBACf,OAAO;AAAA,QAEhB;AAAA,MACD;AAGI,UAAA;AAKJ,UAAI,KAAK,qBAAqB,KAAK,qBAAqB,YAAY;AAC3C,gCAAA,WAAW,KAAK,iBAAiB;AAAA,MAAA,WAC/C,KAA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AAK,KAAK;AAAA,QAC9B;AAAA,MACD;AAAA,IACD;AAAA,EAAA,CAIA;AAED,SAAO,QAAQ;AAChB;"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const hastUtilSelect = require("hast-util-select");
|
|
4
|
+
const rehypeMinifyWhitespace = require("rehype-minify-whitespace");
|
|
5
|
+
const unistUtilRemove = require("unist-util-remove");
|
|
6
|
+
const unistUtilVisit = require("unist-util-visit");
|
|
7
|
+
const filterRichTextField = require("./filterRichTextField.cjs");
|
|
8
|
+
const hastToRichText = require("./hastToRichText.cjs");
|
|
9
|
+
const rehypeRichText = function rehypeRichText2(config) {
|
|
10
|
+
const self = this;
|
|
11
|
+
self.use(() => {
|
|
12
|
+
return (tree) => {
|
|
13
|
+
if (config == null ? void 0 : config.container) {
|
|
14
|
+
const element = hastUtilSelect.select(config.container, tree);
|
|
15
|
+
if (!element) {
|
|
16
|
+
throw new Error(`No container matching \`${config == null ? void 0 : config.container}\` could be found in the input AST.`);
|
|
17
|
+
}
|
|
18
|
+
tree.children = [element];
|
|
19
|
+
}
|
|
20
|
+
if (config == null ? void 0 : config.exclude) {
|
|
21
|
+
const nodesToExclude = hastUtilSelect.selectAll(config.exclude.join(", "), tree);
|
|
22
|
+
unistUtilRemove.remove(tree, (node) => nodesToExclude.includes(node));
|
|
23
|
+
}
|
|
24
|
+
if (config == null ? void 0 : config.include) {
|
|
25
|
+
const nodesToInclude = [];
|
|
26
|
+
const selector = config.include.join(", ");
|
|
27
|
+
const rawNodesToInclude = hastUtilSelect.selectAll(selector, tree);
|
|
28
|
+
unistUtilVisit.visit(tree, (node) => {
|
|
29
|
+
if (rawNodesToInclude.includes(node)) {
|
|
30
|
+
nodesToInclude.push(node);
|
|
31
|
+
return unistUtilVisit.SKIP;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
tree.children = nodesToInclude;
|
|
35
|
+
}
|
|
36
|
+
if (config == null ? void 0 : config.serializer) {
|
|
37
|
+
for (const key in config.serializer) {
|
|
38
|
+
if (!/^[a-z]+[1-6]?$/.test(key)) {
|
|
39
|
+
const matches = hastUtilSelect.selectAll(key, tree);
|
|
40
|
+
for (let i = 0; i < matches.length; i++) {
|
|
41
|
+
matches[i].matchesSerializer = key;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
self.use(rehypeMinifyWhitespace);
|
|
49
|
+
self.compiler = compiler;
|
|
50
|
+
function compiler(tree, file) {
|
|
51
|
+
const richTextField = hastToRichText.hastToRichText(tree, file, config);
|
|
52
|
+
if (config == null ? void 0 : config.model) {
|
|
53
|
+
return filterRichTextField.filterRichTextField(richTextField, config.model);
|
|
54
|
+
}
|
|
55
|
+
return richTextField;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
exports.rehypeRichText = rehypeRichText;
|
|
59
|
+
//# sourceMappingURL=rehypeRichText.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rehypeRichText.cjs","sources":["../../../src/lib/rehypeRichText.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRichTextField,\n} from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { select, selectAll } from \"hast-util-select\"\nimport rehypeMinifyWhitespace from \"rehype-minify-whitespace\"\nimport type { Plugin, Processor } from \"unified\"\nimport { remove } from \"unist-util-remove\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { filterRichTextField } from \"./filterRichTextField\"\nimport type { HASTToRichTextConfig } from \"./hastToRichText\"\nimport { hastToRichText } from \"./hastToRichText\"\n\n/**\n * Configuration options for {@link rehypeRichText}.\n */\nexport type RehypeRichTextConfig = {\n\t/**\n\t * A CSS selector that targets the section of the document to convert to rich\n\t * text.\n\t *\n\t * @example `div.post`\n\t *\n\t * @defaultValue `\":root\"` - The top-level element of the document.\n\t */\n\tcontainer?: string\n\n\t/**\n\t * A list of CSS selectors to exclude matching nodes from the document to\n\t * process.\n\t *\n\t * @example `[\".hidden\", \"aside\"]`\n\t *\n\t * @defaultValue `[]` - No nodes are excluded.\n\t */\n\texclude?: string[]\n\n\t/**\n\t * A list of CSS selectors to include only matching nodes from the document to\n\t * process.\n\t *\n\t * @example `[\"p\", \"img\"]`\n\t *\n\t * @defaultValue `[]` - All nodes are included.\n\t */\n\tinclude?: string[]\n\n\t/**\n\t * A rich text or title field model definition. When provided the serializer\n\t * will filter out any node types not allowed by the model.\n\t *\n\t * @defaultValue `undefined` - No filtering is applied.\n\t */\n\tmodel?: CustomTypeModelRichTextField\n} & HASTToRichTextConfig\n\n/**\n * A unified plugin that compiles a hast tree to a Prismic rich text field.\n *\n * @param config - Configuration options for the rehype rich text processor.\n */\n// unified requires the function to be typed directly with the\n// `Plugin` type to properly infer return types on processors.\nexport const rehypeRichText: Plugin<\n\t[config?: RehypeRichTextConfig],\n\tRoot,\n\tRichTextField\n> = function rehypeRichText(config) {\n\t// This is a bit dirty, but it seems like that's how rehype intends\n\t// it to be due to JSDocs limitations(?), see:\n\t// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20\n\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\tconst self = this as unknown as Processor<\n\t\tundefined,\n\t\tundefined,\n\t\tundefined,\n\t\tRoot,\n\t\tRichTextField\n\t>\n\n\t// We need to exclude nodes _before_ we minify the tree as excluding\n\t// nodes could end up in more whitespaces to trim.\n\tself.use(() => {\n\t\treturn (tree: Root) => {\n\t\t\t// Extract container node if any is specified.\n\t\t\tif (config?.container) {\n\t\t\t\tconst element = select(config.container, tree)\n\n\t\t\t\tif (!element) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`No container matching \\`${config?.container}\\` could be found in the input AST.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t// its children with the found element.\n\t\t\t\ttree.children = [element]\n\t\t\t}\n\n\t\t\t// Remove excluded nodes if any are specified\n\t\t\tif (config?.exclude) {\n\t\t\t\t// We join selector to only run one query\n\t\t\t\tconst nodesToExclude = selectAll(config.exclude.join(\", \"), tree)\n\n\t\t\t\tremove(tree, (node) => nodesToExclude.includes(node as Element))\n\t\t\t}\n\n\t\t\t// Include only nodes to include\n\t\t\tif (config?.include) {\n\t\t\t\tconst nodesToInclude: Element[] = []\n\n\t\t\t\t// We join selector to only run one query\n\t\t\t\tconst selector = config.include.join(\", \")\n\t\t\t\tconst rawNodesToInclude = selectAll(selector, tree)\n\n\t\t\t\t// We walk the tree to exclude matching nodes that are children of other matching nodes.\n\t\t\t\tvisit(tree, (node) => {\n\t\t\t\t\tif (rawNodesToInclude.includes(node as Element)) {\n\t\t\t\t\t\tnodesToInclude.push(node as Element)\n\n\t\t\t\t\t\t// Stop traversing this part of the tree since we found its matching parent node.\n\t\t\t\t\t\treturn SKIP\n\t\t\t\t\t}\n\t\t\t\t})\n\n\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t// its children with the found element.\n\t\t\t\ttree.children = nodesToInclude\n\t\t\t}\n\n\t\t\t// Mark nodes matching CSS selectors\n\t\t\tif (config?.serializer) {\n\t\t\t\tfor (const key in config.serializer) {\n\t\t\t\t\t// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).\n\t\t\t\t\t// Here we want to match anything that's not a valid HTML tag name and treat\n\t\t\t\t\t// it as a CSS selector. See: https://regex101.com/r/LILLWH/1\n\t\t\t\t\tif (!/^[a-z]+[1-6]?$/.test(key)) {\n\t\t\t\t\t\tconst matches = selectAll(key, tree)\n\n\t\t\t\t\t\tfor (let i = 0; i < matches.length; i++) {\n\t\t\t\t\t\t\tmatches[i].matchesSerializer = key\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t})\n\n\t// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's\n\t// registered within the plugin rather than on the processor directly.\n\tself.use(rehypeMinifyWhitespace)\n\n\tself.compiler = compiler\n\n\tfunction compiler(tree: Root, file: VFile): RichTextField {\n\t\tconst richTextField = hastToRichText(tree, file, config)\n\n\t\tif (config?.model) {\n\t\t\treturn filterRichTextField(richTextField, config.model)\n\t\t}\n\n\t\treturn richTextField\n\t}\n}\n\ndeclare module \"unified\" {\n\t// Register unified processor the result type.\n\tinterface CompileResultMap {\n\t\tRichTextField: RichTextField\n\t}\n}\n\ndeclare module \"hast\" {\n\t// Extend hast node with a rich text value.\n\tinterface Element {\n\t\t/**\n\t\t * A serializer this node matches to. Nodes are marked with this property\n\t\t * when they match a CSS selector from the serializer map.\n\t\t */\n\t\tmatchesSerializer?: string\n\t}\n}\n"],"names":["rehypeRichText","select","selectAll","remove","visit","SKIP","hastToRichText","filterRichTextField"],"mappings":";;;;;;;;AAkEa,MAAA,iBAIT,SAASA,gBAAe,QAAM;AAKjC,QAAM,OAAO;AAUb,OAAK,IAAI,MAAK;AACb,WAAO,CAAC,SAAc;AAErB,UAAI,iCAAQ,WAAW;AACtB,cAAM,UAAUC,eAAA,OAAO,OAAO,WAAW,IAAI;AAE7C,YAAI,CAAC,SAAS;AACb,gBAAM,IAAI,MACT,2BAA2B,iCAAQ,SAAS,qCAAqC;AAAA,QAEnF;AAIK,aAAA,WAAW,CAAC,OAAO;AAAA,MACzB;AAGA,UAAI,iCAAQ,SAAS;AAEpB,cAAM,iBAAiBC,eAAU,UAAA,OAAO,QAAQ,KAAK,IAAI,GAAG,IAAI;AAEhEC,wBAAA,OAAO,MAAM,CAAC,SAAS,eAAe,SAAS,IAAe,CAAC;AAAA,MAChE;AAGA,UAAI,iCAAQ,SAAS;AACpB,cAAM,iBAA4B,CAAA;AAGlC,cAAM,WAAW,OAAO,QAAQ,KAAK,IAAI;AACnC,cAAA,oBAAoBD,eAAAA,UAAU,UAAU,IAAI;AAG5CE,6BAAA,MAAM,CAAC,SAAQ;AAChB,cAAA,kBAAkB,SAAS,IAAe,GAAG;AAChD,2BAAe,KAAK,IAAe;AAG5B,mBAAAC;UACR;AAAA,QAAA,CACA;AAID,aAAK,WAAW;AAAA,MACjB;AAGA,UAAI,iCAAQ,YAAY;AACZ,mBAAA,OAAO,OAAO,YAAY;AAIpC,cAAI,CAAC,iBAAiB,KAAK,GAAG,GAAG;AAC1B,kBAAA,UAAUH,eAAAA,UAAU,KAAK,IAAI;AAEnC,qBAAS,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;AAChC,sBAAA,CAAC,EAAE,oBAAoB;AAAA,YAChC;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IAAA;AAAA,EACD,CACA;AAID,OAAK,IAAI,sBAAsB;AAE/B,OAAK,WAAW;AAEP,WAAA,SAAS,MAAY,MAAW;AACxC,UAAM,gBAAgBI,eAAA,eAAe,MAAM,MAAM,MAAM;AAEvD,QAAI,iCAAQ,OAAO;AACX,aAAAC,wCAAoB,eAAe,OAAO,KAAK;AAAA,IACvD;AAEO,WAAA;AAAA,EACR;AACD;;"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client";
|
|
2
|
+
import type { Root } from "hast";
|
|
3
|
+
import type { Plugin } from "unified";
|
|
4
|
+
import type { HASTToRichTextConfig } from "./hastToRichText";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for {@link rehypeRichText}.
|
|
7
|
+
*/
|
|
8
|
+
export type RehypeRichTextConfig = {
|
|
9
|
+
/**
|
|
10
|
+
* A CSS selector that targets the section of the document to convert to rich
|
|
11
|
+
* text.
|
|
12
|
+
*
|
|
13
|
+
* @example `div.post`
|
|
14
|
+
*
|
|
15
|
+
* @defaultValue `":root"` - The top-level element of the document.
|
|
16
|
+
*/
|
|
17
|
+
container?: string;
|
|
18
|
+
/**
|
|
19
|
+
* A list of CSS selectors to exclude matching nodes from the document to
|
|
20
|
+
* process.
|
|
21
|
+
*
|
|
22
|
+
* @example `[".hidden", "aside"]`
|
|
23
|
+
*
|
|
24
|
+
* @defaultValue `[]` - No nodes are excluded.
|
|
25
|
+
*/
|
|
26
|
+
exclude?: string[];
|
|
27
|
+
/**
|
|
28
|
+
* A list of CSS selectors to include only matching nodes from the document to
|
|
29
|
+
* process.
|
|
30
|
+
*
|
|
31
|
+
* @example `["p", "img"]`
|
|
32
|
+
*
|
|
33
|
+
* @defaultValue `[]` - All nodes are included.
|
|
34
|
+
*/
|
|
35
|
+
include?: string[];
|
|
36
|
+
/**
|
|
37
|
+
* A rich text or title field model definition. When provided the serializer
|
|
38
|
+
* will filter out any node types not allowed by the model.
|
|
39
|
+
*
|
|
40
|
+
* @defaultValue `undefined` - No filtering is applied.
|
|
41
|
+
*/
|
|
42
|
+
model?: CustomTypeModelRichTextField;
|
|
43
|
+
} & HASTToRichTextConfig;
|
|
44
|
+
/**
|
|
45
|
+
* A unified plugin that compiles a hast tree to a Prismic rich text field.
|
|
46
|
+
*
|
|
47
|
+
* @param config - Configuration options for the rehype rich text processor.
|
|
48
|
+
*/
|
|
49
|
+
export declare const rehypeRichText: Plugin<[
|
|
50
|
+
config?: RehypeRichTextConfig
|
|
51
|
+
], Root, RichTextField>;
|
|
52
|
+
declare module "unified" {
|
|
53
|
+
interface CompileResultMap {
|
|
54
|
+
RichTextField: RichTextField;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
declare module "hast" {
|
|
58
|
+
interface Element {
|
|
59
|
+
/**
|
|
60
|
+
* A serializer this node matches to. Nodes are marked with this property
|
|
61
|
+
* when they match a CSS selector from the serializer map.
|
|
62
|
+
*/
|
|
63
|
+
matchesSerializer?: string;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { select, selectAll } from "hast-util-select";
|
|
2
|
+
import rehypeMinifyWhitespace from "rehype-minify-whitespace";
|
|
3
|
+
import { remove } from "unist-util-remove";
|
|
4
|
+
import { visit, SKIP } from "unist-util-visit";
|
|
5
|
+
import { filterRichTextField } from "./filterRichTextField.js";
|
|
6
|
+
import { hastToRichText } from "./hastToRichText.js";
|
|
7
|
+
const rehypeRichText = function rehypeRichText2(config) {
|
|
8
|
+
const self = this;
|
|
9
|
+
self.use(() => {
|
|
10
|
+
return (tree) => {
|
|
11
|
+
if (config == null ? void 0 : config.container) {
|
|
12
|
+
const element = select(config.container, tree);
|
|
13
|
+
if (!element) {
|
|
14
|
+
throw new Error(`No container matching \`${config == null ? void 0 : config.container}\` could be found in the input AST.`);
|
|
15
|
+
}
|
|
16
|
+
tree.children = [element];
|
|
17
|
+
}
|
|
18
|
+
if (config == null ? void 0 : config.exclude) {
|
|
19
|
+
const nodesToExclude = selectAll(config.exclude.join(", "), tree);
|
|
20
|
+
remove(tree, (node) => nodesToExclude.includes(node));
|
|
21
|
+
}
|
|
22
|
+
if (config == null ? void 0 : config.include) {
|
|
23
|
+
const nodesToInclude = [];
|
|
24
|
+
const selector = config.include.join(", ");
|
|
25
|
+
const rawNodesToInclude = selectAll(selector, tree);
|
|
26
|
+
visit(tree, (node) => {
|
|
27
|
+
if (rawNodesToInclude.includes(node)) {
|
|
28
|
+
nodesToInclude.push(node);
|
|
29
|
+
return SKIP;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
tree.children = nodesToInclude;
|
|
33
|
+
}
|
|
34
|
+
if (config == null ? void 0 : config.serializer) {
|
|
35
|
+
for (const key in config.serializer) {
|
|
36
|
+
if (!/^[a-z]+[1-6]?$/.test(key)) {
|
|
37
|
+
const matches = selectAll(key, tree);
|
|
38
|
+
for (let i = 0; i < matches.length; i++) {
|
|
39
|
+
matches[i].matchesSerializer = key;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
self.use(rehypeMinifyWhitespace);
|
|
47
|
+
self.compiler = compiler;
|
|
48
|
+
function compiler(tree, file) {
|
|
49
|
+
const richTextField = hastToRichText(tree, file, config);
|
|
50
|
+
if (config == null ? void 0 : config.model) {
|
|
51
|
+
return filterRichTextField(richTextField, config.model);
|
|
52
|
+
}
|
|
53
|
+
return richTextField;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
export {
|
|
57
|
+
rehypeRichText
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=rehypeRichText.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rehypeRichText.js","sources":["../../../src/lib/rehypeRichText.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRichTextField,\n} from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { select, selectAll } from \"hast-util-select\"\nimport rehypeMinifyWhitespace from \"rehype-minify-whitespace\"\nimport type { Plugin, Processor } from \"unified\"\nimport { remove } from \"unist-util-remove\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { filterRichTextField } from \"./filterRichTextField\"\nimport type { HASTToRichTextConfig } from \"./hastToRichText\"\nimport { hastToRichText } from \"./hastToRichText\"\n\n/**\n * Configuration options for {@link rehypeRichText}.\n */\nexport type RehypeRichTextConfig = {\n\t/**\n\t * A CSS selector that targets the section of the document to convert to rich\n\t * text.\n\t *\n\t * @example `div.post`\n\t *\n\t * @defaultValue `\":root\"` - The top-level element of the document.\n\t */\n\tcontainer?: string\n\n\t/**\n\t * A list of CSS selectors to exclude matching nodes from the document to\n\t * process.\n\t *\n\t * @example `[\".hidden\", \"aside\"]`\n\t *\n\t * @defaultValue `[]` - No nodes are excluded.\n\t */\n\texclude?: string[]\n\n\t/**\n\t * A list of CSS selectors to include only matching nodes from the document to\n\t * process.\n\t *\n\t * @example `[\"p\", \"img\"]`\n\t *\n\t * @defaultValue `[]` - All nodes are included.\n\t */\n\tinclude?: string[]\n\n\t/**\n\t * A rich text or title field model definition. When provided the serializer\n\t * will filter out any node types not allowed by the model.\n\t *\n\t * @defaultValue `undefined` - No filtering is applied.\n\t */\n\tmodel?: CustomTypeModelRichTextField\n} & HASTToRichTextConfig\n\n/**\n * A unified plugin that compiles a hast tree to a Prismic rich text field.\n *\n * @param config - Configuration options for the rehype rich text processor.\n */\n// unified requires the function to be typed directly with the\n// `Plugin` type to properly infer return types on processors.\nexport const rehypeRichText: Plugin<\n\t[config?: RehypeRichTextConfig],\n\tRoot,\n\tRichTextField\n> = function rehypeRichText(config) {\n\t// This is a bit dirty, but it seems like that's how rehype intends\n\t// it to be due to JSDocs limitations(?), see:\n\t// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20\n\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\tconst self = this as unknown as Processor<\n\t\tundefined,\n\t\tundefined,\n\t\tundefined,\n\t\tRoot,\n\t\tRichTextField\n\t>\n\n\t// We need to exclude nodes _before_ we minify the tree as excluding\n\t// nodes could end up in more whitespaces to trim.\n\tself.use(() => {\n\t\treturn (tree: Root) => {\n\t\t\t// Extract container node if any is specified.\n\t\t\tif (config?.container) {\n\t\t\t\tconst element = select(config.container, tree)\n\n\t\t\t\tif (!element) {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`No container matching \\`${config?.container}\\` could be found in the input AST.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\n\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t// its children with the found element.\n\t\t\t\ttree.children = [element]\n\t\t\t}\n\n\t\t\t// Remove excluded nodes if any are specified\n\t\t\tif (config?.exclude) {\n\t\t\t\t// We join selector to only run one query\n\t\t\t\tconst nodesToExclude = selectAll(config.exclude.join(\", \"), tree)\n\n\t\t\t\tremove(tree, (node) => nodesToExclude.includes(node as Element))\n\t\t\t}\n\n\t\t\t// Include only nodes to include\n\t\t\tif (config?.include) {\n\t\t\t\tconst nodesToInclude: Element[] = []\n\n\t\t\t\t// We join selector to only run one query\n\t\t\t\tconst selector = config.include.join(\", \")\n\t\t\t\tconst rawNodesToInclude = selectAll(selector, tree)\n\n\t\t\t\t// We walk the tree to exclude matching nodes that are children of other matching nodes.\n\t\t\t\tvisit(tree, (node) => {\n\t\t\t\t\tif (rawNodesToInclude.includes(node as Element)) {\n\t\t\t\t\t\tnodesToInclude.push(node as Element)\n\n\t\t\t\t\t\t// Stop traversing this part of the tree since we found its matching parent node.\n\t\t\t\t\t\treturn SKIP\n\t\t\t\t\t}\n\t\t\t\t})\n\n\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t// its children with the found element.\n\t\t\t\ttree.children = nodesToInclude\n\t\t\t}\n\n\t\t\t// Mark nodes matching CSS selectors\n\t\t\tif (config?.serializer) {\n\t\t\t\tfor (const key in config.serializer) {\n\t\t\t\t\t// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).\n\t\t\t\t\t// Here we want to match anything that's not a valid HTML tag name and treat\n\t\t\t\t\t// it as a CSS selector. See: https://regex101.com/r/LILLWH/1\n\t\t\t\t\tif (!/^[a-z]+[1-6]?$/.test(key)) {\n\t\t\t\t\t\tconst matches = selectAll(key, tree)\n\n\t\t\t\t\t\tfor (let i = 0; i < matches.length; i++) {\n\t\t\t\t\t\t\tmatches[i].matchesSerializer = key\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t})\n\n\t// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's\n\t// registered within the plugin rather than on the processor directly.\n\tself.use(rehypeMinifyWhitespace)\n\n\tself.compiler = compiler\n\n\tfunction compiler(tree: Root, file: VFile): RichTextField {\n\t\tconst richTextField = hastToRichText(tree, file, config)\n\n\t\tif (config?.model) {\n\t\t\treturn filterRichTextField(richTextField, config.model)\n\t\t}\n\n\t\treturn richTextField\n\t}\n}\n\ndeclare module \"unified\" {\n\t// Register unified processor the result type.\n\tinterface CompileResultMap {\n\t\tRichTextField: RichTextField\n\t}\n}\n\ndeclare module \"hast\" {\n\t// Extend hast node with a rich text value.\n\tinterface Element {\n\t\t/**\n\t\t * A serializer this node matches to. Nodes are marked with this property\n\t\t * when they match a CSS selector from the serializer map.\n\t\t */\n\t\tmatchesSerializer?: string\n\t}\n}\n"],"names":["rehypeRichText"],"mappings":";;;;;;AAkEa,MAAA,iBAIT,SAASA,gBAAe,QAAM;AAKjC,QAAM,OAAO;AAUb,OAAK,IAAI,MAAK;AACb,WAAO,CAAC,SAAc;AAErB,UAAI,iCAAQ,WAAW;AACtB,cAAM,UAAU,OAAO,OAAO,WAAW,IAAI;AAE7C,YAAI,CAAC,SAAS;AACb,gBAAM,IAAI,MACT,2BAA2B,iCAAQ,SAAS,qCAAqC;AAAA,QAEnF;AAIK,aAAA,WAAW,CAAC,OAAO;AAAA,MACzB;AAGA,UAAI,iCAAQ,SAAS;AAEpB,cAAM,iBAAiB,UAAU,OAAO,QAAQ,KAAK,IAAI,GAAG,IAAI;AAEhE,eAAO,MAAM,CAAC,SAAS,eAAe,SAAS,IAAe,CAAC;AAAA,MAChE;AAGA,UAAI,iCAAQ,SAAS;AACpB,cAAM,iBAA4B,CAAA;AAGlC,cAAM,WAAW,OAAO,QAAQ,KAAK,IAAI;AACnC,cAAA,oBAAoB,UAAU,UAAU,IAAI;AAG5C,cAAA,MAAM,CAAC,SAAQ;AAChB,cAAA,kBAAkB,SAAS,IAAe,GAAG;AAChD,2BAAe,KAAK,IAAe;AAG5B,mBAAA;AAAA,UACR;AAAA,QAAA,CACA;AAID,aAAK,WAAW;AAAA,MACjB;AAGA,UAAI,iCAAQ,YAAY;AACZ,mBAAA,OAAO,OAAO,YAAY;AAIpC,cAAI,CAAC,iBAAiB,KAAK,GAAG,GAAG;AAC1B,kBAAA,UAAU,UAAU,KAAK,IAAI;AAEnC,qBAAS,IAAI,GAAG,IAAI,QAA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