@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,87 +1,90 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const client = require("@prismicio/client");
1
+ let _prismicio_client = require("@prismicio/client");
2
+ //#region src/lib/filterRichTextField.ts
3
+ /**
4
+ * Get allowed nodes types from a rich text or title field custom type model.
5
+ *
6
+ * @param model - A rich text or title field custom type model.
7
+ * @returns An array of allowed nodes types.
8
+ */
4
9
  const getAllowedNodeTypes = (model) => {
5
- var _a;
6
- const allowedNodeTypes = [];
7
- if (model.config) {
8
- if ("multi" in model.config && model.config.multi) {
9
- allowedNodeTypes.push(...model.config.multi.split(","));
10
- } else if ("single" in model.config && model.config.single) {
11
- allowedNodeTypes.push(...model.config.single.split(","));
12
- }
13
- if ((_a = model.config.labels) == null ? void 0 : _a.length) {
14
- allowedNodeTypes.push("label");
15
- }
16
- }
17
- return allowedNodeTypes;
10
+ const allowedNodeTypes = [];
11
+ if (model.config) {
12
+ if ("multi" in model.config && model.config.multi) allowedNodeTypes.push(...model.config.multi.split(","));
13
+ else if ("single" in model.config && model.config.single) allowedNodeTypes.push(...model.config.single.split(","));
14
+ if (model.config.labels?.length) allowedNodeTypes.push("label");
15
+ }
16
+ return allowedNodeTypes;
18
17
  };
18
+ /**
19
+ * Filter an image node based on given model.
20
+ *
21
+ * @param node - An image node to filter.
22
+ * @param model - A rich text or title field custom type model.
23
+ * @returns A filtered image node based on the given model.
24
+ */
19
25
  const filterImageNode = (node, model) => {
20
- var _a, _b;
21
- if (!((_a = model.config) == null ? void 0 : _a.allowTargetBlank) && ((_b = node.linkTo) == null ? void 0 : _b.link_type) === client.LinkType.Web) {
22
- return {
23
- ...node,
24
- linkTo: {
25
- ...node.linkTo,
26
- target: void 0
27
- }
28
- };
29
- }
30
- return node;
26
+ if (!model.config?.allowTargetBlank && node.linkTo?.link_type === _prismicio_client.LinkType.Web) return {
27
+ ...node,
28
+ linkTo: {
29
+ ...node.linkTo,
30
+ target: void 0
31
+ }
32
+ };
33
+ return node;
31
34
  };
35
+ /**
36
+ * Filter a text node based on given model.
37
+ *
38
+ * @param node - A text node to filter.
39
+ * @param model - A rich text or title field custom type model.
40
+ * @param allowedNodeTypes - An array of allowed nodes types.
41
+ * @returns A filtered text node based on the given model.
42
+ */
32
43
  const filterTextNode = (node, model, allowedNodeTypes) => {
33
- var _a, _b, _c;
34
- const filteredNode = {
35
- ...node,
36
- spans: []
37
- };
38
- for (let i = 0; i < node.spans.length; i++) {
39
- const span = node.spans[i];
40
- if (allowedNodeTypes.includes(span.type)) {
41
- if (span.type === client.RichTextNodeType.hyperlink) {
42
- if (!((_a = model.config) == null ? void 0 : _a.allowTargetBlank) && span.data.link_type === client.LinkType.Web) {
43
- filteredNode.spans.push({
44
- ...span,
45
- data: {
46
- ...span.data,
47
- target: void 0
48
- }
49
- });
50
- } else {
51
- filteredNode.spans.push(span);
52
- }
53
- } else if (span.type === client.RichTextNodeType.label) {
54
- if ((_c = (_b = model.config) == null ? void 0 : _b.labels) == null ? void 0 : _c.includes(span.data.label)) {
55
- filteredNode.spans.push(span);
56
- }
57
- } else {
58
- filteredNode.spans.push(span);
59
- }
60
- }
61
- }
62
- return filteredNode;
44
+ const filteredNode = {
45
+ ...node,
46
+ spans: []
47
+ };
48
+ for (let i = 0; i < node.spans.length; i++) {
49
+ const span = node.spans[i];
50
+ if (allowedNodeTypes.includes(span.type)) {
51
+ if (span.type === _prismicio_client.RichTextNodeType.hyperlink) {
52
+ if (!model.config?.allowTargetBlank && span.data.link_type === _prismicio_client.LinkType.Web) filteredNode.spans.push({
53
+ ...span,
54
+ data: {
55
+ ...span.data,
56
+ target: void 0
57
+ }
58
+ });
59
+ else filteredNode.spans.push(span);
60
+ } else if (span.type === _prismicio_client.RichTextNodeType.label) {
61
+ if (model.config?.labels?.includes(span.data.label)) filteredNode.spans.push(span);
62
+ } else filteredNode.spans.push(span);
63
+ }
64
+ }
65
+ return filteredNode;
63
66
  };
