@prismicio/migrate 0.0.1 → 0.0.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.
- package/dist/lib/RichTextFieldBuilder.cjs +4 -1
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.ts +4 -4
- package/dist/lib/RichTextFieldBuilder.js +4 -1
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/package.json +13 -13
- package/src/lib/RichTextFieldBuilder.ts +29 -11
- package/src/types.ts +7 -4
|
@@ -56,7 +56,10 @@ class RichTextFieldBuilder {
|
|
|
56
56
|
}
|
|
57
57
|
} else if (span.type === "hyperlink") {
|
|
58
58
|
if (lastSpan.type === "hyperlink") {
|
|
59
|
-
const isSameLink = Object.entries(span.data).every(([key, value]) =>
|
|
59
|
+
const isSameLink = typeof span.data === "object" && typeof lastSpan.data === "object" ? Object.entries(span.data).every(([key, value]) => {
|
|
60
|
+
var _a2;
|
|
61
|
+
return ((_a2 = lastSpan.data) == null ? void 0 : _a2[key]) === value;
|
|
62
|
+
}) : span.data === lastSpan.data;
|
|
60
63
|
if (isSameLink) {
|
|
61
64
|
lastIdenticalSpanIndex = i;
|
|
62
65
|
break;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.cjs","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\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<\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<RTInlineNode, \"start\" | \"end\">\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(
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.cjs","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<\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<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(\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: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(\n\t\tpartialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>,\n\t\tlength: number,\n\t): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<\n\t\t\tRTInlineNode | RTPartialInlineNode\n\t\t>,\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 (\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\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (\n\t\t\t\t\t\t\t\t\tObject.entries(span.data) as [\n\t\t\t\t\t\t\t\t\t\tkeyof RTLinkNode[\"data\"],\n\t\t\t\t\t\t\t\t\t\tunknown,\n\t\t\t\t\t\t\t\t\t][]\n\t\t\t\t\t\t\t\t).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\"]] ===\n\t\t\t\t\t\t\t\t\t\tvalue,\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/**\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(\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"],"names":["PrismicRichTextError","_a"],"mappings":";;;;;;;;;;MA4Ba,qBAAoB;AAAA;;AAChC,+BAAiD,CAAA;AAAA;AAAA,EAMjD,WAAW,MAA0C;AACpD,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,EAOA,WACC,mBAGA,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;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A,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,WACP,MAA2C;AAE3C,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AAxJA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RTInlineNode, RTNode, RTTextNode, RichTextField } from "@prismicio/client";
|
|
1
|
+
import type { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RTTextNode, RichTextField } from "@prismicio/client";
|
|
2
2
|
/**
|
|
3
3
|
* Omits keys from a type, distributing the operation over a union.
|
|
4
4
|
*
|
|
@@ -13,10 +13,10 @@ type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType
|
|
|
13
13
|
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
14
14
|
export declare class RichTextFieldBuilder {
|
|
15
15
|
#private;
|
|
16
|
-
appendNode(node: RTNode): void;
|
|
16
|
+
appendNode(node: InjectMigrationSpecificTypes<RTNode>): void;
|
|
17
17
|
appendTextNode(type: RTTextNode["type"], direction?: "ltr" | "rtl"): void;
|
|
18
|
-
appendSpan(span: RTInlineNode): void;
|
|
19
|
-
appendSpan(partialSpan: RTPartialInlineNode
|
|
18
|
+
appendSpan(span: InjectMigrationSpecificTypes<RTInlineNode>): void;
|
|
19
|
+
appendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void;
|
|
20
20
|
appendText(text: string): void;
|
|
21
21
|
build(): RichTextField;
|
|
22
22
|
/**
|
|
@@ -54,7 +54,10 @@ class RichTextFieldBuilder {
|
|
|
54
54
|
}
|
|
55
55
|
} else if (span.type === "hyperlink") {
|
|
56
56
|
if (lastSpan.type === "hyperlink") {
|
|
57
|
-
const isSameLink = Object.entries(span.data).every(([key, value]) =>
|
|
57
|
+
const isSameLink = typeof span.data === "object" && typeof lastSpan.data === "object" ? Object.entries(span.data).every(([key, value]) => {
|
|
58
|
+
var _a2;
|
|
59
|
+
return ((_a2 = lastSpan.data) == null ? void 0 : _a2[key]) === value;
|
|
60
|
+
}) : span.data === lastSpan.data;
|
|
58
61
|
if (isSameLink) {
|
|
59
62
|
lastIdenticalSpanIndex = i;
|
|
60
63
|
break;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.js","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\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<\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<RTInlineNode, \"start\" | \"end\">\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(
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.js","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<\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<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(\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: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(\n\t\tpartialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>,\n\t\tlength: number,\n\t): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<\n\t\t\tRTInlineNode | RTPartialInlineNode\n\t\t>,\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 (\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\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (\n\t\t\t\t\t\t\t\t\tObject.entries(span.data) as [\n\t\t\t\t\t\t\t\t\t\tkeyof RTLinkNode[\"data\"],\n\t\t\t\t\t\t\t\t\t\tunknown,\n\t\t\t\t\t\t\t\t\t][]\n\t\t\t\t\t\t\t\t).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\"]] ===\n\t\t\t\t\t\t\t\t\t\tvalue,\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/**\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(\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"],"names":["_a"],"mappings":";;;;;;;;MA4Ba,qBAAoB;AAAA;;AAChC,+BAAiD,CAAA;AAAA;AAAA,EAMjD,WAAW,MAA0C;AACpD,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,EAOA,WACC,mBAGA,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,KAAK,MAAM;AACP,mCAAA;AACzB;AAAA,QACD;AAAA,MAAA,WACU,KAAK,SAAS,SAAS;AAEhC,YAAA,SAAS,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AAA,EASQ,WACP,MAA2C;AAE3C,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AAxJA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RTInlineNode, RTNode, RichTextNodeTypes } from "@prismicio/client";
|
|
1
|
+
import type { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextNodeTypes } from "@prismicio/client";
|
|
2
2
|
import type { Element } from "hast";
|
|
3
3
|
import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder";
|
|
4
4
|
/**
|
|
@@ -43,7 +43,7 @@ type RichTextHTMLMapSerializerFunctionPayload = {
|
|
|
43
43
|
* available as an escape hatch. Prefer returning a
|
|
44
44
|
* {@link RichTextHTMLMapSerializerShorthand} instead.
|
|
45
45
|
*/
|
|
46
|
-
export type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined;
|
|
46
|
+
export type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode> | undefined;
|
|
47
47
|
/**
|
|
48
48
|
* Serializes a hast {@link Element} node matching the given HTML tag name or CSS
|
|
49
49
|
* selector to a Prismic rich text node.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/migrate",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Set of helpers to migrate your data to Prismic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -53,35 +53,35 @@
|
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"hast": "^1.0.0",
|
|
55
55
|
"hast-util-select": "^6.0.2",
|
|
56
|
-
"hast-util-to-html": "^9.0.
|
|
56
|
+
"hast-util-to-html": "^9.0.3",
|
|
57
57
|
"hast-util-to-string": "^3.0.0",
|
|
58
|
-
"rehype-minify-whitespace": "^6.0.
|
|
58
|
+
"rehype-minify-whitespace": "^6.0.1",
|
|
59
59
|
"rehype-parse": "^9.0.0",
|
|
60
60
|
"unified": "^11.0.5",
|
|
61
61
|
"unist-util-remove": "^4.0.0",
|
|
62
62
|
"unist-util-visit": "^5.0.0",
|
|
63
|
-
"vfile": "^6.0.
|
|
63
|
+
"vfile": "^6.0.3"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@prismicio/client": "^7.
|
|
67
|
-
"@size-limit/preset-small-lib": "^11.1.
|
|
66
|
+
"@prismicio/client": "^7.10.1",
|
|
67
|
+
"@size-limit/preset-small-lib": "^11.1.5",
|
|
68
68
|
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
|
|
69
|
-
"@types/node": "^
|
|
69
|
+
"@types/node": "^22.6.1",
|
|
70
70
|
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
|
71
71
|
"@typescript-eslint/parser": "^6.21.0",
|
|
72
|
-
"@vitest/coverage-v8": "^2.
|
|
73
|
-
"eslint": "^8.57.
|
|
72
|
+
"@vitest/coverage-v8": "^2.1.1",
|
|
73
|
+
"eslint": "^8.57.1",
|
|
74
74
|
"eslint-config-prettier": "^9.1.0",
|
|
75
75
|
"eslint-plugin-prettier": "^5.2.1",
|
|
76
76
|
"eslint-plugin-tsdoc": "^0.3.0",
|
|
77
77
|
"prettier": "^3.3.3",
|
|
78
78
|
"prettier-plugin-jsdoc": "^1.3.0",
|
|
79
|
-
"size-limit": "^11.1.
|
|
79
|
+
"size-limit": "^11.1.5",
|
|
80
80
|
"standard-version": "^9.5.0",
|
|
81
|
-
"typescript": "^5.
|
|
82
|
-
"vite": "^5.
|
|
81
|
+
"typescript": "^5.6.2",
|
|
82
|
+
"vite": "^5.4.7",
|
|
83
83
|
"vite-plugin-sdk": "^0.1.2",
|
|
84
|
-
"vitest": "^2.
|
|
84
|
+
"vitest": "^2.1.1"
|
|
85
85
|
},
|
|
86
86
|
"peerDependencies": {
|
|
87
87
|
"@prismicio/client": "^7"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
InjectMigrationSpecificTypes,
|
|
2
3
|
RTInlineNode,
|
|
3
4
|
RTLinkNode,
|
|
4
5
|
RTNode,
|
|
@@ -26,13 +27,13 @@ type DistributedOmit<
|
|
|
26
27
|
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">
|
|
27
28
|
|
|
28
29
|
export class RichTextFieldBuilder {
|
|
29
|
-
#nodes: RTNode[] = []
|
|
30
|
+
#nodes: InjectMigrationSpecificTypes<RTNode>[] = []
|
|
30
31
|
|
|
31
|
-
get #last(): RTNode | undefined {
|
|
32
|
+
get #last(): InjectMigrationSpecificTypes<RTNode> | undefined {
|
|
32
33
|
return this.#nodes[this.#nodes.length - 1]
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
appendNode(node: RTNode): void {
|
|
36
|
+
appendNode(node: InjectMigrationSpecificTypes<RTNode>): void {
|
|
36
37
|
this.cleanupLast()
|
|
37
38
|
|
|
38
39
|
this.#nodes.push(node)
|
|
@@ -50,17 +51,22 @@ export class RichTextFieldBuilder {
|
|
|
50
51
|
})
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
appendSpan(span: RTInlineNode): void
|
|
54
|
-
appendSpan(partialSpan: RTPartialInlineNode, length: number): void
|
|
54
|
+
appendSpan(span: InjectMigrationSpecificTypes<RTInlineNode>): void
|
|
55
55
|
appendSpan(
|
|
56
|
-
|
|
56
|
+
partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>,
|
|
57
|
+
length: number,
|
|
58
|
+
): void
|
|
59
|
+
appendSpan(
|
|
60
|
+
spanOrPartialSpan: InjectMigrationSpecificTypes<
|
|
61
|
+
RTInlineNode | RTPartialInlineNode
|
|
62
|
+
>,
|
|
57
63
|
length?: number,
|
|
58
64
|
): void {
|
|
59
65
|
if (!this.isTextNode(this.#last)) {
|
|
60
66
|
throw new PrismicRichTextError("Cannot add span to non-text last node")
|
|
61
67
|
}
|
|
62
68
|
|
|
63
|
-
let span: RTInlineNode
|
|
69
|
+
let span: InjectMigrationSpecificTypes<RTInlineNode>
|
|
64
70
|
if ("start" in spanOrPartialSpan && "end" in spanOrPartialSpan) {
|
|
65
71
|
span = spanOrPartialSpan
|
|
66
72
|
} else {
|
|
@@ -95,9 +101,19 @@ export class RichTextFieldBuilder {
|
|
|
95
101
|
// Check that all data fields are the same. Since it's not a
|
|
96
102
|
// deep equal operation, this doesn't guarantee a perfect
|
|
97
103
|
// match with content relationship.
|
|
98
|
-
const isSameLink =
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
const isSameLink =
|
|
105
|
+
typeof span.data === "object" && typeof lastSpan.data === "object"
|
|
106
|
+
? (
|
|
107
|
+
Object.entries(span.data) as [
|
|
108
|
+
keyof RTLinkNode["data"],
|
|
109
|
+
unknown,
|
|
110
|
+
][]
|
|
111
|
+
).every(
|
|
112
|
+
([key, value]) =>
|
|
113
|
+
lastSpan.data?.[key as keyof (typeof lastSpan)["data"]] ===
|
|
114
|
+
value,
|
|
115
|
+
)
|
|
116
|
+
: span.data === lastSpan.data
|
|
101
117
|
|
|
102
118
|
if (isSameLink) {
|
|
103
119
|
lastIdenticalSpanIndex = i
|
|
@@ -158,7 +174,9 @@ export class RichTextFieldBuilder {
|
|
|
158
174
|
*
|
|
159
175
|
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
160
176
|
*/
|
|
161
|
-
private isTextNode(
|
|
177
|
+
private isTextNode(
|
|
178
|
+
node?: InjectMigrationSpecificTypes<RTNode>,
|
|
179
|
+
): node is InjectMigrationSpecificTypes<RTTextNode> {
|
|
162
180
|
return !!node && node.type !== "image" && node.type !== "embed"
|
|
163
181
|
}
|
|
164
182
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
InjectMigrationSpecificTypes,
|
|
3
|
+
RTInlineNode,
|
|
4
|
+
RTNode,
|
|
5
|
+
RichTextNodeTypes,
|
|
6
|
+
} from "@prismicio/client"
|
|
2
7
|
import type { Element } from "hast"
|
|
3
8
|
|
|
4
9
|
import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder"
|
|
@@ -52,9 +57,7 @@ export type RichTextHTMLMapSerializerFunction = (
|
|
|
52
57
|
payload: RichTextHTMLMapSerializerFunctionPayload,
|
|
53
58
|
) =>
|
|
54
59
|
| RichTextHTMLMapSerializerShorthand
|
|
55
|
-
| RTNode
|
|
56
|
-
| RTInlineNode
|
|
57
|
-
| RTPartialInlineNode
|
|
60
|
+
| InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode>
|
|
58
61
|
| undefined
|
|
59
62
|
|
|
60
63
|
/**
|