@prismicio/client 7.7.4 → 7.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +0 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.js +1 -9
- package/dist/index.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/package.json +3 -15
- package/src/index.ts +0 -8
- package/dist/errors/PrismicRichTextError.cjs +0 -6
- package/dist/errors/PrismicRichTextError.cjs.map +0 -1
- package/dist/errors/PrismicRichTextError.d.ts +0 -2
- package/dist/errors/PrismicRichTextError.js +0 -6
- package/dist/errors/PrismicRichTextError.js.map +0 -1
- package/dist/errors/PrismicRichTextSerializerError.cjs +0 -7
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +0 -1
- package/dist/errors/PrismicRichTextSerializerError.d.ts +0 -3
- package/dist/errors/PrismicRichTextSerializerError.js +0 -7
- package/dist/errors/PrismicRichTextSerializerError.js.map +0 -1
- package/dist/helpers/unstable_htmlAsRichText.cjs +0 -11
- package/dist/helpers/unstable_htmlAsRichText.cjs.map +0 -1
- package/dist/helpers/unstable_htmlAsRichText.d.ts +0 -81
- package/dist/helpers/unstable_htmlAsRichText.js +0 -11
- package/dist/helpers/unstable_htmlAsRichText.js.map +0 -1
- package/dist/helpers/unstable_markdownAsRichText.cjs +0 -12
- package/dist/helpers/unstable_markdownAsRichText.cjs.map +0 -1
- package/dist/helpers/unstable_markdownAsRichText.d.ts +0 -30
- package/dist/helpers/unstable_markdownAsRichText.js +0 -12
- package/dist/helpers/unstable_markdownAsRichText.js.map +0 -1
- package/dist/lib/RichTextFieldBuilder.cjs +0 -115
- package/dist/lib/RichTextFieldBuilder.cjs.map +0 -1
- package/dist/lib/RichTextFieldBuilder.d.ts +0 -35
- package/dist/lib/RichTextFieldBuilder.js +0 -115
- package/dist/lib/RichTextFieldBuilder.js.map +0 -1
- package/dist/lib/filterRichTextField.cjs +0 -88
- package/dist/lib/filterRichTextField.cjs.map +0 -1
- package/dist/lib/filterRichTextField.d.ts +0 -12
- package/dist/lib/filterRichTextField.js +0 -88
- package/dist/lib/filterRichTextField.js.map +0 -1
- package/dist/lib/hastSerializerHelpers.cjs +0 -96
- package/dist/lib/hastSerializerHelpers.cjs.map +0 -1
- package/dist/lib/hastSerializerHelpers.d.ts +0 -28
- package/dist/lib/hastSerializerHelpers.js +0 -96
- package/dist/lib/hastSerializerHelpers.js.map +0 -1
- package/dist/lib/hastToRichText.cjs +0 -164
- package/dist/lib/hastToRichText.cjs.map +0 -1
- package/dist/lib/hastToRichText.d.ts +0 -30
- package/dist/lib/hastToRichText.js +0 -164
- package/dist/lib/hastToRichText.js.map +0 -1
- package/dist/lib/rehypeRichText.cjs +0 -59
- package/dist/lib/rehypeRichText.cjs.map +0 -1
- package/dist/lib/rehypeRichText.d.ts +0 -66
- package/dist/lib/rehypeRichText.js +0 -59
- package/dist/lib/rehypeRichText.js.map +0 -1
- package/src/errors/PrismicRichTextError.ts +0 -1
- package/src/errors/PrismicRichTextSerializerError.ts +0 -3
- package/src/helpers/unstable_htmlAsRichText.ts +0 -122
- package/src/helpers/unstable_markdownAsRichText.ts +0 -53
- package/src/lib/RichTextFieldBuilder.ts +0 -164
- package/src/lib/filterRichTextField.ts +0 -159
- package/src/lib/hastSerializerHelpers.ts +0 -157
- package/src/lib/hastToRichText.ts +0 -295
- package/src/lib/rehypeRichText.ts +0 -184
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { Element } from "hast";
|
|
2
|
-
import type { RTEmbedNode, RTImageNode, RTLinkNode } from "../types/value/richText";
|
|
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;
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { toHtml } from "hast-util-to-html";
|
|
2
|
-
import { OEmbedType } from "../types/value/embed.js";
|
|
3
|
-
import { LinkType } from "../types/value/link.js";
|
|
4
|
-
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError.js";
|
|
5
|
-
const serializeImage = (node) => {
|
|
6
|
-
var _a, _b, _c, _d, _e;
|
|
7
|
-
const src = (_a = node.properties) == null ? void 0 : _a.src;
|
|
8
|
-
if (!src) {
|
|
9
|
-
throw new PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
|
|
10
|
-
}
|
|
11
|
-
const url = new URL(src, "https://noop.com");
|
|
12
|
-
let width = (_b = node.properties) == null ? void 0 : _b.width;
|
|
13
|
-
let height = (_c = node.properties) == null ? void 0 : _c.height;
|
|
14
|
-
let x = 0;
|
|
15
|
-
let y = 0;
|
|
16
|
-
let zoom = 1;
|
|
17
|
-
if (url.hostname === "images.prismic.io") {
|
|
18
|
-
if (url.searchParams.has("w")) {
|
|
19
|
-
width = Number(url.searchParams.get("w"));
|
|
20
|
-
}
|
|
21
|
-
if (url.searchParams.has("h")) {
|
|
22
|
-
height = Number(url.searchParams.get("h"));
|
|
23
|
-
}
|
|
24
|
-
if (url.searchParams.has("rect")) {
|
|
25
|
-
const [rectX, rextY, rectW, _rectH] = url.searchParams.get("rect").split(",");
|
|
26
|
-
x = Number(rectX);
|
|
27
|
-
y = Number(rextY);
|
|
28
|
-
if (width) {
|
|
29
|
-
zoom = Math.max(1, width / Number(rectW));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return {
|
|
34
|
-
type: "image",
|
|
35
|
-
id: "",
|
|
36
|
-
url: src,
|
|
37
|
-
alt: ((_d = node.properties) == null ? void 0 : _d.alt) ?? "",
|
|
38
|
-
copyright: ((_e = node.properties) == null ? void 0 : _e.copyright) ?? null,
|
|
39
|
-
// This is not accurate. We're doing the casting because it seems the migration API
|
|
40
|
-
// accepts `undefined` values for those fields which is more convenient to us here.
|
|
41
|
-
// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185
|
|
42
|
-
dimensions: { width, height },
|
|
43
|
-
edit: { x, y, zoom, background: "transparent" }
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
const serializeEmbed = (node) => {
|
|
47
|
-
var _a, _b, _c, _d;
|
|
48
|
-
const src = (_a = node.properties) == null ? void 0 : _a.src;
|
|
49
|
-
if (!src) {
|
|
50
|
-
throw new PrismicRichTextSerializerError("Element of type `embed` is missing an `src` attribute");
|
|
51
|
-
}
|
|
52
|
-
const oembedBase = {
|
|
53
|
-
version: "1.0",
|
|
54
|
-
embed_url: src,
|
|
55
|
-
html: toHtml(node),
|
|
56
|
-
title: (_b = node.properties) == null ? void 0 : _b.title
|
|
57
|
-
};
|
|
58
|
-
const width = (_c = node.properties) == null ? void 0 : _c.width;
|
|
59
|
-
const height = (_d = node.properties) == null ? void 0 : _d.height;
|
|
60
|
-
node.children = [];
|
|
61
|
-
if (width && height) {
|
|
62
|
-
return {
|
|
63
|
-
type: "embed",
|
|
64
|
-
oembed: { ...oembedBase, type: OEmbedType.Rich, width, height }
|
|
65
|
-
};
|
|
66
|
-
} else {
|
|
67
|
-
return {
|
|
68
|
-
type: "embed",
|
|
69
|
-
oembed: { ...oembedBase, type: OEmbedType.Link }
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
const serializeSpan = (node, rtPartialInlineNode) => {
|
|
74
|
-
var _a, _b;
|
|
75
|
-
if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
|
|
76
|
-
const url = (_a = node.properties) == null ? void 0 : _a.href;
|
|
77
|
-
if (!url) {
|
|
78
|
-
throw new PrismicRichTextSerializerError("Element of type `hyperlink` is missing an `href` attribute");
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
type: "hyperlink",
|
|
82
|
-
data: {
|
|
83
|
-
link_type: LinkType.Web,
|
|
84
|
-
url,
|
|
85
|
-
target: (_b = node.properties) == null ? void 0 : _b.target
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
return rtPartialInlineNode;
|
|
90
|
-
};
|
|
91
|
-
export {
|
|
92
|
-
serializeEmbed,
|
|
93
|
-
serializeImage,
|
|
94
|
-
serializeSpan
|
|
95
|
-
};
|
|
96
|
-
//# sourceMappingURL=hastSerializerHelpers.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hastSerializerHelpers.js","sources":["../../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import type { Element } from \"hast\"\nimport { toHtml } from \"hast-util-to-html\"\n\nimport { OEmbedType } from \"../types/value/embed\"\nimport { LinkType } from \"../types/value/link\"\nimport type {\n\tRTEmbedNode,\n\tRTImageNode,\n\tRTLinkNode,\n} from \"../types/value/richText\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\n\nimport type { RTPartialInlineNode } from \"./RichTextFieldBuilder\"\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTImageNode}.\n *\n * @param node - A hast node to serialize.\n *\n * @returns Equivalent {@link RTImageNode}.\n */\nexport const serializeImage = (node: Element): RTImageNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\"Element of type `img` is missing an `src` attribute\",\n\t\t)\n\t}\n\n\tconst url = new URL(src, \"https://noop.com\")\n\n\tlet width = node.properties?.width as number | undefined\n\tlet height = node.properties?.height as number | undefined\n\tlet x = 0\n\tlet y = 0\n\tlet zoom = 1\n\n\t// Attempt to infer the image dimensions from the URL imgix parameters.\n\tif (url.hostname === \"images.prismic.io\") {\n\t\tif (url.searchParams.has(\"w\")) {\n\t\t\twidth = Number(url.searchParams.get(\"w\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"h\")) {\n\t\t\theight = Number(url.searchParams.get(\"h\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"rect\")) {\n\t\t\tconst [rectX, rextY, rectW, _rectH] = url.searchParams\n\t\t\t\t.get(\"rect\")!\n\t\t\t\t.split(\",\")\n\n\t\t\tx = Number(rectX)\n\t\t\ty = Number(rextY)\n\n\t\t\t// This is not perfect but it's supposed to work on images without constraints.\n\t\t\tif (width) {\n\t\t\t\tzoom = Math.max(1, width / Number(rectW))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\ttype: \"image\",\n\t\tid: \"\",\n\t\turl: src,\n\t\talt: (node.properties?.alt as string) ?? \"\",\n\t\tcopyright: (node.properties?.copyright as string) ?? null,\n\t\t// This is not accurate. We're doing the casting because it seems the migration API\n\t\t// accepts `undefined` values for those fields which is more convenient to us here.\n\t\t// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185\n\t\tdimensions: { width: width as number, height: height as number },\n\t\tedit: { x, y, zoom, background: \"transparent\" },\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTEmbedNode}.\n *\n * @param node - A hast node to serialize.\n *\n * @returns Equivalent {@link RTEmbedNode}.\n */\nexport const serializeEmbed = (node: Element): RTEmbedNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\"Element of type `embed` is missing an `src` attribute\",\n\t\t)\n\t}\n\n\tconst oembedBase = {\n\t\tversion: \"1.0\",\n\t\tembed_url: src,\n\t\thtml: toHtml(node),\n\t\ttitle: node.properties?.title as string | undefined,\n\t}\n\n\tconst width = node.properties?.width as number | undefined\n\tconst height = node.properties?.height as number | undefined\n\n\t// Remove the children of the embed node as we don't want to process them.\n\tnode.children = []\n\n\tif (width && height) {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Rich, width, height },\n\t\t}\n\t} else {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Link },\n\t\t}\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.\n *\n * @param node - A hast node to serialize.\n * @param rtPartialInlineNode - A partial rich text node.\n *\n * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode:\n\t\t| RTPartialInlineNode\n\t\t| Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (\n\t\trtPartialInlineNode.type === \"hyperlink\" &&\n\t\t!(\"data\" in rtPartialInlineNode)\n\t) {\n\t\tconst url = node.properties?.href as string | undefined\n\t\tif (!url) {\n\t\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\t\"Element of type `hyperlink` is missing an `href` attribute\",\n\t\t\t)\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"hyperlink\",\n\t\t\tdata: {\n\t\t\t\tlink_type: LinkType.Web,\n\t\t\t\turl,\n\t\t\t\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a,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAI,+BACT,uDAAuD;AAAA,EAEzD;AAEA,QAAM,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,WAAW;AAAA,IACX,MAAM,OAAO,IAAI;AAAA,IACjB,QAAO,UAAK,eAAL,mBAAiB;AAAA,EAAA;AAGnB,QAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,QAAA,UAAS,UAAK,eAAL,mBAAiB;AAGhC,OAAK,WAAW;AAEhB,MAAI,SAAS,QAAQ;AACb,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAM,WAAW,MAAM,OAAO,OAAQ;AAAA,IAAA;AAAA,SAE1D;AACC,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAM,WAAW,KAAM;AAAA,IAAA;AAAA,EAElD;AACD;AAUa,MAAA,gBAAgB,CAC5B,MACA,wBAGwB;;AACxB,MACC,oBAAoB,SAAS,eAC7B,EAAE,UAAU,sBACX;AACK,UAAA,OAAM,UAAK,eAAL,mBAAiB;AAC7B,QAAI,CAAC,KAAK;AACH,YAAA,IAAI,+BACT,4DAA4D;AAAA,IAE9D;AAEO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,QACL,WAAW,SAAS;AAAA,QACpB;AAAA,QACA,SAAQ,UAAK,eAAL,mBAAiB;AAAA,MACzB;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;"}
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const mdastUtilToString = require("mdast-util-to-string");
|
|
4
|
-
const unistUtilVisit = require("unist-util-visit");
|
|
5
|
-
const richText = require("../types/value/richText.cjs");
|
|
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 = richText.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 richText.RichTextNodeType.strong:
|
|
64
|
-
case richText.RichTextNodeType.em:
|
|
65
|
-
case richText.RichTextNodeType.label:
|
|
66
|
-
case richText.RichTextNodeType.hyperlink: {
|
|
67
|
-
const length = mdastUtilToString.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 richText.RichTextNodeType.image:
|
|
77
|
-
case richText.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: richText.RichTextNodeType.label, data: shorthand };
|
|
95
|
-
}
|
|
96
|
-
try {
|
|
97
|
-
switch (match.type) {
|
|
98
|
-
case richText.RichTextNodeType.heading1:
|
|
99
|
-
case richText.RichTextNodeType.heading2:
|
|
100
|
-
case richText.RichTextNodeType.heading3:
|
|
101
|
-
case richText.RichTextNodeType.heading4:
|
|
102
|
-
case richText.RichTextNodeType.heading5:
|
|
103
|
-
case richText.RichTextNodeType.heading6:
|
|
104
|
-
case richText.RichTextNodeType.paragraph:
|
|
105
|
-
case richText.RichTextNodeType.preformatted:
|
|
106
|
-
case richText.RichTextNodeType.listItem:
|
|
107
|
-
case richText.RichTextNodeType.oListItem:
|
|
108
|
-
lastRTTextNodeType = match.type;
|
|
109
|
-
builder.appendTextNode(match.type, config == null ? void 0 : config.direction);
|
|
110
|
-
break;
|
|
111
|
-
case richText.RichTextNodeType.list:
|
|
112
|
-
case richText.RichTextNodeType.oList:
|
|
113
|
-
lastListType = match.type;
|
|
114
|
-
break;
|
|
115
|
-
case richText.RichTextNodeType.image:
|
|
116
|
-
builder.appendNode(hastSerializerHelpers.serializeImage(node));
|
|
117
|
-
break;
|
|
118
|
-
case richText.RichTextNodeType.embed:
|
|
119
|
-
builder.appendNode(hastSerializerHelpers.serializeEmbed(node));
|
|
120
|
-
break;
|
|
121
|
-
case richText.RichTextNodeType.strong:
|
|
122
|
-
case richText.RichTextNodeType.em:
|
|
123
|
-
case richText.RichTextNodeType.label:
|
|
124
|
-
case richText.RichTextNodeType.hyperlink: {
|
|
125
|
-
const span = hastSerializerHelpers.serializeSpan(node, match);
|
|
126
|
-
const length = mdastUtilToString.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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hastToRichText.cjs","sources":["../../../src/lib/hastToRichText.ts"],"sourcesContent":["import type { Element, Root } from \"hast\"\nimport { toString } from \"mdast-util-to-string\"\nimport { visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport type {\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTLabelNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"../types/value/richText\"\nimport { RichTextNodeType } from \"../types/value/richText\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\n\nimport type {\n\tRichTextHTMLMapSerializer,\n\tRichTextHTMLMapSerializerFunction,\n\tRichTextHTMLMapSerializerShorthand,\n} from \"../helpers/unstable_htmlAsRichText\"\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","PrismicRichTextSerializer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O,QAAQ;AAChB;;"}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { Element, Root } from "hast";
|
|
2
|
-
import type { VFile } from "vfile";
|
|
3
|
-
import type { RichTextField } from "../types/value/richText";
|
|
4
|
-
import type { RichTextHTMLMapSerializer } from "../helpers/unstable_htmlAsRichText";
|
|
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;
|
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { toString } from "mdast-util-to-string";
|
|
2
|
-
import { visit } from "unist-util-visit";
|
|
3
|
-
import { RichTextNodeType } from "../types/value/richText.js";
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hastToRichText.js","sources":["../../../src/lib/hastToRichText.ts"],"sourcesContent":["import type { Element, Root } from \"hast\"\nimport { toString } from \"mdast-util-to-string\"\nimport { visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport type {\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTLabelNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"../types/value/richText\"\nimport { RichTextNodeType } from \"../types/value/richText\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\n\nimport type {\n\tRichTextHTMLMapSerializer,\n\tRichTextHTMLMapSerializerFunction,\n\tRichTextHTMLMapSerializerShorthand,\n} from \"../helpers/unstable_htmlAsRichText\"\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":";;;;;;AAyCA,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,C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,IAAI;AAAA,QAEP;AAAA,eACQ,OAAO;AACf,YAAI,iBAAiB,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;"}
|
|
@@ -1,59 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"rehypeRichText.cjs","sources":["../../../src/lib/rehypeRichText.ts"],"sourcesContent":["import 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 type { CustomTypeModelRichTextField } from \"../types/model/richText\"\nimport type { RichTextField } from \"../types/value/richText\"\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":";;;;;;;;AAiEa,MAAA,iBAIT,SAASA,gBAAe,QAAM;AAKjC,QAAM,OAAO;AAUb,OAAK,IAAI,MAAK;AACb,WAAO,CAAC,SAAc;AAErB,UA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