@prismicio/migrate 0.0.1 → 0.0.2-canary.2da5f89
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/errors/PrismicRichTextError.cjs +5 -5
- package/dist/errors/PrismicRichTextError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextError.d.cts +4 -0
- package/dist/errors/PrismicRichTextError.d.cts.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +4 -2
- package/dist/errors/PrismicRichTextError.d.ts.map +1 -0
- package/dist/errors/PrismicRichTextError.js +6 -6
- package/dist/errors/PrismicRichTextError.js.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.cjs +6 -6
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.d.cts +5 -0
- package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +5 -3
- package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.js +6 -6
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
- package/dist/htmlAsRichText.cjs +24 -8
- package/dist/htmlAsRichText.cjs.map +1 -1
- package/dist/htmlAsRichText.d.cts +22 -0
- package/dist/htmlAsRichText.d.cts.map +1 -0
- package/dist/htmlAsRichText.d.ts +11 -67
- package/dist/htmlAsRichText.d.ts.map +1 -0
- package/dist/htmlAsRichText.js +21 -7
- package/dist/htmlAsRichText.js.map +1 -1
- package/dist/index.cjs +6 -8
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +2 -7
- package/dist/lib/RichTextFieldBuilder.cjs +82 -113
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.cts +14 -0
- package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +5 -26
- package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -0
- package/dist/lib/RichTextFieldBuilder.js +82 -113
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +81 -78
- package/dist/lib/filterRichTextField.cjs.map +1 -1
- package/dist/lib/filterRichTextField.js +82 -79
- package/dist/lib/filterRichTextField.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs +101 -86
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
- package/dist/lib/hastSerializerHelpers.js +101 -88
- package/dist/lib/hastSerializerHelpers.js.map +1 -1
- package/dist/lib/hastToRichText.cjs +151 -157
- package/dist/lib/hastToRichText.cjs.map +1 -1
- package/dist/lib/hastToRichText.d.cts +21 -0
- package/dist/lib/hastToRichText.d.cts.map +1 -0
- package/dist/lib/hastToRichText.d.ts +19 -28
- package/dist/lib/hastToRichText.d.ts.map +1 -0
- package/dist/lib/hastToRichText.js +150 -156
- package/dist/lib/hastToRichText.js.map +1 -1
- package/dist/lib/rehypeRichText.cjs +57 -57
- package/dist/lib/rehypeRichText.cjs.map +1 -1
- package/dist/lib/rehypeRichText.d.cts +58 -0
- package/dist/lib/rehypeRichText.d.cts.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +52 -59
- package/dist/lib/rehypeRichText.d.ts.map +1 -0
- package/dist/lib/rehypeRichText.js +53 -55
- package/dist/lib/rehypeRichText.js.map +1 -1
- package/dist/types.d.cts +47 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +29 -39
- package/dist/types.d.ts.map +1 -0
- package/package.json +40 -63
- package/src/htmlAsRichText.ts +22 -49
- package/src/lib/RichTextFieldBuilder.ts +26 -31
- package/src/lib/filterRichTextField.ts +6 -25
- package/src/lib/hastSerializerHelpers.ts +4 -17
- package/src/lib/hastToRichText.ts +17 -39
- package/src/lib/rehypeRichText.ts +88 -102
- package/src/types.ts +24 -33
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/filterRichTextField.d.ts +0 -10
- package/dist/lib/hastSerializerHelpers.d.ts +0 -28
|
@@ -1,115 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (__privateGet(this, _RichTextFieldBuilder_instances, last_get).text) {
|
|
81
|
-
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text += text;
|
|
82
|
-
} else {
|
|
83
|
-
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = text.trimStart();
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
build() {
|
|
87
|
-
this.cleanupLast();
|
|
88
|
-
return __privateGet(this, _nodes);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Cleans up the current last node.
|
|
92
|
-
*/
|
|
93
|
-
cleanupLast() {
|
|
94
|
-
if (this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
95
|
-
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = __privateGet(this, _RichTextFieldBuilder_instances, last_get).text.trimEnd();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Determines if a node is a text node.
|
|
100
|
-
*
|
|
101
|
-
* @param node - rich text node to check.
|
|
102
|
-
*
|
|
103
|
-
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
104
|
-
*/
|
|
105
|
-
isTextNode(node) {
|
|
106
|
-
return !!node && node.type !== "image" && node.type !== "embed";
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
_nodes = new WeakMap();
|
|
110
|
-
_RichTextFieldBuilder_instances = new WeakSet();
|
|
111
|
-
last_get = function() {
|
|
112
|
-
return __privateGet(this, _nodes)[__privateGet(this, _nodes).length - 1];
|
|
1
|
+
const require_PrismicRichTextError = require("../errors/PrismicRichTextError.cjs");
|
|
2
|
+
//#region src/lib/RichTextFieldBuilder.ts
|
|
3
|
+
var RichTextFieldBuilder = class {
|
|
4
|
+
#nodes = [];
|
|
5
|
+
get #last() {
|
|
6
|
+
return this.#nodes[this.#nodes.length - 1];
|
|
7
|
+
}
|
|
8
|
+
appendNode(node) {
|
|
9
|
+
this.cleanupLast();
|
|
10
|
+
this.#nodes.push(node);
|
|
11
|
+
}
|
|
12
|
+
appendTextNode(type, direction = "ltr") {
|
|
13
|
+
this.appendNode({
|
|
14
|
+
type,
|
|
15
|
+
text: "",
|
|
16
|
+
spans: [],
|
|
17
|
+
direction
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
appendSpan(spanOrPartialSpan, length) {
|
|
21
|
+
if (!this.isTextNode(this.#last)) throw new require_PrismicRichTextError.PrismicRichTextError("Cannot add span to non-text last node");
|
|
22
|
+
let span;
|
|
23
|
+
if ("start" in spanOrPartialSpan && "end" in spanOrPartialSpan) span = spanOrPartialSpan;
|
|
24
|
+
else {
|
|
25
|
+
const lastLength = this.#last.text.length;
|
|
26
|
+
span = {
|
|
27
|
+
...spanOrPartialSpan,
|
|
28
|
+
start: lastLength,
|
|
29
|
+
end: lastLength + length
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
let lastIdenticalSpanIndex = -1;
|
|
33
|
+
for (let i = this.#last.spans.length - 1; i >= 0; i--) {
|
|
34
|
+
const lastSpan = this.#last.spans[i];
|
|
35
|
+
if (span.type === "strong" || span.type === "em") {
|
|
36
|
+
if (lastSpan.type === span.type) {
|
|
37
|
+
lastIdenticalSpanIndex = i;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
} else if (span.type === "label") {
|
|
41
|
+
if (lastSpan.type === "label" && lastSpan.data.label === span.data.label) {
|
|
42
|
+
lastIdenticalSpanIndex = i;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
} else if (span.type === "hyperlink") {
|
|
46
|
+
if (lastSpan.type === "hyperlink") {
|
|
47
|
+
if (typeof span.data === "object" && typeof lastSpan.data === "object" ? Object.entries(span.data).every(([key, value]) => lastSpan.data?.[key] === value) : span.data === lastSpan.data) {
|
|
48
|
+
lastIdenticalSpanIndex = i;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (span.start === span.end) return;
|
|
55
|
+
if (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) this.#last.spans[lastIdenticalSpanIndex].end = span.end;
|
|
56
|
+
else this.#last.spans.push(span);
|
|
57
|
+
}
|
|
58
|
+
appendText(text) {
|
|
59
|
+
if (!this.isTextNode(this.#last)) throw new require_PrismicRichTextError.PrismicRichTextError("Cannot append text to non-text last node");
|
|
60
|
+
if (this.#last.text) this.#last.text += text;
|
|
61
|
+
else this.#last.text = text.trimStart();
|
|
62
|
+
}
|
|
63
|
+
build() {
|
|
64
|
+
this.cleanupLast();
|
|
65
|
+
return this.#nodes;
|
|
66
|
+
}
|
|
67
|
+
/** Cleans up the current last node. */
|
|
68
|
+
cleanupLast() {
|
|
69
|
+
if (this.isTextNode(this.#last)) this.#last.text = this.#last.text.trimEnd();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Determines if a node is a text node.
|
|
73
|
+
*
|
|
74
|
+
* @param node - Rich text node to check.
|
|
75
|
+
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
76
|
+
*/
|
|
77
|
+
isTextNode(node) {
|
|
78
|
+
return !!node && node.type !== "image" && node.type !== "embed";
|
|
79
|
+
}
|
|
113
80
|
};
|
|
81
|
+
//#endregion
|
|
114
82
|
exports.RichTextFieldBuilder = RichTextFieldBuilder;
|
|
115
|
-
|
|
83
|
+
|
|
84
|
+
//# sourceMappingURL=RichTextFieldBuilder.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.cjs","sources":["
|
|
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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextField } from "@prismicio/client";
|
|
2
|
+
//#region src/lib/RichTextFieldBuilder.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Omits keys from a type, distributing the operation over a union.
|
|
5
|
+
*
|
|
6
|
+
* Taken from the `type-fest` package.
|
|
7
|
+
*
|
|
8
|
+
* @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts
|
|
9
|
+
*/
|
|
10
|
+
type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;
|
|
11
|
+
/** An inline node without its `start` and `end` properties. */
|
|
12
|
+
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
13
|
+
//#endregion
|
|
14
|
+
//# sourceMappingURL=RichTextFieldBuilder.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.d.cts","names":[],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"mappings":";;;;;;;;;KAkBK,gBAAgB,YAAY,sBAAsB,cAAc,6BAClE,KAAK,YAAY;;YAIR,sBAAsB,gBAAgB"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextField } from "@prismicio/client";
|
|
2
|
+
//#region src/lib/RichTextFieldBuilder.d.ts
|
|
2
3
|
/**
|
|
3
4
|
* Omits keys from a type, distributing the operation over a union.
|
|
4
5
|
*
|
|
@@ -7,29 +8,7 @@ import type { RTInlineNode, RTNode, RTTextNode, RichTextField } from "@prismicio
|
|
|
7
8
|
* @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts
|
|
8
9
|
*/
|
|
9
10
|
type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;
|
|
10
|
-
/**
|
|
11
|
-
* An inline node without its `start` and `end` properties.
|
|
12
|
-
*/
|
|
11
|
+
/** An inline node without its `start` and `end` properties. */
|
|
13
12
|
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
appendNode(node: RTNode): void;
|
|
17
|
-
appendTextNode(type: RTTextNode["type"], direction?: "ltr" | "rtl"): void;
|
|
18
|
-
appendSpan(span: RTInlineNode): void;
|
|
19
|
-
appendSpan(partialSpan: RTPartialInlineNode, length: number): void;
|
|
20
|
-
appendText(text: string): void;
|
|
21
|
-
build(): RichTextField;
|
|
22
|
-
/**
|
|
23
|
-
* Cleans up the current last node.
|
|
24
|
-
*/
|
|
25
|
-
private cleanupLast;
|
|
26
|
-
/**
|
|
27
|
-
* Determines if a node is a text node.
|
|
28
|
-
*
|
|
29
|
-
* @param node - rich text node to check.
|
|
30
|
-
*
|
|
31
|
-
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
32
|
-
*/
|
|
33
|
-
private isTextNode;
|
|
34
|
-
}
|
|
35
|
-
export {};
|
|
13
|
+
//#endregion
|
|
14
|
+
//# sourceMappingURL=RichTextFieldBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.d.ts","names":[],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"mappings":";;;;;;;;;KAkBK,gBAAgB,YAAY,sBAAsB,cAAc,6BAClE,KAAK,YAAY;;YAIR,sBAAsB,gBAAgB"}
|
|
@@ -1,115 +1,84 @@
|
|
|
1
|
-
var __typeError = (msg) => {
|
|
2
|
-
throw TypeError(msg);
|
|
3
|
-
};
|
|
4
|
-
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
-
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
|
-
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
|
-
var _nodes, _RichTextFieldBuilder_instances, last_get;
|
|
8
1
|
import { PrismicRichTextError } from "../errors/PrismicRichTextError.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Cleans up the current last node.
|
|
90
|
-
*/
|
|
91
|
-
cleanupLast() {
|
|
92
|
-
if (this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
93
|
-
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = __privateGet(this, _RichTextFieldBuilder_instances, last_get).text.trimEnd();
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Determines if a node is a text node.
|
|
98
|
-
*
|
|
99
|
-
* @param node - rich text node to check.
|
|
100
|
-
*
|
|
101
|
-
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
102
|
-
*/
|
|
103
|
-
isTextNode(node) {
|
|
104
|
-
return !!node && node.type !== "image" && node.type !== "embed";
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
_nodes = new WeakMap();
|
|
108
|
-
_RichTextFieldBuilder_instances = new WeakSet();
|
|
109
|
-
last_get = function() {
|
|
110
|
-
return __privateGet(this, _nodes)[__privateGet(this, _nodes).length - 1];
|
|
111
|
-
};
|
|
112
|
-
export {
|
|
113
|
-
RichTextFieldBuilder
|
|
2
|
+
//#region src/lib/RichTextFieldBuilder.ts
|
|
3
|
+
var RichTextFieldBuilder = class {
|
|
4
|
+
#nodes = [];
|
|
5
|
+
get #last() {
|
|
6
|
+
return this.#nodes[this.#nodes.length - 1];
|
|
7
|
+
}
|
|
8
|
+
appendNode(node) {
|
|
9
|
+
this.cleanupLast();
|
|
10
|
+
this.#nodes.push(node);
|
|
11
|
+
}
|
|
12
|
+
appendTextNode(type, direction = "ltr") {
|
|
13
|
+
this.appendNode({
|
|
14
|
+
type,
|
|
15
|
+
text: "",
|
|
16
|
+
spans: [],
|
|
17
|
+
direction
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
appendSpan(spanOrPartialSpan, length) {
|
|
21
|
+
if (!this.isTextNode(this.#last)) throw new PrismicRichTextError("Cannot add span to non-text last node");
|
|
22
|
+
let span;
|
|
23
|
+
if ("start" in spanOrPartialSpan && "end" in spanOrPartialSpan) span = spanOrPartialSpan;
|
|
24
|
+
else {
|
|
25
|
+
const lastLength = this.#last.text.length;
|
|
26
|
+
span = {
|
|
27
|
+
...spanOrPartialSpan,
|
|
28
|
+
start: lastLength,
|
|
29
|
+
end: lastLength + length
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
let lastIdenticalSpanIndex = -1;
|
|
33
|
+
for (let i = this.#last.spans.length - 1; i >= 0; i--) {
|
|
34
|
+
const lastSpan = this.#last.spans[i];
|
|
35
|
+
if (span.type === "strong" || span.type === "em") {
|
|
36
|
+
if (lastSpan.type === span.type) {
|
|
37
|
+
lastIdenticalSpanIndex = i;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
} else if (span.type === "label") {
|
|
41
|
+
if (lastSpan.type === "label" && lastSpan.data.label === span.data.label) {
|
|
42
|
+
lastIdenticalSpanIndex = i;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
} else if (span.type === "hyperlink") {
|
|
46
|
+
if (lastSpan.type === "hyperlink") {
|
|
47
|
+
if (typeof span.data === "object" && typeof lastSpan.data === "object" ? Object.entries(span.data).every(([key, value]) => lastSpan.data?.[key] === value) : span.data === lastSpan.data) {
|
|
48
|
+
lastIdenticalSpanIndex = i;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (span.start === span.end) return;
|
|
55
|
+
if (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) this.#last.spans[lastIdenticalSpanIndex].end = span.end;
|
|
56
|
+
else this.#last.spans.push(span);
|
|
57
|
+
}
|
|
58
|
+
appendText(text) {
|
|
59
|
+
if (!this.isTextNode(this.#last)) throw new PrismicRichTextError("Cannot append text to non-text last node");
|
|
60
|
+
if (this.#last.text) this.#last.text += text;
|
|
61
|
+
else this.#last.text = text.trimStart();
|
|
62
|
+
}
|
|
63
|
+
build() {
|
|
64
|
+
this.cleanupLast();
|
|
65
|
+
return this.#nodes;
|
|
66
|
+
}
|
|
67
|
+
/** Cleans up the current last node. */
|
|
68
|
+
cleanupLast() {
|
|
69
|
+
if (this.isTextNode(this.#last)) this.#last.text = this.#last.text.trimEnd();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Determines if a node is a text node.
|
|
73
|
+
*
|
|
74
|
+
* @param node - Rich text node to check.
|
|
75
|
+
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
76
|
+
*/
|
|
77
|
+
isTextNode(node) {
|
|
78
|
+
return !!node && node.type !== "image" && node.type !== "embed";
|
|
79
|
+
}
|
|
114
80
|
};
|
|
115
|
-
//#
|
|
81
|
+
//#endregion
|
|
82
|
+
export { RichTextFieldBuilder };
|
|
83
|
+
|
|
84
|
+
//# sourceMappingURL=RichTextFieldBuilder.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.js","sources":["
|
|
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,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,IAAI,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,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"}
|