@prismicio/migrate 0.0.2-canary.3e6d6e6 → 0.0.2-canary.701c2e5
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 +1 -2
- package/dist/errors/PrismicRichTextError.cjs +1 -2
- package/dist/errors/PrismicRichTextError.d.cts +1 -4
- package/dist/errors/PrismicRichTextError.d.cts.map +1 -1
- package/dist/errors/PrismicRichTextError.d.ts +1 -4
- package/dist/errors/PrismicRichTextError.d.ts.map +1 -1
- package/dist/errors/PrismicRichTextError.js +1 -2
- package/dist/errors/PrismicRichTextSerializerError.cjs +1 -2
- package/dist/errors/PrismicRichTextSerializerError.d.cts +1 -4
- package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.d.ts +1 -4
- package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.js +1 -2
- package/dist/htmlAsRichText.cjs +1 -2
- package/dist/htmlAsRichText.d.cts +5 -8
- package/dist/htmlAsRichText.d.cts.map +1 -1
- package/dist/htmlAsRichText.d.ts +6 -8
- package/dist/htmlAsRichText.d.ts.map +1 -1
- package/dist/htmlAsRichText.js +1 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/lib/RichTextFieldBuilder.cjs +1 -2
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.cts +3 -5
- package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.ts +3 -5
- package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -1
- package/dist/lib/RichTextFieldBuilder.js +1 -2
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +20 -16
- package/dist/lib/filterRichTextField.cjs.map +1 -1
- package/dist/lib/filterRichTextField.js +20 -16
- package/dist/lib/filterRichTextField.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs +1 -2
- package/dist/lib/hastSerializerHelpers.js +1 -2
- package/dist/lib/hastToRichText.cjs +1 -2
- package/dist/lib/hastToRichText.cjs.map +1 -1
- package/dist/lib/hastToRichText.d.cts +1 -7
- package/dist/lib/hastToRichText.d.cts.map +1 -1
- package/dist/lib/hastToRichText.d.ts +3 -6
- package/dist/lib/hastToRichText.d.ts.map +1 -1
- package/dist/lib/hastToRichText.js +1 -2
- package/dist/lib/rehypeRichText.cjs +1 -2
- package/dist/lib/rehypeRichText.cjs.map +1 -1
- package/dist/lib/rehypeRichText.d.cts +1 -6
- package/dist/lib/rehypeRichText.d.cts.map +1 -1
- package/dist/lib/rehypeRichText.d.ts +1 -4
- package/dist/lib/rehypeRichText.d.ts.map +1 -1
- package/dist/lib/rehypeRichText.js +1 -2
- package/dist/types.d.cts +4 -6
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.ts +4 -6
- package/dist/types.d.ts.map +1 -1
- package/package.json +11 -11
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -19,5 +19,4 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
19
19
|
value: mod,
|
|
20
20
|
enumerable: true
|
|
21
21
|
}) : target, mod));
|
|
22
|
-
//#endregion
|
|
23
22
|
exports.__toESM = __toESM;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextError.d.cts","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"PrismicRichTextError.d.cts","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"mappings":"A,qBAAa,6BAA6B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextError.d.ts","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"PrismicRichTextError.d.ts","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"mappings":"A,qBAAa,6BAA6B"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const require_PrismicRichTextError = require("./PrismicRichTextError.cjs");
|
|
2
|
-
|
|
2
|
+
// src/errors/PrismicRichTextSerializerError.ts
|
|
3
3
|
var PrismicRichTextSerializerError = class extends require_PrismicRichTextError.PrismicRichTextError {};
|
|
4
|
-
//#endregion
|
|
5
4
|
exports.PrismicRichTextSerializerError = PrismicRichTextSerializerError;
|
|
6
5
|
|
|
7
6
|
//# sourceMappingURL=PrismicRichTextSerializerError.cjs.map
|
|
@@ -1,6 +1,3 @@
|
|
|
1
1
|
import { PrismicRichTextError } from "./PrismicRichTextError.cjs";
|
|
2
|
-
|
|
3
|
-
declare class PrismicRichTextSerializerError extends PrismicRichTextError {}
|
|
4
|
-
//#endregion
|
|
5
|
-
export { PrismicRichTextSerializerError };
|
|
2
|
+
export declare class PrismicRichTextSerializerError extends PrismicRichTextError {}
|
|
6
3
|
//# sourceMappingURL=PrismicRichTextSerializerError.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextSerializerError.d.cts","names":[],"sources":["../../src/errors/PrismicRichTextSerializerError.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"PrismicRichTextSerializerError.d.cts","names":[],"sources":["../../src/errors/PrismicRichTextSerializerError.ts"],"mappings":";qBAEa,uCAAuC"}
|
|
@@ -1,6 +1,3 @@
|
|
|
1
1
|
import { PrismicRichTextError } from "./PrismicRichTextError.js";
|
|
2
|
-
|
|
3
|
-
declare class PrismicRichTextSerializerError extends PrismicRichTextError {}
|
|
4
|
-
//#endregion
|
|
5
|
-
export { PrismicRichTextSerializerError };
|
|
2
|
+
export declare class PrismicRichTextSerializerError extends PrismicRichTextError {}
|
|
6
3
|
//# sourceMappingURL=PrismicRichTextSerializerError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextSerializerError.d.ts","names":[],"sources":["../../src/errors/PrismicRichTextSerializerError.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"PrismicRichTextSerializerError.d.ts","names":[],"sources":["../../src/errors/PrismicRichTextSerializerError.ts"],"mappings":";qBAEa,uCAAuC"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { PrismicRichTextError } from "./PrismicRichTextError.js";
|
|
2
|
-
|
|
2
|
+
// src/errors/PrismicRichTextSerializerError.ts
|
|
3
3
|
var PrismicRichTextSerializerError = class extends PrismicRichTextError {};
|
|
4
|
-
//#endregion
|
|
5
4
|
export { PrismicRichTextSerializerError };
|
|
6
5
|
|
|
7
6
|
//# sourceMappingURL=PrismicRichTextSerializerError.js.map
|
package/dist/htmlAsRichText.cjs
CHANGED
|
@@ -3,7 +3,7 @@ const require_rehypeRichText = require("./lib/rehypeRichText.cjs");
|
|
|
3
3
|
let rehype_parse = require("rehype-parse");
|
|
4
4
|
rehype_parse = require_runtime.__toESM(rehype_parse, 1);
|
|
5
5
|
let unified = require("unified");
|
|
6
|
-
|
|
6
|
+
// src/htmlAsRichText.ts
|
|
7
7
|
/**
|
|
8
8
|
* Converts an HTML string to a rich text field.
|
|
9
9
|
*
|
|
@@ -21,7 +21,6 @@ const htmlAsRichText = (html, config) => {
|
|
|
21
21
|
warnings: messages.map((message) => message.toString())
|
|
22
22
|
};
|
|
23
23
|
};
|
|
24
|
-
//#endregion
|
|
25
24
|
exports.htmlAsRichText = htmlAsRichText;
|
|
26
25
|
|
|
27
26
|
//# sourceMappingURL=htmlAsRichText.cjs.map
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import "./lib/RichTextFieldBuilder.cjs";
|
|
2
1
|
import { RehypeRichTextConfig } from "./lib/rehypeRichText.cjs";
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
//#region src/htmlAsRichText.d.ts
|
|
2
|
+
import "./lib/RichTextFieldBuilder.cjs";
|
|
3
|
+
import { RichTextField } from "@prismicio/client";
|
|
6
4
|
/** Configuration that determines the output of {@link htmlAsRichText}. */
|
|
7
|
-
type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
5
|
+
export type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
8
6
|
/** The return type of {@link htmlAsRichText}. */
|
|
9
7
|
type HTMLAsRichTextReturnType = {
|
|
10
8
|
result: RichTextField;
|
|
@@ -17,7 +15,6 @@ type HTMLAsRichTextReturnType = {
|
|
|
17
15
|
* @param config - Configuration that determines the output the function.
|
|
18
16
|
* @returns `html` as rich text.
|
|
19
17
|
*/
|
|
20
|
-
declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
21
|
-
|
|
22
|
-
export { HTMLAsRichTextConfig, htmlAsRichText };
|
|
18
|
+
export declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
19
|
+
export {};
|
|
23
20
|
//# sourceMappingURL=htmlAsRichText.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"htmlAsRichText.d.cts","names":[],"sources":["../src/htmlAsRichText.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"htmlAsRichText.d.cts","names":[],"sources":["../src/htmlAsRichText.ts"],"mappings":";;;;YAgEY,uBAAuB;;KAG9B;EACJ,QAAQ;EACR;;;;;;;;;qBAUY,iBAAc,cACd,SACH,yBACP"}
|
package/dist/htmlAsRichText.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import "./lib/RichTextFieldBuilder.js";
|
|
2
1
|
import { RehypeRichTextConfig } from "./lib/rehypeRichText.js";
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
2
|
+
import "./lib/RichTextFieldBuilder.js";
|
|
3
|
+
import { RichTextField } from "@prismicio/client";
|
|
4
|
+
import "hast";
|
|
6
5
|
/** Configuration that determines the output of {@link htmlAsRichText}. */
|
|
7
|
-
type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
6
|
+
export type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
8
7
|
/** The return type of {@link htmlAsRichText}. */
|
|
9
8
|
type HTMLAsRichTextReturnType = {
|
|
10
9
|
result: RichTextField;
|
|
@@ -17,7 +16,6 @@ type HTMLAsRichTextReturnType = {
|
|
|
17
16
|
* @param config - Configuration that determines the output the function.
|
|
18
17
|
* @returns `html` as rich text.
|
|
19
18
|
*/
|
|
20
|
-
declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
21
|
-
|
|
22
|
-
export { HTMLAsRichTextConfig, htmlAsRichText };
|
|
19
|
+
export declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
20
|
+
export {};
|
|
23
21
|
//# sourceMappingURL=htmlAsRichText.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"htmlAsRichText.d.ts","names":[],"sources":["../src/htmlAsRichText.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"htmlAsRichText.d.ts","names":[],"sources":["../src/htmlAsRichText.ts"],"mappings":";;;;;YAgEY,uBAAuB;;KAG9B;EACJ,QAAQ;EACR;;;;;;;;;qBAUY,iBAAc,cACd,SACH,yBACP"}
|
package/dist/htmlAsRichText.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { rehypeRichText } from "./lib/rehypeRichText.js";
|
|
2
2
|
import rehypeParse from "rehype-parse";
|
|
3
3
|
import { unified } from "unified";
|
|
4
|
-
|
|
4
|
+
// src/htmlAsRichText.ts
|
|
5
5
|
/**
|
|
6
6
|
* Converts an HTML string to a rich text field.
|
|
7
7
|
*
|
|
@@ -19,7 +19,6 @@ const htmlAsRichText = (html, config) => {
|
|
|
19
19
|
warnings: messages.map((message) => message.toString())
|
|
20
20
|
};
|
|
21
21
|
};
|
|
22
|
-
//#endregion
|
|
23
22
|
export { htmlAsRichText };
|
|
24
23
|
|
|
25
24
|
//# sourceMappingURL=htmlAsRichText.js.map
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_htmlAsRichText = require("./htmlAsRichText.cjs");
|
|
2
3
|
const require_PrismicRichTextError = require("./errors/PrismicRichTextError.cjs");
|
|
3
4
|
const require_PrismicRichTextSerializerError = require("./errors/PrismicRichTextSerializerError.cjs");
|
|
4
|
-
const require_htmlAsRichText = require("./htmlAsRichText.cjs");
|
|
5
5
|
exports.PrismicRichTextError = require_PrismicRichTextError.PrismicRichTextError;
|
|
6
6
|
exports.PrismicRichTextSerializerError = require_PrismicRichTextSerializerError.PrismicRichTextSerializerError;
|
|
7
7
|
exports.htmlAsRichText = require_htmlAsRichText.htmlAsRichText;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RichTextHTMLMapSerializer } from "./types.cjs";
|
|
2
1
|
import { htmlAsRichText } from "./htmlAsRichText.cjs";
|
|
2
|
+
import { RichTextHTMLMapSerializer } from "./types.cjs";
|
|
3
3
|
import { PrismicRichTextError } from "./errors/PrismicRichTextError.cjs";
|
|
4
4
|
import { PrismicRichTextSerializerError } from "./errors/PrismicRichTextSerializerError.cjs";
|
|
5
5
|
export { PrismicRichTextError, PrismicRichTextSerializerError, type RichTextHTMLMapSerializer, htmlAsRichText };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { RichTextHTMLMapSerializer } from "./types.js";
|
|
2
1
|
import { htmlAsRichText } from "./htmlAsRichText.js";
|
|
2
|
+
import { RichTextHTMLMapSerializer } from "./types.js";
|
|
3
3
|
import { PrismicRichTextError } from "./errors/PrismicRichTextError.js";
|
|
4
4
|
import { PrismicRichTextSerializerError } from "./errors/PrismicRichTextSerializerError.js";
|
|
5
5
|
export { PrismicRichTextError, PrismicRichTextSerializerError, type RichTextHTMLMapSerializer, htmlAsRichText };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
+
import { htmlAsRichText } from "./htmlAsRichText.js";
|
|
1
2
|
import { PrismicRichTextError } from "./errors/PrismicRichTextError.js";
|
|
2
3
|
import { PrismicRichTextSerializerError } from "./errors/PrismicRichTextSerializerError.js";
|
|
3
|
-
import { htmlAsRichText } from "./htmlAsRichText.js";
|
|
4
4
|
export { PrismicRichTextError, PrismicRichTextSerializerError, htmlAsRichText };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const require_PrismicRichTextError = require("../errors/PrismicRichTextError.cjs");
|
|
2
|
-
|
|
2
|
+
// src/lib/RichTextFieldBuilder.ts
|
|
3
3
|
var RichTextFieldBuilder = class {
|
|
4
4
|
#nodes = [];
|
|
5
5
|
get #last() {
|
|
@@ -78,7 +78,6 @@ var RichTextFieldBuilder = class {
|
|
|
78
78
|
return !!node && node.type !== "image" && node.type !== "embed";
|
|
79
79
|
}
|
|
80
80
|
};
|
|
81
|
-
//#endregion
|
|
82
81
|
exports.RichTextFieldBuilder = RichTextFieldBuilder;
|
|
83
82
|
|
|
84
83
|
//# sourceMappingURL=RichTextFieldBuilder.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.cjs","names":["
|
|
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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,IAAI,KAAK,WAAW,KAAK,KAAK,GAC7B,KAAK,MAAM,OAAO,KAAK,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQQ,WACP,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/lib/RichTextFieldBuilder.d.ts
|
|
1
|
+
import { RTInlineNode } from "@prismicio/client";
|
|
3
2
|
/**
|
|
4
3
|
* Omits keys from a type, distributing the operation over a union.
|
|
5
4
|
*
|
|
@@ -9,7 +8,6 @@ import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextField } fro
|
|
|
9
8
|
*/
|
|
10
9
|
type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;
|
|
11
10
|
/** An inline node without its `start` and `end` properties. */
|
|
12
|
-
type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
13
|
-
|
|
14
|
-
export { RTPartialInlineNode };
|
|
11
|
+
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
12
|
+
export {};
|
|
15
13
|
//# sourceMappingURL=RichTextFieldBuilder.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.d.cts","names":[],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"mappings":"
|
|
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,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/lib/RichTextFieldBuilder.d.ts
|
|
1
|
+
import { RTInlineNode } from "@prismicio/client";
|
|
3
2
|
/**
|
|
4
3
|
* Omits keys from a type, distributing the operation over a union.
|
|
5
4
|
*
|
|
@@ -9,7 +8,6 @@ import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextField } fro
|
|
|
9
8
|
*/
|
|
10
9
|
type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;
|
|
11
10
|
/** An inline node without its `start` and `end` properties. */
|
|
12
|
-
type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
13
|
-
|
|
14
|
-
export { RTPartialInlineNode };
|
|
11
|
+
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
12
|
+
export {};
|
|
15
13
|
//# sourceMappingURL=RichTextFieldBuilder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.d.ts","names":[],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"mappings":"
|
|
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,5 +1,5 @@
|
|
|
1
1
|
import { PrismicRichTextError } from "../errors/PrismicRichTextError.js";
|
|
2
|
-
|
|
2
|
+
// src/lib/RichTextFieldBuilder.ts
|
|
3
3
|
var RichTextFieldBuilder = class {
|
|
4
4
|
#nodes = [];
|
|
5
5
|
get #last() {
|
|
@@ -78,7 +78,6 @@ var RichTextFieldBuilder = class {
|
|
|
78
78
|
return !!node && node.type !== "image" && node.type !== "embed";
|
|
79
79
|
}
|
|
80
80
|
};
|
|
81
|
-
//#endregion
|
|
82
81
|
export { RichTextFieldBuilder };
|
|
83
82
|
|
|
84
83
|
//# sourceMappingURL=RichTextFieldBuilder.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.js","names":[
|
|
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;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AM,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;;CAGQ,cAAoB;EAC3B,IAAI,KAAK,WAAW,KAAK,KAAK,GAC7B,KAAK,MAAM,OAAO,KAAK,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQQ,WACP,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
let _prismicio_client = require("@prismicio/client");
|
|
2
|
-
|
|
2
|
+
// src/lib/filterRichTextField.ts
|
|
3
3
|
/**
|
|
4
4
|
* Get allowed nodes types from a rich text or title field custom type model.
|
|
5
5
|
*
|
|
@@ -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))
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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,13 +76,14 @@ 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))
|
|
77
|
-
|
|
78
|
-
|
|
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
|
};
|
|
82
|
-
//#endregion
|
|
83
87
|
exports.filterRichTextField = filterRichTextField;
|
|
84
88
|
|
|
85
89
|
//# sourceMappingURL=filterRichTextField.cjs.map
|
|
@@ -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,KAAK;GACR,QAAQ,KAAA;EACT;CACD;CAGD,OAAO;AACR;;;;;;;;;AAUA,MAAM,kBACL,MACA,OACA,qBACgB;CAChB,MAAM,eAA2B;EAChC,GAAG;EACH,OAAO,CAAC;CACT;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;EAC3C,MAAM,OAAO,KAAK,MAAM;EAExB,IAAI,iBAAiB,SAAS,KAAK,IAAI,
|
|
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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 +1,5 @@
|
|
|
1
1
|
import { LinkType, RichTextNodeType } from "@prismicio/client";
|
|
2
|
-
|
|
2
|
+
// src/lib/filterRichTextField.ts
|
|
3
3
|
/**
|
|
4
4
|
* Get allowed nodes types from a rich text or title field custom type model.
|
|
5
5
|
*
|
|
@@ -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))
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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,13 +76,14 @@ 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))
|
|
77
|
-
|
|
78
|
-
|
|
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
|
};
|
|
82
|
-
//#endregion
|
|
83
87
|
export { filterRichTextField };
|
|
84
88
|
|
|
85
89
|
//# sourceMappingURL=filterRichTextField.js.map
|
|
@@ -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,CAAC,MAAM,QAAQ,oBAAoB,KAAK,QAAQ,cAAc,SAAS,KAC1E,OAAO;EACN,GAAG;EACH,QAAQ;GACP,GAAG,KAAK;GACR,QAAQ,KAAA;EACT;CACD;CAGD,OAAO;AACR;;;;;;;;;AAUA,MAAM,kBACL,MACA,OACA,qBACgB;CAChB,MAAM,eAA2B;EAChC,GAAG;EACH,OAAO,CAAC;CACT;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;EAC3C,MAAM,OAAO,KAAK,MAAM;EAExB,IAAI,iBAAiB,SAAS,KAAK,IAAI,
|
|
1
|
+
{"version":3,"file":"filterRichTextField.js","names":[],"sources":["../../src/lib/filterRichTextField.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n\tTitleField,\n} from \"@prismicio/client\"\nimport { LinkType, RichTextNodeType } from \"@prismicio/client\"\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (model: CustomTypeModelRichTextField): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(...(model.config.multi.split(\",\") as RichTextNodeTypes[]))\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(...(model.config.single.split(\",\") as RichTextNodeTypes[]))\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\")\n\t\t}\n\t}\n\n\treturn allowedNodeTypes\n}\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (node: RTImageNode, model: CustomTypeModelRichTextField): RTImageNode => {\n\tif (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn node\n}\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t}\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i]\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode\n}\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = []\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model)\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i]\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model))\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node)\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field\n}\n"],"mappings":";;;;;;;;AAiBA,MAAM,uBAAuB,UAA6D;CACzF,MAAM,mBAAwC,CAAC;CAE/C,IAAI,MAAM,QAAQ;EACjB,IAAI,WAAW,MAAM,UAAU,MAAM,OAAO,OAC3C,iBAAiB,KAAK,GAAI,MAAM,OAAO,MAAM,MAAM,GAAG,CAAyB;OACzE,IAAI,YAAY,MAAM,UAAU,MAAM,OAAO,QACnD,iBAAiB,KAAK,GAAI,MAAM,OAAO,OAAO,MAAM,GAAG,CAAyB;EAGjF,IAAI,MAAM,OAAO,QAAQ,QACxB,iBAAiB,KAAK,OAAO;CAE/B;CAEA,OAAO;AACR;;;;;;;;AASA,MAAM,mBAAmB,MAAmB,UAAqD;CAChG,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,QAAQ,cAAc,SAAS,KAC1E,OAAO;EACN,GAAG;EACH,QAAQ;GACP,GAAG,KAAK;GACR,QAAQ,KAAA;EACT;CACD;CAGD,OAAO;AACR;;;;;;;;;AAUA,MAAM,kBACL,MACA,OACA,qBACgB;CAChB,MAAM,eAA2B;EAChC,GAAG;EACH,OAAO,CAAC;CACT;CAEA,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,MAAM,QAAQ,KAAK;EAC3C,MAAM,OAAO,KAAK,MAAM;EAExB,IAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;GACzC,IAAI,KAAK,SAAS,iBAAiB,WAAW;IAC7C,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,KAAK,cAAc,SAAS,KACvE,aAAa,MAAM,KAAK;KACvB,GAAG;KACH,MAAM;MACL,GAAG,KAAK;MACR,QAAQ,KAAA;KACT;IACD,CAAC;SAED,aAAa,MAAM,KAAK,IAAI;GAE9B,OAAO,IAAI,KAAK,SAAS,iBAAiB,OACrC;QAAA,MAAM,QAAQ,QAAQ,SAAS,KAAK,KAAK,KAAK,GACjD,aAAa,MAAM,KAAK,IAAI;GAAA,OAG7B,aAAa,MAAM,KAAK,IAAI;EAE9B;CACD;CAEA,OAAO;AACR;;;;;;;;AASA,MAAa,uBACZ,eACA,UACW;CACX,MAAM,QAAkB,CAAC;CAEzB,MAAM,mBAAmB,oBAAoB,KAAK;CAElD,KACC,IAAI,IAAI,GAER,KAAK,MAAM,UAAU,WAAW,MAAM,SAAS,cAAc,SAAS,IACtE,KACC;EACD,MAAM,OAAO,cAAc;EAE3B,IAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;GACzC,IAAI,KAAK,SAAS,iBAAiB,OAClC,MAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;QACjC,IAAI,KAAK,SAAS,iBAAiB,OACzC,MAAM,KAAK,IAAI;QAEf,MAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;EAE1D;CACD;CAEA,OAAO;AACR"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const require_PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
|
|
2
2
|
let _prismicio_client = require("@prismicio/client");
|
|
3
3
|
let hast_util_to_html = require("hast-util-to-html");
|
|
4
|
-
|
|
4
|
+
// src/lib/hastSerializerHelpers.ts
|
|
5
5
|
/**
|
|
6
6
|
* Serializes a hast {@link Element} node to a {@link RTImageNode}.
|
|
7
7
|
*
|
|
@@ -102,7 +102,6 @@ const serializeSpan = (node, rtPartialInlineNode) => {
|
|
|
102
102
|
}
|
|
103
103
|
return rtPartialInlineNode;
|
|
104
104
|
};
|
|
105
|
-
//#endregion
|
|
106
105
|
exports.serializeEmbed = serializeEmbed;
|
|
107
106
|
exports.serializeImage = serializeImage;
|
|
108
107
|
exports.serializeSpan = serializeSpan;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError.js";
|
|
2
2
|
import { LinkType, OEmbedType } from "@prismicio/client";
|
|
3
3
|
import { toHtml } from "hast-util-to-html";
|
|
4
|
-
|
|
4
|
+
// src/lib/hastSerializerHelpers.ts
|
|
5
5
|
/**
|
|
6
6
|
* Serializes a hast {@link Element} node to a {@link RTImageNode}.
|
|
7
7
|
*
|
|
@@ -102,7 +102,6 @@ const serializeSpan = (node, rtPartialInlineNode) => {
|
|
|
102
102
|
}
|
|
103
103
|
return rtPartialInlineNode;
|
|
104
104
|
};
|
|
105
|
-
//#endregion
|
|
106
105
|
export { serializeEmbed, serializeImage, serializeSpan };
|
|
107
106
|
|
|
108
107
|
//# sourceMappingURL=hastSerializerHelpers.js.map
|
|
@@ -4,7 +4,7 @@ const require_RichTextFieldBuilder = require("./RichTextFieldBuilder.cjs");
|
|
|
4
4
|
let unist_util_visit = require("unist-util-visit");
|
|
5
5
|
let _prismicio_client = require("@prismicio/client");
|
|
6
6
|
let hast_util_to_string = require("hast-util-to-string");
|
|
7
|
-
|
|
7
|
+
// src/lib/hastToRichText.ts
|
|
8
8
|
const DEFAULT_SERIALIZER = {
|
|
9
9
|
h1: "heading1",
|
|
10
10
|
h2: "heading2",
|
|
@@ -152,7 +152,6 @@ const hastToRichText = (tree, file, config) => {
|
|
|
152
152
|
});
|
|
153
153
|
return builder.build();
|
|
154
154
|
};
|
|
155
|
-
//#endregion
|
|
156
155
|
exports.hastToRichText = hastToRichText;
|
|
157
156
|
|
|
158
157
|
//# sourceMappingURL=hastToRichText.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastToRichText.cjs","names":["RichTextFieldBuilder","RichTextNodeType","toString","serializeImage","serializeEmbed","serializeSpan","PrismicRichTextSerializerError"],"sources":["../../src/lib/hastToRichText.ts"],"sourcesContent":["import type {\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTLabelNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\nimport { RichTextNodeType } from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { toString } from \"hast-util-to-string\"\nimport { visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\nimport type {\n\tRichTextHTMLMapSerializer,\n\tRichTextHTMLMapSerializerFunction,\n\tRichTextHTMLMapSerializerShorthand,\n} from \"../types\"\nimport { serializeEmbed, serializeImage, serializeSpan } from \"./hastSerializerHelpers\"\nimport { RichTextFieldBuilder } from \"./RichTextFieldBuilder\"\n\n/**\n * Pick 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-pick.d.ts\n */\ntype DistributedPick<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Pick<ObjectType, KeyType>\n\t: never\n\nconst DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {\n\th1: \"heading1\",\n\th2: \"heading2\",\n\th3: \"heading3\",\n\th4: \"heading4\",\n\th5: \"heading5\",\n\th6: \"heading6\",\n\tp: \"paragraph\",\n\tpre: \"preformatted\",\n\tstrong: \"strong\",\n\tem: \"em\",\n\tli: ({ context }) => (context.listType === \"group-o-list-item\" ? \"o-list-item\" : \"list-item\"),\n\tul: \"group-list-item\",\n\tol: \"group-o-list-item\",\n\timg: \"image\",\n\tiframe: \"embed\",\n\ta: \"hyperlink\",\n}\n\nconst VFILE_RULE = \"failed-to-serialize-node\"\nconst VFILE_SOURCE = \"prismic\"\n\n/** Configuration that determines the output of `toRichText`. */\nexport type HASTToRichTextConfig = {\n\t/**\n\t * An optional HTML to rich text serializer. Will be merged with the default HTML to rich text\n\t * serializer.\n\t */\n\tserializer?: RichTextHTMLMapSerializer\n\n\t/**\n\t * Whether or not the text processed should be marked as right-to-left.\n\t *\n\t * @defaultValue `false`\n\t */\n\tdirection?: \"ltr\" | \"rtl\"\n}\n\n/**\n * Transfor a hast tree to a rich text field.\n *\n * @param tree - The hast tree to transform.\n * @param file - The vfile to attach warnings to.\n * @param config - Configuration that determines the output of the function.\n * @returns The rich text field equivalent of the provided hast tree.\n */\nexport const hastToRichText = (\n\ttree: Root | Element,\n\tfile: VFile,\n\tconfig?: HASTToRichTextConfig,\n): RichTextField => {\n\tconst builder = new RichTextFieldBuilder()\n\n\t// Merge the default serializer with the user-provided one.\n\tconst serializer = {\n\t\t...DEFAULT_SERIALIZER,\n\t\t...config?.serializer,\n\t}\n\n\t// Keep track of the last text node type to append text nodes to in\n\t// case of an image or an embed node is present inside a paragraph.\n\tlet lastRTTextNodeType: RTTextNode[\"type\"] = RichTextNodeType.paragraph\n\n\t// Keep track of the last list type to know whether we need to append\n\t// `list-item` or `o-list-item` nodes.\n\tlet lastListType: \"group-list-item\" | \"group-o-list-item\" | undefined = undefined\n\n\tvisit(tree, (node) => {\n\t\tif (node.type === \"element\") {\n\t\t\t// Transforms line break elements to line breaks\n\t\t\tif (node.tagName === \"br\") {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(\"\\n\")\n\t\t\t\t} catch {\n\t\t\t\t\t// noop\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Resolves the serializer responsible for the current node.\n\t\t\tlet serializerOrShorthand:\n\t\t\t\t| RichTextHTMLMapSerializerShorthand\n\t\t\t\t| RichTextHTMLMapSerializerFunction\n\n\t\t\t// We give priority to CSS selectors over tag names.\n\t\t\tif (node.matchesSerializer && node.matchesSerializer in serializer) {\n\t\t\t\tserializerOrShorthand = serializer[node.matchesSerializer]\n\t\t\t} else if (node.tagName in serializer) {\n\t\t\t\tserializerOrShorthand = serializer[node.tagName]\n\t\t\t} else {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tlet shorthand: RichTextHTMLMapSerializerShorthand\n\t\t\tif (typeof serializerOrShorthand === \"function\") {\n\t\t\t\tconst shorthandOrNode = serializerOrShorthand({\n\t\t\t\t\tnode,\n\t\t\t\t\tcontext: { listType: lastListType },\n\t\t\t\t})\n\n\t\t\t\tif (!shorthandOrNode) {\n\t\t\t\t\t// Exit on unhandled node.\n\t\t\t\t\treturn\n\t\t\t\t} else if (typeof shorthandOrNode === \"object\" && \"type\" in shorthandOrNode) {\n\t\t\t\t\t// When the serializer returns a rich text node, we append it and return.\n\t\t\t\t\tswitch (shorthandOrNode.type) {\n\t\t\t\t\t\tcase RichTextNodeType.strong:\n\t\t\t\t\t\tcase RichTextNodeType.em:\n\t\t\t\t\t\tcase RichTextNodeType.label:\n\t\t\t\t\t\tcase RichTextNodeType.hyperlink: {\n\t\t\t\t\t\t\tconst length = toString(node).trimEnd().length\n\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, length)\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode and that\n\t\t\t\t\t\t\t\t// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, length)\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tcase RichTextNodeType.image:\n\t\t\t\t\t\tcase RichTextNodeType.embed:\n\t\t\t\t\t\t\tbuilder.appendNode(shorthandOrNode)\n\n\t\t\t\t\t\t\treturn\n\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tlastRTTextNodeType = shorthandOrNode.type\n\t\t\t\t\t\t\tbuilder.appendNode(shorthandOrNode)\n\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Else it's a shorthand.\n\t\t\t\tshorthand = shorthandOrNode\n\t\t\t} else {\n\t\t\t\tshorthand = serializerOrShorthand\n\t\t\t}\n\n\t\t\tlet match:\n\t\t\t\t| DistributedPick<RTBlockNode | Exclude<RTInlineNode, RTLabelNode>, \"type\">\n\t\t\t\t| { type: \"label\"; data: { label: string } }\n\t\t\tif (typeof shorthand === \"string\") {\n\t\t\t\tmatch = { type: shorthand }\n\t\t\t} else {\n\t\t\t\tmatch = { type: RichTextNodeType.label, data: shorthand }\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tswitch (match.type) {\n\t\t\t\t\tcase RichTextNodeType.heading1:\n\t\t\t\t\tcase RichTextNodeType.heading2:\n\t\t\t\t\tcase RichTextNodeType.heading3:\n\t\t\t\t\tcase RichTextNodeType.heading4:\n\t\t\t\t\tcase RichTextNodeType.heading5:\n\t\t\t\t\tcase RichTextNodeType.heading6:\n\t\t\t\t\tcase RichTextNodeType.paragraph:\n\t\t\t\t\tcase RichTextNodeType.preformatted:\n\t\t\t\t\tcase RichTextNodeType.listItem:\n\t\t\t\t\tcase RichTextNodeType.oListItem:\n\t\t\t\t\t\tlastRTTextNodeType = match.type\n\t\t\t\t\t\tbuilder.appendTextNode(match.type, config?.direction)\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.list:\n\t\t\t\t\tcase RichTextNodeType.oList:\n\t\t\t\t\t\tlastListType = match.type\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.image:\n\t\t\t\t\t\tbuilder.appendNode(serializeImage(node))\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.embed:\n\t\t\t\t\t\tbuilder.appendNode(serializeEmbed(node))\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.strong:\n\t\t\t\t\tcase RichTextNodeType.em:\n\t\t\t\t\tcase RichTextNodeType.label:\n\t\t\t\t\tcase RichTextNodeType.hyperlink: {\n\t\t\t\t\t\tconst span = serializeSpan(node, match)\n\t\t\t\t\t\tconst length = toString(node).trimEnd().length\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode and that\n\t\t\t\t\t\t\t// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown rich text node type: \\`${(match as { type: string }).type}\\``)\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof PrismicRichTextSerializerError) {\n\t\t\t\t\tfile.message(error.message, {\n\t\t\t\t\t\tcause: error,\n\t\t\t\t\t\tplace: node.position,\n\t\t\t\t\t\truleId: VFILE_RULE,\n\t\t\t\t\t\tsource: VFILE_SOURCE,\n\t\t\t\t\t})\n\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tthrow error\n\t\t\t}\n\t\t} else if (node.type === \"text\") {\n\t\t\t// Inspired from `hast-util-whitespace`, see:\n\t\t\t// https://github.com/syntax-tree/hast-util-whitespace/blob/main/lib/index.js\n\t\t\tif (!(node.value.replace(/[ \\t\\n\\f\\r]/g, \"\") === \"\")) {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(node.value)\n\t\t\t\t} catch {\n\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode. The last RT\n\t\t\t\t\t// node type is then an image/embed node, so we need to resume a new RT text node.\n\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\tbuilder.appendText(node.value)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// We ignore the following node types:\n\t\t// root, doctype, comment, raw\n\t})\n\n\treturn builder.build()\n}\n"],"mappings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iBAAiB,WAAW;MAChC,MAAM,OAAOI,8BAAAA,cAAc,MAAM,KAAK;MACtC,MAAM,UAAA,GAASH,oBAAAA,SAAAA,CAAS,IAAI,CAAC,CAAC,QAAQ,CAAC,CAAC;MAExC,IAAI;OACH,QAAQ,WAAW,MAAM,MAAM;MAChC,QAAQ;OAIP,QAAQ,eAAe,oBAAoB,QAAQ,SAAS;OAC5D,QAAQ,WAAW,MAAM,MAAM;MAChC;MACA;KACD;KAEA,SACC,MAAM,IAAI,MAAM,kCAAmC,MAA2B,KAAK,GAAG;IACxF;GACD,SAAS,OAAO;IACf,IAAI,iBAAiBI,uCAAAA,gCAAgC;KACpD,KAAK,QAAQ,MAAM,SAAS;MAC3B,OAAO;MACP,OAAO,KAAK;MACZ,QAAQ;MACR,QAAQ;KACT,CAAC;KAED;IACD;IAEA,MAAM;GACP;EACD,OAAO,IAAI,KAAK,SAAS,QAGpB;OAAA,EAAE,KAAK,MAAM,QAAQ,gBAAgB,EAAE,MAAM,KAChD,IAAI;IACH,QAAQ,WAAW,KAAK,KAAK;GAC9B,QAAQ;IAGP,QAAQ,eAAe,oBAAoB,QAAQ,SAAS;IAC5D,QAAQ,WAAW,KAAK,KAAK;GAC9B;;CAMH,CAAC;CAED,OAAO,QAAQ,MAAM;AACtB"}
|
|
1
|
+
{"version":3,"file":"hastToRichText.cjs","names":["RichTextFieldBuilder","RichTextNodeType","visit","toString","serializeImage","serializeEmbed","serializeSpan","PrismicRichTextSerializerError"],"sources":["../../src/lib/hastToRichText.ts"],"sourcesContent":["import type {\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTLabelNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\nimport { RichTextNodeType } from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { toString } from \"hast-util-to-string\"\nimport { visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\nimport type {\n\tRichTextHTMLMapSerializer,\n\tRichTextHTMLMapSerializerFunction,\n\tRichTextHTMLMapSerializerShorthand,\n} from \"../types\"\nimport { serializeEmbed, serializeImage, serializeSpan } from \"./hastSerializerHelpers\"\nimport { RichTextFieldBuilder } from \"./RichTextFieldBuilder\"\n\n/**\n * Pick 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-pick.d.ts\n */\ntype DistributedPick<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Pick<ObjectType, KeyType>\n\t: never\n\nconst DEFAULT_SERIALIZER: RichTextHTMLMapSerializer = {\n\th1: \"heading1\",\n\th2: \"heading2\",\n\th3: \"heading3\",\n\th4: \"heading4\",\n\th5: \"heading5\",\n\th6: \"heading6\",\n\tp: \"paragraph\",\n\tpre: \"preformatted\",\n\tstrong: \"strong\",\n\tem: \"em\",\n\tli: ({ context }) => (context.listType === \"group-o-list-item\" ? \"o-list-item\" : \"list-item\"),\n\tul: \"group-list-item\",\n\tol: \"group-o-list-item\",\n\timg: \"image\",\n\tiframe: \"embed\",\n\ta: \"hyperlink\",\n}\n\nconst VFILE_RULE = \"failed-to-serialize-node\"\nconst VFILE_SOURCE = \"prismic\"\n\n/** Configuration that determines the output of `toRichText`. */\nexport type HASTToRichTextConfig = {\n\t/**\n\t * An optional HTML to rich text serializer. Will be merged with the default HTML to rich text\n\t * serializer.\n\t */\n\tserializer?: RichTextHTMLMapSerializer\n\n\t/**\n\t * Whether or not the text processed should be marked as right-to-left.\n\t *\n\t * @defaultValue `false`\n\t */\n\tdirection?: \"ltr\" | \"rtl\"\n}\n\n/**\n * Transfor a hast tree to a rich text field.\n *\n * @param tree - The hast tree to transform.\n * @param file - The vfile to attach warnings to.\n * @param config - Configuration that determines the output of the function.\n * @returns The rich text field equivalent of the provided hast tree.\n */\nexport const hastToRichText = (\n\ttree: Root | Element,\n\tfile: VFile,\n\tconfig?: HASTToRichTextConfig,\n): RichTextField => {\n\tconst builder = new RichTextFieldBuilder()\n\n\t// Merge the default serializer with the user-provided one.\n\tconst serializer = {\n\t\t...DEFAULT_SERIALIZER,\n\t\t...config?.serializer,\n\t}\n\n\t// Keep track of the last text node type to append text nodes to in\n\t// case of an image or an embed node is present inside a paragraph.\n\tlet lastRTTextNodeType: RTTextNode[\"type\"] = RichTextNodeType.paragraph\n\n\t// Keep track of the last list type to know whether we need to append\n\t// `list-item` or `o-list-item` nodes.\n\tlet lastListType: \"group-list-item\" | \"group-o-list-item\" | undefined = undefined\n\n\tvisit(tree, (node) => {\n\t\tif (node.type === \"element\") {\n\t\t\t// Transforms line break elements to line breaks\n\t\t\tif (node.tagName === \"br\") {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(\"\\n\")\n\t\t\t\t} catch {\n\t\t\t\t\t// noop\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Resolves the serializer responsible for the current node.\n\t\t\tlet serializerOrShorthand:\n\t\t\t\t| RichTextHTMLMapSerializerShorthand\n\t\t\t\t| RichTextHTMLMapSerializerFunction\n\n\t\t\t// We give priority to CSS selectors over tag names.\n\t\t\tif (node.matchesSerializer && node.matchesSerializer in serializer) {\n\t\t\t\tserializerOrShorthand = serializer[node.matchesSerializer]\n\t\t\t} else if (node.tagName in serializer) {\n\t\t\t\tserializerOrShorthand = serializer[node.tagName]\n\t\t\t} else {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tlet shorthand: RichTextHTMLMapSerializerShorthand\n\t\t\tif (typeof serializerOrShorthand === \"function\") {\n\t\t\t\tconst shorthandOrNode = serializerOrShorthand({\n\t\t\t\t\tnode,\n\t\t\t\t\tcontext: { listType: lastListType },\n\t\t\t\t})\n\n\t\t\t\tif (!shorthandOrNode) {\n\t\t\t\t\t// Exit on unhandled node.\n\t\t\t\t\treturn\n\t\t\t\t} else if (typeof shorthandOrNode === \"object\" && \"type\" in shorthandOrNode) {\n\t\t\t\t\t// When the serializer returns a rich text node, we append it and return.\n\t\t\t\t\tswitch (shorthandOrNode.type) {\n\t\t\t\t\t\tcase RichTextNodeType.strong:\n\t\t\t\t\t\tcase RichTextNodeType.em:\n\t\t\t\t\t\tcase RichTextNodeType.label:\n\t\t\t\t\t\tcase RichTextNodeType.hyperlink: {\n\t\t\t\t\t\t\tconst length = toString(node).trimEnd().length\n\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, length)\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode and that\n\t\t\t\t\t\t\t\t// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, length)\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tcase RichTextNodeType.image:\n\t\t\t\t\t\tcase RichTextNodeType.embed:\n\t\t\t\t\t\t\tbuilder.appendNode(shorthandOrNode)\n\n\t\t\t\t\t\t\treturn\n\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tlastRTTextNodeType = shorthandOrNode.type\n\t\t\t\t\t\t\tbuilder.appendNode(shorthandOrNode)\n\n\t\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Else it's a shorthand.\n\t\t\t\tshorthand = shorthandOrNode\n\t\t\t} else {\n\t\t\t\tshorthand = serializerOrShorthand\n\t\t\t}\n\n\t\t\tlet match:\n\t\t\t\t| DistributedPick<RTBlockNode | Exclude<RTInlineNode, RTLabelNode>, \"type\">\n\t\t\t\t| { type: \"label\"; data: { label: string } }\n\t\t\tif (typeof shorthand === \"string\") {\n\t\t\t\tmatch = { type: shorthand }\n\t\t\t} else {\n\t\t\t\tmatch = { type: RichTextNodeType.label, data: shorthand }\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\tswitch (match.type) {\n\t\t\t\t\tcase RichTextNodeType.heading1:\n\t\t\t\t\tcase RichTextNodeType.heading2:\n\t\t\t\t\tcase RichTextNodeType.heading3:\n\t\t\t\t\tcase RichTextNodeType.heading4:\n\t\t\t\t\tcase RichTextNodeType.heading5:\n\t\t\t\t\tcase RichTextNodeType.heading6:\n\t\t\t\t\tcase RichTextNodeType.paragraph:\n\t\t\t\t\tcase RichTextNodeType.preformatted:\n\t\t\t\t\tcase RichTextNodeType.listItem:\n\t\t\t\t\tcase RichTextNodeType.oListItem:\n\t\t\t\t\t\tlastRTTextNodeType = match.type\n\t\t\t\t\t\tbuilder.appendTextNode(match.type, config?.direction)\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.list:\n\t\t\t\t\tcase RichTextNodeType.oList:\n\t\t\t\t\t\tlastListType = match.type\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.image:\n\t\t\t\t\t\tbuilder.appendNode(serializeImage(node))\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.embed:\n\t\t\t\t\t\tbuilder.appendNode(serializeEmbed(node))\n\t\t\t\t\t\tbreak\n\n\t\t\t\t\tcase RichTextNodeType.strong:\n\t\t\t\t\tcase RichTextNodeType.em:\n\t\t\t\t\tcase RichTextNodeType.label:\n\t\t\t\t\tcase RichTextNodeType.hyperlink: {\n\t\t\t\t\t\tconst span = serializeSpan(node, match)\n\t\t\t\t\t\tconst length = toString(node).trimEnd().length\n\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode and that\n\t\t\t\t\t\t\t// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown rich text node type: \\`${(match as { type: string }).type}\\``)\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof PrismicRichTextSerializerError) {\n\t\t\t\t\tfile.message(error.message, {\n\t\t\t\t\t\tcause: error,\n\t\t\t\t\t\tplace: node.position,\n\t\t\t\t\t\truleId: VFILE_RULE,\n\t\t\t\t\t\tsource: VFILE_SOURCE,\n\t\t\t\t\t})\n\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tthrow error\n\t\t\t}\n\t\t} else if (node.type === \"text\") {\n\t\t\t// Inspired from `hast-util-whitespace`, see:\n\t\t\t// https://github.com/syntax-tree/hast-util-whitespace/blob/main/lib/index.js\n\t\t\tif (!(node.value.replace(/[ \\t\\n\\f\\r]/g, \"\") === \"\")) {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(node.value)\n\t\t\t\t} catch {\n\t\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode. The last RT\n\t\t\t\t\t// node type is then an image/embed node, so we need to resume a new RT text node.\n\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\tbuilder.appendText(node.value)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// We ignore the following node types:\n\t\t// root, doctype, comment, raw\n\t})\n\n\treturn builder.build()\n}\n"],"mappings":";;;;;;;AAiCA,MAAM,qBAAgD;CACrD,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,IAAI;CACJ,GAAG;CACH,KAAK;CACL,QAAQ;CACR,IAAI;CACJ,KAAK,EAAE,cAAe,QAAQ,aAAa,sBAAsB,g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C,QAAQ;OAIP,QAAQ,eAAe,oBAAoB,QAAQ,SAAS;OAC5D,QAAQ,WAAW,MAAM,MAAM;MAChC;MACA;KACD;KAEA,SACC,MAAM,IAAI,MAAM,kCAAmC,MAA2B,KAAK,GAAG;IACxF;GACD,SAAS,OAAO;IACf,IAAI,iBAAiBI,uCAAAA,gCAAgC;KACpD,KAAK,QAAQ,MAAM,SAAS;MAC3B,OAAO;MACP,OAAO,KAAK;MACZ,QAAQ;MACR,QAAQ;KACT,CAAC;KAED;IACD;IAEA,MAAM;GACP;EACD,OAAO,IAAI,KAAK,SAAS,QAGpB;OAAA,EAAE,KAAK,MAAM,QAAQ,gBAAgB,EAAE,MAAM,KAChD,IAAI;IACH,QAAQ,WAAW,KAAK,KAAK;GAC9B,QAAQ;IAGP,QAAQ,eAAe,oBAAoB,QAAQ,SAAS;IAC5D,QAAQ,WAAW,KAAK,KAAK;GAC9B;;CAMH,CAAC;CAED,OAAO,QAAQ,MAAM;AACtB"}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { RichTextHTMLMapSerializer } from "../types.cjs";
|
|
2
|
-
import { RichTextField } from "@prismicio/client";
|
|
3
|
-
import { Element } from "hast";
|
|
4
|
-
import "vfile";
|
|
5
|
-
//#region src/lib/hastToRichText.d.ts
|
|
6
2
|
/** Configuration that determines the output of `toRichText`. */
|
|
7
|
-
type HASTToRichTextConfig = {
|
|
3
|
+
export type HASTToRichTextConfig = {
|
|
8
4
|
/**
|
|
9
5
|
* An optional HTML to rich text serializer. Will be merged with the default HTML to rich text
|
|
10
6
|
* serializer.
|
|
@@ -17,6 +13,4 @@ type HASTToRichTextConfig = {
|
|
|
17
13
|
*/
|
|
18
14
|
direction?: "ltr" | "rtl";
|
|
19
15
|
};
|
|
20
|
-
//#endregion
|
|
21
|
-
export { HASTToRichTextConfig };
|
|
22
16
|
//# sourceMappingURL=hastToRichText.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastToRichText.d.cts","names":[],"sources":["../../src/lib/hastToRichText.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"hastToRichText.d.cts","names":[],"sources":["../../src/lib/hastToRichText.ts"],"mappings":";;YAwDY;;;;;EAKX,aAAa;;;;;;EAOb"}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { RichTextHTMLMapSerializer } from "../types.js";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import "@prismicio/client";
|
|
3
|
+
import "hast";
|
|
4
4
|
import "vfile";
|
|
5
|
-
//#region src/lib/hastToRichText.d.ts
|
|
6
5
|
/** Configuration that determines the output of `toRichText`. */
|
|
7
|
-
type HASTToRichTextConfig = {
|
|
6
|
+
export type HASTToRichTextConfig = {
|
|
8
7
|
/**
|
|
9
8
|
* An optional HTML to rich text serializer. Will be merged with the default HTML to rich text
|
|
10
9
|
* serializer.
|
|
@@ -17,6 +16,4 @@ type HASTToRichTextConfig = {
|
|
|
17
16
|
*/
|
|
18
17
|
direction?: "ltr" | "rtl";
|
|
19
18
|
};
|
|
20
|
-
//#endregion
|
|
21
|
-
export { HASTToRichTextConfig };
|
|
22
19
|
//# sourceMappingURL=hastToRichText.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastToRichText.d.ts","names":[],"sources":["../../src/lib/hastToRichText.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"hastToRichText.d.ts","names":[],"sources":["../../src/lib/hastToRichText.ts"],"mappings":";;;;;YAwDY;;;;;EAKX,aAAa;;;;;;EAOb"}
|
|
@@ -4,7 +4,7 @@ import { RichTextFieldBuilder } from "./RichTextFieldBuilder.js";
|
|
|
4
4
|
import { visit } from "unist-util-visit";
|
|
5
5
|
import { RichTextNodeType } from "@prismicio/client";
|
|
6
6
|
import { toString } from "hast-util-to-string";
|
|
7
|
-
|
|
7
|
+
// src/lib/hastToRichText.ts
|
|
8
8
|
const DEFAULT_SERIALIZER = {
|
|
9
9
|
h1: "heading1",
|
|
10
10
|
h2: "heading2",
|
|
@@ -152,7 +152,6 @@ const hastToRichText = (tree, file, config) => {
|
|
|
152
152
|
});
|
|
153
153
|
return builder.build();
|
|
154
154
|
};
|
|
155
|
-
//#endregion
|
|
156
155
|
export { hastToRichText };
|
|
157
156
|
|
|
158
157
|
//# sourceMappingURL=hastToRichText.js.map
|
|
@@ -6,7 +6,7 @@ let rehype_minify_whitespace = require("rehype-minify-whitespace");
|
|
|
6
6
|
rehype_minify_whitespace = require_runtime.__toESM(rehype_minify_whitespace, 1);
|
|
7
7
|
let unist_util_remove = require("unist-util-remove");
|
|
8
8
|
let unist_util_visit = require("unist-util-visit");
|
|
9
|
-
|
|
9
|
+
// src/lib/rehypeRichText.ts
|
|
10
10
|
/**
|
|
11
11
|
* A unified plugin that compiles a hast tree to a Prismic rich text field.
|
|
12
12
|
*
|
|
@@ -53,7 +53,6 @@ const rehypeRichText = function rehypeRichText(config) {
|
|
|
53
53
|
return richTextField;
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
|
-
//#endregion
|
|
57
56
|
exports.rehypeRichText = rehypeRichText;
|
|
58
57
|
|
|
59
58
|
//# sourceMappingURL=rehypeRichText.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehypeRichText.cjs","names":["select","selectAll","SKIP","rehypeMinifyWhitespace","hastToRichText","filterRichTextField"],"sources":["../../src/lib/rehypeRichText.ts"],"sourcesContent":["import type { CustomTypeModelRichTextField, RichTextField } from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { select, selectAll } from \"hast-util-select\"\nimport rehypeMinifyWhitespace from \"rehype-minify-whitespace\"\nimport type { Plugin, Processor } from \"unified\"\nimport { remove } from \"unist-util-remove\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { filterRichTextField } from \"./filterRichTextField\"\nimport type { HASTToRichTextConfig } from \"./hastToRichText\"\nimport { hastToRichText } from \"./hastToRichText\"\n\n/** Configuration options for {@link rehypeRichText}. */\nexport type RehypeRichTextConfig = {\n\t/**\n\t * A CSS selector that targets the section of the document to convert to rich text.\n\t *\n\t * @example\n\t * \t;`div.post`\n\t *\n\t * @defaultValue `\":root\"` - The top-level element of the document.\n\t */\n\tcontainer?: string\n\n\t/**\n\t * A list of CSS selectors to exclude matching nodes from the document to process.\n\t *\n\t * @example\n\t * \t;`[\".hidden\", \"aside\"]`\n\t *\n\t * @defaultValue `[]` - No nodes are excluded.\n\t */\n\texclude?: string[]\n\n\t/**\n\t * A list of CSS selectors to include only matching nodes from the document to process.\n\t *\n\t * @example\n\t * \t;`[\"p\", \"img\"]`\n\t *\n\t * @defaultValue `[]` - All nodes are included.\n\t */\n\tinclude?: string[]\n\n\t/**\n\t * A rich text or title field model definition. When provided the serializer will filter out any\n\t * node types not allowed by the model.\n\t *\n\t * @defaultValue `undefined` - No filtering is applied.\n\t */\n\tmodel?: CustomTypeModelRichTextField\n} & HASTToRichTextConfig\n\n/**\n * A unified plugin that compiles a hast tree to a Prismic rich text field.\n *\n * @param config - Configuration options for the rehype rich text processor.\n */\n// unified requires the function to be typed directly with the\n// `Plugin` type to properly infer return types on processors.\nexport const rehypeRichText: Plugin<[config?: RehypeRichTextConfig], Root, RichTextField> =\n\tfunction rehypeRichText(config) {\n\t\t// This is a bit dirty, but it seems like that's how rehype intends\n\t\t// it to be due to JSDocs limitations(?), see:\n\t\t// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20\n\t\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\t\tconst self = this as unknown as Processor<undefined, undefined, undefined, Root, RichTextField>\n\n\t\t// We need to exclude nodes _before_ we minify the tree as excluding\n\t\t// nodes could end up in more whitespaces to trim.\n\t\tself.use(() => {\n\t\t\treturn (tree: Root) => {\n\t\t\t\t// Extract container node if any is specified.\n\t\t\t\tif (config?.container) {\n\t\t\t\t\tconst element = select(config.container, tree)\n\n\t\t\t\t\tif (!element) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`No container matching \\`${config?.container}\\` could be found in the input AST.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\n\t\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t\t// its children with the found element.\n\t\t\t\t\ttree.children = [element]\n\t\t\t\t}\n\n\t\t\t\t// Remove excluded nodes if any are specified\n\t\t\t\tif (config?.exclude) {\n\t\t\t\t\t// We join selector to only run one query\n\t\t\t\t\tconst nodesToExclude = selectAll(config.exclude.join(\", \"), tree)\n\n\t\t\t\t\tremove(tree, (node) => nodesToExclude.includes(node as Element))\n\t\t\t\t}\n\n\t\t\t\t// Include only nodes to include\n\t\t\t\tif (config?.include) {\n\t\t\t\t\tconst nodesToInclude: Element[] = []\n\n\t\t\t\t\t// We join selector to only run one query\n\t\t\t\t\tconst selector = config.include.join(\", \")\n\t\t\t\t\tconst rawNodesToInclude = selectAll(selector, tree)\n\n\t\t\t\t\t// We walk the tree to exclude matching nodes that are children of other matching nodes.\n\t\t\t\t\tvisit(tree, (node) => {\n\t\t\t\t\t\tif (rawNodesToInclude.includes(node as Element)) {\n\t\t\t\t\t\t\tnodesToInclude.push(node as Element)\n\n\t\t\t\t\t\t\t// Stop traversing this part of the tree since we found its matching parent node.\n\t\t\t\t\t\t\treturn SKIP\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\n\t\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t\t// its children with the found element.\n\t\t\t\t\ttree.children = nodesToInclude\n\t\t\t\t}\n\n\t\t\t\t// Mark nodes matching CSS selectors\n\t\t\t\tif (config?.serializer) {\n\t\t\t\t\tfor (const key in config.serializer) {\n\t\t\t\t\t\t// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).\n\t\t\t\t\t\t// Here we want to match anything that's not a valid HTML tag name and treat\n\t\t\t\t\t\t// it as a CSS selector. See: https://regex101.com/r/LILLWH/1\n\t\t\t\t\t\tif (!/^[a-z]+[1-6]?$/.test(key)) {\n\t\t\t\t\t\t\tconst matches = selectAll(key, tree)\n\n\t\t\t\t\t\t\tfor (let i = 0; i < matches.length; i++) {\n\t\t\t\t\t\t\t\tmatches[i].matchesSerializer = key\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\n\t\t// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's\n\t\t// registered within the plugin rather than on the processor directly.\n\t\tself.use(rehypeMinifyWhitespace)\n\n\t\tself.compiler = compiler\n\n\t\tfunction compiler(tree: Root, file: VFile): RichTextField {\n\t\t\tconst richTextField = hastToRichText(tree, file, config)\n\n\t\t\tif (config?.model) {\n\t\t\t\treturn filterRichTextField(richTextField, config.model)\n\t\t\t}\n\n\t\t\treturn richTextField\n\t\t}\n\t}\n\ndeclare module \"unified\" {\n\t// Register unified processor the result type.\n\tinterface CompileResultMap {\n\t\tRichTextField: RichTextField\n\t}\n}\n\ndeclare module \"hast\" {\n\t// Extend hast node with a rich text value.\n\tinterface Element {\n\t\t/**\n\t\t * A serializer this node matches to. Nodes are marked with this property when they match a CSS\n\t\t * selector from the serializer map.\n\t\t */\n\t\tmatchesSerializer?: string\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;AA6DA,MAAa,iBACZ,SAAS,eAAe,QAAQ;CAK/B,MAAM,OAAO;CAIb,KAAK,UAAU;EACd,QAAQ,SAAe;GAEtB,IAAI,QAAQ,WAAW;IACtB,MAAM,WAAA,GAAUA,iBAAAA,OAAAA,CAAO,OAAO,WAAW,IAAI;IAE7C,IAAI,CAAC,SACJ,MAAM,IAAI,MACT,2BAA2B,QAAQ,UAAU,oCAC9C;IAKD,KAAK,WAAW,CAAC,OAAO;GACzB;GAGA,IAAI,QAAQ,SAAS;IAEpB,MAAM,kBAAA,GAAiBC,iBAAAA,UAAAA,CAAU,OAAO,QAAQ,KAAK,IAAI,GAAG,IAAI;
|
|
1
|
+
{"version":3,"file":"rehypeRichText.cjs","names":["select","selectAll","remove","visit","SKIP","rehypeMinifyWhitespace","hastToRichText","filterRichTextField"],"sources":["../../src/lib/rehypeRichText.ts"],"sourcesContent":["import type { CustomTypeModelRichTextField, RichTextField } from \"@prismicio/client\"\nimport type { Element, Root } from \"hast\"\nimport { select, selectAll } from \"hast-util-select\"\nimport rehypeMinifyWhitespace from \"rehype-minify-whitespace\"\nimport type { Plugin, Processor } from \"unified\"\nimport { remove } from \"unist-util-remove\"\nimport { SKIP, visit } from \"unist-util-visit\"\nimport type { VFile } from \"vfile\"\n\nimport { filterRichTextField } from \"./filterRichTextField\"\nimport type { HASTToRichTextConfig } from \"./hastToRichText\"\nimport { hastToRichText } from \"./hastToRichText\"\n\n/** Configuration options for {@link rehypeRichText}. */\nexport type RehypeRichTextConfig = {\n\t/**\n\t * A CSS selector that targets the section of the document to convert to rich text.\n\t *\n\t * @example\n\t * \t;`div.post`\n\t *\n\t * @defaultValue `\":root\"` - The top-level element of the document.\n\t */\n\tcontainer?: string\n\n\t/**\n\t * A list of CSS selectors to exclude matching nodes from the document to process.\n\t *\n\t * @example\n\t * \t;`[\".hidden\", \"aside\"]`\n\t *\n\t * @defaultValue `[]` - No nodes are excluded.\n\t */\n\texclude?: string[]\n\n\t/**\n\t * A list of CSS selectors to include only matching nodes from the document to process.\n\t *\n\t * @example\n\t * \t;`[\"p\", \"img\"]`\n\t *\n\t * @defaultValue `[]` - All nodes are included.\n\t */\n\tinclude?: string[]\n\n\t/**\n\t * A rich text or title field model definition. When provided the serializer will filter out any\n\t * node types not allowed by the model.\n\t *\n\t * @defaultValue `undefined` - No filtering is applied.\n\t */\n\tmodel?: CustomTypeModelRichTextField\n} & HASTToRichTextConfig\n\n/**\n * A unified plugin that compiles a hast tree to a Prismic rich text field.\n *\n * @param config - Configuration options for the rehype rich text processor.\n */\n// unified requires the function to be typed directly with the\n// `Plugin` type to properly infer return types on processors.\nexport const rehypeRichText: Plugin<[config?: RehypeRichTextConfig], Root, RichTextField> =\n\tfunction rehypeRichText(config) {\n\t\t// This is a bit dirty, but it seems like that's how rehype intends\n\t\t// it to be due to JSDocs limitations(?), see:\n\t\t// https://github.com/rehypejs/rehype/blob/f6912ac680704f1ef4b558ac57cbf0dd62ed0892/packages/rehype-stringify/lib/index.js#L18-L20\n\t\t// eslint-disable-next-line @typescript-eslint/no-this-alias\n\t\tconst self = this as unknown as Processor<undefined, undefined, undefined, Root, RichTextField>\n\n\t\t// We need to exclude nodes _before_ we minify the tree as excluding\n\t\t// nodes could end up in more whitespaces to trim.\n\t\tself.use(() => {\n\t\t\treturn (tree: Root) => {\n\t\t\t\t// Extract container node if any is specified.\n\t\t\t\tif (config?.container) {\n\t\t\t\t\tconst element = select(config.container, tree)\n\n\t\t\t\t\tif (!element) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`No container matching \\`${config?.container}\\` could be found in the input AST.`,\n\t\t\t\t\t\t)\n\t\t\t\t\t}\n\n\t\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t\t// its children with the found element.\n\t\t\t\t\ttree.children = [element]\n\t\t\t\t}\n\n\t\t\t\t// Remove excluded nodes if any are specified\n\t\t\t\tif (config?.exclude) {\n\t\t\t\t\t// We join selector to only run one query\n\t\t\t\t\tconst nodesToExclude = selectAll(config.exclude.join(\", \"), tree)\n\n\t\t\t\t\tremove(tree, (node) => nodesToExclude.includes(node as Element))\n\t\t\t\t}\n\n\t\t\t\t// Include only nodes to include\n\t\t\t\tif (config?.include) {\n\t\t\t\t\tconst nodesToInclude: Element[] = []\n\n\t\t\t\t\t// We join selector to only run one query\n\t\t\t\t\tconst selector = config.include.join(\", \")\n\t\t\t\t\tconst rawNodesToInclude = selectAll(selector, tree)\n\n\t\t\t\t\t// We walk the tree to exclude matching nodes that are children of other matching nodes.\n\t\t\t\t\tvisit(tree, (node) => {\n\t\t\t\t\t\tif (rawNodesToInclude.includes(node as Element)) {\n\t\t\t\t\t\t\tnodesToInclude.push(node as Element)\n\n\t\t\t\t\t\t\t// Stop traversing this part of the tree since we found its matching parent node.\n\t\t\t\t\t\t\treturn SKIP\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\n\t\t\t\t\t// We cannot reassign the tree itself, so we instead replace\n\t\t\t\t\t// its children with the found element.\n\t\t\t\t\ttree.children = nodesToInclude\n\t\t\t\t}\n\n\t\t\t\t// Mark nodes matching CSS selectors\n\t\t\t\tif (config?.serializer) {\n\t\t\t\t\tfor (const key in config.serializer) {\n\t\t\t\t\t\t// HTML tag names are single word, lowercase strings, a-z and 1-6 (headings).\n\t\t\t\t\t\t// Here we want to match anything that's not a valid HTML tag name and treat\n\t\t\t\t\t\t// it as a CSS selector. See: https://regex101.com/r/LILLWH/1\n\t\t\t\t\t\tif (!/^[a-z]+[1-6]?$/.test(key)) {\n\t\t\t\t\t\t\tconst matches = selectAll(key, tree)\n\n\t\t\t\t\t\t\tfor (let i = 0; i < matches.length; i++) {\n\t\t\t\t\t\t\t\tmatches[i].matchesSerializer = key\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t})\n\n\t\t// `rehypeRichText` _depends_ on `rehypeMinifyWhitespace`, that's why it's\n\t\t// registered within the plugin rather than on the processor directly.\n\t\tself.use(rehypeMinifyWhitespace)\n\n\t\tself.compiler = compiler\n\n\t\tfunction compiler(tree: Root, file: VFile): RichTextField {\n\t\t\tconst richTextField = hastToRichText(tree, file, config)\n\n\t\t\tif (config?.model) {\n\t\t\t\treturn filterRichTextField(richTextField, config.model)\n\t\t\t}\n\n\t\t\treturn richTextField\n\t\t}\n\t}\n\ndeclare module \"unified\" {\n\t// Register unified processor the result type.\n\tinterface CompileResultMap {\n\t\tRichTextField: RichTextField\n\t}\n}\n\ndeclare module \"hast\" {\n\t// Extend hast node with a rich text value.\n\tinterface Element {\n\t\t/**\n\t\t * A serializer this node matches to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sB;CAE/B,KAAK,WAAW;CAEhB,SAAS,SAAS,MAAY,MAA4B;EACzD,MAAM,gBAAgBC,uBAAAA,eAAe,MAAM,MAAM,MAAM;EAEvD,IAAI,QAAQ,OACX,OAAOC,4BAAAA,oBAAoB,eAAe,OAAO,KAAK;EAGvD,OAAO;CACR;AACD"}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { HASTToRichTextConfig } from "./hastToRichText.cjs";
|
|
2
2
|
import { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client";
|
|
3
|
-
import "hast";
|
|
4
|
-
import "unified";
|
|
5
|
-
//#region src/lib/rehypeRichText.d.ts
|
|
6
3
|
/** Configuration options for {@link rehypeRichText}. */
|
|
7
|
-
type RehypeRichTextConfig = {
|
|
4
|
+
export type RehypeRichTextConfig = {
|
|
8
5
|
/**
|
|
9
6
|
* A CSS selector that targets the section of the document to convert to rich text.
|
|
10
7
|
*
|
|
@@ -54,6 +51,4 @@ declare module "hast" {
|
|
|
54
51
|
matchesSerializer?: string;
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
|
-
//#endregion
|
|
58
|
-
export { RehypeRichTextConfig };
|
|
59
54
|
//# sourceMappingURL=rehypeRichText.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehypeRichText.d.cts","names":[],"sources":["../../src/lib/rehypeRichText.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"rehypeRichText.d.cts","names":[],"sources":["../../src/lib/rehypeRichText.ts"],"mappings":";;;YAcY;;;;;;;;;EASX;;;;;;;;;EAUA;;;;;;;;;EAUA;;;;;;;EAQA,QAAQ;IACL;;YAwGO;IACT,eAAe;;;;YAMN;;;;;IAKT"}
|
|
@@ -2,9 +2,8 @@ import { HASTToRichTextConfig } from "./hastToRichText.js";
|
|
|
2
2
|
import "unified";
|
|
3
3
|
import { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client";
|
|
4
4
|
import "hast";
|
|
5
|
-
//#region src/lib/rehypeRichText.d.ts
|
|
6
5
|
/** Configuration options for {@link rehypeRichText}. */
|
|
7
|
-
type RehypeRichTextConfig = {
|
|
6
|
+
export type RehypeRichTextConfig = {
|
|
8
7
|
/**
|
|
9
8
|
* A CSS selector that targets the section of the document to convert to rich text.
|
|
10
9
|
*
|
|
@@ -54,6 +53,4 @@ declare module "hast" {
|
|
|
54
53
|
matchesSerializer?: string;
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
|
-
//#endregion
|
|
58
|
-
export { RehypeRichTextConfig };
|
|
59
56
|
//# sourceMappingURL=rehypeRichText.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehypeRichText.d.ts","names":[],"sources":["../../src/lib/rehypeRichText.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"rehypeRichText.d.ts","names":[],"sources":["../../src/lib/rehypeRichText.ts"],"mappings":";;;;;YAcY;;;;;;;;;EASX;;;;;;;;;EAUA;;;;;;;;;EAUA;;;;;;;EAQA,QAAQ;IACL;;YAwGO;IACT,eAAe;;;;YAMN;;;;;IAKT"}
|
|
@@ -4,7 +4,7 @@ import { select, selectAll } from "hast-util-select";
|
|
|
4
4
|
import rehypeMinifyWhitespace from "rehype-minify-whitespace";
|
|
5
5
|
import { remove } from "unist-util-remove";
|
|
6
6
|
import { SKIP, visit } from "unist-util-visit";
|
|
7
|
-
|
|
7
|
+
// src/lib/rehypeRichText.ts
|
|
8
8
|
/**
|
|
9
9
|
* A unified plugin that compiles a hast tree to a Prismic rich text field.
|
|
10
10
|
*
|
|
@@ -51,7 +51,6 @@ const rehypeRichText = function rehypeRichText(config) {
|
|
|
51
51
|
return richTextField;
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
|
-
//#endregion
|
|
55
54
|
export { rehypeRichText };
|
|
56
55
|
|
|
57
56
|
//# sourceMappingURL=rehypeRichText.js.map
|
package/dist/types.d.cts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { RTPartialInlineNode } from "./lib/RichTextFieldBuilder.cjs";
|
|
2
2
|
import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextNodeTypes } from "@prismicio/client";
|
|
3
3
|
import { Element } from "hast";
|
|
4
|
-
//#region src/types.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.
|
|
7
6
|
*
|
|
@@ -12,7 +11,7 @@ import { Element } from "hast";
|
|
|
12
11
|
* The `span` rich text node type is not available as it is not relevant in the context of going
|
|
13
12
|
* from HTML to Prismic rich text.
|
|
14
13
|
*/
|
|
15
|
-
type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "span"> | {
|
|
14
|
+
export type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "span"> | {
|
|
16
15
|
label: string;
|
|
17
16
|
};
|
|
18
17
|
/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */
|
|
@@ -33,7 +32,7 @@ type RichTextHTMLMapSerializerFunctionPayload = {
|
|
|
33
32
|
* Serializing to a rich text node directly is not recommended and is only available as an escape
|
|
34
33
|
* hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.
|
|
35
34
|
*/
|
|
36
|
-
type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode> | undefined;
|
|
35
|
+
export type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode> | undefined;
|
|
37
36
|
/**
|
|
38
37
|
* Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a
|
|
39
38
|
* Prismic rich text node.
|
|
@@ -42,7 +41,6 @@ type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunc
|
|
|
42
41
|
* This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich
|
|
43
42
|
* text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.
|
|
44
43
|
*/
|
|
45
|
-
type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
|
|
46
|
-
|
|
47
|
-
export { RichTextHTMLMapSerializer, RichTextHTMLMapSerializerFunction, RichTextHTMLMapSerializerShorthand };
|
|
44
|
+
export type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
|
|
45
|
+
export {};
|
|
48
46
|
//# sourceMappingURL=types.d.cts.map
|
package/dist/types.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;;;YAoBY,qCACT,QAAQ;EACN;;;KAGA;;EAEJ,MAAM;;EAGN;;IAEC;;;;;;;;;;;YAYU,qCACX,SAAS,6CAEP,qCACA,6BAA6B,SAAS,eAAe;;;;;;;;;YAW5C,4BAA4B,eAEvC,qCAAqC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { RTPartialInlineNode } from "./lib/RichTextFieldBuilder.js";
|
|
2
2
|
import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextNodeTypes } from "@prismicio/client";
|
|
3
3
|
import { Element } from "hast";
|
|
4
|
-
//#region src/types.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.
|
|
7
6
|
*
|
|
@@ -12,7 +11,7 @@ import { Element } from "hast";
|
|
|
12
11
|
* The `span` rich text node type is not available as it is not relevant in the context of going
|
|
13
12
|
* from HTML to Prismic rich text.
|
|
14
13
|
*/
|
|
15
|
-
type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "span"> | {
|
|
14
|
+
export type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "span"> | {
|
|
16
15
|
label: string;
|
|
17
16
|
};
|
|
18
17
|
/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */
|
|
@@ -33,7 +32,7 @@ type RichTextHTMLMapSerializerFunctionPayload = {
|
|
|
33
32
|
* Serializing to a rich text node directly is not recommended and is only available as an escape
|
|
34
33
|
* hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.
|
|
35
34
|
*/
|
|
36
|
-
type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode> | undefined;
|
|
35
|
+
export type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | InjectMigrationSpecificTypes<RTNode | RTInlineNode | RTPartialInlineNode> | undefined;
|
|
37
36
|
/**
|
|
38
37
|
* Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a
|
|
39
38
|
* Prismic rich text node.
|
|
@@ -42,7 +41,6 @@ type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunc
|
|
|
42
41
|
* This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich
|
|
43
42
|
* text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.
|
|
44
43
|
*/
|
|
45
|
-
type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
|
|
46
|
-
|
|
47
|
-
export { RichTextHTMLMapSerializer, RichTextHTMLMapSerializerFunction, RichTextHTMLMapSerializerShorthand };
|
|
44
|
+
export type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
|
|
45
|
+
export {};
|
|
48
46
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;;;;;;;YAoBY,qCACT,QAAQ;EACN;;;KAGA;;EAEJ,MAAM;;EAGN;;IAEC;;;;;;;;;;;YAYU,qCACX,SAAS,6CAEP,qCACA,6BAA6B,SAAS,eAAe;;;;;;;;;YAW5C,4BAA4B,eAEvC,qCAAqC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/migrate",
|
|
3
|
-
"version": "0.0.2-canary.
|
|
3
|
+
"version": "0.0.2-canary.701c2e5",
|
|
4
4
|
"description": "Set of helpers to migrate your data to Prismic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"prismic",
|
|
@@ -44,29 +44,29 @@
|
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"hast": "^1.0.0",
|
|
46
46
|
"hast-util-select": "^6.0.4",
|
|
47
|
-
"hast-util-to-html": "^9.0.
|
|
47
|
+
"hast-util-to-html": "^9.0.6",
|
|
48
48
|
"hast-util-to-string": "^3.0.1",
|
|
49
49
|
"rehype-minify-whitespace": "^6.0.2",
|
|
50
50
|
"rehype-parse": "^9.0.1",
|
|
51
51
|
"unified": "^11.0.5",
|
|
52
52
|
"unist-util-remove": "^4.0.0",
|
|
53
|
-
"unist-util-visit": "^5.
|
|
53
|
+
"unist-util-visit": "^5.2.0",
|
|
54
54
|
"vfile": "^6.0.3"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@prismicio/client": "^7.
|
|
58
|
-
"@types/node": "^26.
|
|
59
|
-
"@vitest/coverage-v8": "^
|
|
60
|
-
"oxfmt": "^0.
|
|
61
|
-
"oxlint": "^1.
|
|
62
|
-
"tsdown": "^0.
|
|
57
|
+
"@prismicio/client": "^7.23.1",
|
|
58
|
+
"@types/node": "^26.6.6",
|
|
59
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
60
|
+
"oxfmt": "^0.78.0",
|
|
61
|
+
"oxlint": "^1.93.0",
|
|
62
|
+
"tsdown": "^0.23.7",
|
|
63
63
|
"typescript": "^7.0.2",
|
|
64
|
-
"vitest": "^
|
|
64
|
+
"vitest": "^5.1.0"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
67
|
"@prismicio/client": "^7.21.8"
|
|
68
68
|
},
|
|
69
69
|
"engines": {
|
|
70
|
-
"node": ">=
|
|
70
|
+
"node": ">=22"
|
|
71
71
|
}
|
|
72
72
|
}
|