67
+ /**
68
+ * Filter a rich text field based on given model.
69
+ *
70
+ * @param richTextField - A rich text or title field from Prismic.
71
+ * @param model - A rich text or title field custom type model.
72
+ * @returns A rich text or title field filtered based on the given model.
73
+ */
64
74
  const filterRichTextField = (richTextField, model) => {
65
- const nodes = [];
66
- const allowedNodeTypes = getAllowedNodeTypes(model);
67
- for (
68
- let i = 0;
69
- // Only process the first node if it's a "single" node rich text type
70
- i < (model.config && "multi" in model.config ? richTextField.length : 1);
71
- i++
72
- ) {
73
- const node = richTextField[i];
74
- if (allowedNodeTypes.includes(node.type)) {
75
- if (node.type === client.RichTextNodeType.image) {
76
- nodes.push(filterImageNode(node, model));
77
- } else if (node.type === client.RichTextNodeType.embed) {
78
- nodes.push(node);
79
- } else {
80
- nodes.push(filterTextNode(node, model, allowedNodeTypes));
81
- }
82
- }
83
- }
84
- return nodes;
75
+ const nodes = [];
76
+ const allowedNodeTypes = getAllowedNodeTypes(model);
77
+ for (let i = 0; i < (model.config && "multi" in model.config ? richTextField.length : 1); i++) {
78
+ const node = richTextField[i];
79
+ if (allowedNodeTypes.includes(node.type)) {
80
+ if (node.type === _prismicio_client.RichTextNodeType.image) nodes.push(filterImageNode(node, model));
81
+ else if (node.type === _prismicio_client.RichTextNodeType.embed) nodes.push(node);
82
+ else nodes.push(filterTextNode(node, model, allowedNodeTypes));
83
+ }
84
+ }
85
+ return nodes;
85
86
  };
87
+ //#endregion
86
88
  exports.filterRichTextField = filterRichTextField;
