Skip to content

Commit

Permalink
Merge a24cc02 into c65e158
Browse files Browse the repository at this point in the history
  • Loading branch information
othercorey committed Apr 11, 2020
2 parents c65e158 + a24cc02 commit 163acf7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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"
},
Expand Down
22 changes: 11 additions & 11 deletions src/View/TwigView.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
];

/**
Expand Down Expand Up @@ -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
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/TestCase/View/TwigViewTest.php
Expand Up @@ -22,6 +22,7 @@
use TestApp\View\AppView;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Extra\Markdown\DefaultMarkdown;

/**
* Class TwigViewTest.
Expand Down Expand Up @@ -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("<h1>Title</h1>\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("<h1>Title</h1>\n", $output);

AppView::destroyTwig();
}

/**
* Tests deprecated element and cell tags render.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/test_app/src/View/AppView.php
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions tests/test_app/templates/markdown.twig
@@ -0,0 +1 @@
{{ '# Title'|markdown_to_html }}

0 comments on commit 163acf7

Please sign in to comment.