Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Strong and Emphasis Renderers to use settings available in AST Node #11

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Renderer/Inline/EmphasisRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): s
return "*{$content}*";
}

return "_{$content}_";
if ($this->config->get('commonmark/use_underscore')) {
return "_{$content}_";
}

$openingDelimiter = $node->getOpeningDelimiter();
$closingDelimiter = $node->getClosingDelimiter();

return "$openingDelimiter{$content}$closingDelimiter";
}
}
9 changes: 8 additions & 1 deletion src/Renderer/Inline/StrongRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): s
return "**{$content}**";
}

return "__{$content}__";
if ($this->config->get('commonmark/use_underscore')) {
return "__{$content}__";
}

$openingDelimiter = $node->getOpeningDelimiter();
$closingDelimiter = $node->getClosingDelimiter();

return "$openingDelimiter{$content}$closingDelimiter";
}
}
41 changes: 40 additions & 1 deletion tests/Renderer/Inline/EmphasisRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void
}

#[Test]
public function it_renders_emphasis(): void
public function it_renders_emphasis_using_asterisk(): void
{
$block = new Emphasis();
$fakeRenderer = new FakeChildNodeRenderer();
Expand All @@ -36,6 +36,45 @@ public function it_renders_emphasis(): void
$this->assertEquals('*::children::*', $result);
}

#[Test]
public function it_renders_emphasis_using_underscore(): void
{
$block = new Emphasis('_');
$fakeRenderer = new FakeChildNodeRenderer();
$fakeRenderer->pretendChildrenExist();

$this->renderer->setConfiguration($this->createConfiguration([
'commonmark' => [
'use_asterisk' => false,
],
]));

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

$this->assertIsString($result);
$this->assertEquals('_::children::_', $result);
}

#[Test]
public function it_renders_emphasis_with_whatever_delimiter_used(): void
{
$block = new Emphasis('$');
$fakeRenderer = new FakeChildNodeRenderer();
$fakeRenderer->pretendChildrenExist();

$this->renderer->setConfiguration($this->createConfiguration([
'commonmark' => [
'use_asterisk' => false,
'use_underscore' => false,
],
]));

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

$this->assertIsString($result);
$this->assertEquals('$::children::$', $result);
}

/**
* @param array<string, mixed> $values
*/
Expand Down
41 changes: 40 additions & 1 deletion tests/Renderer/Inline/StrongRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function setUp(): void
}

#[Test]
public function it_renders_strong(): void
public function it_renders_strong_with_asterisks(): void
{
$block = new Strong();
$fakeRenderer = new FakeChildNodeRenderer();
Expand All @@ -36,6 +36,45 @@ public function it_renders_strong(): void
$this->assertEquals('**::children::**', $result);
}

#[Test]
public function it_renders_strong_using_underscores_if_use_asterik_is_disabled(): void
{
$block = new Strong('__');
$fakeRenderer = new FakeChildNodeRenderer();
$fakeRenderer->pretendChildrenExist();

$this->renderer->setConfiguration($this->createConfiguration([
'commonmark' => [
'use_asterisk' => false,
],
]));

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

$this->assertIsString($result);
$this->assertEquals('__::children::__', $result);
}

#[Test]
public function it_renders_strong_using_whatever_delimiter_used_in_the_original(): void
{
$block = new Strong('$$');
$fakeRenderer = new FakeChildNodeRenderer();
$fakeRenderer->pretendChildrenExist();

$this->renderer->setConfiguration($this->createConfiguration([
'commonmark' => [
'use_asterisk' => false,
'use_underscore' => false,
],
]));

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

$this->assertIsString($result);
$this->assertEquals('$$::children::$$', $result);
}

/**
* @param array<string, mixed> $values
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/stubs/kitchen-sink-expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
*This Text is Italic*

`\Class->getFoo()`

- List Item A with `inline code`
- List Item B
3 changes: 3 additions & 0 deletions tests/stubs/kitchen-sink.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
*This Text is Italic*

`\Class->getFoo()`

- List Item A with `inline code`
- List Item B
Loading