Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions packages/html-to-slate-ast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,39 @@ const ELEMENT_TAGS: Record<
? el.getAttribute('title')
: '(Image)';
if (href === null) return {};

// Fix text node when pasting images; sanitize URLs;
// if the image is not hosted on graphassets.com, we convert it to a link
if (href.includes('graphassets.com') === false) {
return {
type: 'link',
href: sanitizeUrl(href),
title,
openInNewTab: true,
};
}

// if href includes graphassets.com, we convert it to a image
// handle is always the last part of the href
const handle = href.split('/').pop();

return {
type: 'link',
href: sanitizeUrl(href),
title,
openInNewTab: true,
type: 'image',
src: href,
...(el.hasAttribute('title') && { title: el.getAttribute('title') }),
...(el.hasAttribute('width') && {
width: Number(el.getAttribute('width')),
}),
...(el.hasAttribute('height') && {
height: Number(el.getAttribute('height')),
}),
...(el.hasAttribute('class') && {
className: el.getAttribute('class'),
}),
...(el.hasAttribute('style') && {
style: el.getAttribute('style'),
}),
...(handle && { handle }),
};
},
PRE: () => ({ type: 'code-block' }),
Expand Down
23 changes: 23 additions & 0 deletions packages/html-to-slate-ast/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,29 @@
);
});

test('Keeps image if it is hosted on hygraph', () => {
return htmlToSlateAST(
`<img
title="this is this image&#39;s title"
src="https://media.graphassets.com/output=format:webp/resize=,width:667,height:1000/8xrjYm4CR721mAZ1YAoy"
width="600" height="1000" style="margin-left:0px;margin-top:0px;" />`
).then(ast => {
expect(ast).toStrictEqual([
{
type: 'image',
src:
'https://media.graphassets.com/output=format:webp/resize=,width:667,height:1000/8xrjYm4CR721mAZ1YAoy',
width: 600,
height: 1000,
title: "this is this image's title",
style: 'margin-left:0px;margin-top:0px;',
handle: '8xrjYm4CR721mAZ1YAoy',
children: [],
},
]);
});
});

test('Reshape an incorrectly structured table', () => {
return htmlToSlateAST(
'<table><colgroup><col /><col /></colgroup><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr><tr></tr></tbody></table>'
Expand Down Expand Up @@ -1360,7 +1383,7 @@
describe('Should parse embeds', () => {
describe('Block embeds', () => {
test('Assets', () => {
const input = `<div data-gcms-embed-type=\"Asset\" data-gcms-embed-id=\"cl0wps6je01cw0cubrunozmfs\"></div>`;

Check warning on line 1386 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"

Check warning on line 1386 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"

Check warning on line 1386 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"

Check warning on line 1386 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"
return htmlToSlateAST(input).then(result => {
expect(result).toStrictEqual([
{
Expand All @@ -1377,7 +1400,7 @@
});
});
test('Non-asset models', () => {
const input = `<div data-gcms-embed-type=\"SourceModel\" data-gcms-embed-id=\"cl13daqqz00bi08uctxdel1x3\"></div>`;

Check warning on line 1403 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"

Check warning on line 1403 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"

Check warning on line 1403 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"

Check warning on line 1403 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"
return htmlToSlateAST(input).then(result => {
expect(result).toStrictEqual([
{
Expand All @@ -1396,7 +1419,7 @@
});
describe('Inline embeds', () => {
test('Asset model', () => {
const input = `<p>Some text with an inline asset embed: <span data-gcms-embed-type=\"Asset\" data-gcms-embed-id=\"cl0wps6je01cw0cubrunozmfs\" data-gcms-embed-inline></span></p>`;

Check warning on line 1422 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"

Check warning on line 1422 in packages/html-to-slate-ast/test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary escape character: \"
return htmlToSlateAST(input).then(result => {
expect(result).toStrictEqual([
{
Expand Down