Skip to content

Commit 3dd3376

Browse files
committed
Add a way to throw exceptions
1 parent bdec478 commit 3dd3376

File tree

4 files changed

+64
-6
lines changed

4 files changed

+64
-6
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ When you mark lines as highlighted, added, deleted or focused, Shiki will apply
149149
}
150150
```
151151

152+
## Throwing on exceptions
153+
154+
By default, the Shiki highlighter will not throw when something goes wrong and just return the non-highlighted code. If you want to throw the exception anyway, instantiate the highlighter with `throw` as `true`:
155+
156+
```php
157+
$environment = (new Environment())
158+
->addExtension(new CommonMarkCoreExtension())
159+
->addExtension(new HighlightCodeExtension(theme: $theme, throw: true));
160+
```
161+
152162
## A word on performance
153163

154164
Highlighting with Shiki is a resource intensive process. We highly recommend using some form of caching.

src/HighlightCodeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class HighlightCodeExtension implements ExtensionInterface
1414
{
1515
protected ShikiHighlighter $shikiHighlighter;
1616

17-
public function __construct(string $theme = 'nord', Shiki $shiki = null)
17+
public function __construct(string $theme = 'nord', Shiki $shiki = null, bool $throw = false)
1818
{
19-
$this->shikiHighlighter = new ShikiHighlighter($shiki ?? new Shiki($theme));
19+
$this->shikiHighlighter = new ShikiHighlighter($shiki ?? new Shiki($theme), $throw);
2020
}
2121

2222
public function register(EnvironmentBuilderInterface $environment): void

src/ShikiHighlighter.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
class ShikiHighlighter
99
{
1010
public function __construct(
11-
protected Shiki $shiki
11+
protected Shiki $shiki,
12+
protected bool $throw = false,
1213
) {
1314
}
1415

@@ -35,7 +36,11 @@ public function highlight(string $codeBlock, ?string $infoLine = null): string
3536
'focusLines' => $definition['focusLines'],
3637
],
3738
);
38-
} catch (Exception) {
39+
} catch (Exception $e) {
40+
if ($this->throw) {
41+
throw $e;
42+
}
43+
3944
$highlightedContents = $codeBlock;
4045
}
4146

tests/HighlightCodeExtensionTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Spatie\CommonMarkShikiHighlighter\HighlightCodeExtension;
1010
use Spatie\ShikiPhp\Shiki;
1111
use Spatie\Snapshots\MatchesSnapshots;
12+
use Symfony\Component\Process\Exception\ProcessFailedException;
1213

1314
class HighlightCodeExtensionTest extends TestCase
1415
{
@@ -81,11 +82,53 @@ public function can_create_with_a_shiki_instance()
8182

8283
$commonMarkConverter = new MarkdownConverter(environment: $environment);
8384

84-
$highlightedCode = $commonMarkConverter->convertToHtml($markdown);
85+
$highlightedCode = $commonMarkConverter->convert($markdown);
8586

8687
$this->assertMatchesSnapshot((string) $highlightedCode);
8788
}
8889

90+
/** @test */
91+
public function it_will_not_throw_by_default()
92+
{
93+
$markdown = <<<MD
94+
Here is a piece of fenced PHP code
95+
```phpp
96+
<?php echo "Hello World"; ?>
97+
```
98+
MD;
99+
100+
$environment = (new Environment())
101+
->addExtension(new CommonMarkCoreExtension())
102+
->addExtension(new HighlightCodeExtension(shiki: new Shiki()));
103+
104+
$commonMarkConverter = new MarkdownConverter(environment: $environment);
105+
106+
$commonMarkConverter->convert($markdown);
107+
108+
$this->expectNotToPerformAssertions();
109+
}
110+
111+
/** @test */
112+
public function can_throw_on_exceptions()
113+
{
114+
$markdown = <<<MD
115+
Here is a piece of fenced PHP code
116+
```phpp
117+
<?php echo "Hello World"; ?>
118+
```
119+
MD;
120+
121+
$environment = (new Environment())
122+
->addExtension(new CommonMarkCoreExtension())
123+
->addExtension(new HighlightCodeExtension(shiki: new Shiki(), throw: true));
124+
125+
$commonMarkConverter = new MarkdownConverter(environment: $environment);
126+
127+
$this->expectException(ProcessFailedException::class);
128+
129+
$commonMarkConverter->convert($markdown);
130+
}
131+
89132
protected function convertToHtml(string $markdown): string
90133
{
91134
$environment = (new Environment())
@@ -94,6 +137,6 @@ protected function convertToHtml(string $markdown): string
94137

95138
$commonMarkConverter = new MarkdownConverter(environment: $environment);
96139

97-
return $commonMarkConverter->convertToHtml($markdown);
140+
return $commonMarkConverter->convert($markdown);
98141
}
99142
}

0 commit comments

Comments
 (0)