Skip to content

Commit 2da5fef

Browse files
committed
Add api attributes onto the code component
1 parent d528caf commit 2da5fef

9 files changed

+128
-18
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

5+
## 0.5.8 - 2022-01-19
6+
7+
### Added
8+
9+
- Attributes from the API will now be passed on to the code component. (The API now returns 'data-lang' as an attribute.)
10+
511
## 0.5.7 - 2021-11-02
612

713
### Added

src/Blade/BladeManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ public static function renderContent($content)
7878
$swap[$block->placeholder()] = $block->highlighted;
7979
$swap[$block->placeholder('classes')] = $block->classes;
8080
$swap[$block->placeholder('styles')] = $block->styles;
81+
$swap[$block->placeholder('attrs')] = $block->attrsAsString();
8182
}
8283

8384
// If this version of Laravel is affected by the spacing bug, then
84-
// we will swap our our placeholders with a preceding space, and
85+
// we will swap out our placeholders with a preceding space, and
8586
// a following space. This effectively fixes the bug.
8687
if (static::$affectedBySpacingBug) {
8788
$swap[' ##PRE_TL_COMPONENT##'] = '';

src/Blade/CodeComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function render()
110110
// We have to add the ##PRE## and ##POST## tags to cover a framework bug.
111111
// @see BladeManager::renderContent.
112112
return <<<'EOT'
113-
##PRE_TL_COMPONENT##<code {{
113+
##PRE_TL_COMPONENT##<code {{ $block->placeholder('attrs') }}{{
114114
$attributes->except('style')->merge([
115115
'class' => $block->placeholder('classes'),
116116
'style' => $attributes->get('style') . $block->placeholder('styles')

src/Block.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class Block
7373
*/
7474
public $styles;
7575

76+
/**
77+
* Attributes to apply to the code tag.
78+
*
79+
* @var array
80+
*/
81+
public $attrs = [];
82+
7683
/**
7784
* The unique ID for the block.
7885
*
@@ -181,6 +188,21 @@ public function code($code)
181188
return $this;
182189
}
183190

191+
/**
192+
* @return string
193+
*/
194+
public function attrsAsString()
195+
{
196+
$attrs = [];
197+
198+
foreach ($this->attrs as $key => $value) {
199+
$value = addslashes($value);
200+
$attrs[] = "$key=\"$value\"";
201+
}
202+
203+
return implode(' ', $attrs) . ' ';
204+
}
205+
184206
/**
185207
* @param $processor
186208
* @return $this

src/Client.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected function blocksAsRequestParam(Collection $blocks)
153153

154154
protected function applyDirectlyFromResponse()
155155
{
156-
return ['wrapped', 'highlighted', 'styles', 'classes'];
156+
return ['wrapped', 'highlighted', 'styles', 'classes', 'attrs'];
157157
}
158158

159159
protected function setCacheFromBlocks(Collection $blocks, Collection $ids)
@@ -214,7 +214,10 @@ protected function defaultResponse(Block $block)
214214
'highlighted' => $highlighted,
215215
'classes' => 'torchlight',
216216
'styles' => '',
217-
'wrapped' => "<pre><code class='torchlight'>{$highlighted}</code></pre>",
217+
'attrs' => [
218+
'data-lang' => $block->language
219+
],
220+
'wrapped' => "<pre><code data-lang='{$block->language}' class='torchlight'>{$highlighted}</code></pre>",
218221
];
219222
}
220223
}

tests/BaseTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ protected function fakeApi()
6363
$response[] = array_merge($block, [
6464
'classes' => 'torchlight',
6565
'styles' => 'background-color: #000000;',
66+
'attrs' => [
67+
'data-lang' => $block['language']
68+
],
6669
'wrapped' => "<pre><code>$highlighted</code></pre>",
6770
'highlighted' => $highlighted,
6871
], $fake);

tests/ClientTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function if_theres_no_response_then_it_sets_a_default()
181181
Torchlight::highlight($block);
182182

183183
$this->assertEquals('<div class=\'line\'>echo &quot;hello world&quot;;</div>', $block->highlighted);
184-
$this->assertEquals('<pre><code class=\'torchlight\'><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>', $block->wrapped);
184+
$this->assertEquals('<pre><code data-lang=\'php\' class=\'torchlight\'><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>', $block->wrapped);
185185
}
186186

187187
/** @test */
@@ -196,6 +196,6 @@ public function a_500_error_returns_a_default_in_production()
196196
Torchlight::highlight($block);
197197

198198
$this->assertEquals('<div class=\'line\'>echo &quot;hello world&quot;;</div>', $block->highlighted);
199-
$this->assertEquals('<pre><code class=\'torchlight\'><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>', $block->wrapped);
199+
$this->assertEquals('<pre><code data-lang=\'php\' class=\'torchlight\'><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>', $block->wrapped);
200200
}
201201
}

tests/MiddlewareAndComponentTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function it_sends_a_simple_request_with_no_response()
3939
$response = $this->getView('simple-php-hello-world.blade.php');
4040

4141
$this->assertEquals(
42-
'<pre><code class="torchlight" style=""><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>',
42+
'<pre><code data-lang="php" class="torchlight" style=""><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>',
4343
$response->content()
4444
);
4545

@@ -66,7 +66,7 @@ public function it_sends_a_simple_request_with_highlighted_response()
6666
$response = $this->getView('simple-php-hello-world.blade.php');
6767

6868
$this->assertEquals(
69-
'<pre><code class="torchlight" style="background-color: #292D3E;">this is the highlighted response from the server</code></pre>',
69+
'<pre><code data-lang="php" class="torchlight" style="background-color: #292D3E;">this is the highlighted response from the server</code></pre>',
7070
$response->content()
7171
);
7272
}
@@ -83,7 +83,7 @@ public function it_sends_a_simple_request_with_style()
8383
$response = $this->getView('simple-php-hello-world-with-style.blade.php');
8484

