Last active
February 6, 2017 14:54
-
-
Save MetalArend/23a33b97119a4c7c1526f3e742b81bf6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use League\CommonMark\Block\Element\AbstractBlock; | |
use League\CommonMark\Block\Element\Paragraph; | |
use League\CommonMark\Block\Renderer\BlockRendererInterface; | |
use League\CommonMark\CommonMarkConverter; | |
use League\CommonMark\ElementRendererInterface; | |
use League\CommonMark\Environment; | |
use Twig_Extension; | |
class CommonMarkExtension extends Twig_Extension implements BlockRendererInterface | |
{ | |
private $converter; | |
public function getFilters() | |
{ | |
return [ | |
new \Twig_SimpleFilter('markdown', [$this, 'markdown'], ['is_safe' => ['html']]), | |
]; | |
} | |
public function markdown($value, $inline = false) | |
{ | |
if (null === $this->converter) { | |
$environment = Environment::createCommonMarkEnvironment(); | |
$environment->addBlockRenderer(Paragraph::class, $this); | |
$this->converter = new CommonMarkConverter(['html_input' => 'strip'], $environment); | |
} | |
return $this->converter->convertToHtml($value); | |
} | |
public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) | |
{ | |
return $htmlRenderer->renderInlines($block->children()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment