Skip to content

Commit 5cb47ad

Browse files
committed
Add ability to set theme from markdown
1 parent 365d349 commit 5cb47ad

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## 0.3.1 - 2021-06-17
4+
5+
### Added
6+
- Ability to set a theme per block by using `theme:name` syntax, e.g.:
7+
8+
````
9+
```php theme:dark-plus
10+
// Use dark-plus for this block.
11+
```
12+
````
13+
314
## 0.3.0 - 2021-05-28
415

516
- Bump `torchlight/torchlight-laravel` dependency.

src/TorchlightExtension.php

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

33
namespace Torchlight\Commonmark;
44

5+
use Illuminate\Support\Str;
56
use League\CommonMark\Block\Element\AbstractBlock;
67
use League\CommonMark\Block\Element\FencedCode;
78
use League\CommonMark\Block\Element\IndentedCode;
@@ -73,6 +74,7 @@ protected function makeTorchlightBlock($node)
7374
{
7475
return Block::make()
7576
->language($this->getLanguage($node))
77+
->theme($this->getTheme($node))
7678
->code($node->getStringContent());
7779
}
7880

@@ -81,18 +83,31 @@ protected function isCodeNode($node)
8183
return $node instanceof FencedCode || $node instanceof IndentedCode;
8284
}
8385

84-
protected function getLanguage($node)
86+
protected function getInfo($node)
8587
{
8688
if (!$this->isCodeNode($node) || $node instanceof IndentedCode) {
8789
return null;
8890
}
8991

9092
$infoWords = $node->getInfoWords();
9193

92-
if (empty($infoWords) || empty($infoWords[0])) {
93-
return null;
94-
}
94+
return empty($infoWords) ? [] : $infoWords;
95+
}
96+
97+
protected function getLanguage($node)
98+
{
99+
$language = $this->getInfo($node)[0];
95100

96-
return Xml::escape($infoWords[0], true);
101+
return $language ? Xml::escape($language, true) : null;
97102
}
103+
104+
protected function getTheme($node)
105+
{
106+
foreach ($this->getInfo($node) as $item) {
107+
if (Str::startsWith($item, 'theme:')) {
108+
return Str::after($item, 'theme:');
109+
}
110+
}
111+
}
112+
98113
}

tests/CodeRendererTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,31 @@ public function gets_language_and_contents()
9393

9494
Http::assertSent(function ($request) {
9595
return $request['blocks'][0]['language'] === 'foobarlang'
96-
&& $request['blocks'][0]['code'] === '<div>test</div>';
96+
&& $request['blocks'][0]['code'] === '<div>test</div>'
97+
&& $request['blocks'][0]['theme'] === null;
98+
});
99+
}
100+
101+
/** @test */
102+
public function can_set_theme()
103+
{
104+
$markdown = <<<'EOT'
105+
before
106+
107+
```lang theme:daft-punk
108+
<div>test</div>
109+
```
110+
after
111+
EOT;
112+
113+
Http::fake();
114+
115+
$this->render($markdown);
116+
117+
Http::assertSent(function ($request) {
118+
return $request['blocks'][0]['language'] === 'lang'
119+
&& $request['blocks'][0]['code'] === '<div>test</div>'
120+
&& $request['blocks'][0]['theme'] === 'daft-punk';
97121
});
98122
}
99123

0 commit comments

Comments
 (0)