8585
$this->assertEquals(
86-
'<pre><code class="torchlight" style="display: none;background-color: #292D3E;">this is the highlighted response from the server</code></pre>',
86+
'<pre><code data-lang="php" class="torchlight" style="display: none;background-color: #292D3E;">this is the highlighted response from the server</code></pre>',
8787
$response->content()
8888
);
8989
}
@@ -100,7 +100,7 @@ public function classes_get_merged()
100100
$response = $this->getView('simple-php-hello-world-with-classes.blade.php');
101101

102102
$this->assertEquals(
103-
'<code class="torchlight mt-4" style="background-color: #292D3E;">this is the highlighted response from the server</code>',
103+
'<code data-lang="php" class="torchlight mt-4" style="background-color: #292D3E;">this is the highlighted response from the server</code>',
104104
$response->content()
105105
);
106106
}
@@ -117,7 +117,7 @@ public function attributes_are_preserved()
117117
$response = $this->getView('simple-php-hello-world-with-attributes.blade.php');
118118

119119
$this->assertEquals(
120-
'<code class="torchlight" style="background-color: #292D3E;" x-data="{}">this is the highlighted response from the server</code>',
120+
'<code data-lang="php" class="torchlight" style="background-color: #292D3E;" x-data="{}">this is the highlighted response from the server</code>',
121121
$response->content()
122122
);
123123
}
@@ -134,7 +134,7 @@ public function inline_keeps_its_spaces()
134134
$response = $this->getView('an-inline-component.blade.php');
135135

136136
$this->assertEquals(
137-
'this is <code class="torchlight" style="background-color: #292D3E;">this is the highlighted response from the server</code> inline',
137+
'this is <code data-lang="php" class="torchlight" style="background-color: #292D3E;">this is the highlighted response from the server</code> inline',
138138
$response->content()
139139
);
140140
}
@@ -151,7 +151,7 @@ public function inline_swaps_run()
151151
$response = $this->getView('an-inline-component-with-swaps.blade.php');
152152

153153
$this->assertEquals(
154-
'this is <code class="torchlight" style="background-color: #292D3E;">echo "goodbye world"</code> inline',
154+
'this is <code data-lang="php" class="torchlight" style="background-color: #292D3E;">echo "goodbye world"</code> inline',
155155
$response->content()
156156
);
157157
}
@@ -168,7 +168,7 @@ public function inline_processors_run()
168168
$response = $this->getView('an-inline-component-with-post-processors.blade.php');
169169

170170
$this->assertEquals(
171-
'this is <code class="torchlight" style="background-color: #292D3E;">echo "goodbye world"</code> inline',
171+
'this is <code data-lang="php" class="torchlight" style="background-color: #292D3E;">echo "goodbye world"</code> inline',
172172
$response->content()
173173
);
174174
}
@@ -229,7 +229,7 @@ public function dedent_works_properly()
229229

