Skip to content

Commit

Permalink
refined migration
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed May 31, 2024
1 parent 9c40092 commit 05b9ae3
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 21 deletions.
11 changes: 9 additions & 2 deletions src/converters/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,20 @@ const videoBlock = (elem) => {
};

const calloutBlock = (elem) => {
const convertedChild = slateTextBlock(elem);
const convertedChild = slateTextBlock(elem).value[0].children;
let value = convertedChild.map((x) => {
if (!x.type) {
return { type: 'p', children: [x] };
}
return x;
});

const block = {
'@type': 'callout_block',
style: 'base',
icon: 'it-info-circle',
title: '',
text: convertedChild.value,
text: value,
color: 'default',
};
return block;
Expand Down
15 changes: 11 additions & 4 deletions src/converters/fromDraftjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const joinRecursively = (array) =>
if (Array.isArray(child)) {
return joinRecursively(child);
}
return child;
return child.replace('\n', '<br />');
})
.filter((x) => x.length > 0)
.join('')
: '';

Expand Down Expand Up @@ -72,7 +73,9 @@ const addBreaklinesInline = (children) => {
if (s.split('\n').length > 1) {
return s
.split('\n')
.map((child, index) => (child?.length > 0 ? `${child}<br />` : child));
.map((child, index) => (child?.length > 0 ? `${child}<br />` : child))
.filter((x) => x.length)
.join('');
}
}
return joinRecursively(children);
Expand Down Expand Up @@ -268,9 +271,13 @@ const blocks = {
'header-six': (children, { keys }) =>
children.map((child, i) => `<h6>${joinRecursively(child)}</h6>`),
callout: (children, { keys }) =>
children.map((child, i) => `<q>${joinRecursively(child)}</q>`),
children.map(
(child, i) => `<blockquote><p>${joinRecursively(child)}</p></blockquote>`,
),
'callout-bg': (children, { keys }) =>
children.map((child, i) => `<q>${joinRecursively(child)}</q>`),
children.map(
(child, i) => `<blockquote><p>${joinRecursively(child)}</p></blockquote>`,
),
buttons: (children, { keys }) => {
let html = children[0].filter((x) => x !== undefined);
const tag = parser.parseFromString(html, 'text/html');
Expand Down
50 changes: 48 additions & 2 deletions src/converters/fromDraftjs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ describe('convertFromDraftJS parsing draftjs callout', () => {
describe('with html converter', () => {
const result = converFromDraftJS(draftjs);

test('will return a <q> tag', () => {
expect(result).toBe('<q><strong>Callout text with bold</strong></q>');
test('will return a <blockquote> tag', () => {
expect(result).toBe(
'<blockquote><p><strong>Callout text with bold</strong></p></blockquote>',
);
});
});
});
Expand Down Expand Up @@ -448,3 +450,47 @@ describe('convertFromDraftJS parsing draftjs blockquote', () => {
});
});
});

