Skip to content

Commit

Permalink
refactor: Do not issue a in slate, always link types. This is consist…
Browse files Browse the repository at this point in the history
…ent with how is the default behavior in slate.
  • Loading branch information
sneridagh authored and ericof committed Feb 18, 2022
1 parent f57da58 commit 5951c59
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
32 changes: 22 additions & 10 deletions src/converters/slate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import { jsx } from 'slate-hyperscript';

const getId = () => Math.floor(Math.random() * Math.pow(2, 24)).toString(32);

const simpleLinkDeserializer = (el) => {
let parent = el;

let children = Array.from(parent.childNodes)
.map((el) => deserialize(el))
.flat();

if (!children.length) children = [''];

const attrs = {
type: 'link',
data: {
url: el.getAttribute('href'),
title: el.getAttribute('title'),
target: el.getAttribute('target'),
},
};

return jsx('element', attrs, children);
};

const deserialize = (el) => {
if (el.nodeType === 3) {
return el.textContent;
Expand Down Expand Up @@ -48,16 +69,7 @@ const deserialize = (el) => {
case 'LI':
return jsx('element', { type: 'li' }, children);
case 'A':
return jsx(
'element',
{
type: 'a',
url: el.getAttribute('href'),
title: el.getAttribute('title'),
target: el.getAttribute('target'),
},
children,
);
return simpleLinkDeserializer(el);
default:
return jsx('element', { type: 'p' }, children);
}
Expand Down
12 changes: 6 additions & 6 deletions src/converters/slate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ describe('slateTextBlock processing a link', () => {
test('will have a nested structure in the value', () => {
const result = slateTextBlock(elem);
const valueElement = result.value[0];
expect(valueElement['type']).toBe('a');
expect(valueElement['url']).toBe('https://plone.org/');
expect(valueElement['title']).toBe('Plone website');
expect(valueElement['target']).toBe('_blank');
expect(valueElement['type']).toBe('link');
expect(valueElement['data']['url']).toBe('https://plone.org/');
expect(valueElement['data']['title']).toBe('Plone website');
expect(valueElement['data']['target']).toBe('_blank');
expect(valueElement['children'][0]['text']).toBe('Welcome to Plone!');
});
});
Expand Down Expand Up @@ -295,8 +295,8 @@ describe('slateTableBlock processing a table with a link', () => {
expect(rows[0].cells[0].type).toBe('data');
expect(rows[0].cells[0].value).toHaveLength(1);
const value = rows[0].cells[0].value[0];
expect(value['type']).toBe('a');
expect(value['url']).toBe('https://plone.org');
expect(value['type']).toBe('link');
expect(value['data']['url']).toBe('https://plone.org');
expect(value['children'][0]['text']).toBe('Plone');
});
});

0 comments on commit 5951c59

Please sign in to comment.