Skip to content

Commit

Permalink
Parser: Parse <!-- more --> tag and <!-- noteaser --> (#1460)
Browse files Browse the repository at this point in the history
* Parser: Parse <!-- more --> tag and <!-- noteaser -->

These tags are officially supported by WordPress and are an exception
with the concept of blocks. They contain structural data and yet they
are just HTML comments without any further indication.

The "more" tag supports an optional custom text string and if it is
immediately followed by the "no teaser" tag then that contains
additional information.

In this change we parse those tags and combine them into a single "more"
block with attributes of the custom text and whether or not the
`noTeaser` option has been selected.
  • Loading branch information
dmsnell authored Jul 4, 2017
1 parent 1fd55b2 commit fdbfc9b
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 120 deletions.
25 changes: 24 additions & 1 deletion blocks/api/post.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,33 @@ WP_Block_List
= WP_Block*

WP_Block
= WP_Block_Void
= WP_Tag_More
/ WP_Block_Void
/ WP_Block_Balanced
/ WP_Block_Html

WP_Tag_More
= "<!--" WS* "more" customText:(WS+ text:$((!(WS* "-->") .)+) { /** <?php return $text; ?> **/ return text })? WS* "-->" noTeaser:(WS* "<!--noteaser-->")?
{ /** <?php
return array(
'blockName' => 'wp:core/more',
'attrs' => array(
'customText' => $customText,
'noTeaser' => (bool) $noTeaser
),
'rawContent' => ''
);
?> **/
return {
blockName: 'wp:core/more',
attrs: {
customText: customText,
noTeaser: !! noTeaser
},
rawContent: ''
}
}

WP_Block_Void
= "<!--" WS+ "wp:" blockName:WP_Block_Name WS+ attrs:(a:WP_Block_Attributes WS+ {
/** <?php return $a; ?> **/
Expand Down
4 changes: 4 additions & 0 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export function serializeBlock( block ) {
const saveContent = getSaveContent( blockType, block.attributes );
const saveAttributes = getCommentAttributes( block.attributes, parseBlockAttributes( saveContent, blockType ) );

if ( 'wp:core/more' === blockName ) {
return `<!-- more ${ saveAttributes.customText ? `${ saveAttributes.customText } ` : '' }-->${ saveAttributes.noTeaser ? '\n<!--noteaser-->' : '' }`;
}

const serializedAttributes = ! isEmpty( saveAttributes )
? serializeAttributes( saveAttributes ) + ' '
: '';
Expand Down
Loading

0 comments on commit fdbfc9b

Please sign in to comment.