@prismicio/client 7.7.0 → 7.7.2

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.
@@ -64,6 +64,9 @@ class RichTextFieldBuilder {
64
64
  }
65
65
  }
66
66
  }
67
+ if (span.start === span.end) {
68
+ return;
69
+ }
67
70
  if (((_a = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex]) == null ? void 0 : _a.end) === span.start) {
68
71
  __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex].end = span.end;
69
72
  } else {
@@ -1 +1 @@
1
- {"version":3,"file":"RichTextFieldBuilder.cjs","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import {\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"../types/value/richText\";\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\";\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;\n\n/**\n * An inline node without its `start` and `end` properties.\n */\nexport type RTPartialInlineNode = DistributedOmit<\n\tRTInlineNode,\n\t\"start\" | \"end\"\n>;\n\nexport class RichTextFieldBuilder {\n\t#nodes: RTNode[] = [];\n\n\tget #last(): RTNode | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1];\n\t}\n\n\tappendNode(node: RTNode): void {\n\t\tthis.cleanupLast();\n\n\t\tthis.#nodes.push(node);\n\t}\n\n\tappendTextNode(\n\t\ttype: RTTextNode[\"type\"],\n\t\tdirection: \"ltr\" | \"rtl\" = \"ltr\",\n\t): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t});\n\t}\n\n\tappendSpan(span: RTInlineNode): void;\n\tappendSpan(partialSpan: RTPartialInlineNode, length: number): void;\n\tappendSpan(\n\t\tspanOrPartialSpan: RTInlineNode | RTPartialInlineNode,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\");\n\t\t}\n\n\t\tlet span: RTInlineNode;\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan;\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length;\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t};\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1;\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i];\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (\n\t\t\t\t\tlastSpan.type === \"label\" &&\n\t\t\t\t\tlastSpan.data.label === span.data.label\n\t\t\t\t) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink = (\n\t\t\t\t\t\tObject.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]\n\t\t\t\t\t).every(([key, value]) => lastSpan.data[key] === value);\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end;\n\t\t} else {\n\t\t\tthis.#last.spans.push(span);\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\n\t\t\t\t\"Cannot append text to non-text last node\",\n\t\t\t);\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text;\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart();\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast();\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField;\n\t}\n\n\t/**\n\t * Cleans up the current last node.\n\t */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd();\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - rich text node to check.\n\t *\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(node?: RTNode): node is RTTextNode {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\";\n\t}\n}\n"],"names":["PrismicRichTextError"],"mappings":";;;;;;;;;;MA8Ba,qBAAoB;AAAA;;AAChC,+BAAmB,CAAA;AAAA;AAAA,EAMnB,WAAW,MAAY;AACtB,SAAK,YAAW;AAEX,uBAAA,QAAO,KAAK,IAAI;A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BAAK,2CAAM,MAAM,sBAAsB,EAAE,MAAM,KAAK;AAAA,IAAA,OAC9C;AACD,yBAAA,2CAAM,MAAM,KAAK,IAAI;AAAA,IAC3B;AAAA,EACD;AAAA,EAEA,WAAW,MAAY;AACtB,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAIA,qBAAAA,qBACT,0CAA0C;AAAA,IAE5C;AAEI,QAAA,mBAAK,2CAAM,MAAM;AACpB,yBAAK,2CAAM,QAAQ;AAAA,IAAA,OACb;AACD,yBAAA,2CAAM,OAAO,KAAK,UAAS;AAAA,IACjC;AAAA,EACD;AAAA,EAEA,QAAK;AAEJ,SAAK,YAAW;AAIhB,WAAO,mBAAK;AAAA,EACb;AAAA;AAAA;AAAA;AAAA,EAKQ,cAAW;AAClB,QAAI,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAChC,yBAAK,2CAAM,OAAO,mBAAK,2CAAM,KAAK;IACnC;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,WAAW,MAAa;AAC/B,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AApIA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;;"}
1
+ {"version":3,"file":"RichTextFieldBuilder.cjs","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import {\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"../types/value/richText\";\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\";\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;\n\n/**\n * An inline node without its `start` and `end` properties.\n */\nexport type RTPartialInlineNode = DistributedOmit<\n\tRTInlineNode,\n\t\"start\" | \"end\"\n>;\n\nexport class RichTextFieldBuilder {\n\t#nodes: RTNode[] = [];\n\n\tget #last(): RTNode | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1];\n\t}\n\n\tappendNode(node: RTNode): void {\n\t\tthis.cleanupLast();\n\n\t\tthis.#nodes.push(node);\n\t}\n\n\tappendTextNode(\n\t\ttype: RTTextNode[\"type\"],\n\t\tdirection: \"ltr\" | \"rtl\" = \"ltr\",\n\t): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t});\n\t}\n\n\tappendSpan(span: RTInlineNode): void;\n\tappendSpan(partialSpan: RTPartialInlineNode, length: number): void;\n\tappendSpan(\n\t\tspanOrPartialSpan: RTInlineNode | RTPartialInlineNode,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\");\n\t\t}\n\n\t\tlet span: RTInlineNode;\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan;\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length;\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t};\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1;\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i];\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (\n\t\t\t\t\tlastSpan.type === \"label\" &&\n\t\t\t\t\tlastSpan.data.label === span.data.label\n\t\t\t\t) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink = (\n\t\t\t\t\t\tObject.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]\n\t\t\t\t\t).every(([key, value]) => lastSpan.data[key] === value);\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Ignore empty spans.\n\t\tif (span.start === span.end) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end;\n\t\t} else {\n\t\t\tthis.#last.spans.push(span);\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\n\t\t\t\t\"Cannot append text to non-text last node\",\n\t\t\t);\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text;\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart();\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast();\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField;\n\t}\n\n\t/**\n\t * Cleans up the current last node.\n\t */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd();\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - rich text node to check.\n\t *\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(node?: RTNode): node is RTTextNode {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\";\n\t}\n}\n"],"names":["PrismicRichTextError"],"mappings":";;;;;;;;;;MA8Ba,qBAAoB;AAAA;;AAChC,+BAAmB,CAAA;AAAA;AAAA,EAMnB,WAAW,MAAY;AACtB,SAAK,YAAW;AAEX,uBAAA,QAAO,KAAK,IAAI;AAAA,EACtB;AAAA,EAEA,eACC,MACA,YAA2B,OAAK;AAEhC,SAAK,WAAW;AAAA,MACf;AAAA,MACA,MAAM;AAAA,MACN,OAAO,CAAE;AAAA,MACT;AAAA,IAAA,CACA;AAAA,EACF;AAAA,EAIA,WACC,mBACA,QAAe;;AAEf,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAIA,qBAAAA,qBAAqB,uCAAuC;AAAA,IACvE;AAEI,QAAA;AACA,QAAA,WAAW,qBAAqB,SAAS,mBAAmB;AACxD,aAAA;AAAA,IAAA,OACD;AACA,YAAA,aAAa,mBAAK,2CAAM,KAAK;AAE5B,aAAA;AAAA,QACN,GAAG;AAAA,QACH,OAAO;AAAA,QACP,KAAK,aAAa;AAAA,MAAA;AAAA,IAEpB;AAEA,QAAI,yBAAyB;AACpB,aAAA,IAAI,mBAAK,2CAAM,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;AACtD,YAAM,WAAW,mBAAK,2CAAM,MAAM,CAAC;AAEnC,UAAI,KAAK,SAAS,YAAY,KAAK,SAAS,MAAM;AAC7C,YAAA,SAAS,SAAS,KAAK,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AW,MAAa;AAC/B,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AAzIA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;;"}
@@ -62,6 +62,9 @@ class RichTextFieldBuilder {
62
62
  }
