@prismicio/client 7.6.0 → 7.7.1
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/_node_modules/hast-util-whitespace/lib/index.cjs +11 -0
- package/dist/_node_modules/hast-util-whitespace/lib/index.cjs.map +1 -0
- package/dist/_node_modules/hast-util-whitespace/lib/index.js +11 -0
- package/dist/_node_modules/hast-util-whitespace/lib/index.js.map +1 -0
- package/dist/buildQueryURL.cjs.map +1 -1
- package/dist/buildQueryURL.js.map +1 -1
- package/dist/createClient.cjs +1 -4
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.js +1 -4
- package/dist/createClient.js.map +1 -1
- package/dist/errors/PrismicError.cjs +1 -4
- package/dist/errors/PrismicError.cjs.map +1 -1
- package/dist/errors/PrismicError.js +1 -4
- package/dist/errors/PrismicError.js.map +1 -1
- package/dist/errors/PrismicRichTextError.cjs +6 -0
- package/dist/errors/PrismicRichTextError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +2 -0
- package/dist/errors/PrismicRichTextError.js +6 -0
- package/dist/errors/PrismicRichTextError.js.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs +7 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +3 -0
- package/dist/errors/PrismicRichTextSerializerError.js +7 -0
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -0
- package/dist/filter.cjs.map +1 -1
- package/dist/filter.js.map +1 -1
- package/dist/getGraphQLEndpoint.cjs.map +1 -1
- package/dist/getGraphQLEndpoint.js.map +1 -1
- package/dist/getRepositoryEndpoint.cjs.map +1 -1
- package/dist/getRepositoryEndpoint.js.map +1 -1
- package/dist/getRepositoryName.cjs.map +1 -1
- package/dist/getRepositoryName.js.map +1 -1
- package/dist/getToolbarSrc.cjs.map +1 -1
- package/dist/getToolbarSrc.js.map +1 -1
- package/dist/helpers/asDate.cjs.map +1 -1
- package/dist/helpers/asDate.d.ts +1 -1
- package/dist/helpers/asDate.js.map +1 -1
- package/dist/helpers/asHTML.cjs +1 -1
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.js +1 -1
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
- package/dist/helpers/asImageSrc.cjs.map +1 -1
- package/dist/helpers/asImageSrc.js.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +2 -2
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLink.cjs.map +1 -1
- package/dist/helpers/asLink.js.map +1 -1
- package/dist/helpers/asLinkAttrs.cjs.map +1 -1
- package/dist/helpers/asLinkAttrs.d.ts +1 -21
- package/dist/helpers/asLinkAttrs.js.map +1 -1
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.js.map +1 -1
- package/dist/helpers/documentToLinkField.cjs +1 -0
- package/dist/helpers/documentToLinkField.cjs.map +1 -1
- package/dist/helpers/documentToLinkField.d.ts +1 -1
- package/dist/helpers/documentToLinkField.js +1 -0
- package/dist/helpers/documentToLinkField.js.map +1 -1
- package/dist/helpers/isFilled.cjs.map +1 -1
- package/dist/helpers/isFilled.d.ts +20 -66
- package/dist/helpers/isFilled.js.map +1 -1
- package/dist/helpers/mapSliceZone.cjs.map +1 -1
- package/dist/helpers/mapSliceZone.js.map +1 -1
- package/dist/helpers/unstable_htmlAsRichText.cjs +11 -0
- package/dist/helpers/unstable_htmlAsRichText.cjs.map +1 -0
- package/dist/helpers/unstable_htmlAsRichText.d.ts +81 -0
- package/dist/helpers/unstable_htmlAsRichText.js +11 -0
- package/dist/helpers/unstable_htmlAsRichText.js.map +1 -0
- package/dist/helpers/unstable_markdownAsRichText.cjs +12 -0
- package/dist/helpers/unstable_markdownAsRichText.cjs.map +1 -0
- package/dist/helpers/unstable_markdownAsRichText.d.ts +30 -0
- package/dist/helpers/unstable_markdownAsRichText.js +12 -0
- package/dist/helpers/unstable_markdownAsRichText.js.map +1 -0
- package/dist/index.cjs +8 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/isRepositoryEndpoint.cjs.map +1 -1
- package/dist/isRepositoryEndpoint.js.map +1 -1
- package/dist/lib/RichTextFieldBuilder.cjs +112 -0
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +35 -0
- package/dist/lib/RichTextFieldBuilder.js +112 -0
- package/dist/lib/RichTextFieldBuilder.js.map +1 -0
- package/dist/lib/castThunk.d.ts +1 -1
- package/dist/lib/escapeHTML.cjs.map +1 -1
- package/dist/lib/escapeHTML.js.map +1 -1
- package/dist/lib/filterRichTextField.cjs +88 -0
- package/dist/lib/filterRichTextField.cjs.map +1 -0
- package/dist/lib/filterRichTextField.d.ts +12 -0
- package/dist/lib/filterRichTextField.js +88 -0
- package/dist/lib/filterRichTextField.js.map +1 -0
- package/dist/lib/findRef.cjs.map +1 -1
- package/dist/lib/findRef.js.map +1 -1
- package/dist/lib/getPreviewCookie.cjs.map +1 -1
- package/dist/lib/getPreviewCookie.js.map +1 -1
- package/dist/lib/hastSerializerHelpers.cjs +96 -0
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -0
- package/dist/lib/hastSerializerHelpers.d.ts +28 -0
- package/dist/lib/hastSerializerHelpers.js +96 -0
- package/dist/lib/hastSerializerHelpers.js.map +1 -0
- package/dist/lib/hastToRichText.cjs +165 -0
- package/dist/lib/hastToRichText.cjs.map +1 -0
- package/dist/lib/hastToRichText.d.ts +30 -0
- package/dist/lib/hastToRichText.js +165 -0
- package/dist/lib/hastToRichText.js.map +1 -0
- package/dist/lib/rehypeRichText.cjs +59 -0
- package/dist/lib/rehypeRichText.cjs.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +66 -0
- package/dist/lib/rehypeRichText.js +59 -0
- package/dist/lib/rehypeRichText.js.map +1 -0
- package/dist/lib/serializerHelpers.cjs.map +1 -1
- package/dist/lib/serializerHelpers.d.ts +1 -1
- package/dist/lib/serializerHelpers.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/richtext/asText.cjs.map +1 -1
- package/dist/richtext/asText.js.map +1 -1
- package/dist/richtext/asTree.cjs.map +1 -1
- package/dist/richtext/asTree.js.map +1 -1
- package/dist/richtext/composeSerializers.cjs.map +1 -1
- package/dist/richtext/composeSerializers.js.map +1 -1
- package/dist/richtext/serialize.cjs.map +1 -1
- package/dist/richtext/serialize.js.map +1 -1
- package/dist/richtext/types.cjs.map +1 -1
- package/dist/richtext/types.d.ts +3 -3
- package/dist/richtext/types.js.map +1 -1
- package/dist/richtext/wrapMapSerializer.cjs.map +1 -1
- package/dist/richtext/wrapMapSerializer.js.map +1 -1
- package/dist/types/model/richText.d.ts +2 -0
- package/dist/types/value/embed.cjs.map +1 -1
- package/dist/types/value/embed.js.map +1 -1
- package/dist/types/value/richText.cjs.map +1 -1
- package/dist/types/value/richText.d.ts +8 -1
- package/dist/types/value/richText.js.map +1 -1
- package/package.json +33 -21
- package/src/createClient.ts +4 -3
- package/src/errors/PrismicRichTextError.ts +1 -0
- package/src/errors/PrismicRichTextSerializerError.ts +3 -0
- package/src/helpers/asHTML.ts +21 -19
- package/src/helpers/asImagePixelDensitySrcSet.ts +14 -13
- package/src/helpers/asImageWidthSrcSet.ts +16 -15
- package/src/helpers/asLinkAttrs.ts +4 -4
- package/src/helpers/mapSliceZone.ts +20 -19
- package/src/helpers/unstable_htmlAsRichText.ts +121 -0
- package/src/helpers/unstable_markdownAsRichText.ts +52 -0
- package/src/index.ts +10 -0
- package/src/lib/RichTextFieldBuilder.ts +164 -0
- package/src/lib/filterRichTextField.ts +159 -0
- package/src/lib/hastSerializerHelpers.ts +153 -0
- package/src/lib/hastToRichText.ts +294 -0
- package/src/lib/rehypeRichText.ts +183 -0
- package/src/richtext/types.ts +3 -2
- package/src/types/model/richText.ts +4 -0
- package/src/types/value/embed.ts +1 -1
- package/src/types/value/geoPoint.ts +1 -1
- package/src/types/value/richText.ts +10 -1
- package/src/types/value/title.ts +1 -1
package/dist/lib/findRef.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findRef.cjs","sources":["../../../src/lib/findRef.ts"],"sourcesContent":["import { Ref } from \"../types/api/ref\";\n\nimport { PrismicError } from \"../errors/PrismicError\";\n\n/**\n * Returns the first ref from a list that passes a filter (a function that\n * returns true).\n *\n * @param refs - A list of refs to search.\n * @param filter - A function that determines if a ref from the list matches the\n * criteria.\n *\n * @returns The first matching ref.\n *\n * @throws If a matching ref cannot be found.\n */\nexport const findRef = (refs: Ref[], filter: (ref: Ref) => boolean): Ref => {\n\tconst ref = refs.find((ref) => filter(ref));\n\n\tif (!ref) {\n\t\tthrow new PrismicError(\"Ref could not be found.\", undefined, undefined);\n\t}\n\n\treturn ref;\n};\n"],"names":["ref","PrismicError"],"mappings":";;;AAgBa,MAAA,UAAU,CAAC,MAAa,WAAsC;AAC1E,QAAM,MAAM,KAAK,KAAK,CAACA,SAAQ,OAAOA,IAAG,CAAC;AAE1C,MAAI,CAAC,KAAK;AACT,UAAM,IAAIC,aAAA,aAAa,2BAA2B,QAAW,MAAS;AAAA,
|
|
1
|
+
{"version":3,"file":"findRef.cjs","sources":["../../../src/lib/findRef.ts"],"sourcesContent":["import { Ref } from \"../types/api/ref\";\n\nimport { PrismicError } from \"../errors/PrismicError\";\n\n/**\n * Returns the first ref from a list that passes a filter (a function that\n * returns true).\n *\n * @param refs - A list of refs to search.\n * @param filter - A function that determines if a ref from the list matches the\n * criteria.\n *\n * @returns The first matching ref.\n *\n * @throws If a matching ref cannot be found.\n */\nexport const findRef = (refs: Ref[], filter: (ref: Ref) => boolean): Ref => {\n\tconst ref = refs.find((ref) => filter(ref));\n\n\tif (!ref) {\n\t\tthrow new PrismicError(\"Ref could not be found.\", undefined, undefined);\n\t}\n\n\treturn ref;\n};\n"],"names":["ref","PrismicError"],"mappings":";;;AAgBa,MAAA,UAAU,CAAC,MAAa,WAAsC;AAC1E,QAAM,MAAM,KAAK,KAAK,CAACA,SAAQ,OAAOA,IAAG,CAAC;AAE1C,MAAI,CAAC,KAAK;AACT,UAAM,IAAIC,aAAA,aAAa,2BAA2B,QAAW,MAAS;AAAA,EACvE;AAEO,SAAA;AACR;;"}
|
package/dist/lib/findRef.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findRef.js","sources":["../../../src/lib/findRef.ts"],"sourcesContent":["import { Ref } from \"../types/api/ref\";\n\nimport { PrismicError } from \"../errors/PrismicError\";\n\n/**\n * Returns the first ref from a list that passes a filter (a function that\n * returns true).\n *\n * @param refs - A list of refs to search.\n * @param filter - A function that determines if a ref from the list matches the\n * criteria.\n *\n * @returns The first matching ref.\n *\n * @throws If a matching ref cannot be found.\n */\nexport const findRef = (refs: Ref[], filter: (ref: Ref) => boolean): Ref => {\n\tconst ref = refs.find((ref) => filter(ref));\n\n\tif (!ref) {\n\t\tthrow new PrismicError(\"Ref could not be found.\", undefined, undefined);\n\t}\n\n\treturn ref;\n};\n"],"names":["ref"],"mappings":";AAgBa,MAAA,UAAU,CAAC,MAAa,WAAsC;AAC1E,QAAM,MAAM,KAAK,KAAK,CAACA,SAAQ,OAAOA,IAAG,CAAC;AAE1C,MAAI,CAAC,KAAK;AACT,UAAM,IAAI,aAAa,2BAA2B,QAAW,MAAS;AAAA,
|
|
1
|
+
{"version":3,"file":"findRef.js","sources":["../../../src/lib/findRef.ts"],"sourcesContent":["import { Ref } from \"../types/api/ref\";\n\nimport { PrismicError } from \"../errors/PrismicError\";\n\n/**\n * Returns the first ref from a list that passes a filter (a function that\n * returns true).\n *\n * @param refs - A list of refs to search.\n * @param filter - A function that determines if a ref from the list matches the\n * criteria.\n *\n * @returns The first matching ref.\n *\n * @throws If a matching ref cannot be found.\n */\nexport const findRef = (refs: Ref[], filter: (ref: Ref) => boolean): Ref => {\n\tconst ref = refs.find((ref) => filter(ref));\n\n\tif (!ref) {\n\t\tthrow new PrismicError(\"Ref could not be found.\", undefined, undefined);\n\t}\n\n\treturn ref;\n};\n"],"names":["ref"],"mappings":";AAgBa,MAAA,UAAU,CAAC,MAAa,WAAsC;AAC1E,QAAM,MAAM,KAAK,KAAK,CAACA,SAAQ,OAAOA,IAAG,CAAC;AAE1C,MAAI,CAAC,KAAK;AACT,UAAM,IAAI,aAAa,2BAA2B,QAAW,MAAS;AAAA,EACvE;AAEO,SAAA;AACR;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPreviewCookie.cjs","sources":["../../../src/lib/getPreviewCookie.ts"],"sourcesContent":["import { preview as previewCookieName } from \"../cookie\";\n\nconst readValue = (value: string): string => {\n\treturn value.replace(/%3B/g, \";\");\n};\n\n/**\n * Returns the value of a cookie from a given cookie store.\n *\n * @param cookieJar - The stringified cookie store from which to read the\n * cookie.\n *\n * @returns The value of the cookie, if it exists.\n */\nexport const getPreviewCookie = (cookieJar: string): string | undefined => {\n\tconst cookies = cookieJar.split(\"; \");\n\n\tlet value: string | undefined;\n\n\tfor (const cookie of cookies) {\n\t\tconst parts = cookie.split(\"=\");\n\t\tconst name = readValue(parts[0]).replace(/%3D/g, \"=\");\n\n\t\tif (name === previewCookieName) {\n\t\t\tvalue = readValue(parts.slice(1).join(\"=\"));\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn value;\n};\n"],"names":["cookie","previewCookieName"],"mappings":";;;AAEA,MAAM,YAAY,CAAC,UAAyB;AACpC,SAAA,MAAM,QAAQ,QAAQ,GAAG;AACjC;AAUa,MAAA,mBAAmB,CAAC,cAAyC;AACnE,QAAA,UAAU,UAAU,MAAM,IAAI;AAEhC,MAAA;AAEJ,aAAWA,YAAU,SAAS;AACvB,UAAA,QAAQA,SAAO,MAAM,GAAG;AACxB,UAAA,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,QAAQ,QAAQ,GAAG;AAEpD,QAAI,SAASC,OAAAA,SAAmB;AAC/B,cAAQ,UAAU,MAAM,MAAM,CAAC,EAAE,KAAK,GAAG,CAAC;AAC1C;AAAA,
|
|
1
|
+
{"version":3,"file":"getPreviewCookie.cjs","sources":["../../../src/lib/getPreviewCookie.ts"],"sourcesContent":["import { preview as previewCookieName } from \"../cookie\";\n\nconst readValue = (value: string): string => {\n\treturn value.replace(/%3B/g, \";\");\n};\n\n/**\n * Returns the value of a cookie from a given cookie store.\n *\n * @param cookieJar - The stringified cookie store from which to read the\n * cookie.\n *\n * @returns The value of the cookie, if it exists.\n */\nexport const getPreviewCookie = (cookieJar: string): string | undefined => {\n\tconst cookies = cookieJar.split(\"; \");\n\n\tlet value: string | undefined;\n\n\tfor (const cookie of cookies) {\n\t\tconst parts = cookie.split(\"=\");\n\t\tconst name = readValue(parts[0]).replace(/%3D/g, \"=\");\n\n\t\tif (name === previewCookieName) {\n\t\t\tvalue = readValue(parts.slice(1).join(\"=\"));\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn value;\n};\n"],"names":["cookie","previewCookieName"],"mappings":";;;AAEA,MAAM,YAAY,CAAC,UAAyB;AACpC,SAAA,MAAM,QAAQ,QAAQ,GAAG;AACjC;AAUa,MAAA,mBAAmB,CAAC,cAAyC;AACnE,QAAA,UAAU,UAAU,MAAM,IAAI;AAEhC,MAAA;AAEJ,aAAWA,YAAU,SAAS;AACvB,UAAA,QAAQA,SAAO,MAAM,GAAG;AACxB,UAAA,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,QAAQ,QAAQ,GAAG;AAEpD,QAAI,SAASC,OAAAA,SAAmB;AAC/B,cAAQ,UAAU,MAAM,MAAM,CAAC,EAAE,KAAK,GAAG,CAAC;AAC1C;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AACR;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPreviewCookie.js","sources":["../../../src/lib/getPreviewCookie.ts"],"sourcesContent":["import { preview as previewCookieName } from \"../cookie\";\n\nconst readValue = (value: string): string => {\n\treturn value.replace(/%3B/g, \";\");\n};\n\n/**\n * Returns the value of a cookie from a given cookie store.\n *\n * @param cookieJar - The stringified cookie store from which to read the\n * cookie.\n *\n * @returns The value of the cookie, if it exists.\n */\nexport const getPreviewCookie = (cookieJar: string): string | undefined => {\n\tconst cookies = cookieJar.split(\"; \");\n\n\tlet value: string | undefined;\n\n\tfor (const cookie of cookies) {\n\t\tconst parts = cookie.split(\"=\");\n\t\tconst name = readValue(parts[0]).replace(/%3D/g, \"=\");\n\n\t\tif (name === previewCookieName) {\n\t\t\tvalue = readValue(parts.slice(1).join(\"=\"));\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn value;\n};\n"],"names":["previewCookieName"],"mappings":";AAEA,MAAM,YAAY,CAAC,UAAyB;AACpC,SAAA,MAAM,QAAQ,QAAQ,GAAG;AACjC;AAUa,MAAA,mBAAmB,CAAC,cAAyC;AACnE,QAAA,UAAU,UAAU,MAAM,IAAI;AAEhC,MAAA;AAEJ,aAAW,UAAU,SAAS;AACvB,UAAA,QAAQ,OAAO,MAAM,GAAG;AACxB,UAAA,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,QAAQ,QAAQ,GAAG;AAEpD,QAAI,SAASA,SAAmB;AAC/B,cAAQ,UAAU,MAAM,MAAM,CAAC,EAAE,KAAK,GAAG,CAAC;AAC1C;AAAA,
|
|
1
|
+
{"version":3,"file":"getPreviewCookie.js","sources":["../../../src/lib/getPreviewCookie.ts"],"sourcesContent":["import { preview as previewCookieName } from \"../cookie\";\n\nconst readValue = (value: string): string => {\n\treturn value.replace(/%3B/g, \";\");\n};\n\n/**\n * Returns the value of a cookie from a given cookie store.\n *\n * @param cookieJar - The stringified cookie store from which to read the\n * cookie.\n *\n * @returns The value of the cookie, if it exists.\n */\nexport const getPreviewCookie = (cookieJar: string): string | undefined => {\n\tconst cookies = cookieJar.split(\"; \");\n\n\tlet value: string | undefined;\n\n\tfor (const cookie of cookies) {\n\t\tconst parts = cookie.split(\"=\");\n\t\tconst name = readValue(parts[0]).replace(/%3D/g, \"=\");\n\n\t\tif (name === previewCookieName) {\n\t\t\tvalue = readValue(parts.slice(1).join(\"=\"));\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn value;\n};\n"],"names":["previewCookieName"],"mappings":";AAEA,MAAM,YAAY,CAAC,UAAyB;AACpC,SAAA,MAAM,QAAQ,QAAQ,GAAG;AACjC;AAUa,MAAA,mBAAmB,CAAC,cAAyC;AACnE,QAAA,UAAU,UAAU,MAAM,IAAI;AAEhC,MAAA;AAEJ,aAAW,UAAU,SAAS;AACvB,UAAA,QAAQ,OAAO,MAAM,GAAG;AACxB,UAAA,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,QAAQ,QAAQ,GAAG;AAEpD,QAAI,SAASA,SAAmB;AAC/B,cAAQ,UAAU,MAAM,MAAM,CAAC,EAAE,KAAK,GAAG,CAAC;AAC1C;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AACR;"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const hastUtilToHtml = require("hast-util-to-html");
|
|
4
|
+
const embed = require("../types/value/embed.cjs");
|
|
5
|
+
const link = require("../types/value/link.cjs");
|
|
6
|
+
const PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
|
|
7
|
+
const serializeImage = (node) => {
|
|
8
|
+
var _a, _b, _c, _d, _e;
|
|
9
|
+
const src = (_a = node.properties) == null ? void 0 : _a.src;
|
|
10
|
+
if (!src) {
|
|
11
|
+
throw new PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
|
|
12
|
+
}
|
|
13
|
+
const url = new URL(src, "https://noop.com");
|
|
14
|
+
let width = (_b = node.properties) == null ? void 0 : _b.width;
|
|
15
|
+
let height = (_c = node.properties) == null ? void 0 : _c.height;
|
|
16
|
+
let x = 0;
|
|
17
|
+
let y = 0;
|
|
18
|
+
let zoom = 1;
|
|
19
|
+
if (url.hostname === "images.prismic.io") {
|
|
20
|
+
if (url.searchParams.has("w")) {
|
|
21
|
+
width = Number(url.searchParams.get("w"));
|
|
22
|
+
}
|
|
23
|
+
if (url.searchParams.has("h")) {
|
|
24
|
+
height = Number(url.searchParams.get("h"));
|
|
25
|
+
}
|
|
26
|
+
if (url.searchParams.has("rect")) {
|
|
27
|
+
const [rectX, rextY, rectW, _rectH] = url.searchParams.get("rect").split(",");
|
|
28
|
+
x = Number(rectX);
|
|
29
|
+
y = Number(rextY);
|
|
30
|
+
if (width) {
|
|
31
|
+
zoom = Math.max(1, width / Number(rectW));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
type: "image",
|
|
37
|
+
id: "",
|
|
38
|
+
url: src,
|
|
39
|
+
alt: ((_d = node.properties) == null ? void 0 : _d.alt) ?? "",
|
|
40
|
+
copyright: ((_e = node.properties) == null ? void 0 : _e.copyright) ?? null,
|
|
41
|
+
// This is not accurate. We're doing the casting because it seems the migration API
|
|
42
|
+
// accepts `undefined` values for those fields which is more convenient to us here.
|
|
43
|
+
// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185
|
|
44
|
+
dimensions: { width, height },
|
|
45
|
+
edit: { x, y, zoom, background: "transparent" }
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
const serializeEmbed = (node) => {
|
|
49
|
+
var _a, _b, _c, _d;
|
|
50
|
+
const src = (_a = node.properties) == null ? void 0 : _a.src;
|
|
51
|
+
if (!src) {
|
|
52
|
+
throw new PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `embed` is missing an `src` attribute");
|
|
53
|
+
}
|
|
54
|
+
const oembedBase = {
|
|
55
|
+
version: "1.0",
|
|
56
|
+
embed_url: src,
|
|
57
|
+
html: hastUtilToHtml.toHtml(node),
|
|
58
|
+
title: (_b = node.properties) == null ? void 0 : _b.title
|
|
59
|
+
};
|
|
60
|
+
const width = (_c = node.properties) == null ? void 0 : _c.width;
|
|
61
|
+
const height = (_d = node.properties) == null ? void 0 : _d.height;
|
|
62
|
+
node.children = [];
|
|
63
|
+
if (width && height) {
|
|
64
|
+
return {
|
|
65
|
+
type: "embed",
|
|
66
|
+
oembed: { ...oembedBase, type: embed.OEmbedType.Rich, width, height }
|
|
67
|
+
};
|
|
68
|
+
} else {
|
|
69
|
+
return {
|
|
70
|
+
type: "embed",
|
|
71
|
+
oembed: { ...oembedBase, type: embed.OEmbedType.Link }
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const serializeSpan = (node, rtPartialInlineNode) => {
|
|
76
|
+
var _a, _b;
|
|
77
|
+
if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
|
|
78
|
+
const url = (_a = node.properties) == null ? void 0 : _a.href;
|
|
79
|
+
if (!url) {
|
|
80
|
+
throw new PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `hyperlink` is missing an `href` attribute");
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
type: "hyperlink",
|
|
84
|
+
data: {
|
|
85
|
+
link_type: link.LinkType.Web,
|
|
86
|
+
url,
|
|
87
|
+
target: (_b = node.properties) == null ? void 0 : _b.target
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return rtPartialInlineNode;
|
|
92
|
+
};
|
|
93
|
+
exports.serializeEmbed = serializeEmbed;
|
|
94
|
+
exports.serializeImage = serializeImage;
|
|
95
|
+
exports.serializeSpan = serializeSpan;
|
|
96
|
+
//# sourceMappingURL=hastSerializerHelpers.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hastSerializerHelpers.cjs","sources":["../../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { Element } from \"hast\";\nimport { toHtml } from \"hast-util-to-html\";\n\nimport { OEmbedType } from \"../types/value/embed\";\nimport { LinkType } from \"../types/value/link\";\nimport { RTEmbedNode, RTImageNode, RTLinkNode } from \"../types/value/richText\";\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\";\n\nimport { 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 *\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(\n\t\t\t\"Element of type `img` is missing an `src` attribute\",\n\t\t);\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\n\t\t\t\t.get(\"rect\")!\n\t\t\t\t.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 *\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 *\n * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode:\n\t\t| RTPartialInlineNode\n\t\t| Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (\n\t\trtPartialInlineNode.type === \"hyperlink\" &&\n\t\t!(\"data\" in rtPartialInlineNode)\n\t) {\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"],"names":["PrismicRichTextSerializerError","toHtml","OEmbedType","LinkType"],"mappings":";;;;;;AAkBa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAIA,+BAAAA,+BACT,qDAAqD;AAAA,EAEvD;AAEA,QAAM,MAAM,IAAI,IAAI,KAAK,kBAAkB;AAEvC,MAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,MAAA,UAAS,UAAK,eAAL,mBAAiB;AAC9B,MAAI,IAAI;AACR,MAAI,IAAI;AACR,MAAI,OAAO;AAGP,MAAA,IAAI,aAAa,qBAAqB;AACzC,QAAI,IAAI,aAAa,IAAI,GAAG,GAAG;AAC9B,cAAQ,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;AAAA,IACzC;AAEA,QAAI,IAAI,aAAa,IAAI,GAAG,GAAG;AAC9B,eAAS,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;AAAA,IAC1C;AAEA,QAAI,IAAI,aAAa,IAAI,MAAM,GAAG;AACjC,YAAM,CAAC,OAAO,OAAO,OAAO,MAAM,IAAI,IAAI,aACxC,IAAI,MAAM,EACV,MAAM,GAAG;AAEX,UAAI,OAAO,KAAK;AAChB,UAAI,OAAO,KAAK;AAGhB,UAAI,OAAO;AACV,eAAO,KAAK,IAAI,GAAG,QAAQ,OA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AAAA,MACN,MAAM;AAAA,QACL,WAAWG,KAAS,SAAA;AAAA,QACpB;AAAA,QACA,SAAQ,UAAK,eAAL,mBAAiB;AAAA,MACzB;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;;;;"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Element } from "hast";
|
|
2
|
+
import { RTEmbedNode, RTImageNode, RTLinkNode } from "../types/value/richText";
|
|
3
|
+
import { RTPartialInlineNode } from "./RichTextFieldBuilder";
|
|
4
|
+
/**
|
|
5
|
+
* Serializes a hast {@link Element} node to a {@link RTImageNode}.
|
|
6
|
+
*
|
|
7
|
+
* @param node - A hast node to serialize.
|
|
8
|
+
*
|
|
9
|
+
* @returns Equivalent {@link RTImageNode}.
|
|
10
|
+
*/
|
|
11
|
+
export declare const serializeImage: (node: Element) => RTImageNode;
|
|
12
|
+
/**
|
|
13
|
+
* Serializes a hast {@link Element} node to a {@link RTEmbedNode}.
|
|
14
|
+
*
|
|
15
|
+
* @param node - A hast node to serialize.
|
|
16
|
+
*
|
|
17
|
+
* @returns Equivalent {@link RTEmbedNode}.
|
|
18
|
+
*/
|
|
19
|
+
export declare const serializeEmbed: (node: Element) => RTEmbedNode;
|
|
20
|
+
/**
|
|
21
|
+
* Serializes a hast {@link Element} node to a {@link RTPartialInlineNode}.
|
|
22
|
+
*
|
|
23
|
+
* @param node - A hast node to serialize.
|
|
24
|
+
* @param rtPartialInlineNode - A partial rich text node.
|
|
25
|
+
*
|
|
26
|
+
* @returns Equivalent {@link RTPartialInlineNode}.
|
|
27
|
+
*/
|
|
28
|
+
export declare const serializeSpan: (node: Element, rtPartialInlineNode: RTPartialInlineNode | Omit<RTLinkNode, "start" | "end" | "data">) => RTPartialInlineNode;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { toHtml } from "hast-util-to-html";
|
|
2
|
+
import { OEmbedType } from "../types/value/embed.js";
|
|
3
|
+
import { LinkType } from "../types/value/link.js";
|
|
4
|
+
import { PrismicRichTextSerializerError } from "../errors/PrismicRichTextSerializerError.js";
|
|
5
|
+
const serializeImage = (node) => {
|
|
6
|
+
var _a, _b, _c, _d, _e;
|
|
7
|
+
const src = (_a = node.properties) == null ? void 0 : _a.src;
|
|
8
|
+
if (!src) {
|
|
9
|
+
throw new PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
|
|
10
|
+
}
|
|
11
|
+
const url = new URL(src, "https://noop.com");
|
|
12
|
+
let width = (_b = node.properties) == null ? void 0 : _b.width;
|
|
13
|
+
let height = (_c = node.properties) == null ? void 0 : _c.height;
|
|
14
|
+
let x = 0;
|
|
15
|
+
let y = 0;
|
|
16
|
+
let zoom = 1;
|
|
17
|
+
if (url.hostname === "images.prismic.io") {
|
|
18
|
+
if (url.searchParams.has("w")) {
|
|
19
|
+
width = Number(url.searchParams.get("w"));
|
|
20
|
+
}
|
|
21
|
+
if (url.searchParams.has("h")) {
|
|
22
|
+
height = Number(url.searchParams.get("h"));
|
|
23
|
+
}
|
|
24
|
+
if (url.searchParams.has("rect")) {
|
|
25
|
+
const [rectX, rextY, rectW, _rectH] = url.searchParams.get("rect").split(",");
|
|
26
|
+
x = Number(rectX);
|
|
27
|
+
y = Number(rextY);
|
|
28
|
+
if (width) {
|
|
29
|
+
zoom = Math.max(1, width / Number(rectW));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
type: "image",
|
|
35
|
+
id: "",
|
|
36
|
+
url: src,
|
|
37
|
+
alt: ((_d = node.properties) == null ? void 0 : _d.alt) ?? "",
|
|
38
|
+
copyright: ((_e = node.properties) == null ? void 0 : _e.copyright) ?? null,
|
|
39
|
+
// This is not accurate. We're doing the casting because it seems the migration API
|
|
40
|
+
// accepts `undefined` values for those fields which is more convenient to us here.
|
|
41
|
+
// See: https://github.com/prismicio/prismic-client/pull/342#discussion_r1683650185
|
|
42
|
+
dimensions: { width, height },
|
|
43
|
+
edit: { x, y, zoom, background: "transparent" }
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
const serializeEmbed = (node) => {
|
|
47
|
+
var _a, _b, _c, _d;
|
|
48
|
+
const src = (_a = node.properties) == null ? void 0 : _a.src;
|
|
49
|
+
if (!src) {
|
|
50
|
+
throw new PrismicRichTextSerializerError("Element of type `embed` is missing an `src` attribute");
|
|
51
|
+
}
|
|
52
|
+
const oembedBase = {
|
|
53
|
+
version: "1.0",
|
|
54
|
+
embed_url: src,
|
|
55
|
+
html: toHtml(node),
|
|
56
|
+
title: (_b = node.properties) == null ? void 0 : _b.title
|
|
57
|
+
};
|
|
58
|
+
const width = (_c = node.properties) == null ? void 0 : _c.width;
|
|
59
|
+
const height = (_d = node.properties) == null ? void 0 : _d.height;
|
|
60
|
+
node.children = [];
|
|
61
|
+
if (width && height) {
|
|
62
|
+
return {
|
|
63
|
+
type: "embed",
|
|
64
|
+
oembed: { ...oembedBase, type: OEmbedType.Rich, width, height }
|
|
65
|
+
};
|
|
66
|
+
} else {
|
|
67
|
+
return {
|
|
68
|
+
type: "embed",
|
|
69
|
+
oembed: { ...oembedBase, type: OEmbedType.Link }
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
const serializeSpan = (node, rtPartialInlineNode) => {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
if (rtPartialInlineNode.type === "hyperlink" && !("data" in rtPartialInlineNode)) {
|
|
76
|
+
const url = (_a = node.properties) == null ? void 0 : _a.href;
|
|
77
|
+
if (!url) {
|
|
78
|
+
throw new PrismicRichTextSerializerError("Element of type `hyperlink` is missing an `href` attribute");
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
type: "hyperlink",
|
|
82
|
+
data: {
|
|
83
|
+
link_type: LinkType.Web,
|
|
84
|
+
url,
|
|
85
|
+
target: (_b = node.properties) == null ? void 0 : _b.target
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return rtPartialInlineNode;
|
|
90
|
+
};
|
|
91
|
+
export {
|
|
92
|
+
serializeEmbed,
|
|
93
|
+
serializeImage,
|
|
94
|
+
serializeSpan
|
|
95
|
+
};
|
|
96
|
+
//# sourceMappingURL=hastSerializerHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hastSerializerHelpers.js","sources":["../../../src/lib/hastSerializerHelpers.ts"],"sourcesContent":["import { Element } from \"hast\";\nimport { toHtml } from \"hast-util-to-html\";\n\nimport { OEmbedType } from \"../types/value/embed\";\nimport { LinkType } from \"../types/value/link\";\nimport { RTEmbedNode, RTImageNode, RTLinkNode } from \"../types/value/richText\";\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\";\n\nimport { 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 *\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(\n\t\t\t\"Element of type `img` is missing an `src` attribute\",\n\t\t);\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\n\t\t\t\t.get(\"rect\")!\n\t\t\t\t.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 *\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 *\n * @returns Equivalent {@link RTPartialInlineNode}.\n */\nexport const serializeSpan = (\n\tnode: Element,\n\trtPartialInlineNode:\n\t\t| RTPartialInlineNode\n\t\t| Omit<RTLinkNode, \"start\" | \"end\" | \"data\">,\n): RTPartialInlineNode => {\n\tif (\n\t\trtPartialInlineNode.type === \"hyperlink\" &&\n\t\t!(\"data\" in rtPartialInlineNode)\n\t) {\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"],"names":[],"mappings":";;;;AAkBa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAI,+BACT,qDAAqD;AAAA,EAEvD;AAEA,QAAM,MAAM,IAAI,IAAI,KAAK,kBAAkB;AAEvC,MAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,MAAA,UAAS,UAAK,eAAL,mBAAiB;AAC9B,MAAI,IAAI;AACR,MAAI,IAAI;AACR,MAAI,OAAO;AAGP,MAAA,IAAI,aAAa,qBAAqB;AACzC,QAAI,IAAI,aAAa,IAAI,GAAG,GAAG;AAC9B,cAAQ,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;AAAA,IACzC;AAEA,QAAI,IAAI,aAAa,IAAI,GAAG,GAAG;AAC9B,eAAS,OAAO,IAAI,aAAa,IAAI,GAAG,CAAC;AAAA,IAC1C;AAEA,QAAI,IAAI,aAAa,IAAI,MAAM,GAAG;AACjC,YAAM,CAAC,OAAO,OAAO,OAAO,MAAM,IAAI,IAAI,aACxC,IAAI,MAAM,E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AC,KAAK;AACH,YAAA,IAAI,+BACT,4DAA4D;AAAA,IAE9D;AAEO,WAAA;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,QACL,WAAW,SAAS;AAAA,QACpB;AAAA,QACA,SAAQ,UAAK,eAAL,mBAAiB;AAAA,MACzB;AAAA,IAAA;AAAA,EAEH;AAEO,SAAA;AACR;"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const mdastUtilToString = require("mdast-util-to-string");
|
|
4
|
+
const unistUtilVisit = require("unist-util-visit");
|
|
5
|
+
const richText = require("../types/value/richText.cjs");
|
|
6
|
+
const PrismicRichTextSerializerError = require("../errors/PrismicRichTextSerializerError.cjs");
|
|
7
|
+
const RichTextFieldBuilder = require("./RichTextFieldBuilder.cjs");
|
|
8
|
+
const hastSerializerHelpers = require("./hastSerializerHelpers.cjs");
|
|
9
|
+
const index = require('./../_node_modules/hast-util-whitespace/lib/index.cjs');
|
|
10
|
+
const DEFAULT_SERIALIZER = {
|
|
11
|
+
h1: "heading1",
|
|
12
|
+
h2: "heading2",
|
|
13
|
+
h3: "heading3",
|
|
14
|
+
h4: "heading4",
|
|
15
|
+
h5: "heading5",
|
|
16
|
+
h6: "heading6",
|
|
17
|
+
p: "paragraph",
|
|
18
|
+
pre: "preformatted",
|
|
19
|
+
strong: "strong",
|
|
20
|
+
em: "em",
|
|
21
|
+
li: ({ context }) => context.listType === "group-o-list-item" ? "o-list-item" : "list-item",
|
|
22
|
+
ul: "group-list-item",
|
|
23
|
+
ol: "group-o-list-item",
|
|
24
|
+
img: "image",
|
|
25
|
+
iframe: "embed",
|
|
26
|
+
a: "hyperlink"
|
|
27
|
+
};
|
|
28
|
+
const VFILE_RULE = "failed-to-serialize-node";
|
|
29
|
+
const VFILE_SOURCE = "prismic";
|
|
30
|
+
const hastToRichText = (tree, file, config) => {
|
|
31
|
+
const builder = new RichTextFieldBuilder.RichTextFieldBuilder();
|
|
32
|
+
const serializer = {
|
|
33
|
+
...DEFAULT_SERIALIZER,
|
|
34
|
+
...config == null ? void 0 : config.serializer
|
|
35
|
+
};
|
|
36
|
+
let lastRTTextNodeType = richText.RichTextNodeType.paragraph;
|
|
37
|
+
let lastListType = void 0;
|
|
38
|
+
unistUtilVisit.visit(tree, (node) => {
|
|
39
|
+
if (node.type === "element") {
|
|
40
|
+
if (node.tagName === "br") {
|
|
41
|
+
try {
|
|
42
|
+
builder.appendText("\n");
|
|
43
|
+
} catch (error) {
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
let serializerOrShorthand;
|
|
47
|
+
if (node.matchesSerializer && node.matchesSerializer in serializer) {
|
|
48
|
+
serializerOrShorthand = serializer[node.matchesSerializer];
|
|
49
|
+
} else if (node.tagName in serializer) {
|
|
50
|
+
serializerOrShorthand = serializer[node.tagName];
|
|
51
|
+
} else {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
let shorthand;
|
|
55
|
+
if (typeof serializerOrShorthand === "function") {
|
|
56
|
+
const shorthandOrNode = serializerOrShorthand({
|
|
57
|
+
node,
|
|
58
|
+
context: { listType: lastListType }
|
|
59
|
+
});
|
|
60
|
+
if (!shorthandOrNode) {
|
|
61
|
+
return;
|
|
62
|
+
} else if (typeof shorthandOrNode === "object" && "type" in shorthandOrNode) {
|
|
63
|
+
switch (shorthandOrNode.type) {
|
|
64
|
+
case richText.RichTextNodeType.strong:
|
|
65
|
+
case richText.RichTextNodeType.em:
|
|
66
|
+
case richText.RichTextNodeType.label:
|
|
67
|
+
case richText.RichTextNodeType.hyperlink: {
|
|
68
|
+
const length = mdastUtilToString.toString(node).length;
|
|
69
|
+
try {
|
|
70
|
+
builder.appendSpan(shorthandOrNode, length);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
73
|
+
builder.appendSpan(shorthandOrNode, length);
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
case richText.RichTextNodeType.image:
|
|
78
|
+
case richText.RichTextNodeType.embed:
|
|
79
|
+
builder.appendNode(shorthandOrNode);
|
|
80
|
+
return;
|
|
81
|
+
default:
|
|
82
|
+
lastRTTextNodeType = shorthandOrNode.type;
|
|
83
|
+
builder.appendNode(shorthandOrNode);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
shorthand = shorthandOrNode;
|
|
88
|
+
} else {
|
|
89
|
+
shorthand = serializerOrShorthand;
|
|
90
|
+
}
|
|
91
|
+
let match;
|
|
92
|
+
if (typeof shorthand === "string") {
|
|
93
|
+
match = { type: shorthand };
|
|
94
|
+
} else {
|
|
95
|
+
match = { type: richText.RichTextNodeType.label, data: shorthand };
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
switch (match.type) {
|
|
99
|
+
case richText.RichTextNodeType.heading1:
|
|
100
|
+
case richText.RichTextNodeType.heading2:
|
|
101
|
+
case richText.RichTextNodeType.heading3:
|
|
102
|
+
case richText.RichTextNodeType.heading4:
|
|
103
|
+
case richText.RichTextNodeType.heading5:
|
|
104
|
+
case richText.RichTextNodeType.heading6:
|
|
105
|
+
case richText.RichTextNodeType.paragraph:
|
|
106
|
+
case richText.RichTextNodeType.preformatted:
|
|
107
|
+
case richText.RichTextNodeType.listItem:
|
|
108
|
+
case richText.RichTextNodeType.oListItem:
|
|
109
|
+
lastRTTextNodeType = match.type;
|
|
110
|
+
builder.appendTextNode(match.type, config == null ? void 0 : config.direction);
|
|
111
|
+
break;
|
|
112
|
+
case richText.RichTextNodeType.list:
|
|
113
|
+
case richText.RichTextNodeType.oList:
|
|
114
|
+
lastListType = match.type;
|
|
115
|
+
break;
|
|
116
|
+
case richText.RichTextNodeType.image:
|
|
117
|
+
builder.appendNode(hastSerializerHelpers.serializeImage(node));
|
|
118
|
+
break;
|
|
119
|
+
case richText.RichTextNodeType.embed:
|
|
120
|
+
builder.appendNode(hastSerializerHelpers.serializeEmbed(node));
|
|
121
|
+
break;
|
|
122
|
+
case richText.RichTextNodeType.strong:
|
|
123
|
+
case richText.RichTextNodeType.em:
|
|
124
|
+
case richText.RichTextNodeType.label:
|
|
125
|
+
case richText.RichTextNodeType.hyperlink: {
|
|
126
|
+
const span = hastSerializerHelpers.serializeSpan(node, match);
|
|
127
|
+
const length = mdastUtilToString.toString(node).length;
|
|
128
|
+
try {
|
|
129
|
+
builder.appendSpan(span, length);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
132
|
+
builder.appendSpan(span, length);
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
default:
|
|
137
|
+
throw new Error(`Unknown rich text node type: \`${match.type}\``);
|
|
138
|
+
}
|
|
139
|
+
} catch (error) {
|
|
140
|
+
if (error instanceof PrismicRichTextSerializerError.PrismicRichTextSerializerError) {
|
|
141
|
+
file.message(error.message, {
|
|
142
|
+
cause: error,
|
|
143
|
+
place: node.position,
|
|
144
|
+
ruleId: VFILE_RULE,
|
|
145
|
+
source: VFILE_SOURCE
|
|
146
|
+
});
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
} else if (node.type === "text") {
|
|
152
|
+
if (!index.whitespace(node)) {
|
|
153
|
+
try {
|
|
154
|
+
builder.appendText(node.value);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
builder.appendTextNode(lastRTTextNodeType, config == null ? void 0 : config.direction);
|
|
157
|
+
builder.appendText(node.value);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
return builder.build();
|
|
163
|
+
};
|
|
164
|
+
exports.hastToRichText = hastToRichText;
|
|
165
|
+
//# sourceMappingURL=hastToRichText.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hastToRichText.cjs","sources":["../../../src/lib/hastToRichText.ts"],"sourcesContent":["import { Element, Root } from \"hast\";\nimport { whitespace } from \"hast-util-whitespace\";\nimport { toString } from \"mdast-util-to-string\";\nimport { visit } from \"unist-util-visit\";\nimport { VFile } from \"vfile\";\n\nimport {\n\tRTBlockNode,\n\tRTInlineNode,\n\tRTLabelNode,\n\tRTTextNode,\n\tRichTextField,\n\tRichTextNodeType,\n} from \"../types/value/richText\";\n\nimport { PrismicRichTextSerializerError } from \"../errors/PrismicRichTextSerializerError\";\n\nimport {\n\tRichTextHTMLMapSerializer,\n\tRichTextHTMLMapSerializerFunction,\n\tRichTextHTMLMapSerializerShorthand,\n} from \"../helpers/unstable_htmlAsRichText\";\n\nimport { RichTextFieldBuilder } from \"./RichTextFieldBuilder\";\nimport {\n\tserializeEmbed,\n\tserializeImage,\n\tserializeSpan,\n} from \"./hastSerializerHelpers\";\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<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Pick<ObjectType, KeyType> : 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 }) =>\n\t\tcontext.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/**\n * Configuration that determines the output of `toRichText`.\n */\nexport type HASTToRichTextConfig = {\n\t/**\n\t * An optional HTML to rich text serializer. Will be merged with the default\n\t * HTML to rich text 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 *\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 =\n\t\tundefined;\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 (error) {\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 (\n\t\t\t\t\ttypeof shorthandOrNode === \"object\" &&\n\t\t\t\t\t\"type\" in shorthandOrNode\n\t\t\t\t) {\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).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 (error) {\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<\n\t\t\t\t\t\tRTBlockNode | Exclude<RTInlineNode, RTLabelNode>,\n\t\t\t\t\t\t\"type\"\n\t\t\t\t >\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).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 (error) {\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(\n\t\t\t\t\t\t\t`Unknown rich text node type: \\`${\n\t\t\t\t\t\t\t\t(match as { type: string }).type\n\t\t\t\t\t\t\t}\\``,\n\t\t\t\t\t\t);\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\tif (!whitespace(node)) {\n\t\t\t\ttry {\n\t\t\t\t\tbuilder.appendText(node.value);\n\t\t\t\t} catch (error) {\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"],"names":["RichTextFieldBuilder","RichTextNodeType","visit","toString","serializeImage","serializeEmbed","serializeSpan","PrismicRichTextSerializerError","whitespace"],"mappings":";;;;;;;;;AA0CA,MAAM,qBAAgD;AAAA,EACrD,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,IAAI;AAAA,EACJ,IAAI,CAAC,EAAE,cACN,QAAQ,aAAa,sBAAsB,gBAAgB;AAAA,EAC5D,IAAI;AAAA,EACJ,IAAI;AAAA,EACJ,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,GAAG;;AAGJ,MAAM,aAAa;AACnB,MAAM,eAAe;AA6Bd,MAAM,iBAAiB,CAC7B,MACA,MACA,WACkB;AACZ,QAAA,UAAU,IAAIA,qBAAAA;AAGpB,QAAM,aAAa;AAAA,IAClB,GAAG;AAAA,IACH,GAAG,iCAAQ;AAAA,EAAA;AAKZ,MAAI,qBAAyCC,SAAiB,iBAAA;AAI9D,MAAI,eACH;AAEKC,uBAAA,MAAM,CAAC,SAAQ;AAChB,QAAA,KAAK,SAAS,WAAW;AAExB,UAAA,KAAK,YAAY,MAAM;AACtB,YAAA;AACH,kBAAQ,WAAW,IAAI;AAAA,iBACf,OAAO;AAAA,QAEhB;AAAA,MACD;AAGI,UAAA;AAKJ,UAAI,KAAK,qBAAqB,KAAK,qBAAqB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;AAAA,YACR,QAAQ;AAAA,UAAA,CACR;AAED;AAAA,QACD;AAEM,cAAA;AAAA,MACP;AAAA,IAAA,WACU,KAAK,SAAS,QAAQ;AAC5B,UAAA,CAACC,MAAAA,WAAW,IAAI,GAAG;AAClB,YAAA;AACK,kBAAA,WAAW,KAAK,KAAK;AAAA,iBACrB,OAAO;AAGP,kBAAA,eAAe,oBAAoB,iCAAQ,SAAS;AACpD,kBAAA,WAAW,KAAK,KAAK;AAAA,QAC9B;AAAA,MACD;AAAA,IACD;AAAA,EAAA,CAIA;AAED,SAAO,QAAQ;AAChB;;"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Element, Root } from "hast";
|
|
2
|
+
import { VFile } from "vfile";
|
|
3
|
+
import { RichTextField } from "../types/value/richText";
|
|
4
|
+
import { RichTextHTMLMapSerializer } from "../helpers/unstable_htmlAsRichText";
|
|
5
|
+
/**
|
|
6
|
+
* Configuration that determines the output of `toRichText`.
|
|
7
|
+
*/
|
|
8
|
+
export type HASTToRichTextConfig = {
|
|
9
|
+
/**
|
|
10
|
+
* An optional HTML to rich text serializer. Will be merged with the default
|
|
11
|
+
* HTML to rich text serializer.
|
|
12
|
+
*/
|
|
13
|
+
serializer?: RichTextHTMLMapSerializer;
|
|
14
|
+
/**
|
|
15
|
+
* Whether or not the text processed should be marked as right-to-left.
|
|
16
|
+
*
|
|
17
|
+
* @defaultValue `false`
|
|
18
|
+
*/
|
|
19
|
+
direction?: "ltr" | "rtl";
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Transfor a hast tree to a rich text field.
|
|
23
|
+
*
|
|
24
|
+
* @param tree - The hast tree to transform.
|
|
25
|
+
* @param file - The vfile to attach warnings to.
|
|
26
|
+
* @param config - Configuration that determines the output of the function.
|
|
27
|
+
*
|
|
28
|
+
* @returns The rich text field equivalent of the provided hast tree.
|
|
29
|
+
*/
|
|
30
|
+
export declare const hastToRichText: (tree: Root | Element, file: VFile, config?: HASTToRichTextConfig) => RichTextField;
|