Skip to content

Respect HTML attributes from AttributesExtension on the <pre> block #6

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

## 0.5.6 - 2022-07-25

### Fixed

- Respect HTML attributes from AttributesExtension on the `<pre>` block

## 0.5.5 - 2022-02-23

### Changed
Expand Down
12 changes: 9 additions & 3 deletions src/BaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\Str;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Node\Block\AbstractBlock;
use League\CommonMark\Util\HtmlElement;
use League\CommonMark\Util\Xml;
use Torchlight\Block;
use Torchlight\Torchlight;
Expand Down Expand Up @@ -65,7 +67,7 @@ public function useCustomBlockRenderer($callback)
*/
public function defaultBlockRenderer()
{
return function (Block $block) {
return function (Block $block, AbstractBlock $node) {
$inner = '';

// Clones come from multiple themes.
Expand All @@ -76,7 +78,11 @@ public function defaultBlockRenderer()
$inner .= "<code {$block->attrsAsString()}class='{$block->classes}' style='{$block->styles}'>{$block->highlighted}</code>";
}

return "<pre>$inner</pre>";
return new HtmlElement(
'pre',
$node->data->getData('attributes')->export(),
$inner
);
};
}

Expand Down Expand Up @@ -143,7 +149,7 @@ protected function renderNode($node)
if (array_key_exists($hash, static::$torchlightBlocks)) {
$renderer = $this->customBlockRenderer ?? $this->defaultBlockRenderer();

return call_user_func($renderer, static::$torchlightBlocks[$hash]);
return call_user_func($renderer, static::$torchlightBlocks[$hash], $node);
}
}

Expand Down
41 changes: 41 additions & 0 deletions tests/BaseRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,47 @@ public function it_can_set_a_custom_renderer()
$expected = <<<EOT
foo_bar

EOT;

$this->assertEquals($expected, $html);
}

/** @test */
public function sets_attributes_on_pre()
{
$markdown = <<<'EOT'
before

{.large}
```html
<div>html</div>
```
after
EOT;

$response = [
'blocks' => [[
'id' => 'block_id_1',
'classes' => 'torchlight',
'styles' => 'color: red;',
'attrs' => [
'data-lang' => 'lang'
],
'highlighted' => 'highlighted',
]]
];

Http::fake([
'api.torchlight.dev/*' => Http::response($response, 200),
]);

$html = $this->render($markdown);

$expected = <<<EOT
<p>before</p>
<pre class="large"><code data-lang="lang" class='torchlight' style='color: red;'>highlighted</code></pre>
<p>after</p>

EOT;

$this->assertEquals($expected, $html);
Expand Down
2 changes: 2 additions & 0 deletions tests/V1/CodeRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use League\CommonMark\DocParser;
use League\CommonMark\Environment;
use League\CommonMark\Extension\Attributes\AttributesExtension;
use League\CommonMark\HtmlRenderer;
use Torchlight\Commonmark\Tests\BaseRendererTest;

Expand All @@ -22,6 +23,7 @@ protected function extension()
protected function render($markdown, $extension = null)
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new AttributesExtension);
$environment->addExtension($extension ?? $this->extension());

$parser = new DocParser($environment);
Expand Down
2 changes: 2 additions & 0 deletions tests/V2/CodeRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Torchlight\Commonmark\Test\V2;

use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\Attributes\AttributesExtension;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Parser\MarkdownParser;
use League\CommonMark\Renderer\HtmlRenderer;
Expand All @@ -24,6 +25,7 @@ protected function render($markdown, $extension = null)
{
$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension);
$environment->addExtension(new AttributesExtension);
$environment->addExtension($extension ?? $this->extension());

$parser = new MarkdownParser($environment);
Expand Down