Skip to content

Commit da008ee

Browse files
committed
Use render function
1 parent 663a202 commit da008ee

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.4.1 - 2021-08-02
4+
5+
### Changed
6+
- Use a render function instead of the deprecated `wrapped` response from the API.
7+
38
## 0.4.0 - 2021-07-31
49

510
### Changed

src/TorchlightExtension.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ public function render(AbstractBlock $block, ElementRendererInterface $htmlRende
6868
$hash = $this->makeTorchlightBlock($block)->hash();
6969

7070
if (array_key_exists($hash, static::$torchlightBlocks)) {
71-
if ($this->customBlockRenderer) {
72-
return call_user_func($this->customBlockRenderer, static::$torchlightBlocks[$hash]);
73-
}
71+
$renderer = $this->customBlockRenderer ?? $this->defaultBlockRenderer();
7472

75-
return static::$torchlightBlocks[$hash]->wrapped;
73+
return call_user_func($renderer, static::$torchlightBlocks[$hash]);
7674
}
7775
}
7876

@@ -83,6 +81,13 @@ public function useCustomBlockRenderer($callback)
8381
return $this;
8482
}
8583

84+
public function defaultBlockRenderer()
85+
{
86+
return function (Block $block) {
87+
return "<pre><code class='{$block->classes}' style='{$block->styles}'>{$block->highlighted}</code></pre>";
88+
};
89+
}
90+
8691
protected function makeTorchlightBlock($node)
8792
{
8893
return Block::make()

tests/CodeRendererTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function it_highlights_code_blocks()
5555
$response = [
5656
'blocks' => [[
5757
'id' => 'block_id_1',
58-
'wrapped' => '<pre><code>highlighted</code></pre>',
58+
'classes' => 'torchlight',
59+
'styles' => 'color: red;',
60+
'highlighted' => 'highlighted',
5961
]]
6062
];
6163

@@ -67,7 +69,7 @@ public function it_highlights_code_blocks()
6769

6870
$expected = <<<EOT
6971
<p>before</p>
70-
<pre><code>highlighted</code></pre>
72+
<pre><code class='torchlight' style='color: red;'>highlighted</code></pre>
7173
<p>after</p>
7274
7375
EOT;
@@ -187,13 +189,13 @@ public function it_sends_one_request_only_and_matches_by_id()
187189
$response = [
188190
'blocks' => [[
189191
'id' => 'block_id_3',
190-
'wrapped' => 'some js',
192+
'highlighted' => 'some js',
191193
], [
192194
'id' => 'block_id_1',
193-
'wrapped' => 'some php',
195+
'highlighted' => 'some php',
194196
], [
195197
'id' => 'block_id_2',
196-
'wrapped' => 'some ruby',
198+
'highlighted' => 'some ruby',
197199
]]
198200
];
199201

@@ -207,9 +209,9 @@ public function it_sends_one_request_only_and_matches_by_id()
207209

208210
$expected = <<<EOT
209211
<p>before</p>
210-
some php
211-
some ruby
212-
some js
212+
<pre><code class='' style=''>some php</code></pre>
213+
<pre><code class='' style=''>some ruby</code></pre>
214+
<pre><code class='' style=''>some js</code></pre>
213215
<p>after</p>
214216
215217
EOT;

0 commit comments

Comments
 (0)