@prismicio/migrate 0.0.1 → 0.0.2-canary.2da5f89

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.
Files changed (77) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/errors/PrismicRichTextError.cjs +5 -5
  3. package/dist/errors/PrismicRichTextError.cjs.map +1 -1
  4. package/dist/errors/PrismicRichTextError.d.cts +4 -0
  5. package/dist/errors/PrismicRichTextError.d.cts.map +1 -0
  6. package/dist/errors/PrismicRichTextError.d.ts +4 -2
  7. package/dist/errors/PrismicRichTextError.d.ts.map +1 -0
  8. package/dist/errors/PrismicRichTextError.js +6 -6
  9. package/dist/errors/PrismicRichTextError.js.map +1 -1
  10. package/dist/errors/PrismicRichTextSerializerError.cjs +6 -6
  11. package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
  12. package/dist/errors/PrismicRichTextSerializerError.d.cts +5 -0
  13. package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -0
  14. package/dist/errors/PrismicRichTextSerializerError.d.ts +5 -3
  15. package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -0
  16. package/dist/errors/PrismicRichTextSerializerError.js +6 -6
  17. package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
  18. package/dist/htmlAsRichText.cjs +24 -8
  19. package/dist/htmlAsRichText.cjs.map +1 -1
  20. package/dist/htmlAsRichText.d.cts +22 -0
  21. package/dist/htmlAsRichText.d.cts.map +1 -0
  22. package/dist/htmlAsRichText.d.ts +11 -67
  23. package/dist/htmlAsRichText.d.ts.map +1 -0
  24. package/dist/htmlAsRichText.js +21 -7
  25. package/dist/htmlAsRichText.js.map +1 -1
  26. package/dist/index.cjs +6 -8
  27. package/dist/index.d.cts +5 -0
  28. package/dist/index.d.ts +5 -4
  29. package/dist/index.js +2 -7
  30. package/dist/lib/RichTextFieldBuilder.cjs +82 -113
  31. package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
  32. package/dist/lib/RichTextFieldBuilder.d.cts +14 -0
  33. package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -0
  34. package/dist/lib/RichTextFieldBuilder.d.ts +5 -26
  35. package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -0
  36. package/dist/lib/RichTextFieldBuilder.js +82 -113
  37. package/dist/lib/RichTextFieldBuilder.js.map +1 -1
  38. package/dist/lib/filterRichTextField.cjs +81 -78
  39. package/dist/lib/filterRichTextField.cjs.map +1 -1
  40. package/dist/lib/filterRichTextField.js +82 -79
  41. package/dist/lib/filterRichTextField.js.map +1 -1
  42. package/dist/lib/hastSerializerHelpers.cjs +101 -86
  43. package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
  44. package/dist/lib/hastSerializerHelpers.js +101 -88
  45. package/dist/lib/hastSerializerHelpers.js.map +1 -1
  46. package/dist/lib/hastToRichText.cjs +151 -157
  47. package/dist/lib/hastToRichText.cjs.map +1 -1
  48. package/dist/lib/hastToRichText.d.cts +21 -0
  49. package/dist/lib/hastToRichText.d.cts.map +1 -0
  50. package/dist/lib/hastToRichText.d.ts +19 -28
  51. package/dist/lib/hastToRichText.d.ts.map +1 -0
  52. package/dist/lib/hastToRichText.js +150 -156
  53. package/dist/lib/hastToRichText.js.map +1 -1
  54. package/dist/lib/rehypeRichText.cjs +57 -57
  55. package/dist/lib/rehypeRichText.cjs.map +1 -1
  56. package/dist/lib/rehypeRichText.d.cts +58 -0
  57. package/dist/lib/rehypeRichText.d.cts.map +1 -0
  58. package/dist/lib/rehypeRichText.d.ts +52 -59
  59. package/dist/lib/rehypeRichText.d.ts.map +1 -0
  60. package/dist/lib/rehypeRichText.js +53 -55
  61. package/dist/lib/rehypeRichText.js.map +1 -1
  62. package/dist/types.d.cts +47 -0
  63. package/dist/types.d.cts.map +1 -0
  64. package/dist/types.d.ts +29 -39
  65. package/dist/types.d.ts.map +1 -0
  66. package/package.json +40 -63
  67. package/src/htmlAsRichText.ts +22 -49
  68. package/src/lib/RichTextFieldBuilder.ts +26 -31
  69. package/src/lib/filterRichTextField.ts +6 -25
  70. package/src/lib/hastSerializerHelpers.ts +4 -17
  71. package/src/lib/hastToRichText.ts +17 -39
  72. package/src/lib/rehypeRichText.ts +88 -102
  73. package/src/types.ts +24 -33
  74. package/dist/index.cjs.map +0 -1
  75. package/dist/index.js.map +0 -1
  76. package/dist/lib/filterRichTextField.d.ts +0 -10
  77. package/dist/lib/hastSerializerHelpers.d.ts +0 -28
