-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
transforms.js
87 lines (81 loc) · 1.72 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
76
77
78
79
80
81
82
83
84
85
86
87
/**
* WordPress dependencies
*/
import { createBlock, getBlockAttributes } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { getLevelFromHeadingNodeName } from './shared';
import { name } from './block.json';
const transforms = {
from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
attributes.map( ( { content, anchor } ) =>
createBlock( name, {
content,
anchor,
} )
),
},
{
type: 'raw',
selector: 'h1,h2,h3,h4,h5,h6',
schema: ( { phrasingContentSchema, isPaste } ) => {
const schema = {
children: phrasingContentSchema,
attributes: isPaste ? [] : [ 'style', 'id' ],
};
return {
h1: schema,
h2: schema,
h3: schema,
h4: schema,
h5: schema,
h6: schema,
};
},
transform( node ) {
const attributes = getBlockAttributes( name, node.outerHTML );
const { textAlign } = node.style || {};
attributes.level = getLevelFromHeadingNodeName( node.nodeName );
if (
textAlign === 'left' ||
textAlign === 'center' ||
textAlign === 'right'
) {
attributes.align = textAlign;
}
return createBlock( name, attributes );
},
},
...[ 2, 3, 4, 5, 6 ].map( ( level ) => ( {
type: 'prefix',
prefix: Array( level + 1 ).join( '#' ),
transform( content ) {
return createBlock( name, {
level,
content,
} );
},
} ) ),
],
to: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
attributes.map( ( { content, anchor } ) =>
createBlock( 'core/paragraph', {
content,
anchor,
} )
),
},
],
};
export default transforms;