87
- //# sourceMappingURL=filterRichTextField.cjs.map
89
+
90
+ //# sourceMappingURL=filterRichTextField.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"filterRichTextField.cjs","sources":["../../../src/lib/filterRichTextField.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n\tTitleField,\n} from \"@prismicio/client\"\nimport { LinkType, RichTextNodeType } from \"@prismicio/client\"\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n *\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (\n\tmodel: CustomTypeModelRichTextField,\n): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(\n\t\t\t\t...(model.config.multi.split(\",\") as RichTextNodeTypes[]),\n\t\t\t)\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(\n\t\t\t\t...(model.config.single.split(\",\") as RichTextNodeTypes[]),\n\t\t\t)\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\")\n\t\t}\n\t}\n\n\treturn allowedNodeTypes\n}\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n *\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (\n\tnode: RTImageNode,\n\tmodel: CustomTypeModelRichTextField,\n): RTImageNode => {\n\tif (\n\t\t!model.config?.allowTargetBlank &&\n\t\tnode.linkTo?.link_type === LinkType.Web\n\t) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn node\n}\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n *\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t}\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i]\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (\n\t\t\t\t\t!model.config?.allowTargetBlank &&\n\t\t\t\t\tspan.data.link_type === LinkType.Web\n\t\t\t\t) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode\n}\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n *\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = []\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model)\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i]\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model))\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node)\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field\n}\n"],"names":["LinkType","RichTextNodeType"],"mappings":";;;AAkBA,MAAM,sBAAsB,CAC3B,UACwB;;AACxB,QAAM,mBAAwC,CAAA;AAE9C,MAAI,MAAM,QAAQ;AACjB,QAAI,WAAW,MAAM,UAAU,MAAM,OAAO,OAAO;AAClD,uBAAiB,KAChB,GAAI,MAAM,OAAO,MAAM,MAAM,GAAG,CAAyB;AAAA,IAAA,WAEhD,YAAY,MAAM,UAAU,MAAM,OAAO,QAAQ;AAC3D,uBAAiB,KAChB,GAAI,MAAM,OAAO,OAAO,MAAM,GAAG,CAAyB;AAAA,IAE5D;AAEI,SAAA,WAAM,OAAO,WAAb,mBAAqB,QAAQ;AAChC,uBAAiB,KAAK,OAAO;AAAA,IAC9B;AAAA,EACD;AAEO,SAAA;AACR;AAUA,MAAM,kBAAkB,CACvB,MACA,UACgB;;AAEf,MAAA,GAAC,WAAM,WAAN,mBAAc,uBACf,UAAK,WAAL,mBAAa,eAAcA,gBAAS,KACnC;AACM,WAAA;AAAA,MACN,GAAG;AAAA,MACH,QAAQ;AAAA,QACP,GAAG,KAAK;AAAA,QACR,QAAQ;AAAA,MACR;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;AAWA,MAAM,iBAAiB,CACtB,MACA,OACA,qBACe;;AACf,QAAM,eAA2B;AAAA,IAChC,GAAG;AAAA,IACH,OAAO,CAAE;AAAA,EAAA;AAGV,WAAS,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;AACrC,UAAA,OAAO,K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cjs","names":["LinkType","RichTextNodeType"],"sources":["../../src/lib/filterRichTextField.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n\tTitleField,\n} from \"@prismicio/client\"\nimport { LinkType, RichTextNodeType } from \"@prismicio/client\"\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (model: CustomTypeModelRichTextField): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(...(model.config.multi.split(\",\") as RichTextNodeTypes[]))\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(...(model.config.single.split(\",\") as RichTextNodeTypes[]))\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\")\n\t\t}\n\t}\n\n\treturn allowedNodeTypes\n}\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (node: RTImageNode, model: CustomTypeModelRichTextField): RTImageNode => {\n\tif (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn node\n}\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t}\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i]\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode\n}\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = []\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model)\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i]\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model))\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\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,KAAK,SAASC,kBAAAA,iBAAiB,OACrC;QAAA,MAAM,QAAQ,QAAQ,SAAS,KAAK,KAAK,KAAK,GACjD,aAAa,MAAM,KAAK,IAAI;GAAA,OAG7B,aAAa,MAAM,KAAK,IAAI;EAE9B;CACD;CAEA,OAAO;AACR;;;;;;;;AASA,MAAa,uBACZ,eACA,UACW;CACX,MAAM,QAAkB,CAAC;CAEzB,MAAM,mBAAmB,oBAAoB,KAAK;CAElD,KACC,IAAI,IAAI,GAER,KAAK,MAAM,UAAU,WAAW,MAAM,SAAS,cAAc,SAAS,IACtE,KACC;EACD,MAAM,OAAO,cAAc;EAE3B,IAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;GACzC,IAAI,KAAK,SAASA,kBAAAA,iBAAiB,OAClC,MAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;QACjC,IAAI,KAAK,SAASA,kBAAAA,iBAAiB,OACzC,MAAM,KAAK,IAAI;QAEf,MAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;EAE1D;CACD;CAEA,OAAO;AACR"}
@@ -1,87 +1,90 @@
1
- import { RichTextNodeType, LinkType } from "@prismicio/client";
1
+ import { LinkType, RichTextNodeType } from "@prismicio/client";
2
+ //#region src/lib/filterRichTextField.ts
3
+ /**
4
+ * Get allowed nodes types from a rich text or title field custom type model.
5
+ *
6
+ * @param model - A rich text or title field custom type model.
7
+ * @returns An array of allowed nodes types.
8
+ */
2
9
  const getAllowedNodeTypes = (model) => {
3
- var _a;
4
- const allowedNodeTypes = [];
5
- if (model.config) {
6
- if ("multi" in model.config && model.config.multi) {
7
- allowedNodeTypes.push(...model.config.multi.split(","));
8
- } else if ("single" in model.config && model.config.single) {
9
- allowedNodeTypes.push(...model.config.single.split(","));
10
- }
11
- if ((_a = model.config.labels) == null ? void 0 : _a.length) {
12
- allowedNodeTypes.push("label");
13
- }
14
- }
15
- return allowedNodeTypes;
10
+ const allowedNodeTypes = [];
11
+ if (model.config) {
12
+ if ("multi" in model.config && model.config.multi) allowedNodeTypes.push(...model.config.multi.split(","));
13
+ else if ("single" in model.config && model.config.single) allowedNodeTypes.push(...model.config.single.split(","));
14
+ if (model.config.labels?.length) allowedNodeTypes.push("label");
15
+ }
16
+ return allowedNodeTypes;
16
17
  };
