@prismicio/migrate 0.0.2-canary.3e6d6e6 → 0.0.2-pr.3.de727c7

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.
@@ -1 +1 @@
1
- {"version":3,"file":"RichTextFieldBuilder.cjs","names":["#last","#nodes","PrismicRichTextError"],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tInjectMigrationSpecificTypes,\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\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<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Omit<ObjectType, KeyType>\n\t: never\n\n/** An inline node without its `start` and `end` properties. */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: InjectMigrationSpecificTypes<RTNode>[] = []\n\n\tget #last(): InjectMigrationSpecificTypes<RTNode> | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: InjectMigrationSpecificTypes<RTNode>): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(type: RTTextNode[\"type\"], direction: \"ltr\" | \"rtl\" = \"ltr\"): 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: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<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: InjectMigrationSpecificTypes<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 (lastSpan.type === \"label\" && lastSpan.data.label === span.data.label) {\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\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (Object.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]).every(\n\t\t\t\t\t\t\t\t\t([key, value]) =>\n\t\t\t\t\t\t\t\t\t\tlastSpan.data?.[key as keyof (typeof lastSpan)[\"data\"]] === value,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: span.data === lastSpan.data\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(\"Cannot append text to non-text last node\")\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/** Cleans up the current last node. */\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 * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(\n\t\tnode?: InjectMigrationSpecificTypes<RTNode>,\n\t): node is InjectMigrationSpecificTypes<RTTextNode> {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\"\n\t}\n}\n"],"mappings":";;AAyBA,IAAa,uBAAb,MAAkC;CACjC,SAAiD,CAAC;CAElD,IAAIA,QAA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MAAM,uBAAuB,CAAC,MAAM,KAAK;OAEpD,KAAKA,MAAM,MAAM,KAAK,IAAI;CAE5B;CAEA,WAAW,MAAoB;EAC9B,IAAI,CAAC,KAAK,WAAW,KAAKA,KAAK,GAC9B,MAAM,IAAIE,6BAAAA,qBAAqB,0CAA0C;EAG1E,IAAI,KAAKF,MAAM,MACd,KAAKA,MAAM,QAAQ;OAEnB,KAAKA,MAAM,OAAO,KAAK,UAAU;CAEnC;CAEA,QAAuB;EAEtB,KAAK,YAAY;EAIjB,OAAO,KAAKC;CACb;;CAGA,cAA4B;EAC3B,IAAI,KAAK,WAAW,KAAKD,KAAK,GAC7B,KAAKA,MAAM,OAAO,KAAKA,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQA,WACC,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
1
+ {"version":3,"file":"RichTextFieldBuilder.cjs","names":["PrismicRichTextError"],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tInjectMigrationSpecificTypes,\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\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<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Omit<ObjectType, KeyType>\n\t: never\n\n/** An inline node without its `start` and `end` properties. */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: InjectMigrationSpecificTypes<RTNode>[] = []\n\n\tget #last(): InjectMigrationSpecificTypes<RTNode> | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: InjectMigrationSpecificTypes<RTNode>): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(type: RTTextNode[\"type\"], direction: \"ltr\" | \"rtl\" = \"ltr\"): 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: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<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: InjectMigrationSpecificTypes<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 (lastSpan.type === \"label\" && lastSpan.data.label === span.data.label) {\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\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (Object.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]).every(\n\t\t\t\t\t\t\t\t\t([key, value]) =>\n\t\t\t\t\t\t\t\t\t\tlastSpan.data?.[key as keyof (typeof lastSpan)[\"data\"]] === value,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: span.data === lastSpan.data\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(\"Cannot append text to non-text last node\")\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/** Cleans up the current last node. */\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 * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(\n\t\tnode?: InjectMigrationSpecificTypes<RTNode>,\n\t): node is InjectMigrationSpecificTypes<RTTextNode> {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\"\n\t}\n}\n"],"mappings":";;AAyBA,IAAa,uBAAb,MAAkC;CACjC,SAAiD,CAAC;CAElD,IAAI,QAA0D;EAC7D,OAAO,KAAK,OAAO,KAAK,OAAO,SAAS;CACzC;CAEA,WAAW,MAAkD;EAC5D,KAAK,YAAY;EAEjB,KAAK,OAAO,KAAK,IAAI;CACtB;CAEA,eAAe,MAA0B,YAA2B,OAAa;EAChF,KAAK,WAAW;GACf;GACA,MAAM;GACN,OAAO,CAAC;GACR;EACD,CAAC;CACF;CAIA,WACC,mBACA,QACO;EACP,IAAI,CAAC,KAAK,WAAW,KAAK,KAAK,GAC9B,MAAM,IAAIA,6BAAAA,qBAAqB,uCAAuC;EAGvE,IAAI;EACJ,IAAI,WAAW,qBAAqB,SAAS,mBAC5C,OAAO;OACD;GACN,MAAM,aAAa,KAAK,MAAM,KAAK;GAEnC,OAAO;IACN,GAAG;IACH,OAAO;IACP,KAAK,aAAa;GACnB;EACD;EAEA,IAAI,yBAAyB;EAC7B,KAAK,IAAI,IAAI,KAAK,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;GACtD,MAAM,WAAW,KAAK,MAAM,MAAM;GAElC,IAAI,KAAK,SAAS,YAAY,KAAK,SAAS,MACvC;QAAA,SAAS,SAAS,KAAK,MAAM;KAChC,yBAAyB;KACzB;IACD;UACM,IAAI,KAAK,SAAS,SACpB;QAAA,SAAS,SAAS,WAAW,SAAS,KAAK,UAAU,KAAK,KAAK,OAAO;KACzE,yBAAyB;KACzB;IACD;UACM,IAAI,KAAK,SAAS,aACpB;QAAA,SAAS,SAAS,aAKpB;SAAA,OAAO,KAAK,SAAS,YAAY,OAAO,SAAS,SAAS,WACtD,OAAO,QAAQ,KAAK,IAAI,CAAC,CAA2C,OACpE,CAAC,KAAK,WACN,SAAS,OAAO,SAA4C,KAC9D,IACC,KAAK,SAAS,SAAS,MAEX;MACf,yBAAyB;MACzB;KACD;;;EAGH;EAGA,IAAI,KAAK,UAAU,KAAK,KACvB;EAKD,IAAI,KAAK,MAAM,MAAM,uBAAuB,EAAE,QAAQ,KAAK,OAC1D,KAAK,MAAM,MAAM,uBAAuB,CAAC,MAAM,KAAK;OAEpD,KAAK,MAAM,MAAM,KAAK,IAAI;CAE5B;CAEA,WAAW,MAAoB;EAC9B,IAAI,CAAC,KAAK,WAAW,KAAK,KAAK,GAC9B,MAAM,IAAIA,6BAAAA,qBAAqB,0CAA0C;EAG1E,IAAI,KAAK,MAAM,MACd,KAAK,MAAM,QAAQ;OAEnB,KAAK,MAAM,OAAO,KAAK,UAAU;CAEnC;CAEA,QAAuB;EAEtB,KAAK,YAAY;EAIjB,OAAO,KAAK;CACb;;CAGA,cAA4B;EAC3B,IAAI,KAAK,WAAW,KAAK,KAAK,GAC7B,KAAK,MAAM,OAAO,KAAK,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQA,WACC,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
@@ -1 +1 @@
1
- {"version":3,"file":"RichTextFieldBuilder.js","names":["#last","#nodes"],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tInjectMigrationSpecificTypes,\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\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<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Omit<ObjectType, KeyType>\n\t: never\n\n/** An inline node without its `start` and `end` properties. */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: InjectMigrationSpecificTypes<RTNode>[] = []\n\n\tget #last(): InjectMigrationSpecificTypes<RTNode> | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: InjectMigrationSpecificTypes<RTNode>): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(type: RTTextNode[\"type\"], direction: \"ltr\" | \"rtl\" = \"ltr\"): 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: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<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: InjectMigrationSpecificTypes<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 (lastSpan.type === \"label\" && lastSpan.data.label === span.data.label) {\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\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (Object.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]).every(\n\t\t\t\t\t\t\t\t\t([key, value]) =>\n\t\t\t\t\t\t\t\t\t\tlastSpan.data?.[key as keyof (typeof lastSpan)[\"data\"]] === value,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: span.data === lastSpan.data\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(\"Cannot append text to non-text last node\")\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/** Cleans up the current last node. */\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 * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(\n\t\tnode?: InjectMigrationSpecificTypes<RTNode>,\n\t): node is InjectMigrationSpecificTypes<RTTextNode> {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\"\n\t}\n}\n"],"mappings":";;AAyBA,IAAa,uBAAb,MAAkC;CACjC,SAAiD,CAAC;CAElD,IAAIA,QAA0D;EAC7D,OAAO,KAAKC,OAAO,KAAKA,OAAO,SAAS;CACzC;CAEA,WAAW,MAAkD;EAC5D,KAAK,YAAY;EAEjB,KAAKA,OAAO,KAAK,IAAI;CACtB;CAEA,eAAe,MAA0B,YAA2B,OAAa;EAChF,KAAK,WAAW;GACf;GACA,MAAM;GACN,OAAO,CAAC;GACR;EACD,CAAC;CACF;CAIA,WACC,mBACA,QACO;EACP,IAAI,CAAC,KAAK,WAAW,KAAKD,KAAK,GAC9B,MAAM,IAAI,qBAAqB,uCAAuC;EAGvE,IAAI;EACJ,IAAI,WAAW,qBAAqB,SAAS,mBAC5C,OAAO;OACD;GACN,MAAM,aAAa,KAAKA,MAAM,KAAK;GAEnC,OAAO;IACN,GA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mD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
1
+ {"version":3,"file":"RichTextFieldBuilder.js","names":[],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tInjectMigrationSpecificTypes,\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\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<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Omit<ObjectType, KeyType>\n\t: never\n\n/** An inline node without its `start` and `end` properties. */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: InjectMigrationSpecificTypes<RTNode>[] = []\n\n\tget #last(): InjectMigrationSpecificTypes<RTNode> | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: InjectMigrationSpecificTypes<RTNode>): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(type: RTTextNode[\"type\"], direction: \"ltr\" | \"rtl\" = \"ltr\"): 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: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<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: InjectMigrationSpecificTypes<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 (lastSpan.type === \"label\" && lastSpan.data.label === span.data.label) {\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\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (Object.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]).every(\n\t\t\t\t\t\t\t\t\t([key, value]) =>\n\t\t\t\t\t\t\t\t\t\tlastSpan.data?.[key as keyof (typeof lastSpan)[\"data\"]] === value,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: span.data === lastSpan.data\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(\"Cannot append text to non-text last node\")\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/** Cleans up the current last node. */\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 * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(\n\t\tnode?: InjectMigrationSpecificTypes<RTNode>,\n\t): node is InjectMigrationSpecificTypes<RTTextNode> {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\"\n\t}\n}\n"],"mappings":";;AAyBA,IAAa,uBAAb,MAAkC;CACjC,SAAiD,CAAC;CAElD,IAAI,QAA0D;EAC7D,OAAO,KAAK,OAAO,KAAK,OAAO,SAAS;CACzC;CAEA,WAAW,MAAkD;EAC5D,KAAK,YAAY;EAEjB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AK,WAAW,KAAK,KAAK,GAC9B,MAAM,IAAI,qBAAqB,0CAA0C;EAG1E,IAAI,KAAK,MAAM,MACd,KAAK,MAAM,QAAQ;OAEnB,KAAK,MAAM,OAAO,KAAK,UAAU;CAEnC;CAEA,QAAuB;EAEtB,KAAK,YAAY;EAIjB,OAAO,KAAK;CACb;;CAGA,cAA4B;EAC3B,IAAI,KAAK,WAAW,KAAK,KAAK,GAC7B,KAAK,MAAM,OAAO,KAAK,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQA,WACC,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
@@ -47,17 +47,20 @@ const filterTextNode = (node, model, allowedNodeTypes) => {
47
47
  };
48
48
  for (let i = 0; i < node.spans.length; i++) {
49
49
  const span = node.spans[i];
50
- if (allowedNodeTypes.includes(span.type)) if (span.type === _prismicio_client.RichTextNodeType.hyperlink) if (!model.config?.allowTargetBlank && span.data.link_type === _prismicio_client.LinkType.Web) filteredNode.spans.push({
51
- ...span,
52
- data: {
53
- ...span.data,
54
- target: void 0
55
- }
56
- });
57
- else filteredNode.spans.push(span);
58
- else if (span.type === _prismicio_client.RichTextNodeType.label) {
59
- if (model.config?.labels?.includes(span.data.label)) filteredNode.spans.push(span);
60
- } else filteredNode.spans.push(span);
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
+ }
61
64
  }
62
65
  return filteredNode;
63
66
  };
@@ -73,9 +76,11 @@ const filterRichTextField = (richTextField, model) => {
73
76
  const allowedNodeTypes = getAllowedNodeTypes(model);
74
77
  for (let i = 0; i < (model.config && "multi" in model.config ? richTextField.length : 1); i++) {
75
78
  const node = richTextField[i];
76
- if (allowedNodeTypes.includes(node.type)) if (node.type === _prismicio_client.RichTextNodeType.image) nodes.push(filterImageNode(node, model));
77
- else if (node.type === _prismicio_client.RichTextNodeType.embed) nodes.push(node);
78
- else nodes.push(filterTextNode(node, model, allowedNodeTypes));
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
+ }
79
84
  }
80
85
  return nodes;
81
86
  };
@@ -1 +1 @@
1
- {"version":3,"file":"filterRichTextField.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\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,cAAcA,kBAAAA,SAAS,KAC1E,OAAO;EACN,GAAG;EACH,QAAQ;GACP,GAAG,KA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],"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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AiB,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"}
@@ -47,17 +47,20 @@ const filterTextNode = (node, model, allowedNodeTypes) => {
47
47
  };
48
48
  for (let i = 0; i < node.spans.length; i++) {
49
49
  const span = node.spans[i];
50
- if (allowedNodeTypes.includes(span.type)) if (span.type === RichTextNodeType.hyperlink) if (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) filteredNode.spans.push({
51
- ...span,
52
- data: {
53
- ...span.data,
54
- target: void 0
55
- }
56
- });
57
- else filteredNode.spans.push(span);
58
- else if (span.type === RichTextNodeType.label) {
59
- if (model.config?.labels?.includes(span.data.label)) filteredNode.spans.push(span);
60
- } else filteredNode.spans.push(span);
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
+ }
61
64
  }
62
65
  return filteredNode;
63
66
  };
@@ -73,9 +76,11 @@ const filterRichTextField = (richTextField, model) => {
73
76
  const allowedNodeTypes = getAllowedNodeTypes(model);
74
77
  for (let i = 0; i < (model.config && "multi" in model.config ? richTextField.length : 1); i++) {
75
78
  const node = richTextField[i];
76
- if (allowedNodeTypes.includes(node.type)) if (node.type === RichTextNodeType.image) nodes.push(filterImageNode(node, model));
77
- else if (node.type === RichTextNodeType.embed) nodes.push(node);
78
- else nodes.push(filterTextNode(node, model, allowedNodeTypes));
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
+ }
79
84
  }
80
85
  return nodes;
81
86
  };
@@ -1 +1 @@
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,CA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R"}
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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AAiB,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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/migrate",
3
- "version": "0.0.2-canary.3e6d6e6",
3
+ "version": "0.0.2-pr.3.de727c7",
4
4
  "description": "Set of helpers to migrate your data to Prismic",
5
5
  "keywords": [
6
6
  "prismic",