Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the grammar parser to the mobile app #7691

Merged
merged 10 commits into from
Jul 6, 2018
3 changes: 3 additions & 0 deletions blocks/api/index.native.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export {
createBlock,
} from './factory';
export {
default as parse,
} from './parser';
export {
default as serialize,
getBlockContent,
Expand Down
22 changes: 15 additions & 7 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { applyFilters } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { parse as grammarParse } from './post.pegjs';
import { parse as grammarParse } from './post-parser';
import { getBlockType, getUnknownTypeHandlerName } from './registration';
import { createBlock } from './factory';
import { isValidBlock } from './validation';
Expand Down Expand Up @@ -352,20 +352,28 @@ export function createBlockWithFallback( blockNode ) {
}

/**
* Parses the post content with a PegJS grammar and returns a list of blocks.
* Creates a parse implementation for the post content which returns a list of blocks.
*
* @param {string} content The post content.
* @param {Function} parseImplementation Parse implementation.
*
* @return {Array} Block list.
* @return {Function} An implementation which parses the post content.
*/
export function parseWithGrammar( content ) {
return grammarParse( content ).reduce( ( memo, blockNode ) => {
export const createParse = ( parseImplementation ) =>
( content ) => parseImplementation( content ).reduce( ( memo, blockNode ) => {
const block = createBlockWithFallback( blockNode );
if ( block ) {
memo.push( block );
}
return memo;
}, [] );
}

/**
* Parses the post content with a PegJS grammar and returns a list of blocks.
*
* @param {string} content The post content.
*
* @return {Array} Block list.
*/
export const parseWithGrammar = createParse( grammarParse );

export default parseWithGrammar;
Loading