Skip to content

Commit 8146cce

Browse files
committed
Change default response
1 parent 08c8d1e commit 8146cce

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
## 0.4.5 - 2021-07-18
6+
7+
### Changed
8+
- The default response (used if a request fails) now includes the `<div class='line'>` wrappers.s
9+
510
## 0.4.4 - 2021-06-16
611

712
### Fixed

src/Client.php

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,13 @@ protected function request(Collection $blocks)
7272
$response = collect($response)->keyBy('id');
7373

7474
$blocks->each(function (Block $block) use ($response) {
75-
$blockFromResponse = Arr::get($response, "{$block->id()}", []);
75+
$blockFromResponse = Arr::get($response, "{$block->id()}", $this->defaultResponse($block));
7676

7777
foreach ($this->applyDirectlyFromResponse() as $key) {
7878
if (Arr::has($blockFromResponse, $key)) {
7979
$block->{$key} = $blockFromResponse[$key];
8080
}
8181
}
82-
83-
if (!$block->wrapped) {
84-
$block->wrapped = $this->defaultWrapped($block);
85-
}
86-
87-
if (!$block->highlighted) {
88-
$block->highlighted = $this->defaultHighlighted($block);
89-
}
9082
});
9183

9284
// Only store the ones we got back from the API.
@@ -221,21 +213,21 @@ protected function setBlocksFromCache(Collection $blocks)
221213
* In the case where nothing returns from the API, we have to show _something_.
222214
*
223215
* @param Block $block
224-
* @return string
225-
*/
226-
protected function defaultHighlighted(Block $block)
227-
{
228-
return htmlentities($block->code);
229-
}
230-
231-
/**
232-
* In the case where nothing returns from the API, we have to show _something_.
233-
*
234-
* @param Block $block
235-
* @return string
216+
* @return array
236217
*/
237-
protected function defaultWrapped(Block $block)
218+
protected function defaultResponse(Block $block)
238219
{
239-
return "<pre><code class='torchlight'>" . $this->defaultHighlighted($block) . '</code></pre>';
220+
$lines = array_map(function ($line) {
221+
return "<div class='line'>" . htmlentities($line) . "</div>";
222+
}, explode("\n", $block->code));
223+
224+
$highlighted = implode('', $lines);
225+
226+
return [
227+
'highlighted' => $highlighted,
228+
'classes' => 'torchlight',
229+
'styles' => '',
230+
'wrapped' => "<pre><code class='torchlight'>{$highlighted}</code></pre>",
231+
];
240232
}
241233
}

tests/ClientTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ public function if_theres_no_response_then_it_sets_a_default()
174174

175175
Torchlight::highlight($block);
176176

177-
$this->assertEquals('echo &quot;hello world&quot;;', $block->highlighted);
178-
$this->assertEquals('<pre><code class=\'torchlight\'>echo &quot;hello world&quot;;</code></pre>', $block->wrapped);
177+
$this->assertEquals('<div class=\'line\'>echo &quot;hello world&quot;;</div>', $block->highlighted);
178+
$this->assertEquals('<pre><code class=\'torchlight\'><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>', $block->wrapped);
179179
}
180180

181181
/** @test */
@@ -189,7 +189,7 @@ public function a_500_error_returns_a_default_in_production()
189189

190190
Torchlight::highlight($block);
191191

192-
$this->assertEquals('echo &quot;hello world&quot;;', $block->highlighted);
193-
$this->assertEquals('<pre><code class=\'torchlight\'>echo &quot;hello world&quot;;</code></pre>', $block->wrapped);
192+
$this->assertEquals('<div class=\'line\'>echo &quot;hello world&quot;;</div>', $block->highlighted);
193+
$this->assertEquals('<pre><code class=\'torchlight\'><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>', $block->wrapped);
194194
}
195195
}

tests/MiddlewareAndComponentTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function it_sends_a_simple_request_with_no_response()
3838
$response = $this->getView('simple-php-hello-world.blade.php');
3939

4040
$this->assertEquals(
41-
'<pre><code class="" style="">echo &quot;hello world&quot;;</code></pre>',
41+
'<pre><code class="torchlight" style=""><div class=\'line\'>echo &quot;hello world&quot;;</div></code></pre>',
4242
$response->content()
4343
);
4444

@@ -157,6 +157,19 @@ public function code_contents_can_be_a_file()
157157
});
158158
}
159159

160+
/** @test */
161+
public function code_contents_can_be_a_file_2()
162+
{
163+
$this->fakeNullResponse('component');
164+
165+
$this->getView('contents-via-file-2.blade.php');
166+
167+
Http::assertSent(function ($request) {
168+
return $request['blocks'][0]['code'] === rtrim(file_get_contents(config_path('app.php'), '\n'));
169+
});
170+
}
171+
172+
160173
/** @test */
161174
public function two_components_work()
162175
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<x-torchlight-code torchlight-id='component' language='php' contents='{{ config_path("app.php") }}'/>

0 commit comments

Comments
 (0)