summaryrefslogtreecommitdiff
path: root/src/Renderer
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-12-21 23:11:44 +0100
committermsquare <msquare@notrademark.de>2018-12-22 12:01:23 +0100
commit393db492948a16f246790b88a38a1235cb167659 (patch)
tree2c9c837333c04dd06964ea1f0a13679b1b246c31 /src/Renderer
parentb3f059ad04fc0f54282aea98c5c2af8d7e46867a (diff)
Renderer: Added twig extension for markdown rendering
Diffstat (limited to 'src/Renderer')
-rw-r--r--src/Renderer/Twig/Extensions/Markdown.php43
-rw-r--r--src/Renderer/TwigServiceProvider.php2
2 files changed, 45 insertions, 0 deletions
diff --git a/src/Renderer/Twig/Extensions/Markdown.php b/src/Renderer/Twig/Extensions/Markdown.php
new file mode 100644
index 00000000..293efe91
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Markdown.php
@@ -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));
+ }
+}
diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php
index d7b6bd09..6cd885fd 100644
--- a/src/Renderer/TwigServiceProvider.php
+++ b/src/Renderer/TwigServiceProvider.php
@@ -10,6 +10,7 @@ use Engelsystem\Renderer\Twig\Extensions\Config;
use Engelsystem\Renderer\Twig\Extensions\Csrf;
use Engelsystem\Renderer\Twig\Extensions\Globals;
use Engelsystem\Renderer\Twig\Extensions\Legacy;
+use Engelsystem\Renderer\Twig\Extensions\Markdown;
use Engelsystem\Renderer\Twig\Extensions\Session;
use Engelsystem\Renderer\Twig\Extensions\Translation;
use Engelsystem\Renderer\Twig\Extensions\Url;
@@ -28,6 +29,7 @@ class TwigServiceProvider extends ServiceProvider
'globals' => Globals::class,
'session' => Session::class,
'legacy' => Legacy::class,
+ 'markdown' => Markdown::class,
'translation' => Translation::class,
'url' => Url::class,
];