// describe('prova', () => {
// const draftjs = {
// blocks: [
// {
// key: 'jrnb',
// text: 'Trasporto delle persone con difficoltà motorie ai seggi\nIl Comune di Cavriago, in collaborazione con l’Associazione “NOI CON VOI”, organizza il trasporto delle persone disabili mediante il pulmino adibito appositamente a tale servizio.\nIl servizio verrà effettuato nei seguenti orari:\nSABATO 8 GIUGNO dalle 15.00 alle 17.00\nDOMENICA 9 GIUGNO dalle 10.00 alle 12.00.\nIl trasporto dovrà essere prenotato direttamente all’associazione al numero 333 5383663 (Ines).\n',
// type: 'callout',
// depth: 0,
// inlineStyleRanges: [
// {
// offset: 0,
// length: 56,
// style: 'BOLD',
// },
// {
// offset: 116,
// length: 13,
// style: 'BOLD',
// },
// {
// offset: 285,
// length: 81,
// style: 'BOLD',
// },
// {
// offset: 443,
// length: 11,
// style: 'BOLD',
// },
// ],
// entityRanges: [],
// data: {},
// },
// ],
// entityMap: {},
// };
// describe('with html converter', () => {
// const result = converFromDraftJS(draftjs);
// test('will return something', () => {
// expect(result).toBe('');
// });
// });
// });
2 changes: 1 addition & 1 deletion src/converters/fromHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const blockFromElement = (el, defaultTextBlock, href) => {
case 'IFRAME':
raw = iframeBlock(el);
break;
case 'Q':
case 'BLOCKQUOTE':
raw = calloutBlock(el);
break;
default:
Expand Down
73 changes: 61 additions & 12 deletions src/converters/fromHtml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,35 +649,42 @@ describe('convertFromHTML parsing nested tags', () => {

describe('convertFromHTML parsing quote', () => {
test('on its own', () => {
const html = '<q><strong>Callout text with bold</strong></q>';
const html =
'<blockquote><p><strong>Callout text with bold</strong></p></blockquote>';

const result = convertFromHTML(html, 'slate');
expect(result).toHaveLength(1);
expect(result).toEqual([
{
'@type': 'callout_block',
style: 'base',
color: 'default',
icon: 'it-info-circle',
title: '',
style: 'base',
text: [
{
children: [
{
text: 'Callout text with bold',
children: [
{
text: 'Callout text with bold',
},
],
type: 'strong',
},
],
type: 'strong',
type: 'p',
},
],
color: 'default',
title: '',
},
]);
});
});

describe('convertFromHTML create callout_block block from quote tag', () => {
test('on its own', () => {
const html = '<q><strong>Callout text with bold</strong></q>';
const html =
'<blockquote><strong>Callout text with bold</strong></blockquote>';

const result = convertFromHTML(html, 'slate');
expect(result).toHaveLength(1);
Expand Down Expand Up @@ -898,18 +905,60 @@ describe('convertFromHTML create slate block with complex ordered list', () => {
});
});

describe('convertFromHTML create slate block with blockquote', () => {
describe('convertFromHTML create callout_block', () => {
test('on its own', () => {
const html = '<blockquote class="blockquote">text</blockquote>';
const html = '<blockquote class="blockquote"><p>text</p></blockquote>';

const result = convertFromHTML(html, 'slate');
expect(result).toHaveLength(1);
expect(result).toEqual([
{
'@type': 'slate',
plaintext: 'text',
value: [{ children: [{ text: 'text' }], type: 'blockquote' }],
'@type': 'callout_block',
color: 'default',
icon: 'it-info-circle',
style: 'base',
text: [
{
children: [
{
text: 'text',
},
],
type: 'p',
},
],
title: '',
},
]);
});
});

describe('convertFromHTML create slate block with blockquote simple', () => {
test('on its own', () => {
const html = '<blockquote>Testo</blockquote>';

const result = convertFromHTML(html, 'slate');
expect(result).toHaveLength(1);
expect(result).toEqual([
{
'@type': 'callout_block',
style: 'base',
icon: 'it-info-circle',
title: '',
text: [{ type: 'p', children: [{ text: 'Testo' }] }],
color: 'default',
},
]);
});
});

// describe.only('prova', () => {
// test('on its own', () => {
// const html =
// '<blockquote><p><strong>Trasporto delle persone con difficoltà motorie ai seggi<br /></strong>Il Comune di Cavriago, in collaborazione con l’Associazione <strong>“NOI CON VOI”</strong>, organizza il trasporto delle persone disabili mediante il pulmino adibito appositamente a tale servizio. Il servizio verrà effettuato nei seguenti orari:<strong>SABATO 8 GIUGNO dalle 15.00 alle 17.00<br />,DOMENICA 9 GIUGNO dalle 10.00 alle 12.00.<br /></strong>Il trasporto dovrà essere prenotato direttamente all’associazione al numero <strong>333 5383663</strong> (Ines).</p></blockquote>';

// const result = convertFromHTML(html, 'slate');
// expect(result).toHaveLength(1);
// expect(result).toEqual();
// });
// });

0 comments on commit 05b9ae3

Please sign in to comment.