Skip to content

Commit 6d851bc

Browse files
committed
Send options to the API
1 parent ae6d49e commit 6d851bc

File tree

7 files changed

+57
-32
lines changed

7 files changed

+57
-32
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
## Unreleased
44

5+
## 0.4.6 - 2021-07-27
6+
7+
### Added
8+
- Added the ability to send `options` from the config file to the API.
9+
510
## 0.4.5 - 2021-07-18
611

712
### Changed
8-
- The default response (used if a request fails) now includes the `<div class='line'>` wrappers.s
13+
- The default response (used if a request fails) now includes the `<div class='line'>` wrappers.
914

1015
## 0.4.4 - 2021-06-16
1116

config/torchlight.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'cache' => env('TORCHLIGHT_CACHE_DRIVER'),
88

99
// Which theme you want to use. You can find all of the themes at
10-
// https://torchlight.dev/themes.
10+
// https://torchlight.dev/docs/themes.
1111
'theme' => env('TORCHLIGHT_THEME', 'material-theme-palenight'),
1212

1313
// Your API token from torchlight.dev.
@@ -18,4 +18,25 @@
1818

1919
// The Host of the API.
2020
'host' => env('TORCHLIGHT_HOST', 'https://api.torchlight.dev'),
21+
22+
// Global options to control blocks-level settings.
23+
// https://torchlight.dev/docs/options
24+
'options' => [
25+
// Turn line numbers on or off globally.
26+
'lineNumbers' => true,
27+
28+
// Control the `style` attribute applied to line numbers.
29+
// 'lineNumbersStyle' => '',
30+
31+
// Turn on +/- diff indicators.
32+
'diffIndicators' => true,
33+
34+
// If there are any diff indicators for a line, put them
35+
// in place of the line number to save horizontal space.
36+
'diffIndicatorsInPlaceOfLineNumbers' => true,
37+
38+
// When lines are collapsed, this is the text that will
39+
// be shown to indicate that they can be expanded.
40+
// 'summaryCollapsedIndicator' => '...',
41+
]
2142
];

src/Block.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,13 @@ protected function generateId()
114114
*/
115115
public function hash()
116116
{
117-
return md5($this->language . $this->theme . $this->code . Torchlight::config('bust'));
117+
return md5(
118+
$this->language
119+
. $this->theme
120+
. $this->code
121+
. Torchlight::config('bust')
122+
. json_encode(Torchlight::config('options'))
123+
);
118124
}
119125

120126
/**

src/Client.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
namespace Torchlight;
77

8-
use GuzzleHttp\Promise\Promise;
98
use Illuminate\Http\Client\ConnectionException;
109
use Illuminate\Support\Arr;
1110
use Illuminate\Support\Collection;
@@ -52,6 +51,7 @@ protected function request(Collection $blocks)
5251
->withToken($this->getToken())
5352
->post('highlight', [
5453
'blocks' => $this->blocksAsRequestParam($blocks)->values()->toArray(),
54+
'options' => $this->getOptions(),
5555
]);
5656

5757
if ($response->failed()) {
@@ -87,30 +87,6 @@ protected function request(Collection $blocks)
8787
return $blocks;
8888
}
8989

90-
protected function requestChunks($chunks)
91-
{
92-
$host = Torchlight::config('host', 'https://api.torchlight.dev');
93-
$timeout = Torchlight::config('request_timeout', 5);
94-
95-
// Can't use Http::pool here because it's not
96-
// available in Laravel 7 and early 8.
97-
return $chunks
98-
// This first map fires all the requests.
99-
->map(function ($blocks) use ($host, $timeout) {
100-
return Http::async()
101-
->baseUrl($host)
102-
->timeout($timeout)
103-
->withToken($this->getToken())
104-
->post('highlight', [
105-
'blocks' => $this->blocksAsRequestParam($blocks)->values()->toArray(),
106-
]);
107-
})
108-
// The second one waits for them all to finish.
109-
->map(function (Promise $request) {
110-
return $request->wait();
111-
});
112-
}
113-
11490
protected function collectionOfBlocks($blocks)
11591
{
11692
return collect($blocks)->each(function ($block) {
@@ -133,6 +109,17 @@ protected function getToken()
133109
return $token;
134110
}
135111

112+
protected function getOptions()
113+
{
114+
$options = Torchlight::config('options', []);
115+
116+
if (!is_array($options)) {
117+
$options = [];
118+
}
119+
120+
return $options;
121+
}
122+
136123
protected function potentiallyThrowRequestException($exception)
137124
{
138125
if ($exception) {

tests/BlockTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function to_request_params_includes_required_info()
9494

9595
$this->assertEquals([
9696
'id' => 'id',
97-
'hash' => '494dc5134843e4f6fdcc838e8bcd7f7c',
97+
'hash' => '7580494fda334fc2329cdde34ad4a1d1',
9898
'language' => 'new language',
9999
'theme' => 'new theme',
100100
'code' => 'new code',

tests/ClientTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ protected function setUpTorchlight()
3939
config()->set('torchlight', [
4040
'theme' => 'material',
4141
'token' => 'token',
42-
'bust' => 0
42+
'bust' => 0,
43+
'options' => [
44+
'lineNumbers' => true
45+
]
4346
]);
4447
}
4548

@@ -54,9 +57,12 @@ public function it_sends_a_simple_request()
5457

5558
Http::assertSent(function ($request) {
5659
return $request->hasHeader('Authorization', 'Bearer token')
60+
&& $request['options'] === [
61+
'lineNumbers' => true
62+
]
5763
&& $request['blocks'] === [[
5864
'id' => 'id',
59-
'hash' => '49c75d827bf95472ac155c6b6cc42aaf',
65+
'hash' => 'e937def4cb365a758d1bf55ecc7fea5b',
6066
'language' => 'php',
6167
'theme' => 'material',
6268
'code' => 'echo "hello world";',

tests/MiddlewareAndComponentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function it_sends_a_simple_request_with_no_response()
4545
Http::assertSent(function ($request) {
4646
return $request['blocks'][0] === [
4747
'id' => 'component',
48-
'hash' => 'e99681f5450cbaf3774adc5eb74d637f',
48+
'hash' => 'fe47ca550596fd0cea4664cc73774bcb',
4949
'language' => 'php',
5050
'theme' => 'material-theme-palenight',
5151
'code' => 'echo "hello world";',

0 commit comments

Comments
 (0)