diff --git a/CHANGELOG.md b/CHANGELOG.md index 396c489..2541914 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## Unreleased +## 0.5.6 - 2022-07-25 + +### Fixed + +- Respect HTML attributes from AttributesExtension on the `
` block
+
 ## 0.5.5 - 2022-02-23
 
 ### Changed
diff --git a/src/BaseExtension.php b/src/BaseExtension.php
index 085fd7c..300e938 100755
--- a/src/BaseExtension.php
+++ b/src/BaseExtension.php
@@ -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;
@@ -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.
@@ -76,7 +78,11 @@ public function defaultBlockRenderer()
                 $inner .= "attrsAsString()}class='{$block->classes}' style='{$block->styles}'>{$block->highlighted}";
             }
 
-            return "
$inner
"; + return new HtmlElement( + 'pre', + $node->data->getData('attributes')->export(), + $inner + ); }; } @@ -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); } } diff --git a/tests/BaseRendererTest.php b/tests/BaseRendererTest.php index 0824e32..0c47248 100644 --- a/tests/BaseRendererTest.php +++ b/tests/BaseRendererTest.php @@ -374,6 +374,47 @@ public function it_can_set_a_custom_renderer() $expected = <<assertEquals($expected, $html); + } + + /** @test */ + public function sets_attributes_on_pre() + { + $markdown = <<<'EOT' +before + +{.large} +```html +
html
+``` +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 = <<before

+
highlighted
+

after

+ EOT; $this->assertEquals($expected, $html); diff --git a/tests/V1/CodeRendererTest.php b/tests/V1/CodeRendererTest.php index 1e396ae..1215655 100644 --- a/tests/V1/CodeRendererTest.php +++ b/tests/V1/CodeRendererTest.php @@ -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; @@ -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); diff --git a/tests/V2/CodeRendererTest.php b/tests/V2/CodeRendererTest.php index 0038d9b..58e93f2 100644 --- a/tests/V2/CodeRendererTest.php +++ b/tests/V2/CodeRendererTest.php @@ -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; @@ -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);