@prismicio/migrate 0.0.2-canary.6b4d958 → 0.0.2-pr.4.3964365
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.map +1 -1
- 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 +0 -1
- package/dist/errors/PrismicRichTextSerializerError.d.cts.map +1 -1
- package/dist/errors/PrismicRichTextSerializerError.d.ts +0 -1
- 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 +2 -2
- package/dist/htmlAsRichText.d.cts.map +1 -1
- package/dist/htmlAsRichText.d.ts +2 -2
- 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 +0 -1
- package/dist/lib/RichTextFieldBuilder.d.cts.map +1 -1
- package/dist/lib/RichTextFieldBuilder.d.ts +0 -1
- package/dist/lib/RichTextFieldBuilder.d.ts.map +1 -1
- package/dist/lib/RichTextFieldBuilder.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +0 -1
- package/dist/lib/filterRichTextField.cjs.map +1 -1
- 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 +1 -0
- package/dist/lib/hastToRichText.d.cts.map +1 -1
- package/dist/lib/hastToRichText.d.ts +1 -0
- 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 +3 -1
- package/dist/lib/rehypeRichText.d.cts.map +1 -1
- package/dist/lib/rehypeRichText.d.ts +3 -1
- 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 +6 -4
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.ts +6 -4
- package/dist/types.d.ts.map +1 -1
- package/package.json +16 -16
- 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":";cAAa,
|
|
1
|
+
{"version":3,"file":"PrismicRichTextError.d.cts","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"mappings":";cAAa,6BAA6B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicRichTextError.d.ts","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"mappings":";cAAa,
|
|
1
|
+
{"version":3,"file":"PrismicRichTextError.d.ts","names":[],"sources":["../../src/errors/PrismicRichTextError.ts"],"mappings":";cAAa,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 +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":";;cAEa,uCAAuC"}
|
|
@@ -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":";;cAEa,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,7 +1,7 @@
|
|
|
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
7
|
type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
@@ -19,5 +19,5 @@ type HTMLAsRichTextReturnType = {
|
|
|
19
19
|
*/
|
|
20
20
|
declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
21
21
|
//#endregion
|
|
22
|
-
export { htmlAsRichText };
|
|
22
|
+
export { HTMLAsRichTextConfig, htmlAsRichText };
|
|
23
23
|
//# sourceMappingURL=htmlAsRichText.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"htmlAsRichText.d.cts","names":[],"sources":["../src/htmlAsRichText.ts"],"mappings":";;;;;;KAgEY,
|
|
1
|
+
{"version":3,"file":"htmlAsRichText.d.cts","names":[],"sources":["../src/htmlAsRichText.ts"],"mappings":";;;;;;KAgEY,uBAAuB;;KAG9B;EACJ,QAAQ;EACR;;;;;;;;;cAUY,iBAAc,cACd,SACH,yBACP"}
|
package/dist/htmlAsRichText.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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
7
|
type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
@@ -19,5 +19,5 @@ type HTMLAsRichTextReturnType = {
|
|
|
19
19
|
*/
|
|
20
20
|
declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
21
21
|
//#endregion
|
|
22
|
-
export { htmlAsRichText };
|
|
22
|
+
export { HTMLAsRichTextConfig, htmlAsRichText };
|
|
23
23
|
//# sourceMappingURL=htmlAsRichText.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"htmlAsRichText.d.ts","names":[],"sources":["../src/htmlAsRichText.ts"],"mappings":";;;;;;KAgEY,
|
|
1
|
+
{"version":3,"file":"htmlAsRichText.d.ts","names":[],"sources":["../src/htmlAsRichText.ts"],"mappings":";;;;;;KAgEY,uBAAuB;;KAG9B;EACJ,QAAQ;EACR;;;;;;;;;cAUY,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":["#last","#nodes","PrismicRichTextError"],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tInjectMigrationSpecificTypes,\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\"\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Omit<ObjectType, KeyType>\n\t: never\n\n/** An inline node without its `start` and `end` properties. */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: InjectMigrationSpecificTypes<RTNode>[] = []\n\n\tget #last(): InjectMigrationSpecificTypes<RTNode> | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: InjectMigrationSpecificTypes<RTNode>): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(type: RTTextNode[\"type\"], direction: \"ltr\" | \"rtl\" = \"ltr\"): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t})\n\t}\n\n\tappendSpan(span: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<RTInlineNode | RTPartialInlineNode>,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\")\n\t\t}\n\n\t\tlet span: InjectMigrationSpecificTypes<RTInlineNode>\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t}\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i]\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (lastSpan.type === \"label\" && lastSpan.data.label === span.data.label) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink =\n\t\t\t\t\t\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (Object.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]).every(\n\t\t\t\t\t\t\t\t\t([key, value]) =>\n\t\t\t\t\t\t\t\t\t\tlastSpan.data?.[key as keyof (typeof lastSpan)[\"data\"]] === value,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: span.data === lastSpan.data\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Ignore empty spans.\n\t\tif (span.start === span.end) {\n\t\t\treturn\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end\n\t\t} else {\n\t\t\tthis.#last.spans.push(span)\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot append text to non-text last node\")\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart()\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast()\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField\n\t}\n\n\t/** Cleans up the current last node. */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd()\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - Rich text node to check.\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(\n\t\tnode?: InjectMigrationSpecificTypes<RTNode>,\n\t): node is InjectMigrationSpecificTypes<RTTextNode> {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\"\n\t}\n}\n"],"mappings":";;AAyBA,IAAa,uBAAb,MAAkC;CACjC,SAAiD,
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.cjs","names":["#last","#nodes","PrismicRichTextError"],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tInjectMigrationSpecificTypes,\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\"\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Omit<ObjectType, KeyType>\n\t: never\n\n/** An inline node without its `start` and `end` properties. */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: InjectMigrationSpecificTypes<RTNode>[] = []\n\n\tget #last(): InjectMigrationSpecificTypes<RTNode> | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: InjectMigrationSpecificTypes<RTNode>): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(type: RTTextNode[\"type\"], direction: \"ltr\" | \"rtl\" = \"ltr\"): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t})\n\t}\n\n\tappendSpan(span: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<RTInlineNode | RTPartialInlineNode>,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\")\n\t\t}\n\n\t\tlet span: InjectMigrationSpecificTypes<RTInlineNode>\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t}\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i]\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (lastSpan.type === \"label\" && lastSpan.data.label === span.data.label) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink =\n\t\t\t\t\t\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (Object.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]).every(\n\t\t\t\t\t\t\t\t\t([key, value]) =>\n\t\t\t\t\t\t\t\t\t\tlastSpan.data?.[key as keyof (typeof lastSpan)[\"data\"]] === value,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: span.data === lastSpan.data\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Ignore empty spans.\n\t\tif (span.start === span.end) {\n\t\t\treturn\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end\n\t\t} else {\n\t\t\tthis.#last.spans.push(span)\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot append text to non-text last node\")\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart()\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast()\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField\n\t}\n\n\t/** Cleans up the current last node. */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd()\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - Rich text node to check.\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(\n\t\tnode?: InjectMigrationSpecificTypes<RTNode>,\n\t): node is InjectMig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;MACf,yBAAyB;MACzB;KACD;;;EAGH;EAGA,IAAI,KAAK,UAAU,KAAK,KACvB;EAKD,IAAI,KAAKA,MAAM,MAAM,uBAAuB,EAAE,QAAQ,KAAK,OAC1D,KAAKA,MAAM,MAAM,uBAAuB,CAAC,MAAM,KAAK;OAEpD,KAAKA,MAAM,MAAM,KAAK,IAAI;CAE5B;CAEA,WAAW,MAAoB;EAC9B,IAAI,CAAC,KAAK,WAAW,KAAKA,KAAK,GAC9B,MAAM,IAAIE,6BAAAA,qBAAqB,0CAA0C;EAG1E,IAAI,KAAKF,MAAM,MACd,KAAKA,MAAM,QAAQ;OAEnB,KAAKA,MAAM,OAAO,KAAK,UAAU;CAEnC;CAEA,QAAuB;EAEtB,KAAK,YAAY;EAIjB,OAAO,KAAKC;CACb;;CAGA,cAA4B;EAC3B,IAAI,KAAK,WAAW,KAAKD,KAAK,GAC7B,KAAKA,MAAM,OAAO,KAAKA,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQA,WACC,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
|
|
@@ -1 +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;;KAIR,sBAAsB,gBAAgB"}
|
|
@@ -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;;KAIR,sBAAsB,gBAAgB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextFieldBuilder.js","names":["#last","#nodes"],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tInjectMigrationSpecificTypes,\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\"\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Omit<ObjectType, KeyType>\n\t: never\n\n/** An inline node without its `start` and `end` properties. */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: InjectMigrationSpecificTypes<RTNode>[] = []\n\n\tget #last(): InjectMigrationSpecificTypes<RTNode> | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: InjectMigrationSpecificTypes<RTNode>): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(type: RTTextNode[\"type\"], direction: \"ltr\" | \"rtl\" = \"ltr\"): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t})\n\t}\n\n\tappendSpan(span: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<RTInlineNode | RTPartialInlineNode>,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\")\n\t\t}\n\n\t\tlet span: InjectMigrationSpecificTypes<RTInlineNode>\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t}\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i]\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (lastSpan.type === \"label\" && lastSpan.data.label === span.data.label) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink =\n\t\t\t\t\t\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (Object.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]).every(\n\t\t\t\t\t\t\t\t\t([key, value]) =>\n\t\t\t\t\t\t\t\t\t\tlastSpan.data?.[key as keyof (typeof lastSpan)[\"data\"]] === value,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: span.data === lastSpan.data\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Ignore empty spans.\n\t\tif (span.start === span.end) {\n\t\t\treturn\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end\n\t\t} else {\n\t\t\tthis.#last.spans.push(span)\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot append text to non-text last node\")\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart()\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast()\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField\n\t}\n\n\t/** Cleans up the current last node. */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd()\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - Rich text node to check.\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(\n\t\tnode?: InjectMigrationSpecificTypes<RTNode>,\n\t): node is InjectMigrationSpecificTypes<RTTextNode> {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\"\n\t}\n}\n"],"mappings":";;AAyBA,IAAa,uBAAb,MAAkC;CACjC,SAAiD,
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.js","names":["#last","#nodes"],"sources":["../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tInjectMigrationSpecificTypes,\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\"\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<ObjectType, KeyType extends keyof ObjectType> = ObjectType extends unknown\n\t? Omit<ObjectType, KeyType>\n\t: never\n\n/** An inline node without its `start` and `end` properties. */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: InjectMigrationSpecificTypes<RTNode>[] = []\n\n\tget #last(): InjectMigrationSpecificTypes<RTNode> | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: InjectMigrationSpecificTypes<RTNode>): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(type: RTTextNode[\"type\"], direction: \"ltr\" | \"rtl\" = \"ltr\"): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t})\n\t}\n\n\tappendSpan(span: InjectMigrationSpecificTypes<RTInlineNode>): void\n\tappendSpan(partialSpan: InjectMigrationSpecificTypes<RTPartialInlineNode>, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: InjectMigrationSpecificTypes<RTInlineNode | RTPartialInlineNode>,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\")\n\t\t}\n\n\t\tlet span: InjectMigrationSpecificTypes<RTInlineNode>\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t}\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i]\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (lastSpan.type === \"label\" && lastSpan.data.label === span.data.label) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink =\n\t\t\t\t\t\ttypeof span.data === \"object\" && typeof lastSpan.data === \"object\"\n\t\t\t\t\t\t\t? (Object.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]).every(\n\t\t\t\t\t\t\t\t\t([key, value]) =>\n\t\t\t\t\t\t\t\t\t\tlastSpan.data?.[key as keyof (typeof lastSpan)[\"data\"]] === value,\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t: span.data === lastSpan.data\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Ignore empty spans.\n\t\tif (span.start === span.end) {\n\t\t\treturn\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end\n\t\t} else {\n\t\t\tthis.#last.spans.push(span)\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot append text to non-text last node\")\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart()\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast()\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField\n\t}\n\n\t/** Cleans up the current last node. */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd()\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - Rich text node to check.\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(\n\t\tnode?: InjectMigrationSpecificTypes<RTNode>,\n\t): node is InjectMigrationSpecificTypes<RTTextNode> {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\"\n\t}\n}\n"],"mappings":";;AAyBA,IAAa,uBAAb,MAAkC;CACjC,SAAiD,CAAC;CAElD,IAAIA,QAA0D;EAC7D,OAAO,KAAKC,OAAO,KAAKA,OAAO,SAAS;CACzC;CAEA,WAAW,MAAkD;EAC5D,KAAK,YAAY;EAEjB,KAAKA,OAAO,KAAK,IAAI;CACtB;CAEA,eAAe,MAA0B,YAA2B,OAAa;EAChF,KAAK,WAAW;GACf;GACA,MAAM;GACN,OAAO,CAAC;GACR;EACD,CAAC;CACF;CAIA,WACC,mBACA,Q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AAU;CAEnC;CAEA,QAAuB;EAEtB,KAAK,YAAY;EAIjB,OAAO,KAAKC;CACb;;CAGA,cAA4B;EAC3B,IAAI,KAAK,WAAW,KAAKD,KAAK,GAC7B,KAAKA,MAAM,OAAO,KAAKA,MAAM,KAAK,QAAQ;CAE5C;;;;;;;CAQA,WACC,MACmD;EACnD,OAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;CACzD;AACD"}
|
|
@@ -1 +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,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;AAC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Content":["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,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,GACtC,IAAI,KAAK,SAAS,iBAAiB,WAClC,IAAI,CAAC,MAAM,QAAQ,oBAAoB,KAAK,KAAK,cAAc,SAAS,KACvE,aAAa,MAAM,KAAK;GACvB,GAAG;GACH,MAAM;IACL,GAAG,KAAK;IACR,QAAQ,KAAA;GACT;EACD,CAAC;OAED,aAAa,MAAM,KAAK,IAAI;OAEvB,IAAI,KAAK,SAAS,iBAAiB,OACrC;OAAA,MAAM,QAAQ,QAAQ,SAAS,KAAK,KAAK,KAAK,GACjD,aAAa,MAAM,KAAK,IAAI;EAAA,OAG7B,aAAa,MAAM,KAAK,IAAI;CAG/B;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,GACtC,IAAI,KAAK,SAAS,iBAAiB,OAClC,MAAM,KAAK,gBAAgB,MAAM,KAAK,CAAC;OACjC,IAAI,KAAK,SAAS,iBAAiB,OACzC,MAAM,KAAK,IAAI;OAEf,MAAM,KAAK,eAAe,MAAM,OAAO,gBAAgB,CAAC;CAG3D;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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AW;EACX,OAAA,GAAMC,kBAAAA,OAAAA,CAAO,IAAI;EACjB,OAAO,KAAK,YAAY;CACzB;CAEA,MAAM,QAAQ,KAAK,YAAY;CAC/B,MAAM,SAAS,KAAK,YAAY;CAGhC,KAAK,WAAW,CAAC;CAEjB,IAAI,SAAS,QACZ,OAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAMC,kBAAAA,WAAW;GAAM;GAAO;EAAO;CAC/D;MAEA,OAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAMA,kBAAAA,WAAW;EAAK;CAChD;AAEF;;;;;;;;AASA,MAAa,iBACZ,MACA,wBACyB;CACzB,IAAI,oBAAoB,SAAS,eAAe,EAAE,UAAU,sBAAsB;EACjF,MAAM,MAAM,KAAK,YAAY;EAC7B,IAAI,CAAC,KACJ,MAAM,IAAIF,uCAAAA,+BACT,4DACD;EAGD,OAAO;GACN,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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EA,MAAM,QAAQ,KAAK,YAAY;CAC/B,MAAM,SAAS,KAAK,YAAY;CAGhC,KAAK,WAAW,CAAC;CAEjB,IAAI,SAAS,QACZ,OAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAM,WAAW;GAAM;GAAO;EAAO;CAC/D;MAEA,OAAO;EACN,MAAM;EACN,QAAQ;GAAE,GAAG;GAAY,MAAM,WAAW;EAAK;CAChD;AAEF;;;;;;;;AASA,MAAa,iBACZ,MACA,wBACyB;CACzB,IAAI,oBAAoB,SAAS,eAAe,EAAE,UAAU,sBAAsB;EACjF,MAAM,MAAM,KAAK,YAAY;EAC7B,IAAI,CAAC,KACJ,MAAM,IAAI,+BACT,4DACD;EAGD,OAAO;GACN,MAAM;GACN,MAAM;IACL,WAAW,SAAS;IACpB;IACA,QAAQ,KAAK,YAAY;GAC1B;EACD;CACD;CAEA,OAAO;AACR"}
|
|
@@ -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");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastToRichText.cjs","names":["RichTextFieldBuilder","RichTextNodeType","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\ttry {\n\t\t\t\tbuilder.appendText(\"\\n\")\n\t\t\t} catch {\n\t\t\t\t// noop\n\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\ttry {\n\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, 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(shorthandOrNode, length)\n\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\ttry {\n\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t} catch {\n\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// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\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\ttry {\n\t\t\t\tbuilder.appendText(node.value)\n\t\t\t} catch {\n\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode. The last RT\n\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\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\tbuilder.appendText(node.value)\n\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,gBAAgB;CACjF,IAAI;CACJ,IAAI;CACJ,KAAK;CACL,QAAQ;CAC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jF,OAAO;AACf,QAAI,iBAAiBC,uCAAAA,gCAAgC;AACpD,UAAK,QAAQ,MAAM,SAAS;MAC3B,OAAO;MACP,OAAO,KAAK;MACZ,QAAQ;MACR,QAAQ;MACR,CAAC;AAEF;;AAGD,UAAM;;aAEG,KAAK,SAAS;OAGpB,EAAE,KAAK,MAAM,QAAQ,gBAAgB,GAAG,KAAK,IACjD,KAAI;AACH,YAAQ,WAAW,KAAK,MAAM;WACvB;AAGP,YAAQ,eAAe,oBAAoB,QAAQ,UAAU;AAC7D,YAAQ,WAAW,KAAK,MAAM;;;GAO/B;AAEF,QAAO,QAAQ,OAAO"}
|
|
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":";;;;;;;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,gBAAgB;CACjF,IAAI;CACJ,IAAI;CACJ,KAAK;CACL,QAAQ;CACR,GAAG;AACJ;AAEA,MAAM,aAAa;AACnB,MAAM,eAAe;;;;;;;;;AA0BrB,MAAa,kBACZ,MACA,MACA,WACmB;CACnB,MAAM,UAAU,IAAIA,6BAAAA,qBAAqB;CAGzC,MAAM,aAAa;EAClB,GAAG;EACH,GAAG,QAAQ;CACZ;CAIA,IAAI,qBAAyCC,kBAAAA,iBAAiB;CAI9D,IAAI,eAAoE,KAAA;CAExE,CAAA,GAAA,iBAAA,MAAA,CAAM,OAAO,SAAS;EACrB,IAAI,KAAK,SAAS,WAAW;GAE5B,IAA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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,6 +1,7 @@
|
|
|
1
1
|
import { RichTextHTMLMapSerializer } from "../types.cjs";
|
|
2
2
|
import { RichTextField } from "@prismicio/client";
|
|
3
3
|
import { Element } from "hast";
|
|
4
|
+
import "vfile";
|
|
4
5
|
//#region src/lib/hastToRichText.d.ts
|
|
5
6
|
/** Configuration that determines the output of `toRichText`. */
|
|
6
7
|
type HASTToRichTextConfig = {
|
|
@@ -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":";;;;;;KAwDY;;;;;EAKX,aAAa;;;;;;EAOb"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RichTextHTMLMapSerializer } from "../types.js";
|
|
2
2
|
import { RichTextField } from "@prismicio/client";
|
|
3
3
|
import { Element } from "hast";
|
|
4
|
+
import "vfile";
|
|
4
5
|
//#region src/lib/hastToRichText.d.ts
|
|
5
6
|
/** Configuration that determines the output of `toRichText`. */
|
|
6
7
|
type HASTToRichTextConfig = {
|
|
@@ -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":";;;;;;KAwDY;;;;;EAKX,aAAa;;;;;;EAOb"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hastToRichText.js","names":[],"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\ttry {\n\t\t\t\tbuilder.appendText(\"\\n\")\n\t\t\t} catch {\n\t\t\t\t// noop\n\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\ttry {\n\t\t\t\t\t\t\tbuilder.appendSpan(shorthandOrNode, 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(shorthandOrNode, length)\n\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\ttry {\n\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\t\t\t\t\t} catch {\n\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// the next children is a span. The last RT node type is then an image/embed\n\t\t\t\t\t\t// node, so we need to resume a new RT text node.\n\t\t\t\t\t\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\t\t\tbuilder.appendSpan(span, length)\n\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\ttry {\n\t\t\t\tbuilder.appendText(node.value)\n\t\t\t} catch {\n\t\t\t\t// Happens when we extract an image/embed node inside an RTTextNode. The last RT\n\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\tbuilder.appendTextNode(lastRTTextNodeType, config?.direction)\n\t\t\t\tbuilder.appendText(node.value)\n\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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,QAAQ,MAAM,SAAS;MAC3B,OAAO;MACP,OAAO,KAAK;MACZ,QAAQ;MACR,QAAQ;MACR,CAAC;AAEF;;AAGD,UAAM;;aAEG,KAAK,SAAS;OAGpB,EAAE,KAAK,MAAM,QAAQ,gBAAgB,GAAG,KAAK,IACjD,KAAI;AACH,YAAQ,WAAW,KAAK,MAAM;WACvB;AAGP,YAAQ,eAAe,oBAAoB,QAAQ,UAAU;AAC7D,YAAQ,WAAW,KAAK,MAAM;;;GAO/B;AAEF,QAAO,QAAQ,OAAO"}
|
|
1
|
+
{"version":3,"file":"hastToRichText.js","names":[],"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,gBAAgB;CACjF,IAAI;CACJ,IAAI;CACJ,KAAK;CACL,QAAQ;CACR,GAAG;AACJ;AAEA,MAAM,aAAa;AACnB,MAAM,eAAe;;;;;;;;;AA0BrB,MAAa,kBACZ,MACA,MACA,WACmB;CACnB,MAAM,UAAU,IAAI,qBAAqB;CAGzC,MAAM,aAAa;EAClB,GAAG;EACH,GAAG,QAAQ;CACZ;CAIA,IAAI,qBAAyC,iBAAiB;CAI9D,IAAI,eAAoE,KAAA;CAExE,MAAM,OAAO,SAAS;EACrB,IAAI,KAAK,SAAS,WAAW;GAE5B,IAAI,KAAK,YAAY,MACpB,IAAI;IACH,QAAQ,WAAW,IAAI;GACxB,QAAQ,CAER;GAID,IAAI;GAKJ,IAAI,KAAK,qBAAqB,KAAK,qBAAqB,YACvD,wBAAwB,WAAW,KAAK;QAClC,IAAI,KAAK,WAAW,YAC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AAQ,WAAW,eAAe,IAAI,CAAC;MACvC;KAED,KAAK,iBAAiB;MACrB,QAAQ,WAAW,eAAe,IAAI,CAAC;MACvC;KAED,KAAK,iBAAiB;KACtB,KAAK,iBAAiB;KACtB,KAAK,iBAAiB;KACtB,KAAK,iBAAiB,WAAW;MAChC,MAAM,OAAO,cAAc,MAAM,KAAK;MACtC,MAAM,SAAS,SAAS,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,iBAAiB,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"}
|
|
@@ -27,7 +27,8 @@ const rehypeRichText = function rehypeRichText(config) {
|
|
|
27
27
|
}
|
|
28
28
|
if (config?.include) {
|
|
29
29
|
const nodesToInclude = [];
|
|
30
|
-
const
|
|
30
|
+
const selector = config.include.join(", ");
|
|
31
|
+
const rawNodesToInclude = (0, hast_util_select.selectAll)(selector, tree);
|
|
31
32
|
(0, unist_util_visit.visit)(tree, (node) => {
|
|
32
33
|
if (rawNodesToInclude.includes(node)) {
|
|
33
34
|
nodesToInclude.push(node);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehypeRichText.cjs","names":["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;
|
|
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;IAEhE,CAAA,GAAA,kBAAA,OAAA,CAAO,OAAO,SAAS,eAAe,SAAS,IAAe,CAAC;GAChE;GAGA,IAAI,QAAQ,SAAS;IACpB,MAAM,iBAA4B,CAAC;IAGnC,MAAM,WAAW,OAAO,QAAQ,KAAK,IAAI;IACzC,MAAM,qBAAA,GAAoBA,iBAAAA,UAAAA,CAAU,UAAU,IAAI;IAGlD,CAAA,GAAA,iBAAA,MAAA,CAAM,OAAO,SAAS;KACrB,IAAI,kBAAkB,SAAS,IAAe,GAAG;MAChD,eAAe,KAAK,IAAe;MAGnC,OAAOC,iBAAAA;KACR;IACD,CAAC;IAID,KAAK,WAAW;GACjB;GAGA,IAAI,QAAQ,YACN;SAAA,MAAM,OAAO,OAAO,YAIxB,IAAI,CAAC,iBAAiB,KAAK,GAAG,GAAG;KAChC,MAAM,WAAA,GAAUD,iBAAAA,UAAAA,CAAU,KAAK,IAAI;KAEnC,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KACnC,QAAQ,EAAE,CAAC,oBAAoB;IAEjC;;EAGH;CACD,CAAC;CAID,KAAK,IAAIE,yBAAAA,OAAsB;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,5 +1,7 @@
|
|
|
1
1
|
import { HASTToRichTextConfig } from "./hastToRichText.cjs";
|
|
2
2
|
import { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client";
|
|
3
|
+
import "hast";
|
|
4
|
+
import "unified";
|
|
3
5
|
//#region src/lib/rehypeRichText.d.ts
|
|
4
6
|
/** Configuration options for {@link rehypeRichText}. */
|
|
5
7
|
type RehypeRichTextConfig = {
|
|
@@ -51,7 +53,7 @@ declare module "hast" {
|
|
|
51
53
|
*/
|
|
52
54
|
matchesSerializer?: string;
|
|
53
55
|
}
|
|
54
|
-
}
|
|
56
|
+
}
|
|
55
57
|
//#endregion
|
|
56
58
|
export { RehypeRichTextConfig };
|
|
57
59
|
//# 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":";;;;;;KAcY;;;;;;;;;EASX;;;;;;;;;EAUA;;;;;;;;;EAUA;;;;;;;EAQA,QAAQ;IACL;;YAwGO;IACT,eAAe;;;;YAMN;;;;;IAKT"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { HASTToRichTextConfig } from "./hastToRichText.js";
|
|
2
|
+
import "unified";
|
|
2
3
|
import { CustomTypeModelRichTextField, RichTextField } from "@prismicio/client";
|
|
4
|
+
import "hast";
|
|
3
5
|
//#region src/lib/rehypeRichText.d.ts
|
|
4
6
|
/** Configuration options for {@link rehypeRichText}. */
|
|
5
7
|
type RehypeRichTextConfig = {
|
|
@@ -51,7 +53,7 @@ declare module "hast" {
|
|
|
51
53
|
*/
|
|
52
54
|
matchesSerializer?: string;
|
|
53
55
|
}
|
|
54
|
-
}
|
|
56
|
+
}
|
|
55
57
|
//#endregion
|
|
56
58
|
export { RehypeRichTextConfig };
|
|
57
59
|
//# 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":";;;;;;KAcY;;;;;;;;;EASX;;;;;;;;;EAUA;;;;;;;;;EAUA;;;;;;;EAQA,QAAQ;IACL;;YAwGO;IACT,eAAe;;;;YAMN;;;;;IAKT"}
|
|
@@ -25,7 +25,8 @@ const rehypeRichText = function rehypeRichText(config) {
|
|
|
25
25
|
}
|
|
26
26
|
if (config?.include) {
|
|
27
27
|
const nodesToInclude = [];
|
|
28
|
-
const
|
|
28
|
+
const selector = config.include.join(", ");
|
|
29
|
+
const rawNodesToInclude = selectAll(selector, tree);
|
|
29
30
|
visit(tree, (node) => {
|
|
30
31
|
if (rawNodesToInclude.includes(node)) {
|
|
31
32
|
nodesToInclude.push(node);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rehypeRichText.js","names":[],"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;
|
|
1
|
+
{"version":3,"file":"rehypeRichText.js","names":[],"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,UAAU,OAAO,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,iBAAiB,UAAU,OAAO,QAAQ,KAAK,IAAI,GAAG,IAAI;IAEhE,OAAO,OAAO,SAAS,eAAe,SAAS,IAAe,CAAC;GAChE;GAGA,IAAI,QAAQ,SAAS;IACpB,MAAM,iBAA4B,CAAC;IAGnC,MAAM,WAAW,OAAO,QAAQ,KAAK,IAAI;IACzC,MAAM,oBAAoB,UAAU,UAAU,IAAI;IAGlD,MAAM,OAAO,SAAS;KACrB,IAAI,kBAAkB,SAAS,IAAe,GAAG;MAChD,eAAe,KAAK,IAAe;MAGnC,OAAO;KACR;IACD,CAAC;IAID,KAAK,WAAW;GACjB;GAGA,IAAI,QAAQ,YACN;SAAA,MAAM,OAAO,OAAO,YAIxB,IAAI,CAAC,iBAAiB,KAAK,GAAG,GAAG;KAChC,MAAM,UAAU,UAAU,KAAK,IAAI;KAEnC,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KACnC,QAAQ,EAAE,CAAC,oBAAoB;IAEjC;;EAGH;CACD,CAAC;CAID,KAAK,IAAI,sBAAsB;CAE/B,KAAK,WAAW;CAEhB,SAAS,SAAS,MAAY,MAA4B;EACzD,MAAM,gBAAgB,eAAe,MAAM,MAAM,MAAM;EAEvD,IAAI,QAAQ,OACX,OAAO,oBAAoB,eAAe,OAAO,KAAK;EAGvD,OAAO;CACR;AACD"}
|
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
|
-
|
|
5
4
|
//#region src/types.d.ts
|
|
6
5
|
/**
|
|
7
6
|
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.
|
|
@@ -18,9 +17,12 @@ type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "
|
|
|
18
17
|
};
|
|
19
18
|
/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */
|
|
20
19
|
type RichTextHTMLMapSerializerFunctionPayload = {
|
|
21
|
-
/** The hast {@link Element} node to serialize. */
|
|
20
|
+
/** The hast {@link Element} node to serialize. */
|
|
21
|
+
node: Element;
|
|
22
|
+
/** Additional context information to help with the serialization. */
|
|
22
23
|
context: {
|
|
23
|
-
/** The list type of the last list node encountered if any. */
|
|
24
|
+
/** The list type of the last list node encountered if any. */
|
|
25
|
+
listType?: "group-list-item" | "group-o-list-item";
|
|
24
26
|
};
|
|
25
27
|
};
|
|
26
28
|
/**
|
|
@@ -42,5 +44,5 @@ type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunc
|
|
|
42
44
|
*/
|
|
43
45
|
type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
|
|
44
46
|
//#endregion
|
|
45
|
-
export { RichTextHTMLMapSerializer };
|
|
47
|
+
export { RichTextHTMLMapSerializer, RichTextHTMLMapSerializerFunction, RichTextHTMLMapSerializerShorthand };
|
|
46
48
|
//# 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":";;;;;;;;;;;;;;KAoBY,qCACT,QAAQ;EACN;;;KAGA;;EAEJ,MAAM;;EAGN;;IAEC;;;;;;;;;;;KAYU,qCACX,SAAS,6CAEP,qCACA,6BAA6B,SAAS,eAAe;;;;;;;;;KAW5C,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
|
-
|
|
5
4
|
//#region src/types.d.ts
|
|
6
5
|
/**
|
|
7
6
|
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node types.
|
|
@@ -18,9 +17,12 @@ type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "
|
|
|
18
17
|
};
|
|
19
18
|
/** The payload provided to a {@link RichTextHTMLMapSerializerFunction}. */
|
|
20
19
|
type RichTextHTMLMapSerializerFunctionPayload = {
|
|
21
|
-
/** The hast {@link Element} node to serialize. */
|
|
20
|
+
/** The hast {@link Element} node to serialize. */
|
|
21
|
+
node: Element;
|
|
22
|
+
/** Additional context information to help with the serialization. */
|
|
22
23
|
context: {
|
|
23
|
-
/** The list type of the last list node encountered if any. */
|
|
24
|
+
/** The list type of the last list node encountered if any. */
|
|
25
|
+
listType?: "group-list-item" | "group-o-list-item";
|
|
24
26
|
};
|
|
25
27
|
};
|
|
26
28
|
/**
|
|
@@ -42,5 +44,5 @@ type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunc
|
|
|
42
44
|
*/
|
|
43
45
|
type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
|
|
44
46
|
//#endregion
|
|
45
|
-
export { RichTextHTMLMapSerializer };
|
|
47
|
+
export { RichTextHTMLMapSerializer, RichTextHTMLMapSerializerFunction, RichTextHTMLMapSerializerShorthand };
|
|
46
48
|
//# 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":";;;;;;;;;;;;;;KAoBY,qCACT,QAAQ;EACN;;;KAGA;;EAEJ,MAAM;;EAGN;;IAEC;;;;;;;;;;;KAYU,qCACX,SAAS,6CAEP,qCACA,6BAA6B,SAAS,eAAe;;;;;;;;;KAW5C,4BAA4B,eAEvC,qCAAqC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/migrate",
|
|
3
|
-
"version": "0.0.2-
|
|
3
|
+
"version": "0.0.2-pr.4.3964365",
|
|
4
4
|
"description": "Set of helpers to migrate your data to Prismic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"prismic",
|
|
@@ -43,28 +43,28 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"hast": "^1.0.0",
|
|
46
|
-
"hast-util-select": "^6.0.
|
|
47
|
-
"hast-util-to-html": "^9.0.
|
|
48
|
-
"hast-util-to-string": "^3.0.
|
|
49
|
-
"rehype-minify-whitespace": "^6.0.
|
|
50
|
-
"rehype-parse": "^9.0.
|
|
46
|
+
"hast-util-select": "^6.0.4",
|
|
47
|
+
"hast-util-to-html": "^9.0.5",
|
|
48
|
+
"hast-util-to-string": "^3.0.1",
|
|
49
|
+
"rehype-minify-whitespace": "^6.0.2",
|
|
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.1.0",
|
|
54
54
|
"vfile": "^6.0.3"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@prismicio/client": "^7.
|
|
58
|
-
"@types/node": "^
|
|
59
|
-
"@vitest/coverage-v8": "^4.1.
|
|
60
|
-
"oxfmt": "^0.
|
|
61
|
-
"oxlint": "^1.
|
|
62
|
-
"tsdown": "^0.
|
|
63
|
-
"typescript": "^
|
|
64
|
-
"vitest": "^4.1.
|
|
57
|
+
"@prismicio/client": "^7.21.8",
|
|
58
|
+
"@types/node": "^26.1.2",
|
|
59
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
60
|
+
"oxfmt": "^0.62.0",
|
|
61
|
+
"oxlint": "^1.77.0",
|
|
62
|
+
"tsdown": "^0.22.14",
|
|
63
|
+
"typescript": "^7.0.2",
|
|
64
|
+
"vitest": "^4.1.10"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"@prismicio/client": "^7"
|
|
67
|
+
"@prismicio/client": "^7.21.8"
|
|
68
68
|
},
|
|
69
69
|
"engines": {
|
|
70
70
|
"node": ">=20"
|
|
@@ -102,11 +102,11 @@ export const hastToRichText = (
|
|
|
102
102
|
if (node.type === "element") {
|
|
103
103
|
// Transforms line break elements to line breaks
|
|
104
104
|
if (node.tagName === "br") {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
105
|
+
try {
|
|
106
|
+
builder.appendText("\n")
|
|
107
|
+
} catch {
|
|
108
|
+
// noop
|
|
109
|
+
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
// Resolves the serializer responsible for the current node.
|
|
@@ -142,15 +142,15 @@ export const hastToRichText = (
|
|
|
142
142
|
case RichTextNodeType.hyperlink: {
|
|
143
143
|
const length = toString(node).trimEnd().length
|
|
144
144
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
145
|
+
try {
|
|
146
|
+
builder.appendSpan(shorthandOrNode, length)
|
|
147
|
+
} catch {
|
|
148
|
+
// Happens when we extract an image/embed node inside an RTTextNode and that
|
|
149
|
+
// the next children is a span. The last RT node type is then an image/embed
|
|
150
|
+
// node, so we need to resume a new RT text node.
|
|
151
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
152
|
+
builder.appendSpan(shorthandOrNode, length)
|
|
153
|
+
}
|
|
154
154
|
|
|
155
155
|
return
|
|
156
156
|
}
|
|
@@ -220,15 +220,15 @@ export const hastToRichText = (
|
|
|
220
220
|
const span = serializeSpan(node, match)
|
|
221
221
|
const length = toString(node).trimEnd().length
|
|
222
222
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
223
|
+
try {
|
|
224
|
+
builder.appendSpan(span, length)
|
|
225
|
+
} catch {
|
|
226
|
+
// Happens when we extract an image/embed node inside an RTTextNode and that
|
|
227
|
+
// the next children is a span. The last RT node type is then an image/embed
|
|
228
|
+
// node, so we need to resume a new RT text node.
|
|
229
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
230
|
+
builder.appendSpan(span, length)
|
|
231
|
+
}
|
|
232
232
|
break
|
|
233
233
|
}
|
|
234
234
|
|
|
@@ -253,14 +253,14 @@ export const hastToRichText = (
|
|
|
253
253
|
// Inspired from `hast-util-whitespace`, see:
|
|
254
254
|
// https://github.com/syntax-tree/hast-util-whitespace/blob/main/lib/index.js
|
|
255
255
|
if (!(node.value.replace(/[ \t\n\f\r]/g, "") === "")) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
256
|
+
try {
|
|
257
|
+
builder.appendText(node.value)
|
|
258
|
+
} catch {
|
|
259
|
+
// Happens when we extract an image/embed node inside an RTTextNode. The last RT
|
|
260
|
+
// node type is then an image/embed node, so we need to resume a new RT text node.
|
|
261
|
+
builder.appendTextNode(lastRTTextNodeType, config?.direction)
|
|
262
|
+
builder.appendText(node.value)
|
|
263
|
+
}
|
|
264
264
|
}
|
|
265
265
|
}
|
|
266
266
|
|