Skip to content

Commit

Permalink
Render fenced block how it wants to be rendered (#9)
Browse files Browse the repository at this point in the history
The `FencedCode` class stores information about what delimiter character should be used, how many should be used, and what the indent offset is for the block.
  • Loading branch information
GuySartorelli committed Nov 12, 2023
1 parent 7efca2a commit ae2650c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/Renderer/Block/FencedCodeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): s
}

$content = $node->getLiteral();
$delimiter = str_repeat($node->getChar(), $node->getLength());
$offset = str_repeat(' ', $node->getOffset());

return <<<TXT
```{$language}
{$content}
```
{$offset}{$delimiter}{$language}
{$offset}{$content}
{$offset}{$delimiter}
TXT;
}
}
42 changes: 35 additions & 7 deletions tests/Renderer/Block/FencedCodeRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Node\Block\Document;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Wnx\CommonmarkMarkdownRenderer\Renderer\Block\FencedCodeRenderer;
Expand All @@ -21,11 +22,12 @@ protected function setUp(): void
}

#[Test]
public function it_renders_fenced_code(): void
#[DataProvider('provide_fenced_code')]
public function it_renders_fenced_code(array $fencedArgs, string $expected): void
{
$document = new Document();

$block = new FencedCode(3, '~', 0);
$block = new FencedCode(...$fencedArgs);
$block->setInfo('php');
$block->setLiteral('echo "hello world!";');
$block->data->set('attributes', ['id' => 'foo', 'class' => 'bar']);
Expand All @@ -36,10 +38,36 @@ public function it_renders_fenced_code(): void

$result = $this->renderer->render($block, $fakeRenderer);

$this->assertEquals(<<<TXT
```php
echo "hello world!";
```
TXT, $result);
$this->assertEquals($expected, $result);
}

public function provide_fenced_code(): array
{
return [
'tilde as char' => [
'fencedArgs' => [3, '~', 0],
'expected' => <<<TXT
~~~php
echo "hello world!";
~~~
TXT
],
'backtick as char' => [
'fencedArgs' => [3, '`', 0],
'expected' => <<<TXT
```php
echo "hello world!";
```
TXT
],
'offset and >3 chars' => [
'fencedArgs' => [5, '`', 2],
'expected' => <<<TXT
`````php
echo "hello world!";
`````
TXT
],
];
}
}

0 comments on commit ae2650c

Please sign in to comment.