@prismicio/migrate 0.0.2-canary.6b4d958 → 0.0.2-canary.9aa3ce7
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 -1
- package/dist/errors/PrismicRichTextError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextError.d.cts +1 -2
- package/dist/errors/PrismicRichTextError.d.cts.map +1 -1
- package/dist/errors/PrismicRichTextError.d.ts +1 -2
- package/dist/errors/PrismicRichTextError.d.ts.map +1 -1
- package/dist/errors/PrismicRichTextError.js.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.d.cts +1 -3
- package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.d.ts +1 -3
- package/dist/errors/PrismicRichTextSerializerError.d.ts.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -1
- package/dist/htmlAsRichText.cjs.map +1 -1
- package/dist/htmlAsRichText.d.cts +3 -4
- package/dist/htmlAsRichText.d.cts.map +1 -1
- package/dist/htmlAsRichText.d.ts +3 -4
- package/dist/htmlAsRichText.d.ts.map +1 -1
- package/dist/htmlAsRichText.js.map +1 -1
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.cts +1 -3
- package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.ts +1 -3
- package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -1
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +19 -15
- package/dist/lib/filterRichTextField.cjs.map +1 -1
- package/dist/lib/filterRichTextField.js +19 -14
- package/dist/lib/filterRichTextField.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs +0 -1
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
- package/dist/lib/hastSerializerHelpers.js.map +1 -1
- package/dist/lib/hastToRichText.cjs +0 -1
- package/dist/lib/hastToRichText.cjs.map +1 -1
- package/dist/lib/hastToRichText.d.cts +2 -2
- package/dist/lib/hastToRichText.d.cts.map +1 -1
- package/dist/lib/hastToRichText.d.ts +2 -2
- package/dist/lib/hastToRichText.d.ts.map +1 -1
- package/dist/lib/hastToRichText.js.map +1 -1
- package/dist/lib/rehypeRichText.cjs +2 -1
- package/dist/lib/rehypeRichText.cjs.map +1 -1
- package/dist/lib/rehypeRichText.d.cts +4 -3
- package/dist/lib/rehypeRichText.d.cts.map +1 -1
- package/dist/lib/rehypeRichText.d.ts +4 -3
- package/dist/lib/rehypeRichText.d.ts.map +1 -1
- package/dist/lib/rehypeRichText.js +2 -1
- package/dist/lib/rehypeRichText.js.map +1 -1
- package/dist/types.d.cts +8 -7
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.ts +8 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +17 -17
- package/src/lib/hastToRichText.ts +31 -31
|
@@ -15,7 +15,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
19
19
|
value: mod,
|
|
20
20
|
enumerable: true
|
|
21
21
|
}) : target, mod));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextError.cjs","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"sourcesContent":["export class PrismicRichTextError extends Error {}\n"],"mappings":";AAAA,IAAa,uBAAb,cAA0C,MAAM"}
|
|
1
|
+
{"version":3,"file":"PrismicRichTextError.cjs","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"sourcesContent":["export class PrismicRichTextError extends Error {}\n"],"mappings":";AAAA,IAAa,uBAAb,cAA0C,MAAM,CAAC"}
|
|
@@ -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":";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":";qBAAa,6BAA6B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextError.js","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"sourcesContent":["export class PrismicRichTextError extends Error {}\n"],"mappings":";AAAA,IAAa,uBAAb,cAA0C,MAAM"}
|
|
1
|
+
{"version":3,"file":"PrismicRichTextError.js","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"sourcesContent":["export class PrismicRichTextError extends Error {}\n"],"mappings":";AAAA,IAAa,uBAAb,cAA0C,MAAM,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextSerializerError.cjs","names":["PrismicRichTextError"],"sources":["../../src/errors/PrismicRichTextSerializerError.ts"],"sourcesContent":["import { PrismicRichTextError } from \"./PrismicRichTextError\"\n\nexport class PrismicRichTextSerializerError extends PrismicRichTextError {}\n"],"mappings":";;AAEA,IAAa,iCAAb,cAAoDA,6BAAAA,qBAAqB"}
|
|
1
|
+
{"version":3,"file":"PrismicRichTextSerializerError.cjs","names":["PrismicRichTextError"],"sources":["../../src/errors/PrismicRichTextSerializerError.ts"],"sourcesContent":["import { PrismicRichTextError } from \"./PrismicRichTextError\"\n\nexport class PrismicRichTextSerializerError extends PrismicRichTextError {}\n"],"mappings":";;AAEA,IAAa,iCAAb,cAAoDA,6BAAAA,qBAAqB,CAAC"}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { PrismicRichTextError } from "./PrismicRichTextError.cjs";
|
|
2
|
-
|
|
3
2
|
//#region src/errors/PrismicRichTextSerializerError.d.ts
|
|
4
|
-
declare class PrismicRichTextSerializerError extends PrismicRichTextError {}
|
|
3
|
+
export declare class PrismicRichTextSerializerError extends PrismicRichTextError {}
|
|
5
4
|
//#endregion
|
|
6
|
-
export { PrismicRichTextSerializerError };
|
|
7
5
|
//# 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,7 +1,5 @@
|
|
|
1
1
|
import { PrismicRichTextError } from "./PrismicRichTextError.js";
|
|
2
|
-
|
|
3
2
|
//#region src/errors/PrismicRichTextSerializerError.d.ts
|
|
4
|
-
declare class PrismicRichTextSerializerError extends PrismicRichTextError {}
|
|
3
|
+
export declare class PrismicRichTextSerializerError extends PrismicRichTextError {}
|
|
5
4
|
//#endregion
|
|
6
|
-
export { PrismicRichTextSerializerError };
|
|
7
5
|
//# 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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextSerializerError.js","names":[],"sources":["../../src/errors/PrismicRichTextSerializerError.ts"],"sourcesContent":["import { PrismicRichTextError } from \"./PrismicRichTextError\"\n\nexport class PrismicRichTextSerializerError extends PrismicRichTextError {}\n"],"mappings":";;AAEA,IAAa,iCAAb,cAAoD,qBAAqB"}
|
|
1
|
+
{"version":3,"file":"PrismicRichTextSerializerError.js","names":[],"sources":["../../src/errors/PrismicRichTextSerializerError.ts"],"sourcesContent":["import { PrismicRichTextError } from \"./PrismicRichTextError\"\n\nexport class PrismicRichTextSerializerError extends PrismicRichTextError {}\n"],"mappings":";;AAEA,IAAa,iCAAb,cAAoD,qBAAqB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"htmlAsRichText.cjs","names":["rehypeParse","rehypeRichText"],"sources":["../src/htmlAsRichText.ts"],"sourcesContent":["// Used for TSDocs only.\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { HTMLRichTextMapSerializer, asHTML } from \"@prismicio/client\"\nimport type { RTInlineNode, RTNode, RichTextField, RichTextNodeTypes } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport rehypeParse from \"rehype-parse\"\nimport { unified } from \"unified\"\n\nimport type { RehypeRichTextConfig } from \"./lib/rehypeRichText\"\nimport { rehypeRichText } from \"./lib/rehypeRichText\"\nimport type { RTPartialInlineNode } from \"./lib/RichTextFieldBuilder\"\n\n/**\n * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.\n *\n * @remarks\n * The `label` rich text node type is not available as is. Use an object containing your label\n * name to convert to label nodes instead. For example: `u: { label: \"underline\" }`\n * @remarks\n * The `span` rich text node type is not available as it is not relevant in the context of going\n * from HTML to Prismic rich text.\n */\nexport type RichTextHTMLMapSerializerShorthand =\n\t| Exclude<RichTextNodeTypes, \"label\" | \"span\">\n\t| { label: string }\n\n/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */\ntype RichTextHTMLMapSerializerFunctionPayload = {\n\t/** The hast {@link Element} node to serialize. */\n\tnode: Element\n\n\t/** Additional context information to help with the serialization. */\n\tcontext: {\n\t\t/** The list type of the last list node encountered if any. */\n\t\tlistType?: \"group-list-item\" | \"group-o-list-item\"\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RichTextHTMLMapSerializerShorthand} or a rich\n * text node.\n *\n * @remarks\n * Serializing to a rich text node directly is not recommended and is only available as an escape\n * hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.\n */\nexport type RichTextHTMLMapSerializerFunction = (\n\tpayload: RichTextHTMLMapSerializerFunctionPayload,\n) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined\n\n/**\n * Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a\n * Prismic rich text node.\n *\n * @remarks\n * This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich\n * text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.\n */\nexport type RichTextHTMLMapSerializer = Record<\n\tstring,\n\tRichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction\n>\n\n/** Configuration that determines the output of {@link htmlAsRichText}. */\nexport type HTMLAsRichTextConfig = RehypeRichTextConfig\n\n/** The return type of {@link htmlAsRichText}. */\ntype HTMLAsRichTextReturnType = {\n\tresult: RichTextField\n\twarnings: string[]\n}\n\n/**\n * Converts an HTML string to a rich text field.\n *\n * @param html - An HTML string.\n * @param config - Configuration that determines the output the function.\n * @returns `html` as rich text.\n */\nexport const htmlAsRichText = (\n\thtml: string,\n\tconfig?: HTMLAsRichTextConfig,\n): HTMLAsRichTextReturnType => {\n\tconst { result, messages } = unified()\n\t\t.use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 })\n\t\t.use(rehypeRichText, config)\n\t\t.processSync(html)\n\n\treturn { result, warnings: messages.map((message) => message.toString()) }\n}\n"],"mappings":";;;;;;;;;;;;;AA+EA,MAAa,kBACZ,MACA,WAC8B;CAC9B,MAAM,EAAE,QAAQ,cAAA,
|
|
1
|
+
{"version":3,"file":"htmlAsRichText.cjs","names":["rehypeParse","rehypeRichText"],"sources":["../src/htmlAsRichText.ts"],"sourcesContent":["// Used for TSDocs only.\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { HTMLRichTextMapSerializer, asHTML } from \"@prismicio/client\"\nimport type { RTInlineNode, RTNode, RichTextField, RichTextNodeTypes } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport rehypeParse from \"rehype-parse\"\nimport { unified } from \"unified\"\n\nimport type { RehypeRichTextConfig } from \"./lib/rehypeRichText\"\nimport { rehypeRichText } from \"./lib/rehypeRichText\"\nimport type { RTPartialInlineNode } from \"./lib/RichTextFieldBuilder\"\n\n/**\n * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.\n *\n * @remarks\n * The `label` rich text node type is not available as is. Use an object containing your label\n * name to convert to label nodes instead. For example: `u: { label: \"underline\" }`\n * @remarks\n * The `span` rich text node type is not available as it is not relevant in the context of going\n * from HTML to Prismic rich text.\n */\nexport type RichTextHTMLMapSerializerShorthand =\n\t| Exclude<RichTextNodeTypes, \"label\" | \"span\">\n\t| { label: string }\n\n/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */\ntype RichTextHTMLMapSerializerFunctionPayload = {\n\t/** The hast {@link Element} node to serialize. */\n\tnode: Element\n\n\t/** Additional context information to help with the serialization. */\n\tcontext: {\n\t\t/** The list type of the last list node encountered if any. */\n\t\tlistType?: \"group-list-item\" | \"group-o-list-item\"\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RichTextHTMLMapSerializerShorthand} or a rich\n * text node.\n *\n * @remarks\n * Serializing to a rich text node directly is not recommended and is only available as an escape\n * hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.\n */\nexport type RichTextHTMLMapSerializerFunction = (\n\tpayload: RichTextHTMLMapSerializerFunctionPayload,\n) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined\n\n/**\n * Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a\n * Prismic rich text node.\n *\n * @remarks\n * This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich\n * text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.\n */\nexport type RichTextHTMLMapSerializer = Record<\n\tstring,\n\tRichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction\n>\n\n/** Configuration that determines the output of {@link htmlAsRichText}. */\nexport type HTMLAsRichTextConfig = RehypeRichTextConfig\n\n/** The return type of {@link htmlAsRichText}. */\ntype HTMLAsRichTextReturnType = {\n\tresult: RichTextField\n\twarnings: string[]\n}\n\n/**\n * Converts an HTML string to a rich text field.\n *\n * @param html - An HTML string.\n * @param config - Configuration that determines the output the function.\n * @returns `html` as rich text.\n */\nexport const htmlAsRichText = (\n\thtml: string,\n\tconfig?: HTMLAsRichTextConfig,\n): HTMLAsRichTextReturnType => {\n\tconst { result, messages } = unified()\n\t\t.use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 })\n\t\t.use(rehypeRichText, config)\n\t\t.processSync(html)\n\n\treturn { result, warnings: messages.map((message) => message.toString()) }\n}\n"],"mappings":";;;;;;;;;;;;;AA+EA,MAAa,kBACZ,MACA,WAC8B;CAC9B,MAAM,EAAE,QAAQ,cAAA,GAAa,QAAA,QAAA,CAAQ,CAAC,CACpC,IAAIA,aAAAA,SAAa;EAAE,iBAAiB;EAAM,gBAAgB;CAAE,CAAC,CAAC,CAC9D,IAAIC,uBAAAA,gBAAgB,MAAM,CAAC,CAC3B,YAAY,IAAI;CAElB,OAAO;EAAE;EAAQ,UAAU,SAAS,KAAK,YAAY,QAAQ,SAAS,CAAC;CAAE;AAC1E"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import "./lib/RichTextFieldBuilder.cjs";
|
|
1
2
|
import { RehypeRichTextConfig } from "./lib/rehypeRichText.cjs";
|
|
2
3
|
import { RTInlineNode, RTNode, RichTextField, RichTextNodeTypes } from "@prismicio/client";
|
|
3
4
|
import { Element } from "hast";
|
|
4
|
-
|
|
5
5
|
//#region src/htmlAsRichText.d.ts
|
|
6
6
|
/** Configuration that determines the output of {@link htmlAsRichText}. */
|
|
7
|
-
type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
7
|
+
export type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
8
8
|
/** The return type of {@link htmlAsRichText}. */
|
|
9
9
|
type HTMLAsRichTextReturnType = {
|
|
10
10
|
result: RichTextField;
|
|
@@ -17,7 +17,6 @@ type HTMLAsRichTextReturnType = {
|
|
|
17
17
|
* @param config - Configuration that determines the output the function.
|
|
18
18
|
* @returns `html` as rich text.
|
|
19
19
|
*/
|
|
20
|
-
declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
20
|
+
export declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
21
21
|
//#endregion
|
|
22
|
-
export { htmlAsRichText };
|
|
23
22
|
//# 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,10 @@
|
|
|
1
|
+
import "./lib/RichTextFieldBuilder.js";
|
|
1
2
|
import { RehypeRichTextConfig } from "./lib/rehypeRichText.js";
|
|
2
3
|
import { RTInlineNode, RTNode, RichTextField, RichTextNodeTypes } from "@prismicio/client";
|
|
3
4
|
import { Element } from "hast";
|
|
4
|
-
|
|
5
5
|
//#region src/htmlAsRichText.d.ts
|
|
6
6
|
/** Configuration that determines the output of {@link htmlAsRichText}. */
|
|
7
|
-
type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
7
|
+
export type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
8
8
|
/** The return type of {@link htmlAsRichText}. */
|
|
9
9
|
type HTMLAsRichTextReturnType = {
|
|
10
10
|
result: RichTextField;
|
|
@@ -17,7 +17,6 @@ type HTMLAsRichTextReturnType = {
|
|
|
17
17
|
* @param config - Configuration that determines the output the function.
|
|
18
18
|
* @returns `html` as rich text.
|
|
19
19
|
*/
|
|
20
|
-
declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
20
|
+
export declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
21
21
|
//#endregion
|
|
22
|
-
export { htmlAsRichText };
|
|
23
22
|
//# 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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"htmlAsRichText.js","names":[],"sources":["../src/htmlAsRichText.ts"],"sourcesContent":["// Used for TSDocs only.\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { HTMLRichTextMapSerializer, asHTML } from \"@prismicio/client\"\nimport type { RTInlineNode, RTNode, RichTextField, RichTextNodeTypes } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport rehypeParse from \"rehype-parse\"\nimport { unified } from \"unified\"\n\nimport type { RehypeRichTextConfig } from \"./lib/rehypeRichText\"\nimport { rehypeRichText } from \"./lib/rehypeRichText\"\nimport type { RTPartialInlineNode } from \"./lib/RichTextFieldBuilder\"\n\n/**\n * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.\n *\n * @remarks\n * The `label` rich text node type is not available as is. Use an object containing your label\n * name to convert to label nodes instead. For example: `u: { label: \"underline\" }`\n * @remarks\n * The `span` rich text node type is not available as it is not relevant in the context of going\n * from HTML to Prismic rich text.\n */\nexport type RichTextHTMLMapSerializerShorthand =\n\t| Exclude<RichTextNodeTypes, \"label\" | \"span\">\n\t| { label: string }\n\n/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */\ntype RichTextHTMLMapSerializerFunctionPayload = {\n\t/** The hast {@link Element} node to serialize. */\n\tnode: Element\n\n\t/** Additional context information to help with the serialization. */\n\tcontext: {\n\t\t/** The list type of the last list node encountered if any. */\n\t\tlistType?: \"group-list-item\" | \"group-o-list-item\"\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RichTextHTMLMapSerializerShorthand} or a rich\n * text node.\n *\n * @remarks\n * Serializing to a rich text node directly is not recommended and is only available as an escape\n * hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.\n */\nexport type RichTextHTMLMapSerializerFunction = (\n\tpayload: RichTextHTMLMapSerializerFunctionPayload,\n) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined\n\n/**\n * Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a\n * Prismic rich text node.\n *\n * @remarks\n * This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich\n * text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.\n */\nexport type RichTextHTMLMapSerializer = Record<\n\tstring,\n\tRichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction\n>\n\n/** Configuration that determines the output of {@link htmlAsRichText}. */\nexport type HTMLAsRichTextConfig = RehypeRichTextConfig\n\n/** The return type of {@link htmlAsRichText}. */\ntype HTMLAsRichTextReturnType = {\n\tresult: RichTextField\n\twarnings: string[]\n}\n\n/**\n * Converts an HTML string to a rich text field.\n *\n * @param html - An HTML string.\n * @param config - Configuration that determines the output the function.\n * @returns `html` as rich text.\n */\nexport const htmlAsRichText = (\n\thtml: string,\n\tconfig?: HTMLAsRichTextConfig,\n): HTMLAsRichTextReturnType => {\n\tconst { result, messages } = unified()\n\t\t.use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 })\n\t\t.use(rehypeRichText, config)\n\t\t.processSync(html)\n\n\treturn { result, warnings: messages.map((message) => message.toString()) }\n}\n"],"mappings":";;;;;;;;;;;AA+EA,MAAa,kBACZ,MACA,WAC8B;CAC9B,MAAM,EAAE,QAAQ,aAAa,
|
|
1
|
+
{"version":3,"file":"htmlAsRichText.js","names":[],"sources":["../src/htmlAsRichText.ts"],"sourcesContent":["// Used for TSDocs only.\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { HTMLRichTextMapSerializer, asHTML } from \"@prismicio/client\"\nimport type { RTInlineNode, RTNode, RichTextField, RichTextNodeTypes } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport rehypeParse from \"rehype-parse\"\nimport { unified } from \"unified\"\n\nimport type { RehypeRichTextConfig } from \"./lib/rehypeRichText\"\nimport { rehypeRichText } from \"./lib/rehypeRichText\"\nimport type { RTPartialInlineNode } from \"./lib/RichTextFieldBuilder\"\n\n/**\n * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.\n *\n * @remarks\n * The `label` rich text node type is not available as is. Use an object containing your label\n * name to convert to label nodes instead. For example: `u: { label: \"underline\" }`\n * @remarks\n * The `span` rich text node type is not available as it is not relevant in the context of going\n * from HTML to Prismic rich text.\n */\nexport type RichTextHTMLMapSerializerShorthand =\n\t| Exclude<RichTextNodeTypes, \"label\" | \"span\">\n\t| { label: string }\n\n/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */\ntype RichTextHTMLMapSerializerFunctionPayload = {\n\t/** The hast {@link Element} node to serialize. */\n\tnode: Element\n\n\t/** Additional context information to help with the serialization. */\n\tcontext: {\n\t\t/** The list type of the last list node encountered if any. */\n\t\tlistType?: \"group-list-item\" | \"group-o-list-item\"\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RichTextHTMLMapSerializerShorthand} or a rich\n * text node.\n *\n * @remarks\n * Serializing to a rich text node directly is not recommended and is only available as an escape\n * hatch. Prefer returning a {@link RichTextHTMLMapSerializerShorthand} instead.\n */\nexport type RichTextHTMLMapSerializerFunction = (\n\tpayload: RichTextHTMLMapSerializerFunctionPayload,\n) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined\n\n/**\n * Serializes a hast {@link Element} node matching the given HTML tag name or CSS selector to a\n * Prismic rich text node.\n *\n * @remarks\n * This serializer is used to serialize HTML to Prismic rich text. When serializing Prismic rich\n * text to HTML with {@link asHTML}, use the {@link HTMLRichTextMapSerializer} type instead.\n */\nexport type RichTextHTMLMapSerializer = Record<\n\tstring,\n\tRichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction\n>\n\n/** Configuration that determines the output of {@link htmlAsRichText}. */\nexport type HTMLAsRichTextConfig = RehypeRichTextConfig\n\n/** The return type of {@link htmlAsRichText}. */\ntype HTMLAsRichTextReturnType = {\n\tresult: RichTextField\n\twarnings: string[]\n}\n\n/**\n * Converts an HTML string to a rich text field.\n *\n * @param html - An HTML string.\n * @param config - Configuration that determines the output the function.\n * @returns `html` as rich text.\n */\nexport const htmlAsRichText = (\n\thtml: string,\n\tconfig?: HTMLAsRichTextConfig,\n): HTMLAsRichTextReturnType => {\n\tconst { result, messages } = unified()\n\t\t.use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 })\n\t\t.use(rehypeRichText, config)\n\t\t.processSync(html)\n\n\treturn { result, warnings: messages.map((message) => message.toString()) }\n}\n"],"mappings":";;;;;;;;;;;AA+EA,MAAa,kBACZ,MACA,WAC8B;CAC9B,MAAM,EAAE,QAAQ,aAAa,QAAQ,CAAC,CACpC,IAAI,aAAa;EAAE,iBAAiB;EAAM,gBAAgB;CAAE,CAAC,CAAC,CAC9D,IAAI,gBAAgB,MAAM,CAAC,CAC3B,YAAY,IAAI;CAElB,OAAO;EAAE;EAAQ,UAAU,SAAS,KAAK,YAAY,QAAQ,SAAS,CAAC;CAAE;AAC1E"}
|
|
@@ -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,IAAI;EACJ,IAAI,WAAW,qBAAqB,SAAS,mBAC5C,OAAO;OACD;GACN,MAAM,aAAa,KAAK,MAAM,KAAK;GAEnC,OAAO;IACN,GAAG;IACH,OAAO;IACP,KAAK,aAAa;GACnB;EACD;EAEA,IAAI,yBAAyB;EAC7B,KAAK,IAAI,IAAI,KAAK,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;GACtD,MAAM,WAAW,KAAK,MAAM,MAAM;GAElC,IAAI,KAAK,SAAS,YAAY,KAAK,SAAS,MACvC;QAAA,SAAS,SAAS,KAAK,MAAM;KAChC,yBAAyB;KACzB;IACD;UACM,IAAI,KAAK,SAAS,SACpB;QAAA,SAAS,SAAS,WAAW,SAAS,KAAK,UAAU,KAAK,KAAK,OAAO;KACzE,yBAAyB;KACzB;IACD;UACM,IAAI,KAAK,SAAS,aACpB;QAAA,SAAS,SAAS,aAKpB;SAAA,OAAO,KAAK,SAAS,YAAY,OAAO,SAAS,SAAS,WACtD,OAAO,QAAQ,KAAK,IAAI,CAAC,CAA2C,OACpE,CAAC,KAAK,WACN,SAAS,OAAO,SAA4C,KAC9D,IACC,KAAK,SAAS,SAAS,MAEX;MACf,yBAAyB;MACzB;KACD;;;EAGH;EAGA,IAAI,KAAK,UAAU,KAAK,KACvB;EAKD,IAAI,KAAK,MAAM,MAAM,uBAAuB,EAAE,QAAQ,KAAK,OAC1D,KAAK,MAAM,MAAM,uBAAuB,CAAC,MAAM,KAAK;OAEpD,KAAK,MAAM,MAAM,KAAK,IAAI;CAE5B;CAEA,WAAW,MAAoB;EAC9B,IAAI,CAAC,KAAK,WAAW,KAAK,KAAK,GAC9B,MAAM,IAAIA,6BAAAA,qBAAqB,0CAA0C;EAG1E,IAAI,KAAK,MAAM,MACd,KAAK,MAAM,QAAQ;OAEnB,KAAK,MAAM,OAAO,KAAK,UAAU;CAEnC;CAEA,QAAuB;EAEtB,KAAK,YAAY;EAIjB,OAAO,KAAK;CACb;;CAGA,cAA4B;EAC3B,IAAI,KAAK,WAAW,KAAK,KAAK,GAC7B,KAAK,MAAM,OAAO,KAAK,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQA,WACC,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextField } from "@prismicio/client";
|
|
2
|
-
|
|
3
2
|
//#region src/lib/RichTextFieldBuilder.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Omits keys from a type, distributing the operation over a union.
|
|
@@ -10,7 +9,6 @@ import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextField } fro
|
|
|
10
9
|
*/
|
|
11
10
|
type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;
|
|
12
11
|
/** An inline node without its `start` and `end` properties. */
|
|
13
|
-
type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
12
|
+
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
14
13
|
//#endregion
|
|
15
|
-
export { RTPartialInlineNode };
|
|
16
14
|
//# 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
1
|
import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextField } from "@prismicio/client";
|
|
2
|
-
|
|
3
2
|
//#region src/lib/RichTextFieldBuilder.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Omits keys from a type, distributing the operation over a union.
|
|
@@ -10,7 +9,6 @@ import { InjectMigrationSpecificTypes, RTInlineNode, RTNode, RichTextField } fro
|
|
|
10
9
|
*/
|
|
11
10
|
type DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never;
|
|
12
11
|
/** An inline node without its `start` and `end` properties. */
|
|
13
|
-
type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
12
|
+
export type RTPartialInlineNode = DistributedOmit<RTInlineNode, "start" | "end">;
|
|
14
13
|
//#endregion
|
|
15
|
-
export { RTPartialInlineNode };
|
|
16
14
|
//# 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 +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<RT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EAGA,IAAI,KAAK,UAAU,KAAK,KACvB;EAKD,IAAI,KAAK,MAAM,MAAM,uBAAuB,EAAE,QAAQ,KAAK,OAC1D,KAAK,MAAM,MAAM,uBAAuB,CAAC,MAAM,KAAK;OAEpD,KAAK,MAAM,MAAM,KAAK,IAAI;CAE5B;CAEA,WAAW,MAAoB;EAC9B,IAAI,CAAC,KAAK,WAAW,KAAK,KAAK,GAC9B,MAAM,IAAI,qBAAqB,0CAA0C;EAG1E,IAAI,KAAK,MAAM,MACd,KAAK,MAAM,QAAQ;OAEnB,KAAK,MAAM,OAAO,KAAK,UAAU;CAEnC;CAEA,QAAuB;EAEtB,KAAK,YAAY;EAIjB,OAAO,KAAK;CACb;;CAGA,cAA4B;EAC3B,IAAI,KAAK,WAAW,KAAK,KAAK,GAC7B,KAAK,MAAM,OAAO,KAAK,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQA,WACC,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
1
|
let _prismicio_client = require("@prismicio/client");
|
|
3
2
|
//#region src/lib/filterRichTextField.ts
|
|
4
3
|
/**
|
|
@@ -48,17 +47,20 @@ const filterTextNode = (node, model, allowedNodeTypes) => {
|
|
|
48
47
|
};
|
|
49
48
|
for (let i = 0; i < node.spans.length; i++) {
|
|
50
49
|
const span = node.spans[i];
|
|
51
|
-
if (allowedNodeTypes.includes(span.type))
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
+
}
|
|
62
64
|
}
|
|
63
65
|
return filteredNode;
|
|
64
66
|
};
|
|
@@ -74,9 +76,11 @@ const filterRichTextField = (richTextField, model) => {
|
|
|
74
76
|
const allowedNodeTypes = getAllowedNodeTypes(model);
|
|
75
77
|
for (let i = 0; i < (model.config && "multi" in model.config ? richTextField.length : 1); i++) {
|
|
76
78
|
const node = richTextField[i];
|
|
77
|
-
if (allowedNodeTypes.includes(node.type))
|
|
78
|
-
|
|
79
|
-
|
|
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
|
+
}
|
|
80
84
|
}
|
|
81
85
|
return nodes;
|
|
82
86
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filterRichTextField.cjs","names":["LinkType","RichTextNodeType"],"sources":["../../src/lib/filterRichTextField.ts"],"sourcesContent":["import type {\n\tCustomTypeModelRichTextField,\n\tRTImageNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n\tTitleField,\n} from \"@prismicio/client\"\nimport { LinkType, RichTextNodeType } from \"@prismicio/client\"\n\n/**\n * Get allowed nodes types from a rich text or title field custom type model.\n *\n * @param model - A rich text or title field custom type model.\n * @returns An array of allowed nodes types.\n */\nconst getAllowedNodeTypes = (model: CustomTypeModelRichTextField): RichTextNodeTypes[] => {\n\tconst allowedNodeTypes: RichTextNodeTypes[] = [] // An empty array means no nodes are allowed by default.\n\n\tif (model.config) {\n\t\tif (\"multi\" in model.config && model.config.multi) {\n\t\t\tallowedNodeTypes.push(...(model.config.multi.split(\",\") as RichTextNodeTypes[]))\n\t\t} else if (\"single\" in model.config && model.config.single) {\n\t\t\tallowedNodeTypes.push(...(model.config.single.split(\",\") as RichTextNodeTypes[]))\n\t\t}\n\n\t\tif (model.config.labels?.length) {\n\t\t\tallowedNodeTypes.push(\"label\")\n\t\t}\n\t}\n\n\treturn allowedNodeTypes\n}\n\n/**\n * Filter an image node based on given model.\n *\n * @param node - An image node to filter.\n * @param model - A rich text or title field custom type model.\n * @returns A filtered image node based on the given model.\n */\nconst filterImageNode = (node: RTImageNode, model: CustomTypeModelRichTextField): RTImageNode => {\n\tif (!model.config?.allowTargetBlank && node.linkTo?.link_type === LinkType.Web) {\n\t\treturn {\n\t\t\t...node,\n\t\t\tlinkTo: {\n\t\t\t\t...node.linkTo,\n\t\t\t\ttarget: undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn node\n}\n\n/**\n * Filter a text node based on given model.\n *\n * @param node - A text node to filter.\n * @param model - A rich text or title field custom type model.\n * @param allowedNodeTypes - An array of allowed nodes types.\n * @returns A filtered text node based on the given model.\n */\nconst filterTextNode = (\n\tnode: RTTextNode,\n\tmodel: CustomTypeModelRichTextField,\n\tallowedNodeTypes: RichTextNodeTypes[],\n): RTTextNode => {\n\tconst filteredNode: RTTextNode = {\n\t\t...node,\n\t\tspans: [],\n\t}\n\n\tfor (let i = 0; i < node.spans.length; i++) {\n\t\tconst span = node.spans[i]\n\n\t\tif (allowedNodeTypes.includes(span.type)) {\n\t\t\tif (span.type === RichTextNodeType.hyperlink) {\n\t\t\t\tif (!model.config?.allowTargetBlank && span.data.link_type === LinkType.Web) {\n\t\t\t\t\tfilteredNode.spans.push({\n\t\t\t\t\t\t...span,\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t...span.data,\n\t\t\t\t\t\t\ttarget: undefined,\n\t\t\t\t\t\t},\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else if (span.type === RichTextNodeType.label) {\n\t\t\t\tif (model.config?.labels?.includes(span.data.label)) {\n\t\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tfilteredNode.spans.push(span)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn filteredNode\n}\n\n/**\n * Filter a rich text field based on given model.\n *\n * @param richTextField - A rich text or title field from Prismic.\n * @param model - A rich text or title field custom type model.\n * @returns A rich text or title field filtered based on the given model.\n */\nexport const filterRichTextField = <Field extends RichTextField | TitleField>(\n\trichTextField: Field,\n\tmodel: CustomTypeModelRichTextField,\n): Field => {\n\tconst nodes: RTNode[] = []\n\n\tconst allowedNodeTypes = getAllowedNodeTypes(model)\n\n\tfor (\n\t\tlet i = 0;\n\t\t// Only process the first node if it's a \"single\" node rich text type\n\t\ti < (model.config && \"multi\" in model.config ? richTextField.length : 1);\n\t\ti++\n\t) {\n\t\tconst node = richTextField[i]\n\n\t\tif (allowedNodeTypes.includes(node.type)) {\n\t\t\tif (node.type === RichTextNodeType.image) {\n\t\t\t\tnodes.push(filterImageNode(node, model))\n\t\t\t} else if (node.type === RichTextNodeType.embed) {\n\t\t\t\tnodes.push(node)\n\t\t\t} else {\n\t\t\t\tnodes.push(filterTextNode(node, model, allowedNodeTypes))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nodes as Field\n}\n"],"mappings":"
|
|
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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I;EAE9B;CACD;CAEA,OAAO;AACR;;;;;;;;AASA,MAAa,uBACZ,eACA,UACW;CACX,MAAM,QAAkB,CAAC;CAEzB,MAAM,mBAAmB,oBAAoB,KAAK;CAElD,KACC,IAAI,IAAI,GAER,KAAK,MAAM,UAAU,WAAW,MAAM,SAAS,cAAc,SAAS,IACtE,KACC;EACD,MAAM,OAAO,cAAc;EAE3B,IAAI,iBAAiB,SAAS,KAAK,IAAI,GAAG;GACzC,IAAI,KAAK,SAASA,kBAAAA,iBAAiB,OAClC,MAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;QACjC,IAAI,KAAK,SAASA,kBAAAA,iBAAiB,OACzC,MAAM,KAAK,IAAI;QAEf,MAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;EAE1D;CACD;CAEA,OAAO;AACR"}
|
|
@@ -47,17 +47,20 @@ const filterTextNode = (node, model, allowedNodeTypes) => {
|
|
|
47
47
|
};
|
|
48
48
|
for (let i = 0; i < node.spans.length; i++) {
|
|
49
49
|
const span = node.spans[i];
|
|
50
|
-
if (allowedNodeTypes.includes(span.type))
|
|
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,9 +76,11 @@ const filterRichTextField = (richTextField, model) => {
|
|
|
73
76
|
const allowedNodeTypes = getAllowedNodeTypes(model);
|
|
74
77
|
for (let i = 0; i < (model.config && "multi" in model.config ? richTextField.length : 1); i++) {
|
|
75
78
|
const node = richTextField[i];
|
|
76
|
-
if (allowedNodeTypes.includes(node.type))
|
|
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
|
};
|
|
@@ -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,
|
|
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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AK,CAAC;QACjC,IAAI,KAAK,SAAS,iBAAiB,OACzC,MAAM,KAAK,IAAI;QAEf,MAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;EAE1D;CACD;CAEA,OAAO;AACR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastSerializerHelpers.cjs","names":["PrismicRichTextSerializerError","OEmbedType","LinkType"],"sources":["../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { LinkType, OEmbedType } from \"@prismicio/client\"\nimport type { RTEmbedNode, RTImageNode, RTLinkNode } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport { toHtml } from \"hast-util-to-html\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\nimport type { RTPartialInlineNode } from \"./RichTextFieldBuilder\"\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTImageNode}.\n *\n * @param node - A hast node to serialize.\n * @returns Equivalent {@link RTImageNode}.\n */\nexport const serializeImage = (node: Element): RTImageNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\"Element of type `img` is missing an `src` attribute\")\n\t}\n\n\tconst url = new URL(src, \"https://noop.com\")\n\n\tlet width = node.properties?.width as number | undefined\n\tlet height = node.properties?.height as number | undefined\n\tlet x = 0\n\tlet y = 0\n\tlet zoom = 1\n\n\t// Attempt to infer the image dimensions from the URL imgix parameters.\n\tif (url.hostname === \"images.prismic.io\") {\n\t\tif (url.searchParams.has(\"w\")) {\n\t\t\twidth = Number(url.searchParams.get(\"w\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"h\")) {\n\t\t\theight = Number(url.searchParams.get(\"h\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"rect\")) {\n\t\t\tconst [rectX, rextY, rectW, _rectH] = url.searchParams.get(\"rect\")!.split(\",\")\n\n\t\t\tx = Number(rectX)\n\t\t\ty = Number(rextY)\n\n\t\t\t// This is not perfect but it's supposed to work on images without constraints.\n\t\t\tif (width) {\n\t\t\t\tzoom = Math.max(1, width / Number(rectW))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\ttype: \"image\",\n\t\tid: \"\",\n\t\turl: src,\n\t\talt: (node.properties?.alt as string) ?? \"\",\n\t\tcopyright: (node.properties?.copyright as string) ?? null,\n\t\t// This is not accurate. We're doing the casting because it seems the migration API\n\t\t// accepts `undefined` values for those fields which is more convenient to us here.\n\t\t// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185\n\t\tdimensions: { width: width as number, height: height as number },\n\t\tedit: { x, y, zoom, background: \"transparent\" },\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTEmbedNode}.\n *\n * @param node - A hast node to serialize.\n * @returns Equivalent {@link RTEmbedNode}.\n */\nexport const serializeEmbed = (node: Element): RTEmbedNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\"Element of type `embed` is missing an `src` attribute\",\n\t\t)\n\t}\n\n\tconst oembedBase = {\n\t\tversion: \"1.0\",\n\t\tembed_url: src,\n\t\thtml: toHtml(node),\n\t\ttitle: node.properties?.title as string | undefined,\n\t}\n\n\tconst width = node.properties?.width as number | undefined\n\tconst height = node.properties?.height as number | undefined\n\n\t// Remove the children of the embed node as we don't want to process them.\n\tnode.children = []\n\n\tif (width && height) {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Rich, width, height },\n\t\t}\n\t} else {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Link },\n\t\t}\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.\n *\n * @param node - A hast node to serialize.\n * @param rtPartialInlineNode - A partial rich text node.\n * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (rtPartialInlineNode.type === \"hyperlink\" && !(\"data\" in rtPartialInlineNode)) {\n\t\tconst url = node.properties?.href as string | undefined\n\t\tif (!url) {\n\t\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\t\"Element of type `hyperlink` is missing an `href` attribute\",\n\t\t\t)\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"hyperlink\",\n\t\t\tdata: {\n\t\t\t\tlink_type: LinkType.Web,\n\t\t\t\turl,\n\t\t\t\ttarget: node.properties?.target as string | undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn rtPartialInlineNode\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"hastSerializerHelpers.cjs","names":["PrismicRichTextSerializerError","toHtml","OEmbedType","LinkType"],"sources":["../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { LinkType, OEmbedType } from \"@prismicio/client\"\nimport type { RTEmbedNode, RTImageNode, RTLinkNode } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport { toHtml } from \"hast-util-to-html\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\nimport type { RTPartialInlineNode } from \"./RichTextFieldBuilder\"\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTImageNode}.\n *\n * @param node - A hast node to serialize.\n * @returns Equivalent {@link RTImageNode}.\n */\nexport const serializeImage = (node: Element): RTImageNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\"Element of type `img` is missing an `src` attribute\")\n\t}\n\n\tconst url = new URL(src, \"https://noop.com\")\n\n\tlet width = node.properties?.width as number | undefined\n\tlet height = node.properties?.height as number | undefined\n\tlet x = 0\n\tlet y = 0\n\tlet zoom = 1\n\n\t// Attempt to infer the image dimensions from the URL imgix parameters.\n\tif (url.hostname === \"images.prismic.io\") {\n\t\tif (url.searchParams.has(\"w\")) {\n\t\t\twidth = Number(url.searchParams.get(\"w\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"h\")) {\n\t\t\theight = Number(url.searchParams.get(\"h\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"rect\")) {\n\t\t\tconst [rectX, rextY, rectW, _rectH] = url.searchParams.get(\"rect\")!.split(\",\")\n\n\t\t\tx = Number(rectX)\n\t\t\ty = Number(rextY)\n\n\t\t\t// This is not perfect but it's supposed to work on images without constraints.\n\t\t\tif (width) {\n\t\t\t\tzoom = Math.max(1, width / Number(rectW))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\ttype: \"image\",\n\t\tid: \"\",\n\t\turl: src,\n\t\talt: (node.properties?.alt as string) ?? \"\",\n\t\tcopyright: (node.properties?.copyright as string) ?? null,\n\t\t// This is not accurate. We're doing the casting because it seems the migration API\n\t\t// accepts `undefined` values for those fields which is more convenient to us here.\n\t\t// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185\n\t\tdimensions: { width: width as number, height: height as number },\n\t\tedit: { x, y, zoom, background: \"transparent\" },\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTEmbedNode}.\n *\n * @param node - A hast node to serialize.\n * @returns Equivalent {@link RTEmbedNode}.\n */\nexport const serializeEmbed = (node: Element): RTEmbedNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\"Element of type `embed` is missing an `src` attribute\",\n\t\t)\n\t}\n\n\tconst oembedBase = {\n\t\tversion: \"1.0\",\n\t\tembed_url: src,\n\t\thtml: toHtml(node),\n\t\ttitle: node.properties?.title as string | undefined,\n\t}\n\n\tconst width = node.properties?.width as number | undefined\n\tconst height = node.properties?.height as number | undefined\n\n\t// Remove the children of the embed node as we don't want to process them.\n\tnode.children = []\n\n\tif (width && height) {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Rich, width, height },\n\t\t}\n\t} else {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Link },\n\t\t}\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.\n *\n * @param node - A hast node to serialize.\n * @param rtPartialInlineNode - A partial rich text node.\n * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (rtPartialInlineNode.type === \"hyperlink\" && !(\"data\" in rtPartialInlineNode)) {\n\t\tconst url = node.properties?.href as string | undefined\n\t\tif (!url) {\n\t\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\t\"Element of type `hyperlink` is missing an `href` attribute\",\n\t\t\t)\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"hyperlink\",\n\t\t\tdata: {\n\t\t\t\tlink_type: LinkType.Web,\n\t\t\t\turl,\n\t\t\t\ttarget: node.properties?.target as string | undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn rtPartialInlineNode\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAa,kBAAkB,SAA+B;CAC7D,MAAM,MAAM,KAAK,YAAY;CAE7B,IAAI,CAAC,KACJ,MAAM,IAAIA,uCAAAA,+BAA+B,qDAAqD;CAG/F,MAAM,MAAM,IAAI,IAAI,KAAK,kBAAkB;CAE3C,IAAI,QAAQ,KAAK,YAAY;CAC7B,IAAI,SAAS,KAAK,YAAY;CAC9B,IAAI,IAAI;CACR,IAAI,IAAI;CACR,IAAI,OAAO;CAGX,IAAI,IAAI,aAAa,qBAAqB;EACzC,IAAI,IAAI,aAAa,IAAI,GAAG,GAC3B,QAAQ,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;EAGzC,IAAI,IAAI,aAAa,IAAI,GAAG,GAC3B,SAAS,OAAO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N,MAAM;GACN,MAAM;IACL,WAAWG,kBAAAA,SAAS;IACpB;IACA,QAAQ,KAAK,YAAY;GAC1B;EACD;CACD;CAEA,OAAO;AACR"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastSerializerHelpers.js","names":[],"sources":["../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { LinkType, OEmbedType } from \"@prismicio/client\"\nimport type { RTEmbedNode, RTImageNode, RTLinkNode } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport { toHtml } from \"hast-util-to-html\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\nimport type { RTPartialInlineNode } from \"./RichTextFieldBuilder\"\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTImageNode}.\n *\n * @param node - A hast node to serialize.\n * @returns Equivalent {@link RTImageNode}.\n */\nexport const serializeImage = (node: Element): RTImageNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\"Element of type `img` is missing an `src` attribute\")\n\t}\n\n\tconst url = new URL(src, \"https://noop.com\")\n\n\tlet width = node.properties?.width as number | undefined\n\tlet height = node.properties?.height as number | undefined\n\tlet x = 0\n\tlet y = 0\n\tlet zoom = 1\n\n\t// Attempt to infer the image dimensions from the URL imgix parameters.\n\tif (url.hostname === \"images.prismic.io\") {\n\t\tif (url.searchParams.has(\"w\")) {\n\t\t\twidth = Number(url.searchParams.get(\"w\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"h\")) {\n\t\t\theight = Number(url.searchParams.get(\"h\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"rect\")) {\n\t\t\tconst [rectX, rextY, rectW, _rectH] = url.searchParams.get(\"rect\")!.split(\",\")\n\n\t\t\tx = Number(rectX)\n\t\t\ty = Number(rextY)\n\n\t\t\t// This is not perfect but it's supposed to work on images without constraints.\n\t\t\tif (width) {\n\t\t\t\tzoom = Math.max(1, width / Number(rectW))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\ttype: \"image\",\n\t\tid: \"\",\n\t\turl: src,\n\t\talt: (node.properties?.alt as string) ?? \"\",\n\t\tcopyright: (node.properties?.copyright as string) ?? null,\n\t\t// This is not accurate. We're doing the casting because it seems the migration API\n\t\t// accepts `undefined` values for those fields which is more convenient to us here.\n\t\t// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185\n\t\tdimensions: { width: width as number, height: height as number },\n\t\tedit: { x, y, zoom, background: \"transparent\" },\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTEmbedNode}.\n *\n * @param node - A hast node to serialize.\n * @returns Equivalent {@link RTEmbedNode}.\n */\nexport const serializeEmbed = (node: Element): RTEmbedNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\"Element of type `embed` is missing an `src` attribute\",\n\t\t)\n\t}\n\n\tconst oembedBase = {\n\t\tversion: \"1.0\",\n\t\tembed_url: src,\n\t\thtml: toHtml(node),\n\t\ttitle: node.properties?.title as string | undefined,\n\t}\n\n\tconst width = node.properties?.width as number | undefined\n\tconst height = node.properties?.height as number | undefined\n\n\t// Remove the children of the embed node as we don't want to process them.\n\tnode.children = []\n\n\tif (width && height) {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Rich, width, height },\n\t\t}\n\t} else {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Link },\n\t\t}\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.\n *\n * @param node - A hast node to serialize.\n * @param rtPartialInlineNode - A partial rich text node.\n * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (rtPartialInlineNode.type === \"hyperlink\" && !(\"data\" in rtPartialInlineNode)) {\n\t\tconst url = node.properties?.href as string | undefined\n\t\tif (!url) {\n\t\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\t\"Element of type `hyperlink` is missing an `href` attribute\",\n\t\t\t)\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"hyperlink\",\n\t\t\tdata: {\n\t\t\t\tlink_type: LinkType.Web,\n\t\t\t\turl,\n\t\t\t\ttarget: node.properties?.target as string | undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn rtPartialInlineNode\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAa,kBAAkB,SAA+B;CAC7D,MAAM,MAAM,KAAK,YAAY;
|
|
1
|
+
{"version":3,"file":"hastSerializerHelpers.js","names":[],"sources":["../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { LinkType, OEmbedType } from \"@prismicio/client\"\nimport type { RTEmbedNode, RTImageNode, RTLinkNode } from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport { toHtml } from \"hast-util-to-html\"\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\"\nimport type { RTPartialInlineNode } from \"./RichTextFieldBuilder\"\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTImageNode}.\n *\n * @param node - A hast node to serialize.\n * @returns Equivalent {@link RTImageNode}.\n */\nexport const serializeImage = (node: Element): RTImageNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\"Element of type `img` is missing an `src` attribute\")\n\t}\n\n\tconst url = new URL(src, \"https://noop.com\")\n\n\tlet width = node.properties?.width as number | undefined\n\tlet height = node.properties?.height as number | undefined\n\tlet x = 0\n\tlet y = 0\n\tlet zoom = 1\n\n\t// Attempt to infer the image dimensions from the URL imgix parameters.\n\tif (url.hostname === \"images.prismic.io\") {\n\t\tif (url.searchParams.has(\"w\")) {\n\t\t\twidth = Number(url.searchParams.get(\"w\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"h\")) {\n\t\t\theight = Number(url.searchParams.get(\"h\"))\n\t\t}\n\n\t\tif (url.searchParams.has(\"rect\")) {\n\t\t\tconst [rectX, rextY, rectW, _rectH] = url.searchParams.get(\"rect\")!.split(\",\")\n\n\t\t\tx = Number(rectX)\n\t\t\ty = Number(rextY)\n\n\t\t\t// This is not perfect but it's supposed to work on images without constraints.\n\t\t\tif (width) {\n\t\t\t\tzoom = Math.max(1, width / Number(rectW))\n\t\t\t}\n\t\t}\n\t}\n\n\treturn {\n\t\ttype: \"image\",\n\t\tid: \"\",\n\t\turl: src,\n\t\talt: (node.properties?.alt as string) ?? \"\",\n\t\tcopyright: (node.properties?.copyright as string) ?? null,\n\t\t// This is not accurate. We're doing the casting because it seems the migration API\n\t\t// accepts `undefined` values for those fields which is more convenient to us here.\n\t\t// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185\n\t\tdimensions: { width: width as number, height: height as number },\n\t\tedit: { x, y, zoom, background: \"transparent\" },\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTEmbedNode}.\n *\n * @param node - A hast node to serialize.\n * @returns Equivalent {@link RTEmbedNode}.\n */\nexport const serializeEmbed = (node: Element): RTEmbedNode => {\n\tconst src = node.properties?.src as string | undefined\n\n\tif (!src) {\n\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\"Element of type `embed` is missing an `src` attribute\",\n\t\t)\n\t}\n\n\tconst oembedBase = {\n\t\tversion: \"1.0\",\n\t\tembed_url: src,\n\t\thtml: toHtml(node),\n\t\ttitle: node.properties?.title as string | undefined,\n\t}\n\n\tconst width = node.properties?.width as number | undefined\n\tconst height = node.properties?.height as number | undefined\n\n\t// Remove the children of the embed node as we don't want to process them.\n\tnode.children = []\n\n\tif (width && height) {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Rich, width, height },\n\t\t}\n\t} else {\n\t\treturn {\n\t\t\ttype: \"embed\",\n\t\t\toembed: { ...oembedBase, type: OEmbedType.Link },\n\t\t}\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.\n *\n * @param node - A hast node to serialize.\n * @param rtPartialInlineNode - A partial rich text node.\n * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (rtPartialInlineNode.type === \"hyperlink\" && !(\"data\" in rtPartialInlineNode)) {\n\t\tconst url = node.properties?.href as string | undefined\n\t\tif (!url) {\n\t\t\tthrow new PrismicRichTextSerializerError(\n\t\t\t\t\"Element of type `hyperlink` is missing an `href` attribute\",\n\t\t\t)\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"hyperlink\",\n\t\t\tdata: {\n\t\t\t\tlink_type: LinkType.Web,\n\t\t\t\turl,\n\t\t\t\ttarget: node.properties?.target as string | undefined,\n\t\t\t},\n\t\t}\n\t}\n\n\treturn rtPartialInlineNode\n}\n"],"mappings":";;;;;;;;;;AAcA,MAAa,kBAAkB,SAA+B;CAC7D,MAAM,MAAM,KAAK,YAAY;CAE7B,IAAI,CAAC,KACJ,MAAM,IAAI,+BAA+B,qDAAqD;CAG/F,MAAM,MAAM,IAAI,IAAI,KAAK,kBAAkB;CAE3C,IAAI,QAAQ,KAAK,YAAY;CAC7B,IAAI,SAAS,KAAK,YAAY;CAC9B,IAAI,IAAI;CACR,IAAI,IAAI;CACR,IAAI,OAAO;CAGX,IAAI,IAAI,aAAa,qBAAqB;EACzC,IAAI,IAAI,aAAa,IAAI,GAAG,GAC3B,QAAQ,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;EAGzC,IAAI,IAAI,aAAa,IAAI,GAAG,GAC3B,SAAS,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;EAG1C,IAAI,IAAI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-1,4 +1,3 @@
|
|
|
1
|
-
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
1
|
const require_PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
|
|
3
2
|
const require_hastSerializerHelpers = require("./hastSerializerHelpers.cjs");
|
|
4
3
|
const require_RichTextFieldBuilder = require("./RichTextFieldBuilder.cjs");
|