/
transforms.js
75 lines (72 loc) · 1.85 KB
/
transforms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import { __unstableCreateElement as createElement } from '@wordpress/rich-text';
/**
* Internal dependencies
*/
import { getTransformedMetadata } from '../utils/get-transformed-metadata';
const transforms = {
from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/button' ],
transform: ( buttons ) =>
// Creates the buttons block.
createBlock(
'core/buttons',
{},
// Loop the selected buttons.
buttons.map( ( attributes ) =>
// Create singular button in the buttons block.
createBlock( 'core/button', attributes )
)
),
},
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/paragraph' ],
transform: ( buttons ) =>
// Creates the buttons block.
createBlock(
'core/buttons',
{},
// Loop the selected buttons.
buttons.map( ( attributes ) => {
const { content, metadata } = attributes;
const element = createElement( document, content );
// Remove any HTML tags.
const text = element.innerText || '';
// Get first url.
const link = element.querySelector( 'a' );
const url = link?.getAttribute( 'href' );
// Create singular button in the buttons block.
return createBlock( 'core/button', {
text,
url,
metadata: getTransformedMetadata(
metadata,
[ 'id', 'name', 'bindings' ],
{ content: 'text' }
),
} );
} )
),
isMatch: ( paragraphs ) => {
return paragraphs.every( ( attributes ) => {
const element = createElement(
document,
attributes.content
);
const text = element.innerText || '';
const links = element.querySelectorAll( 'a' );
return text.length <= 30 && links.length <= 1;
} );
},
},
],
};
export default transforms;