News: Render as markdown

This commit is contained in:
Igor Scheller 2020-10-21 01:08:03 +02:00 committed by msquare
parent a309d873a7
commit 620c9a02bb
4 changed files with 29 additions and 4 deletions

View File

@ -73,7 +73,7 @@
</div> </div>
<div class="panel-body"> <div class="panel-body">
{{ news.text|raw|nl2br }} {{ news.text|raw|markdown(false) }}
</div> </div>
</div> </div>
</div> </div>

View File

@ -61,7 +61,7 @@
{% endif %} {% endif %}
<div class="panel-body"> <div class="panel-body">
{{ news.text|raw|nl2br }} {{ news.text|raw|markdown(false) }}
</div> </div>
<div class="panel-footer text-muted"> <div class="panel-footer text-muted">

View File

@ -34,10 +34,16 @@ class Markdown extends TwigExtension
/** /**
* @param string $text * @param string $text
* @param bool $escapeHtml
*
* @return string * @return string
*/ */
public function render(string $text): string public function render(string $text, bool $escapeHtml = true): string
{ {
return $this->renderer->text(htmlspecialchars($text)); if ($escapeHtml) {
$text = htmlspecialchars($text);
}
return $this->renderer->text($text);
} }
} }

View File

@ -41,4 +41,23 @@ class MarkdownTest extends ExtensionTest
$extension = new Markdown($renderer); $extension = new Markdown($renderer);
$this->assertEquals($return, $extension->render('Lorem *"Ipsum"*')); $this->assertEquals($return, $extension->render('Lorem *"Ipsum"*'));
} }
/**
* @covers \Engelsystem\Renderer\Twig\Extensions\Markdown::render
*/
public function testRenderHtml()
{
/** @var Parsedown|MockObject $renderer */
$renderer = $this->createMock(Parsedown::class);
$input = '<i>**test**</i>';
$return = '<p><strong><i>**test**</i></strong></p>';
$renderer->expects($this->once())
->method('text')
->with($input)
->willReturn($return);
$extension = new Markdown($renderer);
$this->assertEquals($return, $extension->render($input, false));
}
} }