-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
84 lines (75 loc) · 2.51 KB
/
index.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
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import { getPhrasingContentSchema } from '@wordpress/dom';
/**
* Internal dependencies
*/
import { htmlToBlocks } from './html-to-blocks';
import parse from '../parser';
import normaliseBlocks from './normalise-blocks';
import specialCommentConverter from './special-comment-converter';
import listReducer from './list-reducer';
import blockquoteNormaliser from './blockquote-normaliser';
import figureContentReducer from './figure-content-reducer';
import shortcodeConverter from './shortcode-converter';
import { deepFilterHTML, getBlockContentSchema } from './utils';
export { pasteHandler } from './paste-handler';
export function deprecatedGetPhrasingContentSchema( context ) {
deprecated( 'wp.blocks.getPhrasingContentSchema', {
since: '5.6',
alternative: 'wp.dom.getPhrasingContentSchema',
} );
return getPhrasingContentSchema( context );
}
/**
* Converts an HTML string to known blocks.
*
* @param {Object} $1
* @param {string} $1.HTML The HTML to convert.
*
* @return {Array} A list of blocks.
*/
export function rawHandler( { HTML = '' } ) {
// If we detect block delimiters, parse entirely as blocks.
if ( HTML.indexOf( '<!-- wp:' ) !== -1 ) {
const parseResult = parse( HTML );
const isSingleFreeFormBlock =
parseResult.length === 1 &&
parseResult[ 0 ].name === 'core/freeform';
if ( ! isSingleFreeFormBlock ) {
return parseResult;
}
}
// An array of HTML strings and block objects. The blocks replace matched
// shortcodes.
const pieces = shortcodeConverter( HTML );
const blockContentSchema = getBlockContentSchema();
return pieces
.map( ( piece ) => {
// Already a block from shortcode.
if ( typeof piece !== 'string' ) {
return piece;
}
// These filters are essential for some blocks to be able to transform
// from raw HTML. These filters move around some content or add
// additional tags, they do not remove any content.
const filters = [
// Needed to adjust invalid lists.
listReducer,
// Needed to create more and nextpage blocks.
specialCommentConverter,
// Needed to create media blocks.
figureContentReducer,
// Needed to create the quote block, which cannot handle text
// without wrapper paragraphs.
blockquoteNormaliser( { raw: true } ),
];
piece = deepFilterHTML( piece, filters, blockContentSchema );
piece = normaliseBlocks( piece, { raw: true } );
return htmlToBlocks( piece, rawHandler );
} )
.flat()
.filter( Boolean );
}