diff --git a/doc/filters/markdown_to_html.rst b/doc/filters/markdown_to_html.rst index 3903746e8..8e3fede00 100644 --- a/doc/filters/markdown_to_html.rst +++ b/doc/filters/markdown_to_html.rst @@ -29,7 +29,7 @@ You can also use the filter on an included file or a variable: .. code-block:: twig {{ include('some_template.markdown.twig')|markdown_to_html }} - + {{ changelog|markdown_to_html }} .. note:: @@ -67,6 +67,13 @@ You can also use the filter on an included file or a variable: } } }); - + Afterwards you need to install a markdown library of your choice. Some of them are mentioned in the ``require-dev`` section of the ``twig/markdown-extra`` package. + +.. note:: + + If using Symfony (full-stack), ``twig/extra-bundle`` with ``league/commonmark`` as + your Markdown library you can configure CommonMark extensions. Register the desired + extension(s) as a service, then tag the service with + ``twig.markdown.league_extension``. diff --git a/extra/markdown-extra/composer.json b/extra/markdown-extra/composer.json index fd66512e5..3001b056a 100644 --- a/extra/markdown-extra/composer.json +++ b/extra/markdown-extra/composer.json @@ -21,7 +21,7 @@ "require-dev": { "symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0", "erusev/parsedown": "^1.7", - "league/commonmark": "^1.0", + "league/commonmark": "^1.0|^2.0", "league/html-to-markdown": "^4.8|^5.0", "michelf/php-markdown": "^1.8" }, diff --git a/extra/twig-extra-bundle/DependencyInjection/TwigExtraExtension.php b/extra/twig-extra-bundle/DependencyInjection/TwigExtraExtension.php index b77b2e412..c822fe99a 100644 --- a/extra/twig-extra-bundle/DependencyInjection/TwigExtraExtension.php +++ b/extra/twig-extra-bundle/DependencyInjection/TwigExtraExtension.php @@ -11,6 +11,7 @@ namespace Twig\Extra\TwigExtraBundle\DependencyInjection; +use League\CommonMark\CommonMarkConverter; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; @@ -37,5 +38,9 @@ class TwigExtraExtension extends Extension $loader->load($extension.'.php'); } } + + if (\class_exists(CommonMarkConverter::class)) { + $loader->load('markdown_league.php'); + } } } diff --git a/extra/twig-extra-bundle/LeagueCommonMarkConverterFactory.php b/extra/twig-extra-bundle/LeagueCommonMarkConverterFactory.php new file mode 100644 index 000000000..a2b90a25f --- /dev/null +++ b/extra/twig-extra-bundle/LeagueCommonMarkConverterFactory.php @@ -0,0 +1,42 @@ +extensions = $extensions; + } + + public function __invoke(): CommonMarkConverter + { + $converter = new CommonMarkConverter(); + + foreach ($this->extensions as $extension) { + $converter->getEnvironment()->addExtension($extension); + } + + return $converter; + } +} diff --git a/extra/twig-extra-bundle/Resources/config/markdown_league.php b/extra/twig-extra-bundle/Resources/config/markdown_league.php new file mode 100644 index 000000000..e4af03ae0 --- /dev/null +++ b/extra/twig-extra-bundle/Resources/config/markdown_league.php @@ -0,0 +1,31 @@ +services() + ->set('twig.markdown.league_common_mark_converter_factory', LeagueCommonMarkConverterFactory::class) + ->args([new TaggedIteratorArgument('twig.markdown.league_extension')]) + + ->set('twig.markdown.league_common_mark_converter', CommonMarkConverter::class) + ->factory([$service('twig.markdown.league_common_mark_converter_factory')]) + + ->set('twig.markdown.default', LeagueMarkdown::class) + ->args([$service('twig.markdown.league_common_mark_converter')]) + ; +}; diff --git a/extra/twig-extra-bundle/Tests/DependencyInjection/TwigExtraExtensionTest.php b/extra/twig-extra-bundle/Tests/DependencyInjection/TwigExtraExtensionTest.php index 852162dfa..05174582c 100644 --- a/extra/twig-extra-bundle/Tests/DependencyInjection/TwigExtraExtensionTest.php +++ b/extra/twig-extra-bundle/Tests/DependencyInjection/TwigExtraExtensionTest.php @@ -14,6 +14,7 @@ namespace Twig\Extra\TwigExtraBundle\Tests\DependencyInjection; use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; +use Twig\Extra\Markdown\LeagueMarkdown; use Twig\Extra\TwigExtraBundle\DependencyInjection\TwigExtraExtension; use Twig\Extra\TwigExtraBundle\Extensions; @@ -34,5 +35,7 @@ class TwigExtraExtensionTest extends TestCase foreach (Extensions::getClasses() as $name => $class) { $this->assertEquals($class, $container->getDefinition('twig.extension.'.$name)->getClass()); } + + $this->assertSame(LeagueMarkdown::class, $container->getDefinition('twig.markdown.default')->getClass()); } } diff --git a/extra/twig-extra-bundle/composer.json b/extra/twig-extra-bundle/composer.json index ec2f25b6e..643ff7755 100644 --- a/extra/twig-extra-bundle/composer.json +++ b/extra/twig-extra-bundle/composer.json @@ -21,6 +21,7 @@ "twig/twig": "^2.4|^3.0" }, "require-dev": { + "league/commonmark": "^1.0|^2.0", "symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0", "twig/cache-extra": "^3.0", "twig/cssinliner-extra": "^2.12|^3.0",