18
+ /**
19
+ * Filter an image node based on given model.
20
+ *
21
+ * @param node - An image node to filter.
22
+ * @param model - A rich text or title field custom type model.
23
+ * @returns A filtered image node based on the given model.
24
+ */
17
25
  const filterImageNode = (node, model) => {
18
- var _a, _b;
19
- if (!((_a = model.config) == null ? void 0 : _a.allowTargetBlank) && ((_b = node.linkTo) == null ? void 0 : _b.link_type) === LinkType.Web) {
20
- return {
21
- ...node,
22
- linkTo: {
23
- ...node.linkTo,
24
- target: void 0
25
- }
26
- };
27
- }
28
- return node;
26
+ if (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) return {
27
+ ...node,
28
+ linkTo: {
29
+ ...node.linkTo,
30
+ target: void 0
31
+ }
32
+ };
33
+ return node;
29
34
  };
35
+ /**
36
+ * Filter a text node based on given model.
37
+ *
38
+ * @param node - A text node to filter.
39
+ * @param model - A rich text or title field custom type model.
40
+ * @param allowedNodeTypes - An array of allowed nodes types.
41
+ * @returns A filtered text node based on the given model.
42
+ */
30
43
  const filterTextNode = (node, model, allowedNodeTypes) => {
31
- var _a, _b, _c;
32
- const filteredNode = {
33
- ...node,
34
- spans: []
35
- };
36
- for (let i = 0; i < node.spans.length; i++) {
37
- const span = node.spans[i];
38
- if (allowedNodeTypes.includes(span.type)) {
39
- if (span.type === RichTextNodeType.hyperlink) {
40
- if (!((_a = model.config) == null ? void 0 : _a.allowTargetBlank) && span.data.link_type === LinkType.Web) {
41
- filteredNode.spans.push({
42
- ...span,
43
- data: {
44
- ...span.data,
45
- target: void 0
46
- }
47
- });
48
- } else {
49
- filteredNode.spans.push(span);
50
- }
51
- } else if (span.type === RichTextNodeType.label) {
52
- if ((_c = (_b = model.config) == null ? void 0 : _b.labels) == null ? void 0 : _c.includes(span.data.label)) {
53
- filteredNode.spans.push(span);
54
- }
55
- } else {
56
- filteredNode.spans.push(span);
57
- }
58
- }
59
- }
60
- return filteredNode;
44
+ const filteredNode = {
45
+ ...node,
46
+ spans: []
47
+ };
48
+ for (let i = 0; i < node.spans.length; i++) {
49
+ const span = node.spans[i];
50
+ if (allowedNodeTypes.includes(span.type)) {
51
+ if (span.type === RichTextNodeType.hyperlink) {
52
+ if (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) filteredNode.spans.push({
53
+ ...span,
54
+ data: {
55
+ ...span.data,
56
+ target: void 0
57
+ }
58
+ });
59
+ else filteredNode.spans.push(span);
60
+ } else if (span.type === RichTextNodeType.label) {
61
+ if (model.config?.labels?.includes(span.data.label)) filteredNode.spans.push(span);
62
+ } else filteredNode.spans.push(span);
63
+ }
64
+ }
65
+ return filteredNode;
61
66
  };
