From 5951c59e2604321cca5c6dc83051cb9a00812041 Mon Sep 17 00:00:00 2001 From: Victor Fernandez de Alba Date: Sat, 15 Jan 2022 18:28:14 +0100 Subject: [PATCH] refactor: Do not issue a in slate, always link types. This is consistent with how is the default behavior in slate. --- src/converters/slate.js | 32 ++++++++++++++++++++++---------- src/converters/slate.test.js | 12 ++++++------ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/converters/slate.js b/src/converters/slate.js index 3e22d61..620dd9d 100644 --- a/src/converters/slate.js +++ b/src/converters/slate.js @@ -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; @@ -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); } diff --git a/src/converters/slate.test.js b/src/converters/slate.test.js index 910fa74..b09a2ee 100644 --- a/src/converters/slate.test.js +++ b/src/converters/slate.test.js @@ -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!'); }); }); @@ -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'); }); });