diff --git a/composer.json b/composer.json index c719910..dc3ec5e 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,7 @@ "require-dev": { "cakephp/cakephp-codesniffer": "^4.0", "cakephp/debug_kit": "^4.0", + "michelf/php-markdown": "^1.9", "mikey179/vfsstream": "^1.6", "phpunit/phpunit": "^8.5" }, diff --git a/src/View/TwigView.php b/src/View/TwigView.php index c9a7771..67ee65a 100644 --- a/src/View/TwigView.php +++ b/src/View/TwigView.php @@ -35,6 +35,7 @@ use Twig\Environment; use Twig\Extension\DebugExtension; use Twig\Extension\StringLoaderExtension; +use Twig\Extra\Markdown\DefaultMarkdown; use Twig\Extra\Markdown\MarkdownExtension; use Twig\Extra\Markdown\MarkdownInterface; use Twig\Extra\Markdown\MarkdownRuntime; @@ -62,20 +63,18 @@ class TwigView extends View * * Use ViewBuilder::setOption()/setOptions() in your controller to set these options. * - * - `environment` - Array of config you would pass into \Twig\Environment to overwrite the default settings, - * see: http://twig.sensiolabs.org/doc/api.html#environment-options - * - `markdown` - Config for MarkdownExtension. Array must contain `engine` key which is - * an instance of Twig\Extra\Markdown\MarkdownInterface, - * see https://twig.symfony.com/doc/3.x/filters/markdown_to_html.html + * - `environment` - Array of config you would pass into \Twig\Environment to overwrite the default settings. + * See http://twig.sensiolabs.org/doc/api.html#environment-options. + * - `markdown` - Set to 'default' to use `DefaultMarkdown` or + * provide custom Twig\Extra\Markdown\MarkdownInterface instance. + * See https://twig.symfony.com/doc/3.x/filters/markdown_to_html.html. * * @var array */ protected $_defaultConfig = [ 'environment' => [ ], - 'markdown' => [ - 'engine' => null, - ], + 'markdown' => null, ]; /** @@ -228,11 +227,12 @@ protected function initializeExtensions(): void $twig->addExtension(new Extension\ViewExtension()); // Markdown extension - $markdownEngine = $this->getConfig('markdown.engine'); - if ($markdownEngine instanceof MarkdownInterface) { + $markdown = $this->getConfig('markdown'); + if ($markdown !== null) { $twig->addExtension(new MarkdownExtension()); - $twig->addRuntimeLoader(new class ($markdownEngine) implements RuntimeLoaderInterface { + $engine = $markdown === 'default' ? new DefaultMarkdown() : $markdown; + $twig->addRuntimeLoader(new class ($engine) implements RuntimeLoaderInterface { /** * @var \Twig\Extra\Markdown\MarkdownInterface */ diff --git a/tests/TestCase/View/TwigViewTest.php b/tests/TestCase/View/TwigViewTest.php index 540738f..d5f9a2c 100644 --- a/tests/TestCase/View/TwigViewTest.php +++ b/tests/TestCase/View/TwigViewTest.php @@ -22,6 +22,7 @@ use TestApp\View\AppView; use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; +use Twig\Extra\Markdown\DefaultMarkdown; /** * Class TwigViewTest. @@ -120,6 +121,38 @@ public function testCellsShareTwig() $this->assertSame($this->view->getTwig(), $cell->createView(AppView::class)->getTwig()); } + /** + * Tests rendering with markdown. + * + * @return void; + */ + public function testMarkdownExtensionDefault() + { + AppView::destroyTwig(); + + $view = new AppView(null, null, null, ['markdown' => 'default']); + $output = $this->view->render('markdown', false); + $this->assertSame("

Title

\n", $output); + + AppView::destroyTwig(); + } + + /** + * Tests rendering with markdown. + * + * @return void; + */ + public function testMarkdownExtensionCustom() + { + AppView::destroyTwig(); + + $view = new AppView(null, null, null, ['markdown' => new DefaultMarkdown()]); + $output = $this->view->render('markdown', false); + $this->assertSame("

Title

\n", $output); + + AppView::destroyTwig(); + } + /** * Tests deprecated element and cell tags render. * diff --git a/tests/test_app/src/View/AppView.php b/tests/test_app/src/View/AppView.php index 035616a..5cd907b 100644 --- a/tests/test_app/src/View/AppView.php +++ b/tests/test_app/src/View/AppView.php @@ -30,4 +30,15 @@ public function initialize(): void parent::initialize(); $this->loadHelper('TestSecond'); } + + /** + * Clear internal Twig instances for testing. + * + * @return void + */ + public static function destroyTwig(): void + { + static::$profile = null; + static::$twig = null; + } } diff --git a/tests/test_app/templates/markdown.twig b/tests/test_app/templates/markdown.twig new file mode 100644 index 0000000..6d7dc19 --- /dev/null +++ b/tests/test_app/templates/markdown.twig @@ -0,0 +1 @@ +{{ '# Title'|markdown_to_html }} \ No newline at end of file