67
+ /**
68
+ * Filter a rich text field based on given model.
69
+ *
70
+ * @param richTextField - A rich text or title field from Prismic.
71
+ * @param model - A rich text or title field custom type model.
72
+ * @returns A rich text or title field filtered based on the given model.
73
+ */
62
74
  const filterRichTextField = (richTextField, model) => {
63
- const nodes = [];
64
- const allowedNodeTypes = getAllowedNodeTypes(model);
65
- for (
66
- let i = 0;
67
- // Only process the first node if it's a "single" node rich text type
68
- i < (model.config && "multi" in model.config ? richTextField.length : 1);
69
- i++
70
- ) {
71
- const node = richTextField[i];
72
- if (allowedNodeTypes.includes(node.type)) {
73
- if (node.type === RichTextNodeType.image) {
74
- nodes.push(filterImageNode(node, model));
75
- } else if (node.type === RichTextNodeType.embed) {
76
- nodes.push(node);
77
- } else {
78
- nodes.push(filterTextNode(node, model, allowedNodeTypes));
79
- }
80
- }
81
- }
82
- return nodes;
75
+ const nodes = [];
76
+ const allowedNodeTypes = getAllowedNodeTypes(model);
77
+ for (let i = 0; i < (model.config && "multi" in model.config ? richTextField.length : 1); i++) {
78
+ const node = richTextField[i];
79
+ if (allowedNodeTypes.includes(node.type)) {
80
+ if (node.type === RichTextNodeType.image) nodes.push(filterImageNode(node, model));
81
+ else if (node.type === RichTextNodeType.embed) nodes.push(node);
82
+ else nodes.push(filterTextNode(node, model, allowedNodeTypes));
83
+ }
84
+ }
85
+ return nodes;
83
86
  };
