@prismicio/migrate 0.0.1 → 0.0.2-canary.6b4d958
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/errors/PrismicRichTextError.cjs +5 -5
- package/dist/errors/PrismicRichTextError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextError.d.cts +5 -0
- package/dist/errors/PrismicRichTextError.d.cts.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +5 -2
- package/dist/errors/PrismicRichTextError.d.ts.map +1 -0
- package/dist/errors/PrismicRichTextError.js +6 -6
- package/dist/errors/PrismicRichTextError.js.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.cjs +6 -6
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.d.cts +7 -0
- package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +7 -3
- package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.js +6 -6
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
- package/dist/htmlAsRichText.cjs +24 -8
- package/dist/htmlAsRichText.cjs.map +1 -1
- package/dist/htmlAsRichText.d.cts +23 -0
- package/dist/htmlAsRichText.d.cts.map +1 -0
- package/dist/htmlAsRichText.d.ts +14 -69
- package/dist/htmlAsRichText.d.ts.map +1 -0
- package/dist/htmlAsRichText.js +21 -7
- package/dist/htmlAsRichText.js.map +1 -1
- package/dist/index.cjs +6 -8
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +2 -7
- package/dist/lib/RichTextFieldBuilder.cjs +82 -113
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.cts +16 -0
- package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +8 -27
- package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -0
- package/dist/lib/RichTextFieldBuilder.js +82 -113
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +77 -78
- package/dist/lib/filterRichTextField.cjs.map +1 -1
- package/dist/lib/filterRichTextField.js +77 -79
- package/dist/lib/filterRichTextField.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs +102 -86
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
- package/dist/lib/hastSerializerHelpers.js +101 -88
- package/dist/lib/hastSerializerHelpers.js.map +1 -1
- package/dist/lib/hastToRichText.cjs +152 -157
- package/dist/lib/hastToRichText.cjs.map +1 -1
- package/dist/lib/hastToRichText.d.cts +21 -0
- package/dist/lib/hastToRichText.d.cts.map +1 -0
- package/dist/lib/hastToRichText.d.ts +20 -29
- package/dist/lib/hastToRichText.d.ts.map +1 -0
- package/dist/lib/hastToRichText.js +150 -156
- package/dist/lib/hastToRichText.js.map +1 -1
- package/dist/lib/rehypeRichText.cjs +56 -57
- package/dist/lib/rehypeRichText.cjs.map +1 -1
- package/dist/lib/rehypeRichText.d.cts +57 -0
- package/dist/lib/rehypeRichText.d.cts.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +53 -61
- package/dist/lib/rehypeRichText.d.ts.map +1 -0
- package/dist/lib/rehypeRichText.js +52 -55
- package/dist/lib/rehypeRichText.js.map +1 -1
- package/dist/types.d.cts +46 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +30 -41
- package/dist/types.d.ts.map +1 -0
- package/package.json +35 -58
- package/src/htmlAsRichText.ts +22 -49
- package/src/lib/RichTextFieldBuilder.ts +26 -31
- package/src/lib/filterRichTextField.ts +6 -25
- package/src/lib/hastSerializerHelpers.ts +4 -17
- package/src/lib/hastToRichText.ts +44 -66
- package/src/lib/rehypeRichText.ts +88 -102
- package/src/types.ts +24 -33
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/filterRichTextField.d.ts +0 -10
- package/dist/lib/hastSerializerHelpers.d.ts +0 -28
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastSerializerHelpers.cjs","sources":["
|
|
1
|
+
{"version":3,"file":"hastSerializerHelpers.cjs","names":["PrismicRichTextSerializerError","OEmbedType","LinkType"],"sources":["../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { LinkType, OEmbedType } from \"@prismicio/client\"\nimport type { RTEmbedNode, RTImageNode, RTLinkNode } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport { toHtml } from \"hast-util-to-html\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\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 * @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(\"Element of type `img` is missing an `src` attribute\")\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.get(\"rect\")!.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 * @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 * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (rtPartialInlineNode.type === \"hyperlink\" && !(\"data\" in rtPartialInlineNode)) {\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\ttarget: node.properties?.target as string | undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn rtPartialInlineNode\n}\n"],"mappings":";;;;;;;;;;;AAcA,MAAa,kBAAkB,SAA+B;CAC7D,MAAM,MAAM,KAAK,YAAY;AAE7B,KAAI,CAAC,IACJ,OAAM,IAAIA,uCAAAA,+BAA+B,sDAAsD;CAGhG,MAAM,MAAM,IAAI,IAAI,KAAK,mBAAmB;CAE5C,IAAI,QAAQ,KAAK,YAAY;CAC7B,IAAI,SAAS,KAAK,YAAY;CAC9B,IAAI,IAAI;CACR,IAAI,IAAI;CACR,IAAI,OAAO;AAGX,KAAI,IAAI,aAAa,qBAAqB;AACzC,MAAI,IAAI,aAAa,IAAI,IAAI,CAC5B,SAAQ,OAAO,IAAI,aAAa,IAAI,IAAI,CAAC;AAG1C,MAAI,IAAI,aAAa,IAAI,IAAI,CAC5B,UAAS,OAAO,IAAI,aAAa,IAAI,IAAI,CAAC;AAG3C,MAAI,IAAI,aAAa,IAAI,OAAO,EAAE;GACjC,MAAM,CAAC,OAAO,OAAO,OAAO,UAAU,IAAI,aAAa,IAAI,OAAO,CAAE,MAAM,IAAI;AAE9E,OAAI,OAAO,MAAM;AACjB,OAAI,OAAO,MAAM;AAGjB,OAAI,MACH,QAAO,KAAK,IAAI,GAAG,QAAQ,OAAO,MAAM,CAAC;;;AAK5C,QAAO;EACN,MAAM;EACN,IAAI;EACJ,KAAK;EACL,KAAM,KAAK,YAAY,OAAkB;EACzC,WAAY,KAAK,YAAY,aAAwB;EAIrD,YAAY;GAAS;GAAyB;GAAkB;EAChE,MAAM;GAAE;GAAG;GAAG;GAAM,YAAY;GAAe;EAC/C;;;;;;;;AASF,MAAa,kBAAkB,SAA+B;CAC7D,MAAM,MAAM,KAAK,YAAY;AAE7B,KAAI,CAAC,IACJ,OAAM,IAAIA,uCAAAA,+BACT,wDACA;CAGF,MAAM,aAAa;EAClB,SAAS;EACT,WAAW;EACX,OAAA,GAAA,kBAAA,QAAa,KAAK;EAClB,OAAO,KAAK,YAAY;EACxB;CAED,MAAM,QAAQ,KAAK,YAAY;CAC/B,MAAM,SAAS,KAAK,YAAY;AAGhC,MAAK,WAAW,EAAE;AAElB,KAAI,SAAS,OACZ,QAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAMC,kBAAAA,WAAW;GAAM;GAAO;GAAQ;EAC/D;KAED,QAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAMA,kBAAAA,WAAW;GAAM;EAChD;;;;;;;;;AAWH,MAAa,iBACZ,MACA,wBACyB;AACzB,KAAI,oBAAoB,SAAS,eAAe,EAAE,UAAU,sBAAsB;EACjF,MAAM,MAAM,KAAK,YAAY;AAC7B,MAAI,CAAC,IACJ,OAAM,IAAID,uCAAAA,+BACT,6DACA;AAGF,SAAO;GACN,MAAM;GACN,MAAM;IACL,WAAWE,kBAAAA,SAAS;IACpB;IACA,QAAQ,KAAK,YAAY;IACzB;GACD;;AAGF,QAAO"}
|
|
@@ -1,95 +1,108 @@
|
|
|
1
|
-
import { OEmbedType, LinkType } from "@prismicio/client";
|
|
2
|
-
import { toHtml } from "hast-util-to-html";
|
|
3
1
|
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError.js";
|
|
2
|
+
import { LinkType, OEmbedType } from "@prismicio/client";
|
|
3
|
+
import { toHtml } from "hast-util-to-html";
|
|
4
|
+
//#region src/lib/hastSerializerHelpers.ts
|
|
5
|
+
/**
|
|
6
|
+
* Serializes a hast {@link Element} node to a {@link RTImageNode}.
|
|
7
|
+
*
|
|
8
|
+
* @param node - A hast node to serialize.
|
|
9
|
+
* @returns Equivalent {@link RTImageNode}.
|
|
10
|
+
*/
|
|
4
11
|
const serializeImage = (node) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185
|
|
41
|
-
dimensions: { width, height },
|
|
42
|
-
edit: { x, y, zoom, background: "transparent" }
|
|
43
|
-
};
|
|
12
|
+
const src = node.properties?.src;
|
|
13
|
+
if (!src) throw new PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
|
|
14
|
+
const url = new URL(src, "https://noop.com");
|
|
15
|
+
let width = node.properties?.width;
|
|
16
|
+
let height = node.properties?.height;
|
|
17
|
+
let x = 0;
|
|
18
|
+
let y = 0;
|
|
19
|
+
let zoom = 1;
|
|
20
|
+
if (url.hostname === "images.prismic.io") {
|
|
21
|
+
if (url.searchParams.has("w")) width = Number(url.searchParams.get("w"));
|
|
22
|
+
if (url.searchParams.has("h")) height = Number(url.searchParams.get("h"));
|
|
23
|
+
if (url.searchParams.has("rect")) {
|
|
24
|
+
const [rectX, rextY, rectW, _rectH] = url.searchParams.get("rect").split(",");
|
|
25
|
+
x = Number(rectX);
|
|
26
|
+
y = Number(rextY);
|
|
27
|
+
if (width) zoom = Math.max(1, width / Number(rectW));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
type: "image",
|
|
32
|
+
id: "",
|
|
33
|
+
url: src,
|
|
34
|
+
alt: node.properties?.alt ?? "",
|
|
35
|
+
copyright: node.properties?.copyright ?? null,
|
|
36
|
+
dimensions: {
|
|
37
|
+
width,
|
|
38
|
+
height
|
|
39
|
+
},
|
|
40
|
+
edit: {
|
|
41
|
+
x,
|
|
42
|
+
y,
|
|
43
|
+
zoom,
|
|
44
|
+
background: "transparent"
|
|
45
|
+
}
|
|
46
|
+
};
|
|
44
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* Serializes a hast {@link Element} node to a {@link RTEmbedNode}.
|
|
50
|
+
*
|
|
51
|
+
* @param node - A hast node to serialize.
|
|
52
|
+
* @returns Equivalent {@link RTEmbedNode}.
|
|
53
|
+
*/
|
|
45
54
|
const serializeEmbed = (node) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
55
|
+
const src = node.properties?.src;
|
|
56
|
+
if (!src) throw new PrismicRichTextSerializerError("Element of type `embed` is missing an `src` attribute");
|
|
57
|
+
const oembedBase = {
|
|
58
|
+
version: "1.0",
|
|
59
|
+
embed_url: src,
|
|
60
|
+
html: toHtml(node),
|
|
61
|
+
title: node.properties?.title
|
|
62
|
+
};
|
|
63
|
+
const width = node.properties?.width;
|
|
64
|
+
const height = node.properties?.height;
|
|
65
|
+
node.children = [];
|
|
66
|
+
if (width && height) return {
|
|
67
|
+
type: "embed",
|
|
68
|
+
oembed: {
|
|
69
|
+
...oembedBase,
|
|
70
|
+
type: OEmbedType.Rich,
|
|
71
|
+
width,
|
|
72
|
+
height
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
else return {
|
|
76
|
+
type: "embed",
|
|
77
|
+
oembed: {
|
|
78
|
+
...oembedBase,
|
|
79
|
+
type: OEmbedType.Link
|
|
80
|
+
}
|
|
81
|
+
};
|
|
71
82
|
};
|
|
83
|
+
/**
|
|
84
|
+
* Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.
|
|
85
|
+
*
|
|
86
|
+
* @param node - A hast node to serialize.
|
|
87
|
+
* @param rtPartialInlineNode - A partial rich text node.
|
|
88
|
+
* @returns Equivalent {@link RTPartialInlineNode}.
|
|
89
|
+
*/
|
|
72
90
|
const serializeSpan = (node, rtPartialInlineNode) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return rtPartialInlineNode;
|
|
89
|
-
};
|
|
90
|
-
export {
|
|
91
|
-
serializeEmbed,
|
|
92
|
-
serializeImage,
|
|
93
|
-
serializeSpan
|
|
91
|
+
if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
|
|
92
|
+
const url = node.properties?.href;
|
|
93
|
+
if (!url) throw new PrismicRichTextSerializerError("Element of type `hyperlink` is missing an `href` attribute");
|
|
94
|
+
return {
|
|
95
|
+
type: "hyperlink",
|
|
96
|
+
data: {
|
|
97
|
+
link_type: LinkType.Web,
|
|
98
|
+
url,
|
|
99
|
+
target: node.properties?.target
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return rtPartialInlineNode;
|
|
94
104
|
};
|
|
95
|
-
//#
|
|
105
|
+
//#endregion
|
|
106
|
+
export { serializeEmbed, serializeImage, serializeSpan };
|
|
107
|
+
|
|
108
|
+
//# sourceMappingURL=hastSerializerHelpers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastSerializerHelpers.js","sources":["
|
|
1
|
+
{"version":3,"file":"hastSerializerHelpers.js","names":[],"sources":["../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { LinkType, OEmbedType } from \"@prismicio/client\"\nimport type { RTEmbedNode, RTImageNode, RTLinkNode } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport { toHtml } from \"hast-util-to-html\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\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 * @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(\"Element of type `img` is missing an `src` attribute\")\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.get(\"rect\")!.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 * @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 * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (rtPartialInlineNode.type === \"hyperlink\" && !(\"data\" in rtPartialInlineNode)) {\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\ttarget: node.properties?.target as string | undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn rtPartialInlineNode\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAa,kBAAkB,SAA+B;CAC7D,MAAM,MAAM,KAAK,YAAY;AAE7B,KAAI,CAAC,IACJ,OAAM,IAAI,+BAA+B,sDAAsD;CAGhG,MAAM,MAAM,IAAI,IAAI,KAAK,mBAAmB;CAE5C,IAAI,QAAQ,KAAK,YAAY;CAC7B,IAAI,SAAS,KAAK,YAAY;CAC9B,IAAI,IAAI;CACR,IAAI,IAAI;CACR,IAAI,OAAO;AAGX,KAAI,IAAI,aAAa,qBAAqB;AACzC,MAAI,IAAI,aAAa,IAAI,IAAI,CAC5B,SAAQ,OAAO,IAAI,aAAa,IAAI,IAAI,CAAC;AAG1C,MAAI,IAAI,aAAa,IAAI,IAAI,CAC5B,UAAS,OAAO,IAAI,aAAa,IAAI,IAAI,CAAC;AAG3C,MAAI,IAAI,aAAa,IAAI,OAAO,EAAE;GACjC,MAAM,CAAC,OAAO,OAAO,OAAO,UAAU,IAAI,aAAa,IAAI,OAAO,CAAE,MAAM,IAAI;AAE9E,OAAI,OAAO,MAAM;AACjB,OAAI,OAAO,MAAM;AAGjB,OAAI,MACH,QAAO,KAAK,IAAI,GAAG,QAAQ,OAAO,MAAM,CAAC;;;AAK5C,QAAO;EACN,MAAM;EACN,IAAI;EACJ,KAAK;EACL,KAAM,KAAK,YAAY,OAAkB;EACzC,WAAY,KAAK,YAAY,aAAwB;EAIrD,YAAY;GAAS;GAAyB;GAAkB;EAChE,MAAM;GAAE;GAAG;GAAG;GAAM,YAAY;GAAe;EAC/C;;;;;;;;AASF,MAAa,kBAAkB,SAA+B;CAC7D,MAAM,MAAM,KAAK,YAAY;AAE7B,KAAI,CAAC,IACJ,OAAM,IAAI,+BACT,wDACA;CAGF,MAAM,aAAa;EAClB,SAAS;EACT,WAAW;EACX,MAAM,OAAO,KAAK;EAClB,OAAO,KAAK,YAAY;EACxB;CAED,MAAM,QAAQ,KAAK,YAAY;CAC/B,MAAM,SAAS,KAAK,YAAY;AAGhC,MAAK,WAAW,EAAE;AAElB,KAAI,SAAS,OACZ,QAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAM,WAAW;GAAM;GAAO;GAAQ;EAC/D;KAED,QAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAM,WAAW;GAAM;EAChD;;;;;;;;;AAWH,MAAa,iBACZ,MACA,wBACyB;AACzB,KAAI,oBAAoB,SAAS,eAAe,EAAE,UAAU,sBAAsB;EACjF,MAAM,MAAM,KAAK,YAAY;AAC7B,MAAI,CAAC,IACJ,OAAM,IAAI,+BACT,6DACA;AAGF,SAAO;GACN,MAAM;GACN,MAAM;IACL,WAAW,SAAS;IACpB;IACA,QAAQ,KAAK,YAAY;IACzB;GACD;;AAGF,QAAO"}
|
|
@@ -1,164 +1,159 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
const require_PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
|
|
3
|
+
const require_hastSerializerHelpers = require("./hastSerializerHelpers.cjs");
|
|
4
|
+
const require_RichTextFieldBuilder = require("./RichTextFieldBuilder.cjs");
|
|
5
|
+
let unist_util_visit = require("unist-util-visit");
|
|
6
|
+
let _prismicio_client = require("@prismicio/client");
|
|
7
|
+
let hast_util_to_string = require("hast-util-to-string");
|
|
8
|
+
//#region src/lib/hastToRichText.ts
|
|
9
9
|
const DEFAULT_SERIALIZER = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
26
|
};
|
|
27
27
|
const VFILE_RULE = "failed-to-serialize-node";
|
|
28
28
|
const VFILE_SOURCE = "prismic";
|
|
29
|
+
/**
|
|
30
|
+
* Transfor a hast tree to a rich text field.
|
|
31
|
+
*
|
|
32
|
+
* @param tree - The hast tree to transform.
|
|
33
|
+
* @param file - The vfile to attach warnings to.
|
|
34
|
+
* @param config - Configuration that determines the output of the function.
|
|
35
|
+
* @returns The rich text field equivalent of the provided hast tree.
|
|
36
|
+
*/
|
|
29
37
|
const hastToRichText = (tree, file, config) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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();
|
|
38
|
+
const builder = new require_RichTextFieldBuilder.RichTextFieldBuilder();
|
|
39
|
+
const serializer = {
|
|
40
|
+
...DEFAULT_SERIALIZER,
|
|
41
|
+
...config?.serializer
|
|
42
|
+
};
|
|
43
|
+
let lastRTTextNodeType = _prismicio_client.RichTextNodeType.paragraph;
|
|
44
|
+
let lastListType = void 0;
|
|
45
|
+
(0, unist_util_visit.visit)(tree, (node) => {
|
|
46
|
+
if (node.type === "element") {
|
|
47
|
+
if (node.tagName === "br") try {
|
|
48
|
+
builder.appendText("\n");
|
|
49
|
+
} catch {}
|
|
50
|
+
let serializerOrShorthand;
|
|
51
|
+
if (node.matchesSerializer && node.matchesSerializer in serializer) serializerOrShorthand = serializer[node.matchesSerializer];
|
|
52
|
+
else if (node.tagName in serializer) serializerOrShorthand = serializer[node.tagName];
|
|
53
|
+
else return;
|
|
54
|
+
let shorthand;
|
|
55
|
+
if (typeof serializerOrShorthand === "function") {
|
|
56
|
+
const shorthandOrNode = serializerOrShorthand({
|
|
57
|
+
node,
|
|
58
|
+
context: { listType: lastListType }
|
|
59
|
+
});
|
|
60
|
+
if (!shorthandOrNode) return;
|
|
61
|
+
else if (typeof shorthandOrNode === "object" && "type" in shorthandOrNode) switch (shorthandOrNode.type) {
|
|
62
|
+
case _prismicio_client.RichTextNodeType.strong:
|
|
63
|
+
case _prismicio_client.RichTextNodeType.em:
|
|
64
|
+
case _prismicio_client.RichTextNodeType.label:
|
|
65
|
+
case _prismicio_client.RichTextNodeType.hyperlink: {
|
|
66
|
+
const length = (0, hast_util_to_string.toString)(node).trimEnd().length;
|
|
67
|
+
try {
|
|
68
|
+
builder.appendSpan(shorthandOrNode, length);
|
|
69
|
+
} catch {
|
|
70
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction);
|
|
71
|
+
builder.appendSpan(shorthandOrNode, length);
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
case _prismicio_client.RichTextNodeType.image:
|
|
76
|
+
case _prismicio_client.RichTextNodeType.embed:
|
|
77
|
+
builder.appendNode(shorthandOrNode);
|
|
78
|
+
return;
|
|
79
|
+
default:
|
|
80
|
+
lastRTTextNodeType = shorthandOrNode.type;
|
|
81
|
+
builder.appendNode(shorthandOrNode);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
shorthand = shorthandOrNode;
|
|
85
|
+
} else shorthand = serializerOrShorthand;
|
|
86
|
+
let match;
|
|
87
|
+
if (typeof shorthand === "string") match = { type: shorthand };
|
|
88
|
+
else match = {
|
|
89
|
+
type: _prismicio_client.RichTextNodeType.label,
|
|
90
|
+
data: shorthand
|
|
91
|
+
};
|
|
92
|
+
try {
|
|
93
|
+
switch (match.type) {
|
|
94
|
+
case _prismicio_client.RichTextNodeType.heading1:
|
|
95
|
+
case _prismicio_client.RichTextNodeType.heading2:
|
|
96
|
+
case _prismicio_client.RichTextNodeType.heading3:
|
|
97
|
+
case _prismicio_client.RichTextNodeType.heading4:
|
|
98
|
+
case _prismicio_client.RichTextNodeType.heading5:
|
|
99
|
+
case _prismicio_client.RichTextNodeType.heading6:
|
|
100
|
+
case _prismicio_client.RichTextNodeType.paragraph:
|
|
101
|
+
case _prismicio_client.RichTextNodeType.preformatted:
|
|
102
|
+
case _prismicio_client.RichTextNodeType.listItem:
|
|
103
|
+
case _prismicio_client.RichTextNodeType.oListItem:
|
|
104
|
+
lastRTTextNodeType = match.type;
|
|
105
|
+
builder.appendTextNode(match.type, config?.direction);
|
|
106
|
+
break;
|
|
107
|
+
case _prismicio_client.RichTextNodeType.list:
|
|
108
|
+
case _prismicio_client.RichTextNodeType.oList:
|
|
109
|
+
lastListType = match.type;
|
|
110
|
+
break;
|
|
111
|
+
case _prismicio_client.RichTextNodeType.image:
|
|
112
|
+
builder.appendNode(require_hastSerializerHelpers.serializeImage(node));
|
|
113
|
+
break;
|
|
114
|
+
case _prismicio_client.RichTextNodeType.embed:
|
|
115
|
+
builder.appendNode(require_hastSerializerHelpers.serializeEmbed(node));
|
|
116
|
+
break;
|
|
117
|
+
case _prismicio_client.RichTextNodeType.strong:
|
|
118
|
+
case _prismicio_client.RichTextNodeType.em:
|
|
119
|
+
case _prismicio_client.RichTextNodeType.label:
|
|
120
|
+
case _prismicio_client.RichTextNodeType.hyperlink: {
|
|
121
|
+
const span = require_hastSerializerHelpers.serializeSpan(node, match);
|
|
122
|
+
const length = (0, hast_util_to_string.toString)(node).trimEnd().length;
|
|
123
|
+
try {
|
|
124
|
+
builder.appendSpan(span, length);
|
|
125
|
+
} catch {
|
|
126
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction);
|
|
127
|
+
builder.appendSpan(span, length);
|
|
128
|
+
}
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
default: throw new Error(`Unknown rich text node type: \`${match.type}\``);
|
|
132
|
+
}
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (error instanceof require_PrismicRichTextSerializerError.PrismicRichTextSerializerError) {
|
|
135
|
+
file.message(error.message, {
|
|
136
|
+
cause: error,
|
|
137
|
+
place: node.position,
|
|
138
|
+
ruleId: VFILE_RULE,
|
|
139
|
+
source: VFILE_SOURCE
|
|
140
|
+
});
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
} else if (node.type === "text") {
|
|
146
|
+
if (!(node.value.replace(/[ \t\n\f\r]/g, "") === "")) try {
|
|
147
|
+
builder.appendText(node.value);
|
|
148
|
+
} catch {
|
|
149
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction);
|
|
150
|
+
builder.appendText(node.value);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
return builder.build();
|
|
162
155
|
};
|
|
156
|
+
//#endregion
|
|
163
157
|
exports.hastToRichText = hastToRichText;
|
|
164
|
-
|
|
158
|
+
|
|
159
|
+
//# sourceMappingURL=hastToRichText.cjs.map
|