-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
serializer.js
97 lines (87 loc) · 2.62 KB
/
serializer.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
88
89
90
91
92
93
94
95
96
97
/**
* External dependencies
*/
import { difference } from 'lodash';
import { html as beautifyHtml } from 'js-beautify';
/**
* Internal dependencies
*/
import { getBlockSettings } from './registration';
import { parseBlockAttributes } from './parser';
/**
* Given a block's save render implementation and attributes, returns the
* static markup to be saved.
*
* @param {Function|WPComponent} save Save render implementation
* @param {Object} attributes Block attributes
* @return {string} Save content
*/
export function getSaveContent( save, attributes ) {
let rawContent;
if ( save.prototype instanceof wp.element.Component ) {
rawContent = wp.element.createElement( save, { attributes } );
} else {
rawContent = save( { attributes } );
// Special-case function render implementation to allow raw HTML return
if ( 'string' === typeof rawContent ) {
return rawContent;
}
}
// Otherwise, infer as element
return wp.element.renderToString( rawContent );
}
/**
* Returns comment attributes as serialized string, determined by subset of
* difference between actual attributes of a block and those expected based
* on its settings.
*
* @param {Object} realAttributes Actual block attributes
* @param {Object} expectedAttributes Expected block attributes
* @return {string} Comment attributes
*/
export function getCommentAttributes( realAttributes, expectedAttributes ) {
// Find difference and build into object subset of attributes.
const keys = difference(
Object.keys( realAttributes ),
Object.keys( expectedAttributes )
);
// Serialize the comment attributes
return keys.reduce( ( memo, key ) => {
const value = realAttributes[ key ];
if ( undefined === value ) {
return memo;
}
return memo + `${ key }:${ value } `;
}, '' );
}
/**
* Takes a block list and returns the serialized post content
*
* @param {Array} blocks Block list
* @return {String} The post content
*/
export default function serialize( blocks ) {
return blocks.reduce( ( memo, block ) => {
const blockType = block.blockType;
const settings = getBlockSettings( blockType );
const saveContent = getSaveContent( settings.save, block.attributes );
const beautifyOptions = {
indent_inner_html: true,
wrap_line_length: 0
};
return memo + (
'<!-- wp:' +
blockType +
' ' +
getCommentAttributes(
block.attributes,
parseBlockAttributes( saveContent, settings )
) +
'-->' +
( saveContent ? '\n' + beautifyHtml( saveContent, beautifyOptions ) + '\n' : '' ) +
'<!-- /wp:' +
blockType +
' -->'
) + '\n\n';
}, '' );
}