Skip to content

Commit b959c23

Browse files
committed
Support for dark mode
1 parent b1e4e66 commit b959c23

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
### Added
6+
7+
- Ability to set multiple themes for e.g. dark mode
8+
59
## 0.5.3 - 2022-01-19
610

711
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"require": {
2121
"php": "^7.2|^8.0",
22-
"torchlight/torchlight-laravel": "^0.5.9",
22+
"torchlight/torchlight-laravel": "dev-main",
2323
"league/commonmark": "^1.5|^2.0"
2424
},
2525
"require-dev": {

src/BaseExtension.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class BaseExtension
2121
protected $customBlockRenderer;
2222

2323
/**
24-
* @param DocumentParsedEvent $event
24+
* @param DocumentParsedEvent $event
2525
*/
2626
public function onDocumentParsed(DocumentParsedEvent $event)
2727
{
@@ -50,7 +50,7 @@ public function onDocumentParsed(DocumentParsedEvent $event)
5050
}
5151

5252
/**
53-
* @param callable $callback
53+
* @param callable $callback
5454
* @return $this
5555
*/
5656
public function useCustomBlockRenderer($callback)
@@ -66,7 +66,17 @@ public function useCustomBlockRenderer($callback)
6666
public function defaultBlockRenderer()
6767
{
6868
return function (Block $block) {
69-
return "<pre><code {$block->attrsAsString()}class='{$block->classes}' style='{$block->styles}'>{$block->highlighted}</code></pre>";
69+
$inner = '';
70+
71+
// Clones come from multiple themes.
72+
$blocks = $block->clones();
73+
array_unshift($blocks, $block);
74+
75+
foreach ($blocks as $block) {
76+
$inner .= "<code {$block->attrsAsString()}class='{$block->classes}' style='{$block->styles}'>{$block->highlighted}</code>";
77+
}
78+
79+
return "<pre>$inner</pre>";
7080
};
7181
}
7282

@@ -84,13 +94,13 @@ abstract protected function getLiteralContent($node);
8494
/**
8595
* Bind into a Commonmark V1 or V2 environment.
8696
*
87-
* @param $environment
88-
* @param string $renderMethod
97+
* @param $environment
98+
* @param string $renderMethod
8999
*/
90100
protected function bind($environment, $renderMethod)
91101
{
92102
// We start by walking the document immediately after it's parsed
93-
// to gather up all the code blocks and send off our requests.
103+
// to gather all the code blocks and send off our requests.
94104
$environment->addEventListener(DocumentParsedEvent::class, [$this, 'onDocumentParsed']);
95105

96106
foreach ($this->codeNodes() as $blockType) {

tests/BaseRendererTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\Http;
66
use Orchestra\Testbench\TestCase;
77
use Torchlight\Block;
8+
use Torchlight\Commonmark\BaseExtension;
89

910
abstract class BaseRendererTest extends TestCase
1011
{
@@ -24,6 +25,7 @@ protected function getEnvironmentSetUp($app)
2425
'block_id_3',
2526
];
2627

28+
BaseExtension::$torchlightBlocks = [];
2729
Block::$generateIdsUsing = function () use (&$ids) {
2830
return array_shift($ids);
2931
};
@@ -299,6 +301,51 @@ public function it_sends_one_request_only_and_matches_by_id()
299301
$this->assertEquals($expected, $html);
300302
}
301303

304+
/** @test */
305+
public function can_render_dark_and_light_themes()
306+
{
307+
$markdown = <<<'EOT'
308+
```php theme:dark:github-dark,light:github-light
309+
some php
310+
```
311+
EOT;
312+
313+
$response = [
314+
'blocks' => [[
315+
'id' => 'block_id_1',
316+
'highlighted' => 'some php 1',
317+
],[
318+
'id' => 'block_id_1_clone_0',
319+
'highlighted' => 'some php 2',
320+
]]
321+
];
322+
323+
Http::fake([
324+
'api.torchlight.dev/*' => Http::response($response, 200),
325+
]);
326+
327+
$html = $this->render($markdown);
328+
329+
Http::assertSentCount(1);
330+
331+
Http::assertSent(function ($request) {
332+
return count($request['blocks']) === 2
333+
&& $request['blocks'][0]['theme'] === 'dark:github-dark'
334+
&& $request['blocks'][1]['theme'] === 'light:github-light'
335+
&& $request['blocks'][1]['id'] === 'block_id_1_clone_0';
336+
});
337+
338+
339+
340+
$expected = <<<EOT
341+
<pre><code class='' style=''>some php 1</code><code class='' style=''>some php 2</code></pre>
342+
343+
EOT;
344+
345+
$this->assertEquals($expected, $html);
346+
}
347+
348+
302349
/** @test */
303350
public function it_can_set_a_custom_renderer()
304351
{

0 commit comments

Comments
 (0)