Skip to content

Commit f354926

Browse files
committed
ADD the empty converter type
Adds a new "empty" type to get a converter without the core extension. It is useful to use extensions that *replace* the core one (like InlinesOnly).
1 parent 29f7298 commit f354926

6 files changed

Lines changed: 45 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.2.0] - 2020-10-25
10+
### Added
11+
- New `empty` converter type
12+
913
## [1.1.1] - 2020-10-24
1014
### Fixed
1115
- the converter `options` configuration key is not ignored anymore
@@ -18,7 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1822
### Added
1923
- Bundle base
2024

21-
[Unreleased]: https://github.com/AymDev/CommonMarkBundle/compare/v1.1.1...HEAD
25+
[Unreleased]: https://github.com/AymDev/CommonMarkBundle/compare/v1.2.0...HEAD
26+
[1.2.0]: https://github.com/AymDev/CommonMarkBundle/compare/v1.1.1...v1.2.0
2227
[1.1.1]: https://github.com/AymDev/CommonMarkBundle/compare/v1.1.0...v1.1.1
2328
[1.1.0]: https://github.com/AymDev/CommonMarkBundle/compare/v1.0.0...v1.1.0
2429
[1.0.0]: https://github.com/AymDev/CommonMarkBundle/releases/tag/v1.0.0

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ The `type` key can be used to choose between a *CommonMark* or a *GitHub* conver
4949

5050
- `commonmark` (default): `CommonMarkConverter`
5151
- `github`: `GithubFlavoredMarkdownConverter`
52+
- `empty`: creates a `CommonMarkConverter` without the `CommonMarkCoreExtension` (e.g. to use `InlinesOnlyExtension`)
5253

5354
### Converter options
5455

src/DependencyInjection/Compiler/ConvertersPass.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Aymdev\CommonmarkBundle\DependencyInjection\Compiler;
44

55
use League\CommonMark\CommonMarkConverter;
6+
use League\CommonMark\Environment;
7+
use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension;
68
use Symfony\Component\DependencyInjection\ChildDefinition;
79
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
810
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -32,7 +34,11 @@ public function process(ContainerBuilder $container)
3234
private function registerConverters(array $converterConfig, ContainerBuilder $container): array
3335
{
3436
// Create environment definition
35-
$environment = new ChildDefinition('aymdev_commonmark.environment');
37+
if ($converterConfig['type'] === 'empty') {
38+
$environment = new Definition(Environment::class);
39+
} else {
40+
$environment = new ChildDefinition('aymdev_commonmark.environment');
41+
}
3642

3743
// Register and add extensions
3844
foreach ($converterConfig['extensions'] as $extensionName) {
@@ -52,6 +58,7 @@ private function registerConverters(array $converterConfig, ContainerBuilder $co
5258
$converterDefinition
5359
->addArgument($converterConfig['options'] ?? [])
5460
->addArgument(new Reference($environmentId))
61+
->setPublic(true)
5562
;
5663

5764
$converterId = 'aymdev_commonmark.converter.' . $converterConfig['name'];

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function getConfigTreeBuilder()
1818
->arrayPrototype()
1919
->children()
2020
->enumNode('type')
21-
->values(['commonmark', 'github'])
21+
->values(['commonmark', 'github', 'empty'])
2222
->defaultValue('commonmark')
2323
->end()
2424
->variableNode('options')->end()

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<!-- Converters -->
1616
<service id="aymdev_commonmark.converter.type.commonmark" class="League\CommonMark\CommonMarkConverter" />
1717
<service id="aymdev_commonmark.converter.type.github" class="League\CommonMark\GithubFlavoredMarkdownConverter" />
18+
<service id="aymdev_commonmark.converter.type.empty" alias="aymdev_commonmark.converter.type.commonmark" />
1819

1920
<!-- Twig extension -->
2021
<service id="aymdev_commonmark.twig_extension" class="Aymdev\CommonmarkBundle\Twig\CommonMarkExtension">

tests/DependencyInjection/Compiler/ConvertersPassTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Aymdev\CommonmarkBundle\DependencyInjection\Compiler\ConvertersPass;
88
use Aymdev\CommonmarkBundle\Twig\CommonMarkExtension;
99
use League\CommonMark\CommonMarkConverter;
10+
use League\CommonMark\Extension\InlinesOnly\InlinesOnlyExtension;
1011
use PHPUnit\Framework\TestCase;
1112
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1213
use Symfony\Bundle\TwigBundle\TwigBundle;
@@ -102,6 +103,33 @@ public function testTwigExtensionRegistration()
102103
$html = trim($html);
103104
self::assertSame($expectedOutput, $html);
104105
}
106+
107+
/**
108+
* Test empty environment
109+
* The InlinesOnlyExtension needs to be added to an empty environment
110+
* It must not be combined with the CommonMarkCoreExtension
111+
*/
112+
public function testEmptyEnvironmentSetup()
113+
{
114+
$kernel = new AymdevCommonmarkTestKernel([
115+
'converters' => [
116+
'my_converter' => [
117+
'type' => 'empty',
118+
'extensions' => [
119+
InlinesOnlyExtension::class,
120+
]
121+
],
122+
]
123+
]);
124+
$kernel->boot();
125+
$container = $kernel->getContainer();
126+
127+
/** @var CommonMarkConverter $converter */
128+
$converter = $container->get('aymdev_commonmark.converter.my_converter');
129+
130+
// converting works correctly
131+
self::assertSame('# test', trim($converter->convertToHtml('# test')));
132+
}
105133
}
106134

107135
class AymdevCommonmarkTestKernel extends Kernel

0 commit comments

Comments
 (0)