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
// 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;
}
}