-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Make
serializeBlocks
a util.
- Loading branch information
Showing
5 changed files
with
55 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import memoize from 'memize'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
isUnmodifiedDefaultBlock, | ||
serialize, | ||
getFreeformContentHandlerName, | ||
} from '@wordpress/blocks'; | ||
import { removep } from '@wordpress/autop'; | ||
|
||
/** | ||
* Serializes blocks following backwards compatibility conventions. | ||
* | ||
* @param {Array} blocksForSerialization The blocks to serialize. | ||
* | ||
* @return {string} The blocks serialization. | ||
*/ | ||
const serializeBlocks = memoize( | ||
( blocksForSerialization ) => { | ||
// A single unmodified default block is assumed to | ||
// be equivalent to an empty post. | ||
if ( | ||
blocksForSerialization.length === 1 && | ||
isUnmodifiedDefaultBlock( blocksForSerialization[ 0 ] ) | ||
) { | ||
blocksForSerialization = []; | ||
} | ||
|
||
let content = serialize( blocksForSerialization ); | ||
|
||
// For compatibility, treat a post consisting of a | ||
// single freeform block as legacy content and apply | ||
// pre-block-editor removep'd content formatting. | ||
if ( | ||
blocksForSerialization.length === 1 && | ||
blocksForSerialization[ 0 ].name === getFreeformContentHandlerName() | ||
) { | ||
content = removep( content ); | ||
} | ||
|
||
return content; | ||
}, | ||
{ maxSize: 1 } | ||
); | ||
|
||
export default serializeBlocks; |