PHP code example of vlotysh / markdown-parser

1. Go to this page and download the library: Download vlotysh/markdown-parser 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/ */

    

vlotysh / markdown-parser example snippets




use Vlotysh\MarkdownParser\MarkdownParser;

$parser = new MarkdownParser();
$html = $parser->parse('# Hello World');

echo $html; // <h1>Hello World</h1>


echo "Hello World";

$parser = new MarkdownParser([
    'breaks' => false,    // Line breaks as <br>
    'gfm' => true,        // GitHub Flavored Markdown
    'tables' => true,     // Table support
    'sanitize' => true,   // HTML sanitization
]);

use Vlotysh\MarkdownParser\MarkdownParser;

$parser = new MarkdownParser();

$markdown = '
# My Blog

This is an **important** post about *development*.

## Code


$parser = new MarkdownParser([
    'breaks' => true,
    'sanitize' => false
]);

$html = $parser->parse($markdown);

// In service provider
$this->app->singleton(MarkdownParser::class, function () {
    return new MarkdownParser([
        'gfm' => true,
        'tables' => true
    ]);
});

// In controller
public function show(MarkdownParser $parser, Post $post)
{
    $html = $parser->parse($post->content);
    return view('post.show', compact('html'));
}

// services.yaml
services:
    Vlotysh\MarkdownParser\MarkdownParser:
        arguments:
            $options:
                gfm: true
                tables: true

// In controller
public function show(MarkdownParser $parser, Post $post): Response
{
    $html = $parser->parse($post->getContent());
    return $this->render('post/show.html.twig', ['html' => $html]);
}

$parser = new MarkdownParser(['sanitize' => false]);

use Vlotysh\MarkdownParser\Parsers\BlockParserInterface;

class CustomParser implements BlockParserInterface
{
    public function parse(string $line, array $lines, int $index): ?array
    {
        if (preg_match('/^custom: (.+)$/', $line, $matches)) {
            return [
                'block' => [
                    'type' => 'custom',
                    'content' => $matches[1]
                ],
                'nextIndex' => $index
            ];
        }
        
        return null;
    }
}
`markdown


    // Or with 4 spaces
    echo "Hello World";


## List

- PHP
- JavaScript
- Python
  ';

echo $parser->parse($markdown);

src/
├── MarkdownParser.php          # Main class
├── Parsers/
│   ├── ParserInterface.php     # Interfaces
│   ├── HeaderParser.php        # Header parser
│   ├── CodeBlockParser.php     # Code block parser
│   ├── ListParser.php          # List parser
│   └── TableParser.php         # Table parser
tests/
├── MarkdownParserTest.php      # Main tests
example/
├── usage.php                   # Usage example