230230
$response = $this->getView('dedent_works_properly.blade.php');
231231

232-
$result = "<code class=\"torchlight\" style=\"\"><div class='line'>public function {</div><div class='line'> // test</div><div class='line'>}</div></code>";
232+
$result = "<code data-lang=\"php\" class=\"torchlight\" style=\"\"><div class='line'>public function {</div><div class='line'> // test</div><div class='line'>}</div></code>";
233233

234234
if (BladeManager::$affectedBySpacingBug) {
235235
$this->assertEquals(
@@ -252,7 +252,7 @@ public function two_code_in_one_pre()
252252

253253
$response = $this->getView('two-codes-in-one-tag.blade.php');
254254

255-
$result = "<code class=\"torchlight\" style=\"\"><div class='line'>public function {</div><div class='line'> // test</div><div class='line'>}</div></code>";
255+
$result = "<code data-lang=\"php\" class=\"torchlight\" style=\"\"><div class='line'>public function {</div><div class='line'> // test</div><div class='line'>}</div></code>";
256256

257257
if (BladeManager::$affectedBySpacingBug) {
258258
$this->assertEquals(
@@ -287,9 +287,9 @@ public function two_components_work()
287287
$response = $this->getView('two-simple-php-hello-world.blade.php');
288288

289289
$expected = <<<EOT
290-
<pre><code class="torchlight1" style="background-color: #111111;">response 1</code></pre>
290+
<pre><code data-lang="php" class="torchlight1" style="background-color: #111111;">response 1</code></pre>
291291
292-
<pre><code class="torchlight2" style="background-color: #222222;">response 2</code></pre>
292+
<pre><code data-lang="php" class="torchlight2" style="background-color: #222222;">response 2</code></pre>
293293
EOT;
294294

295295
$this->assertEquals($expected, $response->content());

tests/RealClientTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <[email protected]|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Torchlight\Tests;
7+
8+
use Illuminate\Http\Client\Request;
9+
use Illuminate\Support\Facades\Cache;
10+
use Illuminate\Support\Facades\Http;
11+
use Illuminate\Support\Facades\Route;
12+
use Illuminate\Support\Facades\View;
13+
use Torchlight\Block;
14+
use Torchlight\Client;
15+
use Torchlight\Middleware\RenderTorchlight;
16+
use Torchlight\Torchlight;
17+
18+
class RealClientTest extends BaseTest
19+
{
20+
public function getEnvironmentSetUp($app)
21+
{
22+
$this->setUpTorchlight();
23+
}
24+
25+
protected function setUpCache()
26+
{
27+
config()->set('cache', [
28+
'default' => 'array',
29+
'stores' => [
30+
'array' => [
31+
'driver' => 'array',
32+
'serialize' => false,
33+
],
34+
],
35+
]);
36+
}
37+
38+
protected function setUpTorchlight()
39+
{
40+
config()->set('torchlight', [
41+
'theme' => 'material-theme-lighter',
42+
'token' => '',
43+
'bust' => 0,
44+
'blade_components' => true,
45+
'options' => [
46+
'lineNumbers' => false
47+
]
48+
]);
49+
}
50+
51+
protected function getView($view)
52+
{
53+
// This helps when testing multiple Laravel versions locally.
54+
$this->artisan('view:clear');
55+
56+
Route::get('/torchlight', function () use ($view) {
57+
return View::file(__DIR__ . '/Support/' . $view);
58+
})->middleware(RenderTorchlight::class);
59+
60+
return $this->call('GET', 'torchlight');
61+
}
62+
63+
/** @test */
64+
public function it_sends_a_simple_request_with_highlighted_response_real()
65+
{
66+
return $this->markTestSkipped();
67+
68+
$response = $this->getView('simple-php-hello-world.blade.php');
69+
70+
$this->assertEquals(
71+
'<pre><code data-lang="php" class="torchlight" style="background-color: #FAFAFA; --theme-selection-background: #CCD7DA80;"><!-- Syntax highlighted by torchlight.dev --><div class=\'line\'><span style="color: #6182B8;">echo</span><span style="color: #90A4AE;"> </span><span style="color: #39ADB5;">&quot;</span><span style="color: #91B859;">hello world</span><span style="color: #39ADB5;">&quot;</span><span style="color: #39ADB5;">;</span></div></code></pre>',
72+
$response->content()
73+
);
74+
}
75+
}

0 commit comments

Comments
 (0)