@prismicio/client 7.7.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/lib/hastSerializerHelpers.cjs +1 -1
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -1
- package/dist/lib/hastSerializerHelpers.js +1 -1
- package/dist/lib/hastSerializerHelpers.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/package.json +1 -1
- package/src/lib/hastSerializerHelpers.ts +1 -1
|
@@ -10,7 +10,7 @@ const serializeImage = (node) => {
|
|
|
10
10
|
if (!src) {
|
|
11
11
|
throw new PrismicRichTextSerializerError.PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
|
|
12
12
|
}
|
|
13
|
-
const url = new URL(src);
|
|
13
|
+
const url = new URL(src, "https://noop.com");
|
|
14
14
|
let width = (_b = node.properties) == null ? void 0 : _b.width;
|
|
15
15
|
let height = (_c = node.properties) == null ? void 0 : _c.height;
|
|
16
16
|
let x = 0;
|
|
@@ -1 +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);\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;
|
|
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,OAAO,KAAK,CAAC;AAAA,MACzC;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AAAA,IACN,MAAM;AAAA,IACN,IAAI;AAAA,IACJ,KAAK;AAAA,IACL,OAAM,UAAK,eAAL,mBAAiB,QAAkB;AAAA,IACzC,aAAY,UAAK,eAAL,mBAAiB,cAAwB;AAAA;AAAA;AAAA;AAAA,IAIrD,YAAY,EAAE,OAAwB,OAA0B;AAAA,IAChE,MAAM,EAAE,GAAG,GAAG,MAAM,YAAY,cAAe;AAAA,EAAA;AAEjD;AASa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAIA,+BAAAA,+BACT,uDAAuD;AAAA,EAEzD;AAEA,QAAM,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,WAAW;AAAA,IACX,MAAMC,sBAAO,IAAI;AAAA,IACjB,QAAO,UAAK,eAAL,mBAAiB;AAAA,EAAA;AAGnB,QAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,QAAA,UAAS,UAAK,eAAL,mBAAiB;AAGhC,OAAK,WAAW;AAEhB,MAAI,SAAS,QAAQ;AACb,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAMC,MAAAA,WAAW,MAAM,OAAO,OAAQ;AAAA,IAAA;AAAA,SAE1D;AACC,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAMA,MAAAA,WAAW,KAAM;AAAA,IAAA;AAAA,EAElD;AACD;AAUa,MAAA,gBAAgB,CAC5B,MACA,wBAGwB;;AACxB,MACC,oBAAoB,SAAS,eAC7B,EAAE,UAAU,sBACX;AACK,UAAA,OAAM,UAAK,eAAL,mBAAiB;AAC7B,QAAI,CAAC,KAAK;AACH,YAAA,IAAIF,+BAAAA,+BACT,4DAA4D;AAAA,IAE9D;AAEO,WAAA;AAAA,MACN,MAAM;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;;;;"}
|
|
@@ -8,7 +8,7 @@ const serializeImage = (node) => {
|
|
|
8
8
|
if (!src) {
|
|
9
9
|
throw new PrismicRichTextSerializerError("Element of type `img` is missing an `src` attribute");
|
|
10
10
|
}
|
|
11
|
-
const url = new URL(src);
|
|
11
|
+
const url = new URL(src, "https://noop.com");
|
|
12
12
|
let width = (_b = node.properties) == null ? void 0 : _b.width;
|
|
13
13
|
let height = (_c = node.properties) == null ? void 0 : _c.height;
|
|
14
14
|
let x = 0;
|
|
@@ -1 +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);\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;
|
|
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,EACV,MAAM,GAAG;AAEX,UAAI,OAAO,KAAK;AAChB,UAAI,OAAO,KAAK;AAGhB,UAAI,OAAO;AACV,eAAO,KAAK,IAAI,GAAG,QAAQ,OAAO,KAAK,CAAC;AAAA,MACzC;AAAA,IACD;AAAA,EACD;AAEO,SAAA;AAAA,IACN,MAAM;AAAA,IACN,IAAI;AAAA,IACJ,KAAK;AAAA,IACL,OAAM,UAAK,eAAL,mBAAiB,QAAkB;AAAA,IACzC,aAAY,UAAK,eAAL,mBAAiB,cAAwB;AAAA;AAAA;AAAA;AAAA,IAIrD,YAAY,EAAE,OAAwB,OAA0B;AAAA,IAChE,MAAM,EAAE,GAAG,GAAG,MAAM,YAAY,cAAe;AAAA,EAAA;AAEjD;AASa,MAAA,iBAAiB,CAAC,SAA8B;;AACtD,QAAA,OAAM,UAAK,eAAL,mBAAiB;AAE7B,MAAI,CAAC,KAAK;AACH,UAAA,IAAI,+BACT,uDAAuD;AAAA,EAEzD;AAEA,QAAM,aAAa;AAAA,IAClB,SAAS;AAAA,IACT,WAAW;AAAA,IACX,MAAM,OAAO,IAAI;AAAA,IACjB,QAAO,UAAK,eAAL,mBAAiB;AAAA,EAAA;AAGnB,QAAA,SAAQ,UAAK,eAAL,mBAAiB;AACzB,QAAA,UAAS,UAAK,eAAL,mBAAiB;AAGhC,OAAK,WAAW;AAEhB,MAAI,SAAS,QAAQ;AACb,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAM,WAAW,MAAM,OAAO,OAAQ;AAAA,IAAA;AAAA,SAE1D;AACC,WAAA;AAAA,MACN,MAAM;AAAA,MACN,QAAQ,EAAE,GAAG,YAAY,MAAM,WAAW,KAAM;AAAA,IAAA;AAAA,EAElD;AACD;AAUa,MAAA,gBAAgB,CAC5B,MACA,wBAGwB;;AACxB,MACC,oBAAoB,SAAS,eAC7B,EAAE,UAAU,sBACX;AACK,UAAA,OAAM,UAAK,eAAL,mBAAiB;AAC7B,QAAI,CAAC,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;"}
|
package/dist/package.json.cjs
CHANGED
package/dist/package.json.js
CHANGED
package/package.json
CHANGED
|
@@ -25,7 +25,7 @@ export const serializeImage = (node: Element): RTImageNode => {
|
|
|
25
25
|
);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
const url = new URL(src);
|
|
28
|
+
const url = new URL(src, "https://noop.com");
|
|
29
29
|
|
|
30
30
|
let width = node.properties?.width as number | undefined;
|
|
31
31
|
let height = node.properties?.height as number | undefined;
|