Skip to content

Commit

Permalink
Collapse parser logic into single reduce loop
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth authored and youknowriad committed Mar 24, 2017
1 parent b4e34f8 commit 222fbff
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions modules/blocks/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,20 @@ export function getBlockAttributes( blockNode, blockSettings ) {
/**
* Returns a list of blocks extracted from the Post Content
*
* @param {String} postContent The post content
* @return {Array} Block list
* @param {String} content The post content
* @return {Array} Block list
*/
const parse = ( postContent ) => {
const nodeBlocks = grammarParse( postContent );
export default function parse( content ) {
return grammarParse( content ).reduce( ( memo, blockNode ) => {
const settings = getBlockSettings( blockNode.blockType );

return nodeBlocks
.map( ( blockNode ) => {
return {
blockSettings: getBlockSettings( blockNode.blockType ),
blockNode
};
} )
.filter( ( { blockSettings } ) => !! blockSettings )
.map( ( { blockNode, blockSettings } ) => {
return {
if ( settings ) {
memo.push( {
blockType: blockNode.blockType,
attributes: getBlockAttributes( blockNode, blockSettings )
};
} );
};
attributes: getBlockAttributes( blockNode, settings )
} );
}

export default parse;
return memo;
}, [] );
}

0 comments on commit 222fbff

Please sign in to comment.