Skip to content

Commit

Permalink
Added patterns to protect non-Markdown blocks
Browse files Browse the repository at this point in the history
Added patterns that will attempt to preserve all  Gutenberg non-Markdown blocks from the Markdown parser.
  • Loading branch information
nuzzio committed Apr 29, 2018
1 parent ca238fc commit a1c6611
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions modules/markdown/easy-markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function maybe_load_actions_and_filters( $new_blog_id = null, $old_blog_i
* @return null
*/
public function load_markdown_for_posts() {
add_filter( 'jetpack_markdown_preserve_pattern', array( $this, 'jetpack_markdown_preserve' ), 10, 2 );
add_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2 );
add_action( 'after_wp_tiny_mce', array( $this, 'after_wp_tiny_mce' ) );
add_action( 'wp_insert_post', array( $this, 'wp_insert_post' ) );
Expand All @@ -135,6 +136,7 @@ public function load_markdown_for_posts() {
* @return null
*/
public function unload_markdown_for_posts() {
remove_filter( 'jetpack_markdown_preserve_pattern', array( $this, 'jetpack_markdown_preserve' ), 10, 2 );
remove_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ) );
remove_action( 'after_wp_tiny_mce', array( $this, 'after_wp_tiny_mce' ) );
remove_action( 'wp_insert_post', array( $this, 'wp_insert_post' ) );
Expand Down Expand Up @@ -558,6 +560,33 @@ protected function comment_hash( $content ) {
return 'c-' . substr( md5( $content ), 0, 8 );
}

/**
* Define custom patterns that won't be processed by Markdown.
*
* @return array The patterns that will be ignored by Markdown.
*/
public function jetpack_markdown_preserve( ) {

$regex_patterns = [];
/**
* Preserve all markdown block comments
* This will also hide the markdown blocks from the next pattern
*/
$regex_patterns[] = '{
(<!--\s*[/]{0,1}wp:jetpack\/markdown-block\s*-->)
}xsm';

/**
* Preserve all Gutenberg blocks and their contents
* This will prevent unnecessary munging of non markdown content
*/
$regex_patterns[] = '{
(<!--\s*wp:.+?-->.*?<!--\s*\/wp:.+?-->)
}xsm';

return $regex_patterns;
}

/**
* Markdown conversion. Some DRYness for repetitive tasks.
* @param string $text Content to be run through Markdown
Expand Down

0 comments on commit a1c6611

Please sign in to comment.