PHP code example of wp-php-toolkit / markdown

1. Go to this page and download the library: Download wp-php-toolkit/markdown library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wp-php-toolkit / markdown example snippets



WordPress\Markdown\MarkdownConsumer;

$result = ( new MarkdownConsumer( "# Hello\n\nWelcome to **WordPress**." ) )->consume();
echo $result->get_block_markup();


WordPress\Markdown\MarkdownConsumer;
use WordPress\Markdown\MarkdownProducer;

$md       = "## Round trip\n\n- one\n- two\n- three\n";
$blocks   = ( new MarkdownConsumer( $md ) )->consume();
$markdown = ( new MarkdownProducer( $blocks ) )->produce();

echo $markdown;


WordPress\Markdown\MarkdownSourceDocument;

$source = <<<MD
# Title #

Keep __bold__ syntax.

Edit this sentence.
MD;

$document = MarkdownSourceDocument::from_markdown( $source );
$blocks   = str_replace(
	'<p>Edit this sentence.</p>',
	'<p>Edit only this sentence.</p>',
	$document->get_block_markup()
);

echo $document->patch_markdown( $blocks );


WordPress\Markdown\MarkdownConsumer;

$md = <<<MD
---
post_title: "The Name of the Wind"
post_status: publish
tags: [fantasy, kingkiller]
---

Once upon a time...
MD;

$consumer = new MarkdownConsumer( $md );
$consumer->consume();

echo 'Title: '   . $consumer->get_meta_value( 'post_title' )  . "\n";
echo 'Status: '  . $consumer->get_meta_value( 'post_status' ) . "\n";
$metadata = $consumer->get_all_metadata();
echo 'Tags: ' . implode( ', ', $metadata['tags'][0] ) . "\n";


WordPress\Markdown\MarkdownConsumer;

@mkdir( '/tmp/vault', 0777, true );
file_put_contents( '/tmp/vault/welcome.md', "---\ntitle: Welcome\n---\n\nHello world." );
file_put_contents( '/tmp/vault/roadmap.md', "# Roadmap\n\n1. Ship\n2. Iterate" );

foreach ( glob( '/tmp/vault/*.md' ) as $path ) {
	$consumer = new MarkdownConsumer( file_get_contents( $path ) );
	$consumer->consume();
	$title = $consumer->get_meta_value( 'title' );
	if ( ! $title ) $title = basename( $path, '.md' );
	echo "=== $title ($path) ===\n";
	echo substr( $consumer->get_block_markup(), 0, 120 ) . "...\n\n";
}


WordPress\Markdown\MarkdownConsumer;

$md = <<<MD
# Title

A paragraph with **bold** and *italics*.

| Col A | Col B |
|-------|-------|
| 1     | 2     |


=== roadmap (/tmp/<tempfile>/roadmap.md) ===
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading" id="roadmap">Roadmap</h1>
<!-- /wp:heading -->

<!-- wp:lis...

=== Welcome (/tmp/<tempfile>/welcome.md) ===
<!-- wp:paragraph -->
<p>Hello world.</p>
<!-- /wp:paragraph -->

...


> A quote.
MD;

$blocks = ( new MarkdownConsumer( $md ) )->consume()->get_block_markup();
$counts = array();
$queue  = parse_blocks( $blocks );

while ( $queue ) {
	$block = array_shift( $queue );
	if ( null !== $block['blockName'] ) {
		$name             = $block['blockName'];
		$counts[ $name ] = isset( $counts[ $name ] ) ? $counts[ $name ] + 1 : 1;
	}
	foreach ( $block['innerBlocks'] as $inner_block ) {
		$queue[] = $inner_block;
	}
}
foreach ( $counts as $name => $count ) {
	echo "{$name}: {$count}\n";
}