Renderer: Added twig extension for markdown rendering

This commit is contained in:
Igor Scheller 2018-12-21 23:11:44 +01:00 committed by msquare
parent b3f059ad04
commit 393db49294
3 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace Engelsystem\Renderer\Twig\Extensions;
use Parsedown;
use Twig_Extension as TwigExtension;
use Twig_Filter as TwigFilter;
class Markdown extends TwigExtension
{
/** @var Parsedown */
protected $renderer;
/**
* @param Parsedown $renderer
*/
public function __construct(Parsedown $renderer)
{
$this->renderer = $renderer;
}
/**
* @return array
*/
public function getFilters(): array
{
$options = ['is_safe' => ['html']];
return [
new TwigFilter('markdown', [$this, 'render'], $options),
new TwigFilter('md', [$this, 'render'], $options),
];
}
/**
* @param string $text
* @return string
*/
public function render($text): string
{
return $this->renderer->text(htmlspecialchars($text));
}
}

View File

@ -10,6 +10,7 @@ use Engelsystem\Renderer\Twig\Extensions\Config;
use Engelsystem\Renderer\Twig\Extensions\Csrf; use Engelsystem\Renderer\Twig\Extensions\Csrf;
use Engelsystem\Renderer\Twig\Extensions\Globals; use Engelsystem\Renderer\Twig\Extensions\Globals;
use Engelsystem\Renderer\Twig\Extensions\Legacy; use Engelsystem\Renderer\Twig\Extensions\Legacy;
use Engelsystem\Renderer\Twig\Extensions\Markdown;
use Engelsystem\Renderer\Twig\Extensions\Session; use Engelsystem\Renderer\Twig\Extensions\Session;
use Engelsystem\Renderer\Twig\Extensions\Translation; use Engelsystem\Renderer\Twig\Extensions\Translation;
use Engelsystem\Renderer\Twig\Extensions\Url; use Engelsystem\Renderer\Twig\Extensions\Url;
@ -28,6 +29,7 @@ class TwigServiceProvider extends ServiceProvider
'globals' => Globals::class, 'globals' => Globals::class,
'session' => Session::class, 'session' => Session::class,
'legacy' => Legacy::class, 'legacy' => Legacy::class,
'markdown' => Markdown::class,
'translation' => Translation::class, 'translation' => Translation::class,
'url' => Url::class, 'url' => Url::class,
]; ];

View File

@ -0,0 +1,44 @@
<?php
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
use Engelsystem\Renderer\Twig\Extensions\Markdown;
use Parsedown;
use PHPUnit\Framework\MockObject\MockObject;
class MarkdownTest extends ExtensionTest
{
/**
* @covers \Engelsystem\Renderer\Twig\Extensions\Markdown::getFilters
*/
public function testGeFilters()
{
/** @var Parsedown|MockObject $renderer */
$renderer = $this->createMock(Parsedown::class);
$extension = new Markdown($renderer);
$filters = $extension->getFilters();
$this->assertExtensionExists('markdown', [$extension, 'render'], $filters);
$this->assertExtensionExists('md', [$extension, 'render'], $filters);
}
/**
* @covers \Engelsystem\Renderer\Twig\Extensions\Markdown::__construct
* @covers \Engelsystem\Renderer\Twig\Extensions\Markdown::render
*/
public function testRender()
{
/** @var Parsedown|MockObject $renderer */
$renderer = $this->createMock(Parsedown::class);
$return = '<p>Lorem <em>&quot;Ipsum&quot;</em></p>';
$renderer->expects($this->once())
->method('text')
->with('Lorem *&quot;Ipsum&quot;*')
->willReturn($return);
$extension = new Markdown($renderer);
$this->assertEquals($return, $extension->render('Lorem *"Ipsum"*'));
}
}