diff --git a/src/MarkdownRendererExtension.php b/src/MarkdownRendererExtension.php index a365e94..fc721fc 100644 --- a/src/MarkdownRendererExtension.php +++ b/src/MarkdownRendererExtension.php @@ -37,6 +37,8 @@ use League\CommonMark\Extension\CommonMark\Parser\Inline\OpenBracketParser; use League\CommonMark\Extension\ConfigurableExtensionInterface; use League\CommonMark\Extension\ExtensionInterface; +use League\CommonMark\Extension\TaskList\TaskListItemMarker; +use League\CommonMark\Extension\TaskList\TaskListItemMarkerParser; use League\CommonMark\Node\Block\Document; use League\CommonMark\Node\Block\Paragraph; use League\CommonMark\Node\Inline\Newline; @@ -59,6 +61,7 @@ use Wnx\CommonmarkMarkdownRenderer\Renderer\Inline\LinkRenderer; use Wnx\CommonmarkMarkdownRenderer\Renderer\Inline\NewlineRenderer; use Wnx\CommonmarkMarkdownRenderer\Renderer\Inline\StrongRenderer; +use Wnx\CommonmarkMarkdownRenderer\Renderer\Inline\TaskListItemMarkerRenderer; use Wnx\CommonmarkMarkdownRenderer\Renderer\Inline\TextRenderer; final class MarkdownRendererExtension implements ExtensionInterface, ConfigurableExtensionInterface @@ -93,6 +96,7 @@ public function register(EnvironmentBuilderInterface $environment): void $environment->addInlineParser(new CloseBracketParser(), 30); $environment->addInlineParser(new OpenBracketParser(), 20); $environment->addInlineParser(new BangParser(), 10); + $environment->addInlineParser(new TaskListItemMarkerParser(), 35); $environment->addRenderer(BlockQuote::class, new BlockQuoteRenderer()); @@ -114,6 +118,7 @@ public function register(EnvironmentBuilderInterface $environment): void $environment->addRenderer(Newline::class, new NewlineRenderer()); $environment->addRenderer(Strong::class, new StrongRenderer()); $environment->addRenderer(Text::class, new TextRenderer()); + $environment->addRenderer(TaskListItemMarker::class, new TaskListItemMarkerRenderer()); if ($environment->getConfiguration()->get('commonmark/use_asterisk')) { $environment->addDelimiterProcessor(new EmphasisDelimiterProcessor('*')); diff --git a/src/Renderer/Inline/TaskListItemMarkerRenderer.php b/src/Renderer/Inline/TaskListItemMarkerRenderer.php new file mode 100644 index 0000000..2ef9030 --- /dev/null +++ b/src/Renderer/Inline/TaskListItemMarkerRenderer.php @@ -0,0 +1,25 @@ +isChecked() ? 'x' : ' '); + } +} diff --git a/tests/Renderer/Inline/TaskListItemMarkerRendererTest.php b/tests/Renderer/Inline/TaskListItemMarkerRendererTest.php new file mode 100644 index 0000000..d0ed025 --- /dev/null +++ b/tests/Renderer/Inline/TaskListItemMarkerRendererTest.php @@ -0,0 +1,44 @@ +renderer = new TaskListItemMarkerRenderer(); + } + + /** @test */ + public function it_renders_unchecked_task_list_item() + { + $node = new TaskListItemMarker(false); + $fakeRenderer = new FakeChildNodeRenderer(); + + $result = $this->renderer->render($node, $fakeRenderer); + + $this->assertIsString($result); + $this->assertEquals("[ ]", $result); + } + + /** @test */ + public function it_renders_checked_task_list_item() + { + $node = new TaskListItemMarker(true); + $fakeRenderer = new FakeChildNodeRenderer(); + + $result = $this->renderer->render($node, $fakeRenderer); + + $this->assertIsString($result); + $this->assertEquals("[x]", $result); + } +} diff --git a/tests/stubs/kitchen-sink.md b/tests/stubs/kitchen-sink.md index e21dcbf..2ec39c7 100644 --- a/tests/stubs/kitchen-sink.md +++ b/tests/stubs/kitchen-sink.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Task List + +- [ ] Task 1 +- [x] Task 2 + ## [Unreleased](https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...HEAD) ## [1.0.0](https://github.com/olivierlacan/keep-a-changelog/compare/v0.3.0...v1.0.0) - 2017-06-20