21
21
use League \CommonMark \Extension \Table \TableRow ;
22
22
use League \CommonMark \Extension \Table \TableSection ;
23
23
24
-
25
- class WP_Markdown_To_Blocks {
24
+ class WP_Markdown_To_Blocks implements WP_Block_Markup_Converter {
26
25
const STATE_READY = 'STATE_READY ' ;
27
26
const STATE_COMPLETE = 'STATE_COMPLETE ' ;
28
27
@@ -40,19 +39,26 @@ public function __construct( $markdown ) {
40
39
$ this ->markdown = $ markdown ;
41
40
}
42
41
43
- public function parse () {
42
+ public function convert () {
44
43
if ( self ::STATE_READY !== $ this ->state ) {
45
44
return false ;
46
45
}
47
46
$ this ->convert_markdown_to_blocks ();
48
- $ this ->block_markup = self ::convert_blocks_to_markup ( $ this ->parsed_blocks );
47
+ $ this ->block_markup = WP_Import_Utils ::convert_blocks_to_markup ( $ this ->parsed_blocks );
49
48
return true ;
50
49
}
51
50
52
- public function get_frontmatter () {
51
+ public function get_all_metadata () {
53
52
return $ this ->frontmatter ;
54
53
}
55
54
55
+ public function get_meta_value ( $ key ) {
56
+ if ( ! array_key_exists ( $ key , $ this ->frontmatter ) ) {
57
+ return null ;
58
+ }
59
+ return $ this ->frontmatter [ $ key ][0 ];
60
+ }
61
+
56
62
public function get_block_markup () {
57
63
return $ this ->block_markup ;
58
64
}
@@ -74,7 +80,11 @@ private function convert_markdown_to_blocks() {
74
80
$ parser = new MarkdownParser ( $ environment );
75
81
76
82
$ document = $ parser ->parse ( $ this ->markdown );
77
- $ this ->frontmatter = $ document ->data ;
83
+ $ this ->frontmatter = [];
84
+ foreach ( $ document ->data as $ key => $ value ) {
85
+ // Use an array as a value to comply with the WP_Block_Markup_Converter interface.
86
+ $ this ->frontmatter [ $ key ] = [$ value ];
87
+ }
78
88
79
89
$ walker = $ document ->walker ();
80
90
while ( true ) {
@@ -163,7 +173,7 @@ private function convert_markdown_to_blocks() {
163
173
'content ' => '<pre class="wp-block-code"><code> ' . trim ( str_replace ( "\n" , '<br> ' , htmlspecialchars ( $ node ->getLiteral () ) ) ) . '</code></pre> ' ,
164
174
)
165
175
);
166
- if ( $ node ->getInfo () ) {
176
+ if ( method_exists ( $ node , ' getInfo ' ) && $ node ->getInfo () ) {
167
177
$ this ->current_block ->attrs ['language ' ] = preg_replace ( '/[ \t\r\n\f].*/ ' , '' , $ node ->getInfo () );
168
178
}
169
179
break ;
@@ -339,35 +349,6 @@ private function convert_markdown_to_blocks() {
339
349
$ this ->parsed_blocks = $ this ->root_block ->inner_blocks ;
340
350
}
341
351
342
- private static function convert_blocks_to_markup ( $ blocks ) {
343
- $ block_markup = '' ;
344
-
345
- foreach ( $ blocks as $ block ) {
346
- // Start of block comment
347
- $ comment = '<!-- --> ' ;
348
- $ p = new WP_HTML_Tag_Processor ( $ comment );
349
- $ p ->next_token ();
350
- $ attrs = $ block ->attrs ;
351
- $ content = $ block ->attrs ['content ' ] ?? '' ;
352
- unset( $ attrs ['content ' ] );
353
- $ encoded_attrs = json_encode ( $ attrs );
354
- if ( $ encoded_attrs === '[] ' ) {
355
- $ encoded_attrs = '' ;
356
- }
357
- $ p ->set_modifiable_text ( " wp: {$ block ->block_name } " . $ encoded_attrs . ' ' );
358
- $ open_comment = $ p ->get_updated_html ();
359
-
360
- $ block_markup .= $ open_comment . "\n" ;
361
- $ block_markup .= $ content . "\n" ;
362
- $ block_markup .= self ::convert_blocks_to_markup ( $ block ->inner_blocks );
363
-
364
- // End of block comment
365
- $ block_markup .= "<!-- /wp: {$ block ->block_name } --> \n" ;
366
- }
367
-
368
- return $ block_markup ;
369
- }
370
-
371
352
private function append_content ( $ content ) {
372
353
if ( ! isset ( $ this ->current_block ->attrs ['content ' ] ) ) {
373
354
$ this ->current_block ->attrs ['content ' ] = '' ;
0 commit comments