@@ -37,23 +37,23 @@ public function highlight($blocks)
3737
3838 protected function request (Collection $ blocks )
3939 {
40- // @TODO handle timeout
41- $ response = Http::timeout (10 )
40+ $ response = Http::timeout (5 )
4241 ->withToken (config ('torchlight.token ' ))
4342 ->post ('https://torchlight.dev/api/highlight ' , [
4443 'blocks ' => $ this ->blocksAsRequestParam ($ blocks )->values (),
4544 ])
4645 ->json ();
4746
48- $ response = collect ($ response[ 'blocks ' ] )->keyBy ('id ' );
47+ $ response = collect (Arr:: get ( $ response, 'blocks ' , []) )->keyBy ('id ' );
4948
5049 $ blocks ->each (function (Block $ block ) use ($ response ) {
5150 $ block ->setHtml (
5251 $ block ->html ?? $ this ->getHtmlFromResponse ($ response , $ block )
5352 );
5453 });
5554
56- $ this ->setCacheFromBlocks ($ blocks );
55+ // Only store the ones we got back from the API.
56+ $ this ->setCacheFromBlocks ($ blocks , $ response ->keys ());
5757
5858 return $ blocks ;
5959 }
@@ -86,18 +86,16 @@ protected function blocksAsRequestParam(Collection $blocks)
8686 });
8787 }
8888
89- protected function setCacheFromBlocks (Collection $ blocks )
89+ protected function setCacheFromBlocks (Collection $ blocks, Collection $ ids )
9090 {
91- return $ blocks ->each (function (Block $ block ) {
92- if (!$ block ->html ) {
93- return ;
91+ $ blocks ->only ($ ids )->each (function (Block $ block ) use ($ ids ) {
92+ if ($ block ->html ) {
93+ $ this ->cache ()->put (
94+ $ this ->cacheKey ($ block ),
95+ $ block ->html ,
96+ now ()->addDays (7 )
97+ );
9498 }
95-
96- $ this ->cache ()->put (
97- $ this ->cacheKey ($ block ),
98- $ block ->html ,
99- now ()->addDays (30 )
100- );
10199 });
102100 }
103101
@@ -132,6 +130,6 @@ protected function getHtmlFromResponse(Collection $response, Block $block)
132130 */
133131 protected function defaultHtml (Block $ block )
134132 {
135- return "<pre><code> " . $ block ->code . "</code></pre> " ;
133+ return "<pre class='torchlight' ><code> " . htmlentities ( $ block ->code ) . "</code></pre> " ;
136134 }
137135}
0 commit comments