Skip to content

Commit 5711f04

Browse files
committedJul 23, 2024·
fix: support relative image URL
1 parent b8cf3e5 commit 5711f04

3 files changed

+42
-16
lines changed
 

‎src/lib/hastSerializerHelpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const serializeImage = (node: Element): RTImageNode => {
2525
);
2626
}
2727

28-
const url = new URL(src);
28+
const url = new URL(src, "https://noop.com");
2929

3030
let width = node.properties?.width as number | undefined;
3131
let height = node.properties?.height as number | undefined;

‎test/__snapshots__/helpers-unstable_htmlAsRichText.test.ts.snap

+36-14
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,28 @@ exports[`transforms HTML to rich text > embed > iframe 1`] = `
474474

475475
exports[`transforms HTML to rich text > embed > missing src 1`] = `[]`;
476476

477+
exports[`transforms HTML to rich text > image > absolute 1`] = `
478+
[
479+
{
480+
"alt": "foo",
481+
"copyright": null,
482+
"dimensions": {
483+
"height": undefined,
484+
"width": undefined,
485+
},
486+
"edit": {
487+
"background": "transparent",
488+
"x": 0,
489+
"y": 0,
490+
"zoom": 1,
491+
},
492+
"id": "",
493+
"type": "image",
494+
"url": "https://example.com/foo.png",
495+
},
496+
]
497+
`;
498+
477499
exports[`transforms HTML to rich text > image > empty alt 1`] = `
478500
[
479501
{
@@ -692,46 +714,46 @@ exports[`transforms HTML to rich text > image > missing alt 1`] = `
692714

693715
exports[`transforms HTML to rich text > image > missing src 1`] = `[]`;
694716

695-
exports[`transforms HTML to rich text > image > non-prismic 1`] = `
717+
exports[`transforms HTML to rich text > image > prismic 1`] = `
696718
[
697719
{
698720
"alt": "foo",
699721
"copyright": null,
700722
"dimensions": {
701-
"height": undefined,
702-
"width": undefined,
723+
"height": 1602,
724+
"width": 2400,
703725
},
704726
"edit": {
705727
"background": "transparent",
706-
"x": 0,
707-
"y": 0,
708-
"zoom": 1,
728+
"x": 399,
729+
"y": 259,
730+
"zoom": 1.5,
709731
},
710732
"id": "",
711733
"type": "image",
712-
"url": "https://example.com/foo.png",
734+
"url": "https://images.prismic.io/200629-sms-hoy/f0a757f6-770d-4eb8-a08b-f1727f1a58e4_guilherme-romano-KI2KaOeT670-unsplash.jpg?auto=format%2Ccompress&rect=399%2C259%2C1600%2C1068&w=2400&h=1602",
713735
},
714736
]
715737
`;
716738

717-
exports[`transforms HTML to rich text > image > prismic 1`] = `
739+
exports[`transforms HTML to rich text > image > relative 1`] = `
718740
[
719741
{
720742
"alt": "foo",
721743
"copyright": null,
722744
"dimensions": {
723-
"height": 1602,
724-
"width": 2400,
745+
"height": undefined,
746+
"width": undefined,
725747
},
726748
"edit": {
727749
"background": "transparent",
728-
"x": 399,
729-
"y": 259,
730-
"zoom": 1.5,
750+
"x": 0,
751+
"y": 0,
752+
"zoom": 1,
731753
},
732754
"id": "",
733755
"type": "image",
734-
"url": "https://images.prismic.io/200629-sms-hoy/f0a757f6-770d-4eb8-a08b-f1727f1a58e4_guilherme-romano-KI2KaOeT670-unsplash.jpg?auto=format%2Ccompress&rect=399%2C259%2C1600%2C1068&w=2400&h=1602",
756+
"url": "/foo.png",
735757
},
736758
]
737759
`;

‎test/helpers-unstable_htmlAsRichText.test.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ describe("transforms HTML to rich text", () => {
115115
});
116116

117117
describe("image", () => {
118-
testHTMLAsRichTextHelper("non-prismic", {
118+
testHTMLAsRichTextHelper("absolute", {
119119
input: /* html */ `<img src="https://example.com/foo.png" alt="foo" />`,
120120
});
121121

122+
testHTMLAsRichTextHelper("relative", {
123+
input: /* html */ `<img src="/foo.png" alt="foo" />`,
124+
});
125+
122126
testHTMLAsRichTextHelper("prismic", {
123127
input: /* html */ `<img src="https://images.prismic.io/200629-sms-hoy/f0a757f6-770d-4eb8-a08b-f1727f1a58e4_guilherme-romano-KI2KaOeT670-unsplash.jpg?auto=format%2Ccompress&rect=399%2C259%2C1600%2C1068&w=2400&h=1602" alt="foo" />`,
124128
});

0 commit comments

Comments
 (0)
Please sign in to comment.