63
63
  }
64
64
  }
65
+ if (span.start === span.end) {
66
+ return;
67
+ }
65
68
  if (((_a = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex]) == null ? void 0 : _a.end) === span.start) {
66
69
  __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex].end = span.end;
67
70
  } else {
@@ -1 +1 @@
1
- {"version":3,"file":"RichTextFieldBuilder.js","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import {\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"../types/value/richText\";\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\";\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;\n\n/**\n * An inline node without its `start` and `end` properties.\n */\nexport type RTPartialInlineNode = DistributedOmit<\n\tRTInlineNode,\n\t\"start\" | \"end\"\n>;\n\nexport class RichTextFieldBuilder {\n\t#nodes: RTNode[] = [];\n\n\tget #last(): RTNode | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1];\n\t}\n\n\tappendNode(node: RTNode): void {\n\t\tthis.cleanupLast();\n\n\t\tthis.#nodes.push(node);\n\t}\n\n\tappendTextNode(\n\t\ttype: RTTextNode[\"type\"],\n\t\tdirection: \"ltr\" | \"rtl\" = \"ltr\",\n\t): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t});\n\t}\n\n\tappendSpan(span: RTInlineNode): void;\n\tappendSpan(partialSpan: RTPartialInlineNode, length: number): void;\n\tappendSpan(\n\t\tspanOrPartialSpan: RTInlineNode | RTPartialInlineNode,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\");\n\t\t}\n\n\t\tlet span: RTInlineNode;\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan;\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length;\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t};\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1;\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i];\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (\n\t\t\t\t\tlastSpan.type === \"label\" &&\n\t\t\t\t\tlastSpan.data.label === span.data.label\n\t\t\t\t) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink = (\n\t\t\t\t\t\tObject.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]\n\t\t\t\t\t).every(([key, value]) => lastSpan.data[key] === value);\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end;\n\t\t} else {\n\t\t\tthis.#last.spans.push(span);\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\n\t\t\t\t\"Cannot append text to non-text last node\",\n\t\t\t);\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text;\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart();\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast();\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField;\n\t}\n\n\t/**\n\t * Cleans up the current last node.\n\t */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd();\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - rich text node to check.\n\t *\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(node?: RTNode): node is RTTextNode {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\";\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;MA8Ba,qBAAoB;AAAA;;AAChC,+BAAmB,CAAA;AAAA;AAAA,EAMnB,WAAW,MAAY;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AAvC,mBAA0C,SAAQ,KAAK,OAAO;AACjE,yBAAK,2CAAM,MAAM,sBAAsB,EAAE,MAAM,KAAK;AAAA,IAAA,OAC9C;AACD,yBAAA,2CAAM,MAAM,KAAK,IAAI;AAAA,IAC3B;AAAA,EACD;AAAA,EAEA,WAAW,MAAY;AACtB,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAI,qBACT,0CAA0C;AAAA,IAE5C;AAEI,QAAA,mBAAK,2CAAM,MAAM;AACpB,yBAAK,2CAAM,QAAQ;AAAA,IAAA,OACb;AACD,yBAAA,2CAAM,OAAO,KAAK,UAAS;AAAA,IACjC;AAAA,EACD;AAAA,EAEA,QAAK;AAEJ,SAAK,YAAW;AAIhB,WAAO,mBAAK;AAAA,EACb;AAAA;AAAA;AAAA;AAAA,EAKQ,cAAW;AAClB,QAAI,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAChC,yBAAK,2CAAM,OAAO,mBAAK,2CAAM,KAAK;IACnC;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,WAAW,MAAa;AAC/B,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AApIA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;"}
1
+ {"version":3,"file":"RichTextFieldBuilder.js","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import {\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"../types/value/richText\";\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\";\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;\n\n/**\n * An inline node without its `start` and `end` properties.\n */\nexport type RTPartialInlineNode = DistributedOmit<\n\tRTInlineNode,\n\t\"start\" | \"end\"\n>;\n\nexport class RichTextFieldBuilder {\n\t#nodes: RTNode[] = [];\n\n\tget #last(): RTNode | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1];\n\t}\n\n\tappendNode(node: RTNode): void {\n\t\tthis.cleanupLast();\n\n\t\tthis.#nodes.push(node);\n\t}\n\n\tappendTextNode(\n\t\ttype: RTTextNode[\"type\"],\n\t\tdirection: \"ltr\" | \"rtl\" = \"ltr\",\n\t): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t});\n\t}\n\n\tappendSpan(span: RTInlineNode): void;\n\tappendSpan(partialSpan: RTPartialInlineNode, length: number): void;\n\tappendSpan(\n\t\tspanOrPartialSpan: RTInlineNode | RTPartialInlineNode,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\");\n\t\t}\n\n\t\tlet span: RTInlineNode;\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan;\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length;\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t};\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1;\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i];\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (\n\t\t\t\t\tlastSpan.type === \"label\" &&\n\t\t\t\t\tlastSpan.data.label === span.data.label\n\t\t\t\t) {\n\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink = (\n\t\t\t\t\t\tObject.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]\n\t\t\t\t\t).every(([key, value]) => lastSpan.data[key] === value);\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Ignore empty spans.\n\t\tif (span.start === span.end) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end;\n\t\t} else {\n\t\t\tthis.#last.spans.push(span);\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\n\t\t\t\t\"Cannot append text to non-text last node\",\n\t\t\t);\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text;\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart();\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast();\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField;\n\t}\n\n\t/**\n\t * Cleans up the current last node.\n\t */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd();\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - rich text node to check.\n\t *\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(node?: RTNode): node is RTTextNode {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\";\n\t}\n}\n"],"names":[],"mappings":";;;;;;;;MA8Ba,qBAAoB;AAAA;;AAChC,+BAAmB,CAAA;AAAA;AAAA,EAMnB,WAAW,MAAY;AACtB,SAAK,YAAW;AAEX,uBAAA,QAAO,KAAK,IAAI;AAAA,EACtB;AAAA,EAEA,eACC,MACA,YAA2B,OAAK;AAEhC,SAAK,WAAW;AAAA,MACf;AAAA,MACA,MAAM;AAAA,MACN,OAAO,CAAE;AAAA,MACT;AAAA,IAAA,CACA;AAAA,EACF;AAAA,EAIA,WACC,mBACA,QAAe;;AAEf,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAI,qBAAqB,uCAAuC;AAAA,IACvE;AAEI,QAAA;AACA,QAAA,WAAW,qBAAqB,SAAS,mBAAmB;AACxD,aAAA;AAAA,IAAA,OACD;AACA,YAAA,aAAa,mBAAK,2CAAM,KAAK;AAE5B,aAAA;AAAA,QACN,GAAG;AAAA,QACH,OAAO;AAAA,QACP,KAAK,aAAa;AAAA,MAAA;AAAA,IAEpB;AAEA,QAAI,yBAAyB;AACpB,aAAA,IAAI,mBAAK,2CAAM,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;AACtD,YAAM,WAAW,mBAAK,2CAAM,MAAM,CAAC;AAEnC,UAAI,KAAK,SAAS,YAAY,KAAK,SAAS,MAAM;AAC7C,YAAA,SAAS,SAAS,KA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Aa;AAC/B,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AAzIA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;"}
@@ -10,7 +10,7 @@ const serializeImage = (node) => {
10
10
  if (!src) {
11
11
  throw new PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
12
12
  }
13
- const url = new URL(src);
13
+ const url = new URL(src, "https://noop.com");
14
14
  let width = (_b = node.properties) == null ? void 0 : _b.width;
15
15
  let height = (_c = node.properties) == null ? void 0 : _c.height;
16
16
  let x = 0;
@@ -1 +1 @@
1
- {"version":3,"file":"hastSerializerHelpers.cjs","sources":["../../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { Element } from \"hast\";\nimport { toHtml } from \"hast-util-to-html\";\n\nimport { OEmbedType } from \"../types/value/embed\";\nimport { LinkType } from \"../types/value/link\";\nimport { RTEmbedNode, RTImageNode, RTLinkNode } from \"../types/value/richText\";\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\";\n\nimport { 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);\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":";;;;;;AAkBa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAIA,+BAAAA,+BACT,qDAAqD;AAAA,EAEvD;AAEM,QAAA,MAAM,IAAI,IAAI,GAAG;AAEnB,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,IAA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MC,MAAAA,WAAW,MAAM,OAAO,OAAQ;AAAA,IAAA;AAAA,SAE1D;AACC,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAMA,MAAAA,WAAW,KAAM;AAAA,IAAA;AAAA,EAElD;AACD;AAUa,MAAA,gBAAgB,CAC5B,MACA,wBAGwB;;AACxB,MACC,oBAAoB,SAAS,eAC7B,EAAE,UAAU,sBACX;AACK,UAAA,OAAM,UAAK,eAAL,mBAAiB;AAC7B,QAAI,CAAC,KAAK;AACH,YAAA,IAAIF,+BAAAA,+BACT,4DAA4D;AAAA,IAE9D;AAEO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,QACL,WAAWG,KAAS,SAAA;AAAA,QACpB;AAAA,QACA,SAAQ,UAAK,eAAL,mBAAiB;AAAA,MACzB;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;;;;"}
1
+ {"version":3,"file":"hastSerializerHelpers.cjs","sources":["../../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { Element } from \"hast\";\nimport { toHtml } from \"hast-util-to-html\";\n\nimport { OEmbedType } from \"../types/value/embed\";\nimport { LinkType } from \"../types/value/link\";\nimport { RTEmbedNode, RTImageNode, RTLinkNode } from \"../types/value/richText\";\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\";\n\nimport { 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":";;;;;;AAkBa,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,mBA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AiB;AACzB,QAAA,UAAS,UAAK,eAAL,mBAAiB;AAGhC,OAAK,WAAW;AAEhB,MAAI,SAAS,QAAQ;AACb,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAMC,MAAAA,WAAW,MAAM,OAAO,OAAQ;AAAA,IAAA;AAAA,SAE1D;AACC,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAMA,MAAAA,WAAW,KAAM;AAAA,IAAA;AAAA,EAElD;AACD;AAUa,MAAA,gBAAgB,CAC5B,MACA,wBAGwB;;AACxB,MACC,oBAAoB,SAAS,eAC7B,EAAE,UAAU,sBACX;AACK,UAAA,OAAM,UAAK,eAAL,mBAAiB;AAC7B,QAAI,CAAC,KAAK;AACH,YAAA,IAAIF,+BAAAA,+BACT,4DAA4D;AAAA,IAE9D;AAEO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,QACL,WAAWG,KAAS,SAAA;AAAA,QACpB;AAAA,QACA,SAAQ,UAAK,eAAL,mBAAiB;AAAA,MACzB;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;;;;"}
@@ -8,7 +8,7 @@ const serializeImage = (node) => {
8
8
  if (!src) {
9
9
  throw new PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
10
10
  }
11
- const url = new URL(src);
11
+ const url = new URL(src, "https://noop.com");
12
12
  let width = (_b = node.properties) == null ? void 0 : _b.width;
13
13
  let height = (_c = node.properties) == null ? void 0 : _c.height;
14
14
  let x = 0;
@@ -1 +1 @@
1
- {"version":3,"file":"hastSerializerHelpers.js","sources":["../../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { Element } from \"hast\";\nimport { toHtml } from \"hast-util-to-html\";\n\nimport { OEmbedType } from \"../types/value/embed\";\nimport { LinkType } from \"../types/value/link\";\nimport { RTEmbedNode, RTImageNode, RTLinkNode } from \"../types/value/richText\";\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\";\n\nimport { 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);\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,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A,EAAA;AAEjD;AASa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAI,+BACT,uDAAuD;AAAA,EAEzD;AAEA,QAAM,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,WAAW;AAAA,IACX,MAAM,OAAO,IAAI;AAAA,IACjB,QAAO,UAAK,eAAL,mBAAiB;AAAA,EAAA;AAGnB,QAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,QAAA,UAAS,UAAK,eAAL,mBAAiB;AAGhC,OAAK,WAAW;AAEhB,MAAI,SAAS,QAAQ;AACb,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAM,WAAW,MAAM,OAAO,OAAQ;AAAA,IAAA;AAAA,SAE1D;AACC,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAM,WAAW,KAAM;AAAA,IAAA;AAAA,EAElD;AACD;AAUa,MAAA,gBAAgB,CAC5B,MACA,wBAGwB;;AACxB,MACC,oBAAoB,SAAS,eAC7B,EAAE,UAAU,sBACX;AACK,UAAA,OAAM,UAAK,eAAL,mBAAiB;AAC7B,QAAI,CAAC,KAAK;AACH,YAAA,IAAI,+BACT,4DAA4D;AAAA,IAE9D;AAEO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,QACL,WAAW,SAAS;AAAA,QACpB;AAAA,QACA,SAAQ,UAAK,eAAL,mBAAiB;AAAA,MACzB;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;"}
1
+ {"version":3,"file":"hastSerializerHelpers.js","sources":["../../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { Element } from \"hast\";\nimport { toHtml } from \"hast-util-to-html\";\n\nimport { OEmbedType } from \"../types/value/embed\";\nimport { LinkType } from \"../types/value/link\";\nimport { RTEmbedNode, RTImageNode, RTLinkNode } from \"../types/value/richText\";\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\";\n\nimport { 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":";;;;AAkBa,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,MAAM,EACV,MAAM,GAAG;AAEX,UAAI,OAAO,KAAK;AAChB,UAAI,OAAO,KAAK;AAGhB,UAAI,OAAO;AACV,eAAO,KAAK,IAAI,GAAG,QAAQ,OAAO,KAAK,CAAC;AAAA,MACzC;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AAAA,IACN,MAAM;AAAA,IACN,IAAI;AAAA,IACJ,KAAK;AAAA,IACL,OAAM,UAAK,eAAL,mBAAiB,QAAkB;AAAA,IACzC,aAAY,UAAK,eAAL,mBAAiB,cAAwB;AAAA;AAAA;AAAA;AAAA,IAIrD,YAAY,EAAE,OAAwB,OAA0B;AAAA,IAChE,MAAM,EAAE,GAAG,GAAG,MAAM,YAAY,cAAe;AAAA,EAAA;AAEjD;AASa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAI,+BACT,uDAAuD;AAAA,EAEzD;AAEA,QAAM,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,WAAW;AAAA,IACX,MAAM,OAAO,IAAI;AAAA,IACjB,QAAO,UAAK,eAAL,mBAAiB;AAAA,EAAA;AAGnB,QAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,QAAA,UAAS,UAAK,eAAL,mBAAiB;AAGhC,OAAK,WAAW;AAEhB,MAAI,SAAS,QAAQ;AACb,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAM,WAAW,MAAM,OAAO,OAAQ;AAAA,IAAA;AAAA,SAE1D;AACC,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAM,WAAW,KAAM;AAAA,IAAA;AAAA,EAElD;AACD;AAUa,MAAA,gBAAgB,CAC5B,MACA,wBAGwB;;AACxB,MACC,oBAAoB,SAAS,eAC7B,EAAE,UAAU,sBACX;AACK,UAAA,OAAM,UAAK,eAAL,mBAAiB;AAC7B,QAAI,CAAC,KAAK;AACH,YAAA,IAAI,+BACT,4DAA4D;AAAA,IAE9D;AAEO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,QACL,WAAW,SAAS;AAAA,QACpB;AAAA,QACA,SAAQ,UAAK,eAAL,mBAAiB;AAAA,MACzB;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;"}
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const version = "7.7.0";
3
+ const version = "7.7.2";
4
4
  exports.version = version;
5
5
  //# sourceMappingURL=package.json.cjs.map
@@ -1,4 +1,4 @@
1
- const version = "7.7.0";
1
+ const version = "7.7.2";
2
2
  export {
3
3
  version
4
4
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/client",
3
- "version": "7.7.0",
3
+ "version": "7.7.2",
4
4
  "description": "The official JavaScript + TypeScript client library for Prismic",
5
5
  "keywords": [
6
6
  "typescript",
@@ -110,6 +110,11 @@ export class RichTextFieldBuilder {
110
110
  }
111
111
  }
112
112
 
113
+ // Ignore empty spans.
114
+ if (span.start === span.end) {
115
+ return;
116
+ }
117
+
113
118
  // Prefer to extend the last span of the same type end
114
119
  // position if it ends at the start of the new span.
115
120
  if (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {
@@ -25,7 +25,7 @@ export const serializeImage = (node: Element): RTImageNode => {
25
25
  );
26
26
  }
27
27
 
28
- const url = new URL(src);
28
+ const url = new URL(src, "https://noop.com");
29
29
 
30
30
  let width = node.properties?.width as number | undefined;
31
31
  let height = node.properties?.height as number | undefined;