Skip to content

Commit

Permalink
Add support for task lists (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
weph authored Feb 20, 2023
1 parent d0f3bc1 commit 2c901d0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/MarkdownRendererExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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('*'));
Expand Down
25 changes: 25 additions & 0 deletions src/Renderer/Inline/TaskListItemMarkerRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Wnx\CommonmarkMarkdownRenderer\Renderer\Inline;

use League\CommonMark\Extension\TaskList\TaskListItemMarker;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

class TaskListItemMarkerRenderer implements NodeRendererInterface
{
/**
* @param TaskListItemMarker $node
*
* {@inheritDoc}
*
* @psalm-suppress MoreSpecificImplementedParamType
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
return sprintf('[%s]', $node->isChecked() ? 'x' : ' ');
}
}
44 changes: 44 additions & 0 deletions tests/Renderer/Inline/TaskListItemMarkerRendererTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Wnx\CommonmarkMarkdownRenderer\Tests\Renderer\Inline;

use League\CommonMark\Extension\TaskList\TaskListItemMarker;
use PHPUnit\Framework\TestCase;
use Wnx\CommonmarkMarkdownRenderer\Renderer\Inline\TaskListItemMarkerRenderer;
use Wnx\CommonmarkMarkdownRenderer\Tests\Support\FakeChildNodeRenderer;

class TaskListItemMarkerRendererTest extends TestCase
{
private TaskListItemMarkerRenderer $renderer;

protected function setUp(): void
{
$this->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);
}
}
5 changes: 5 additions & 0 deletions tests/stubs/kitchen-sink.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c901d0

Please sign in to comment.