Skip to content

Commit 4f506a2

Browse files
authored
fix: preserve graphassets images during HTML import (#136)
* fix: preserve graphassets images during HTML import * Fix HTML import to preserve graphassets images This change ensures that graphassets images are preserved when importing HTML.
1 parent 0cfe908 commit 4f506a2

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

.changeset/light-chicken-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphcms/html-to-slate-ast": patch
3+
---
4+
5+
fix: preserve graphassets images during HTML import

packages/html-to-slate-ast/src/index.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,39 @@ const ELEMENT_TAGS: Record<
5454
? el.getAttribute('title')
5555
: '(Image)';
5656
if (href === null) return {};
57+
58+
// Fix text node when pasting images; sanitize URLs;
59+
// if the image is not hosted on graphassets.com, we convert it to a link
60+
if (href.includes('graphassets.com') === false) {
61+
return {
62+
type: 'link',
63+
href: sanitizeUrl(href),
64+
title,
65+
openInNewTab: true,
66+
};
67+
}
68+
69+
// if href includes graphassets.com, we convert it to a image
70+
// handle is always the last part of the href
71+
const handle = href.split('/').pop();
72+
5773
return {
58-
type: 'link',
59-
href: sanitizeUrl(href),
60-
title,
61-
openInNewTab: true,
74+
type: 'image',
75+
src: href,
76+
...(el.hasAttribute('title') && { title: el.getAttribute('title') }),
77+
...(el.hasAttribute('width') && {
78+
width: Number(el.getAttribute('width')),
79+
}),
80+
...(el.hasAttribute('height') && {
81+
height: Number(el.getAttribute('height')),
82+
}),
83+
...(el.hasAttribute('class') && {
84+
className: el.getAttribute('class'),
85+
}),
86+
...(el.hasAttribute('style') && {
87+
style: el.getAttribute('style'),
88+
}),
89+
...(handle && { handle }),
6290
};
6391
},
6492
PRE: () => ({ type: 'code-block' }),

packages/html-to-slate-ast/test/index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,29 @@ test('Converts an image pasted from Google Docs into a link node', () => {
11891189
);
11901190
});
11911191

1192+
test('Keeps image if it is hosted on hygraph', () => {
1193+
return htmlToSlateAST(
1194+
`<img
1195+
title="this is this image&#39;s title"
1196+
src="https://media.graphassets.com/output=format:webp/resize=,width:667,height:1000/8xrjYm4CR721mAZ1YAoy"
1197+
width="600" height="1000" style="margin-left:0px;margin-top:0px;" />`
1198+
).then(ast => {
1199+
expect(ast).toStrictEqual([
1200+
{
1201+
type: 'image',
1202+
src:
1203+
'https://media.graphassets.com/output=format:webp/resize=,width:667,height:1000/8xrjYm4CR721mAZ1YAoy',
1204+
width: 600,
1205+
height: 1000,
1206+
title: "this is this image's title",
1207+
style: 'margin-left:0px;margin-top:0px;',
1208+
handle: '8xrjYm4CR721mAZ1YAoy',
1209+
children: [],
1210+
},
1211+
]);
1212+
});
1213+
});
1214+
11921215
test('Reshape an incorrectly structured table', () => {
11931216
return htmlToSlateAST(
11941217
'<table><colgroup><col /><col /></colgroup><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr><tr></tr></tbody></table>'

0 commit comments

Comments
 (0)