<?php


namespace App\Twig;


use League\CommonMark\CommonMarkConverter;
use League\CommonMark\DocParserInterface;
use League\CommonMark\HtmlRenderer;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class MarkdownTwigExtension extends AbstractExtension
{
    private $parser;
    private $renderer;

    public function __construct(DocParserInterface $parser, HtmlRenderer $renderer)
    {
        $this->parser = $parser;
        $this->renderer = $renderer;
    }

    public function getFilters()
    {
        return [
            new TwigFilter('markdown', function ($text) {
                $block = $this->parser->parse($text);
                return $this->renderer->renderBlock($block);
            }, ['is_safe' => ['html']])
        ];
    }
}
