Skip to content

Commit 87252f2

Browse files
hypestgziolo
authored andcommitted
Expose the grammar parser to the mobile app (WordPress#7691)
* Expose `parse` to RN, use pre-generated post parser * Offer RawHTML to fix warning during save() Warning is: "Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined." * Run parse() testcases for the pre-generated parser too * Move RawHTML() to base module to avoid duplication * Generalize parseWithGrammar to avoid code duplication H/T to @gziolo for providing the createParse() function. * Follow codestyle and add dep block header * Separate describe() for the parse() testcases
1 parent e1fa053 commit 87252f2

9 files changed

+1672
-18
lines changed

blocks/api/index.native.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export {
22
createBlock,
33
} from './factory';
4+
export {
5+
default as parse,
6+
} from './parser';
47
export {
58
default as serialize,
69
getBlockContent,

blocks/api/parser.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import deprecated from '@wordpress/deprecated';
1414
/**
1515
* Internal dependencies
1616
*/
17-
import { parse as grammarParse } from './post.pegjs';
17+
import { parse as grammarParse } from './post-parser';
1818
import { getBlockType, getUnknownTypeHandlerName } from './registration';
1919
import { createBlock } from './factory';
2020
import { isValidBlock } from './validation';
@@ -359,20 +359,28 @@ export function createBlockWithFallback( blockNode ) {
359359
}
360360

361361
/**
362-
* Parses the post content with a PegJS grammar and returns a list of blocks.
362+
* Creates a parse implementation for the post content which returns a list of blocks.
363363
*
364-
* @param {string} content The post content.
364+
* @param {Function} parseImplementation Parse implementation.
365365
*
366-
* @return {Array} Block list.
366+
* @return {Function} An implementation which parses the post content.
367367
*/
368-
export function parseWithGrammar( content ) {
369-
return grammarParse( content ).reduce( ( memo, blockNode ) => {
368+
export const createParse = ( parseImplementation ) =>
369+
( content ) => parseImplementation( content ).reduce( ( memo, blockNode ) => {
370370
const block = createBlockWithFallback( blockNode );
371371
if ( block ) {
372372
memo.push( block );
373373
}
374374
return memo;
375375
}, [] );
376-
}
376+
377+
/**
378+
* Parses the post content with a PegJS grammar and returns a list of blocks.
379+
*
380+
* @param {string} content The post content.
381+
*
382+
* @return {Array} Block list.
383+
*/
384+
export const parseWithGrammar = createParse( grammarParse );
377385

378386
export default parseWithGrammar;

0 commit comments

Comments
 (0)