84
- export {
85
- filterRichTextField
86
- };
87
- //# sourceMappingURL=filterRichTextField.js.map
87
+ //#endregion
88
+ export { filterRichTextField };
89
+
90
+ //# sourceMappingURL=filterRichTextField.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"filterRichTextField.js","sources":["../../../src/lib/filterRichTextField.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n\tTitleField,\n} from \"@prismicio/client\"\nimport { LinkType, RichTextNodeType } from \"@prismicio/client\"\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n *\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (\n\tmodel: CustomTypeModelRichTextField,\n): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(\n\t\t\t\t...(model.config.multi.split(\",\") as RichTextNodeTypes[]),\n\t\t\t)\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(\n\t\t\t\t...(model.config.single.split(\",\") as RichTextNodeTypes[]),\n\t\t\t)\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\")\n\t\t}\n\t}\n\n\treturn allowedNodeTypes\n}\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n *\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (\n\tnode: RTImageNode,\n\tmodel: CustomTypeModelRichTextField,\n): RTImageNode => {\n\tif (\n\t\t!model.config?.allowTargetBlank &&\n\t\tnode.linkTo?.link_type === LinkType.Web\n\t) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn node\n}\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n *\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t}\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i]\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (\n\t\t\t\t\t!model.config?.allowTargetBlank &&\n\t\t\t\t\tspan.data.link_type === LinkType.Web\n\t\t\t\t) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode\n}\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n *\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = []\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model)\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i]\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model))\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node)\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field\n}\n"],"names":[],"mappings":";AAkBA,MAAM,sBAAsB,CAC3B,UACwB;;AACxB,QAAM,mBAAwC,CAAA;AAE9C,MAAI,MAAM,QAAQ;AACjB,QAAI,WAAW,MAAM,UAAU,MAAM,OAAO,OAAO;AAClD,uBAAiB,KAChB,GAAI,MAAM,OAAO,MAAM,MAAM,GAAG,CAAyB;AAAA,IAAA,WAEhD,YAAY,MAAM,UAAU,MAAM,OAAO,QAAQ;AAC3D,uBAAiB,KAChB,GAAI,MAAM,OAAO,OAAO,MAAM,GAAG,CAAyB;AAAA,IAE5D;AAEI,SAAA,WAAM,OAAO,WAAb,mBAAqB,QAAQ;AAChC,uBAAiB,KAAK,OAAO;AAAA,IAC9B;AAAA,EACD;AAEO,SAAA;AACR;AAUA,MAAM,kBAAkB,CACvB,MACA,UACgB;;AAEf,MAAA,GAAC,WAAM,WAAN,mBAAc,uBACf,UAAK,WAAL,mBAAa,eAAc,SAAS,KACnC;AACM,WAAA;AAAA,MACN,GAAG;AAAA,MACH,QAAQ;AAAA,QACP,GAAG,KAAK;AAAA,Q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AAC;AAAA,MAC7B,WAAA,KAAK,SAAS,iBAAiB,OAAO;AAChD,cAAM,KAAK,IAAI;AAAA,MAAA,OACT;AACN,cAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;AAAA,MACzD;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AACR;"}
1
+ {"version":3,"file":"filterRichTextField.js","names":[],"sources":["../../src/lib/filterRichTextField.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n\tTitleField,\n} from \"@prismicio/client\"\nimport { LinkType, RichTextNodeType } from \"@prismicio/client\"\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (model: CustomTypeModelRichTextField): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(...(model.config.multi.split(\",\") as RichTextNodeTypes[]))\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(...(model.config.single.split(\",\") as RichTextNodeTypes[]))\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\")\n\t\t}\n\t}\n\n\treturn allowedNodeTypes\n}\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (node: RTImageNode, model: CustomTypeModelRichTextField): RTImageNode => {\n\tif (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn node\n}\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t}\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i]\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode\n}\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = []\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model)\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i]\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model))\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node)\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field\n}\n"],"mappings":";;;;;;;;AAiBA,MAAM,uBAAuB,UAA6D;CACzF,MAAM,mBAAwC,CAAC;CAE/C,IAAI,MAAM,QAAQ;EACjB,IAAI,WAAW,MAAM,UAAU,MAAM,OAAO,OAC3C,iBAAiB,KAAK,GAAI,MAAM,OAAO,MAAM,MAAM,GAAG,CAAyB;OACzE,IAAI,YAAY,MAAM,UAAU,MAAM,OAAO,QACnD,iBAAiB,KAAK,GAAI,MAAM,OAAO,OAAO,MAAM,GAAG,CAAyB;EAGjF,IAAI,MAAM,OAAO,QAAQ,QACxB,iBAAiB,KAAK,OAAO;CAE/B;CAEA,OAAO;AACR;;;;;;;;AASA,MAAM,mBAAmB,MAAmB,UAAqD;CAChG,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,QAAQ,cAAc,SAAS,KAC1E,OAAO;EACN,GAAG;EACH,QAAQ;GACP,GAAG,KAAK;GACR,QAAQ,KAAA;EACT;CACD;CAGD,OAAO;AACR;;;;;;;;;AAUA,MAAM,kBACL,MACA,OACA,qBACgB;CAChB,MAAM,eAA2B;EAChC,GAAG;EACH,OAAO,CAAC;CACT;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;EAC3C,MAAM,OAAO,KAAK,MAAM;EAExB,IAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;GACzC,IAAI,KAAK,SAAS,iBAAiB,WAAW;IAC7C,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,KAAK,cAAc,SAAS,KACvE,aAAa,MAAM,KAAK;KACvB,GAAG;KACH,MAAM;MACL,GAAG,KAAK;MACR,QAAQ,KAAA;KACT;IACD,CAAC;SAED,aAAa,MAAM,KAAK,IAAI;GAE9B,OAAO,IAAI,KAAK,SAAS,iBAAiB,OACrC;QAAA,MAAM,QAAQ,QAAQ,SAAS,KAAK,KAAK,KAAK,GACjD,aAAa,MAAM,KAAK,IAAI;GAAA,OAG7B,aAAa,MAAM,KAAK,IAAI;EAE9B;CACD;CAEA,OAAO;AACR;;;;;;;;AASA,MAAa,uBACZ,eACA,UACW;CACX,MAAM,QAAkB,CAAC;CAEzB,MAAM,mBAAmB,oBAAoB,KAAK;CAElD,KACC,IAAI,IAAI,GAER,KAAK,MAAM,UAAU,WAAW,MAAM,SAAS,cAAc,SAAS,IACtE,KACC;EACD,MAAM,OAAO,cAAc;EAE3B,IAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;GACzC,IAAI,KAAK,SAAS,iBAAiB,OAClC,MAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;QACjC,IAAI,KAAK,SAAS,iBAAiB,OACzC,MAAM,KAAK,IAAI;QAEf,MAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;EAE1D;CACD;CAEA,OAAO;AACR"}
@@ -1,95 +1,110 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const client = require("@prismicio/client");
4
- const hastUtilToHtml = require("hast-util-to-html");
5
- const PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
1
+ const require_PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
2
+ let _prismicio_client = require("@prismicio/client");
3
+ let hast_util_to_html = require("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
+ */
6
11
  const serializeImage = (node) => {
7
- var _a, _b, _c, _d, _e;
8
- const src = (_a = node.properties) == null ? void 0 : _a.src;
9
- if (!src) {
10
- throw new PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
11
- }
12
- const url = new URL(src, "https://noop.com");
13
- let width = (_b = node.properties) == null ? void 0 : _b.width;
14
- let height = (_c = node.properties) == null ? void 0 : _c.height;
15
- let x = 0;
16
- let y = 0;
17
- let zoom = 1;
18
- if (url.hostname === "images.prismic.io") {
19
- if (url.searchParams.has("w")) {
20
- width = Number(url.searchParams.get("w"));
21
- }
22
- if (url.searchParams.has("h")) {
23
- height = Number(url.searchParams.get("h"));
24
- }
25
- if (url.searchParams.has("rect")) {
26
- const [rectX, rextY, rectW, _rectH] = url.searchParams.get("rect").split(",");
27
- x = Number(rectX);
28
- y = Number(rextY);
29
- if (width) {
30
- zoom = Math.max(1, width / Number(rectW));
31
- }
32
- }
33
- }
34
- return {
35
- type: "image",
36
- id: "",
37
- url: src,
38
- alt: ((_d = node.properties) == null ? void 0 : _d.alt) ?? "",
39
- copyright: ((_e = node.properties) == null ? void 0 : _e.copyright) ?? null,
40
- // This is not accurate. We're doing the casting because it seems the migration API
41
- // accepts `undefined` values for those fields which is more convenient to us here.
42
- // See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185
43
- dimensions: { width, height },
44
- edit: { x, y, zoom, background: "transparent" }
45
- };
12
+ const src = node.properties?.src;
13
+ if (!src) throw new require_PrismicRichTextSerializerError.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
+ };
46
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
+ */
47
54
  const serializeEmbed = (node) => {
48
- var _a, _b, _c, _d;
49
- const src = (_a = node.properties) == null ? void 0 : _a.src;
50
- if (!src) {
51
- throw new PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `embed` is missing an `src` attribute");
52
- }
53
- const oembedBase = {
54
- version: "1.0",
55
- embed_url: src,
56
- html: hastUtilToHtml.toHtml(node),
57
- title: (_b = node.properties) == null ? void 0 : _b.title
58
- };
59
- const width = (_c = node.properties) == null ? void 0 : _c.width;
60
- const height = (_d = node.properties) == null ? void 0 : _d.height;
61
- node.children = [];
62
- if (width && height) {
63
- return {
64
- type: "embed",
65
- oembed: { ...oembedBase, type: client.OEmbedType.Rich, width, height }
66
- };
67
- } else {
68
- return {
69
- type: "embed",
70
- oembed: { ...oembedBase, type: client.OEmbedType.Link }
71
- };
72
- }
55
+ const src = node.properties?.src;
56
+ if (!src) throw new require_PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `embed` is missing an `src` attribute");
57
+ const oembedBase = {
58
+ version: "1.0",
59
+ embed_url: src,
60
+ html: (0, hast_util_to_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: _prismicio_client.OEmbedType.Rich,
71
+ width,
72
+ height
73
+ }
74
+ };
75
+ else return {
76
+ type: "embed",
77
+ oembed: {
78
+ ...oembedBase,
79
+ type: _prismicio_client.OEmbedType.Link
80
+ }
81
+ };
73
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
+ */
74
90
  const serializeSpan = (node, rtPartialInlineNode) => {
75
- var _a, _b;
76
- if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
77
- const url = (_a = node.properties) == null ? void 0 : _a.href;
78
- if (!url) {
79
- throw new PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `hyperlink` is missing an `href` attribute");
80
- }
81
- return {
82
- type: "hyperlink",
83
- data: {
84
- link_type: client.LinkType.Web,
85
- url,
86
- target: (_b = node.properties) == null ? void 0 : _b.target
87
- }
88
- };
89
- }
90
- return rtPartialInlineNode;
91
+ if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
92
+ const url = node.properties?.href;
93
+ if (!url) throw new require_PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `hyperlink` is missing an `href` attribute");
94
+ return {
95
+ type: "hyperlink",
96
+ data: {
97
+ link_type: _prismicio_client.LinkType.Web,
98
+ url,
99
+ target: node.properties?.target
100
+ }
101
+ };
102
+ }
103
+ return rtPartialInlineNode;
91
104
  };
105
+ //#endregion
92
106
  exports.serializeEmbed = serializeEmbed;
93
107
  exports.serializeImage = serializeImage;
94
108
  exports.serializeSpan = serializeSpan;
95
- //# sourceMappingURL=hastSerializerHelpers.cjs.map
109
+
110
+ //# sourceMappingURL=hastSerializerHelpers.cjs.map