Skip to content

Commit

Permalink
Use serialize_blocks to store custom blocks in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Dec 30, 2024
1 parent 724e177 commit c07add9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,14 @@ private function block_to_markdown($block) {
return "\n---\n\n";

default:
$markdown = [];
if($inner_html){
$markdown[] = "```block";
$markdown[] = "<!-- {$block_name} -->";
$markdown[] = $inner_html;
$markdown[] = "<!-- /{$block_name} -->";
$markdown[] = "```";
} else {
$markdown[] = "<!-- {$block_name} /-->";
// Short-circuit empty entries produced by the block parser.
if(!$block_name) {
return '';
}
$markdown = [];
$markdown[] = "```block";
$markdown[] = serialize_block($block);
$markdown[] = "```";
return implode("\n", $markdown);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,13 @@ private function convert_markdown_to_blocks() {
if ( method_exists( $node, 'getInfo' ) && $node->getInfo() ) {
$attrs['language'] = preg_replace( '/[ \t\r\n\f].*/', '', $node->getInfo() );
}
$this->push_block( 'code', $attrs );
$this->append_content( '<pre class="wp-block-code"><code>' . trim( str_replace( "\n", '<br>', htmlspecialchars( $node->getLiteral() ) ) ) . '</code></pre>' );
if('block' === $attrs['language']) {
// This is a special case for preserving block literals that could not be expressed as markdown.
$this->append_content( "\n" . $node->getLiteral() . "\n" );
} else {
$this->push_block( 'code', $attrs );
$this->append_content( '<pre class="wp-block-code"><code>' . trim( str_replace( "\n", '<br>', htmlspecialchars( $node->getLiteral() ) ) ) . '</code></pre>' );
}
break;

case ExtensionBlock\HtmlBlock::class:
Expand Down

0 comments on commit c07add9

Please sign in to comment.