@@ -1 +1 @@
1
- {"version":3,"file":"hastSerializerHelpers.cjs","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\"\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\ttarget: node.properties?.target as string | undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn rtPartialInlineNode\n}\n"],"names":["PrismicRichTextSerializerError","toHtml","OEmbedType","LinkType"],"mappings":";;;;;AAgBa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAIA,+BAAAA,+BACT,qDAAqD;AAAA,EAEvD;AAEA,QAAM,MAAM,IAAI,IAAI,KAAK,kBAAkB;AAEvC,MAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,MAAA,UAAS,UAAK,eAAL,mBAAiB;AAC9B,MAAI,IAAI;AACR,MAAI,IAAI;AACR,MAAI,OAAO;AAGP,MAAA,IAAI,aAAa,qBAAqB;AACzC,QAAI,IAAI,aAAa,IAAI,GAAG,GAAG;AAC9B,cAAQ,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;AAAA,IACzC;AAEA,QAAI,IAAI,aAAa,IAAI,GAAG,GAAG;AAC9B,eAAS,OAAO,IA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AAW,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,IAAIF,+BAAAA,+BACT,4DAA4D;AAAA,IAE9D;AAEO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,QACL,WAAWG,OAAS,SAAA;AAAA,QACpB;AAAA,QACA,SAAQ,UAAK,eAAL,mBAAiB;AAAA,MACzB;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;;;;"}
1
+ {"version":3,"file":"hastSerializerHelpers.cjs","names":["PrismicRichTextSerializerError","toHtml","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;CAE7B,IAAI,CAAC,KACJ,MAAM,IAAIA,uCAAAA,+BAA+B,qDAAqD;CAG/F,MAAM,MAAM,IAAI,IAAI,KAAK,kBAAkB;CAE3C,IAAI,QAAQ,KAAK,YAAY;CAC7B,IAAI,SAAS,KAAK,YAAY;CAC9B,IAAI,IAAI;CACR,IAAI,IAAI;CACR,IAAI,OAAO;CAGX,IAAI,IAAI,aAAa,qBAAqB;EACzC,IAAI,IAAI,aAAa,IAAI,GAAG,GAC3B,QAAQ,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;EAGzC,IAAI,IAAI,aAAa,IAAI,GAAG,GAC3B,SAAS,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;EAG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AAA,SAAS;IACpB;IACA,QAAQ,KAAK,YAAY;GAC1B;EACD;CACD;CAEA,OAAO;AACR"}
@@ -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
- var _a, _b, _c, _d, _e;
6
- const src = (_a = node.properties) == null ? void 0 : _a.src;
7
- if (!src) {
8
- throw new PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
9
- }
10
- const url = new URL(src, "https://noop.com");
11
- let width = (_b = node.properties) == null ? void 0 : _b.width;
12
- let height = (_c = node.properties) == null ? void 0 : _c.height;
13
- let x = 0;
14
- let y = 0;
15
- let zoom = 1;
16
- if (url.hostname === "images.prismic.io") {
17
- if (url.searchParams.has("w")) {
18
- width = Number(url.searchParams.get("w"));
19
- }
20
- if (url.searchParams.has("h")) {
21
- height = Number(url.searchParams.get("h"));
22
- }
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) {
28
- zoom = Math.max(1, width / Number(rectW));
29
- }
30
- }
31
- }
32
- return {
33
- type: "image",
34
- id: "",
35
- url: src,
36
- alt: ((_d = node.properties) == null ? void 0 : _d.alt) ?? "",
37
- copyright: ((_e = node.properties) == null ? void 0 : _e.copyright) ?? null,
38
- // This is not accurate. We're doing the casting because it seems the migration API
39
- // accepts `undefined` values for those fields which is more convenient to us here.
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
- var _a, _b, _c, _d;
47
- const src = (_a = node.properties) == null ? void 0 : _a.src;
48
- if (!src) {
49
- throw new PrismicRichTextSerializerError("Element of type `embed` is missing an `src` attribute");
50
- }
51
- const oembedBase = {
52
- version: "1.0",
53
- embed_url: src,
54
- html: toHtml(node),
55
- title: (_b = node.properties) == null ? void 0 : _b.title
56
- };
57
- const width = (_c = node.properties) == null ? void 0 : _c.width;
58
- const height = (_d = node.properties) == null ? void 0 : _d.height;
59
- node.children = [];
60
- if (width && height) {
61
- return {
62
- type: "embed",
63
- oembed: { ...oembedBase, type: OEmbedType.Rich, width, height }
64
- };
65
- } else {
66
- return {
67
- type: "embed",
68
- oembed: { ...oembedBase, type: OEmbedType.Link }
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
- var _a, _b;
74
- if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
75
- const url = (_a = node.properties) == null ? void 0 : _a.href;
76
- if (!url) {
77
- throw new PrismicRichTextSerializerError("Element of type `hyperlink` is missing an `href` attribute");
78
- }
79
- return {
80
- type: "hyperlink",
81
- data: {
82
- link_type: LinkType.Web,
83
- url,
84
- target: (_b = node.properties) == null ? void 0 : _b.target
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
- //# sourceMappingURL=hastSerializerHelpers.js.map
105
+ //#endregion
106
+ export { serializeEmbed, serializeImage, serializeSpan };
107
+
108
+ //# sourceMappingURL=hastSerializerHelpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"hastSerializerHelpers.js","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\"\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\ttarget: node.properties?.target as string | undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn rtPartialInlineNode\n}\n"],"names":[],"mappings":";;;AAgBa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAI,+BACT,qDAAqD;AAAA,EAEvD;AAEA,QAAM,MAAM,IAAI,IAAI,KAAK,kBAAkB;AAEvC,MAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,MAAA,UAAS,UAAK,eAAL,mBAAiB;AAC9B,MAAI,IAAI;AACR,MAAI,IAAI;AACR,MAAI,OAAO;AAGP,MAAA,IAAI,aAAa,qBAAqB;AACzC,QAAI,IAAI,aAAa,IAAI,GAAG,GAAG;AAC9B,cAAQ,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;AAAA,IACzC;AAEA,QAAI,IAAI,aAAa,IAAI,GAAG,GAAG;AAC9B,eAAS,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;AAAA,IAC1C;AAEA,QAAI,IAAI,aAAa,IAAI,MAAM,GAAG;AACjC,YAAM,CAAC,OAAO,OAAO,OAAO,MAAM,IAAI,IAAI,aACxC,IAAI,M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AI,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
+ {"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;CAE7B,IAAI,CAAC,KACJ,MAAM,IAAI,+BAA+B,qDAAqD;CAG/F,MAAM,MAAM,IAAI,IAAI,KAAK,kBAAkB;CAE3C,IAAI,QAAQ,KAAK,YAAY;CAC7B,IAAI,SAAS,KAAK,YAAY;CAC9B,IAAI,IAAI;CACR,IAAI,IAAI;CACR,IAAI,OAAO;CAGX,IAAI,IAAI,aAAa,qBAAqB;EACzC,IAAI,IAAI,aAAa,IAAI,GAAG,GAC3B,QAAQ,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;EAGzC,IAAI,IAAI,aAAa,IAAI,GAAG,GAC3B,SAAS,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;EAG1C,IAAI,IAAI,aAAa,IAAI,MAAM,GAAG;GACjC,MAAM,CAAC,OAAO,OAAO,OAAO,UAAU,IAAI,aAAa,IAAI,MAAM,CAAC,CAAE,MAAM,GAAG;GAE7E,IAAI,OAAO,KAAK;GAChB,IAAI,OAAO,KAAK;GAGhB,IAAI,OACH,OAAO,KAAK,IAAI,GAAG,QAAQ,OAAO,KAAK,CAAC;EAE1C;CACD;CAEA,OAAO;EACN,MAAM;EACN,IAAI;EACJ,KAAK;EACL,KAAM,KAAK,YAAY,OAAkB;EACzC,WAAY,KAAK,YAAY,aAAwB;EAIrD,YAAY;GAAS;GAAyB;EAAiB;EAC/D,MAAM;GAAE;GAAG;GAAG;GAAM,YAAY;EAAc;CAC/C;AACD;;;;;;;AAQA,MAAa,kBAAkB,SAA+B;CAC7D,MAAM,MAAM,KAAK,YAAY;CAE7B,IAAI,CAAC,KACJ,MAAM,IAAI,+BACT,uDACD;CAGD,MAAM,aAAa;EAClB,SAAS;EACT,WAAW;EACX,MAAM,OAAO,IAAI;EACjB,OAAO,KAAK,YAAY;CACzB;CAEA,MAAM,QAAQ,KAAK,YAAY;CAC/B,MAAM,SAAS,KAAK,YAAY;CAGhC,KAAK,WAAW,CAAC;CAEjB,IAAI,SAAS,QACZ,OAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAM,WAAW;GAAM;GAAO;EAAO;CAC/D;MAEA,OAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAM,WAAW;EAAK;CAChD;AAEF;;;;;;;;AASA,MAAa,iBACZ,MACA,wBACyB;CACzB,IAAI,oBAAoB,SAAS,eAAe,EAAE,UAAU,sBAAsB;EACjF,MAAM,MAAM,KAAK,YAAY;EAC7B,IAAI,CAAC,KACJ,MAAM,IAAI,+BACT,4DACD;EAGD,OAAO;GACN,MAAM;GACN,MAAM;IACL,WAAW,SAAS;IACpB;IACA,QAAQ,KAAK,YAAY;GAC1B;EACD;CACD;CAEA,OAAO;AACR"}
@@ -1,164 +1,158 @@
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");
1
+ const require_PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
2
+ const require_hastSerializerHelpers = require("./hastSerializerHelpers.cjs");
3
+ const require_RichTextFieldBuilder = require("./RichTextFieldBuilder.cjs");
4
+ let unist_util_visit = require("unist-util-visit");
5
+ let _prismicio_client = require("@prismicio/client");
6
+ let hast_util_to_string = require("hast-util-to-string");
7
+ //#region src/lib/hastToRichText.ts
9
8
  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"
9
+ h1: "heading1",
10
+ h2: "heading2",
11
+ h3: "heading3",
12
+ h4: "heading4",
13
+ h5: "heading5",
14
+ h6: "heading6",
15
+ p: "paragraph",
16
+ pre: "preformatted",
17
+ strong: "strong",
18
+ em: "em",
19
+ li: ({ context }) => context.listType === "group-o-list-item" ? "o-list-item" : "list-item",
20
+ ul: "group-list-item",
21
+ ol: "group-o-list-item",
22
+ img: "image",
23
+ iframe: "embed",
24
+ a: "hyperlink"
26
25
  };
27
26
  const VFILE_RULE = "failed-to-serialize-node";
28
27
  const VFILE_SOURCE = "prismic";
28
+ /**
29
+ * Transfor a hast tree to a rich text field.
30
+ *
31
+ * @param tree - The hast tree to transform.
32
+ * @param file - The vfile to attach warnings to.
33
+ * @param config - Configuration that determines the output of the function.
34
+ * @returns The rich text field equivalent of the provided hast tree.
35
+ */
29
36
  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();
37
+ const builder = new require_RichTextFieldBuilder.RichTextFieldBuilder();
38
+ const serializer = {
39
+ ...DEFAULT_SERIALIZER,
40
+ ...config?.serializer
41
+ };
42
+ let lastRTTextNodeType = _prismicio_client.RichTextNodeType.paragraph;
43
+ let lastListType = void 0;
44
+ (0, unist_util_visit.visit)(tree, (node) => {
45
+ if (node.type === "element") {
46
+ if (node.tagName === "br") try {
47
+ builder.appendText("\n");
48
+ } catch {}
49
+ let serializerOrShorthand;
50
+ if (node.matchesSerializer && node.matchesSerializer in serializer) serializerOrShorthand = serializer[node.matchesSerializer];
51
+ else if (node.tagName in serializer) serializerOrShorthand = serializer[node.tagName];
52
+ else return;
53
+ let shorthand;
54
+ if (typeof serializerOrShorthand === "function") {
55
+ const shorthandOrNode = serializerOrShorthand({
56
+ node,
57
+ context: { listType: lastListType }
58
+ });
59
+ if (!shorthandOrNode) return;
60
+ else if (typeof shorthandOrNode === "object" && "type" in shorthandOrNode) switch (shorthandOrNode.type) {
61
+ case _prismicio_client.RichTextNodeType.strong:
62
+ case _prismicio_client.RichTextNodeType.em:
63
+ case _prismicio_client.RichTextNodeType.label:
64
+ case _prismicio_client.RichTextNodeType.hyperlink: {
65
+ const length = (0, hast_util_to_string.toString)(node).trimEnd().length;
66
+ try {
67
+ builder.appendSpan(shorthandOrNode, length);
68
+ } catch {
69
+ builder.appendTextNode(lastRTTextNodeType, config?.direction);
70
+ builder.appendSpan(shorthandOrNode, length);
71
+ }
72
+ return;
73
+ }
74
+ case _prismicio_client.RichTextNodeType.image:
75
+ case _prismicio_client.RichTextNodeType.embed:
76
+ builder.appendNode(shorthandOrNode);
77
+ return;
78
+ default:
79
+ lastRTTextNodeType = shorthandOrNode.type;
80
+ builder.appendNode(shorthandOrNode);
81
+ return;
82
+ }
83
+ shorthand = shorthandOrNode;
84
+ } else shorthand = serializerOrShorthand;
85
+ let match;
86
+ if (typeof shorthand === "string") match = { type: shorthand };
87
+ else match = {
88
+ type: _prismicio_client.RichTextNodeType.label,
89
+ data: shorthand
90
+ };
91
+ try {
92
+ switch (match.type) {
93
+ case _prismicio_client.RichTextNodeType.heading1:
94
+ case _prismicio_client.RichTextNodeType.heading2:
95
+ case _prismicio_client.RichTextNodeType.heading3:
96
+ case _prismicio_client.RichTextNodeType.heading4:
97
+ case _prismicio_client.RichTextNodeType.heading5:
98
+ case _prismicio_client.RichTextNodeType.heading6:
99
+ case _prismicio_client.RichTextNodeType.paragraph:
100
+ case _prismicio_client.RichTextNodeType.preformatted:
101
+ case _prismicio_client.RichTextNodeType.listItem:
102
+ case _prismicio_client.RichTextNodeType.oListItem:
103
+ lastRTTextNodeType = match.type;
104
+ builder.appendTextNode(match.type, config?.direction);
105
+ break;
106
+ case _prismicio_client.RichTextNodeType.list:
107
+ case _prismicio_client.RichTextNodeType.oList:
108
+ lastListType = match.type;
109
+ break;
110
+ case _prismicio_client.RichTextNodeType.image:
111
+ builder.appendNode(require_hastSerializerHelpers.serializeImage(node));
112
+ break;
113
+ case _prismicio_client.RichTextNodeType.embed:
114
+ builder.appendNode(require_hastSerializerHelpers.serializeEmbed(node));
115
+ break;
116
+ case _prismicio_client.RichTextNodeType.strong:
117
+ case _prismicio_client.RichTextNodeType.em:
118
+ case _prismicio_client.RichTextNodeType.label:
119
+ case _prismicio_client.RichTextNodeType.hyperlink: {
120
+ const span = require_hastSerializerHelpers.serializeSpan(node, match);
121
+ const length = (0, hast_util_to_string.toString)(node).trimEnd().length;
122
+ try {
123
+ builder.appendSpan(span, length);
124
+ } catch {
125
+ builder.appendTextNode(lastRTTextNodeType, config?.direction);
126
+ builder.appendSpan(span, length);
127
+ }
128
+ break;
129
+ }
130
+ default: throw new Error(`Unknown rich text node type: \`${match.type}\``);
131
+ }
132
+ } catch (error) {
133
+ if (error instanceof require_PrismicRichTextSerializerError.PrismicRichTextSerializerError) {
134
+ file.message(error.message, {
135
+ cause: error,
136
+ place: node.position,
137
+ ruleId: VFILE_RULE,
138
+ source: VFILE_SOURCE
139
+ });
140
+ return;
141
+ }
142
+ throw error;
143
+ }
144
+ } else if (node.type === "text") {
145
+ if (!(node.value.replace(/[ \t\n\f\r]/g, "") === "")) try {
146
+ builder.appendText(node.value);
147
+ } catch {
148
+ builder.appendTextNode(lastRTTextNodeType, config?.direction);
149
+ builder.appendText(node.value);
150
+ }
151
+ }
152
+ });
153
+ return builder.build();
162
154
  };
155
+ //#endregion
163
156
  exports.hastToRichText = hastToRichText;
164
- //# sourceMappingURL=hastToRichText.cjs.map
157
+
158
+ //# sourceMappingURL=hastToRichText.cjs.map