Skip to content

Commit

Permalink
Add a way to throw exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Apr 11, 2024
1 parent bdec478 commit 3dd3376
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ When you mark lines as highlighted, added, deleted or focused, Shiki will apply
}
```

## Throwing on exceptions

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`:

```php
$environment = (new Environment())
->addExtension(new CommonMarkCoreExtension())
->addExtension(new HighlightCodeExtension(theme: $theme, throw: true));
```

## A word on performance

Highlighting with Shiki is a resource intensive process. We highly recommend using some form of caching.
Expand Down
4 changes: 2 additions & 2 deletions src/HighlightCodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class HighlightCodeExtension implements ExtensionInterface
{
protected ShikiHighlighter $shikiHighlighter;

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

public function register(EnvironmentBuilderInterface $environment): void
Expand Down
9 changes: 7 additions & 2 deletions src/ShikiHighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
class ShikiHighlighter
{
public function __construct(
protected Shiki $shiki
protected Shiki $shiki,
protected bool $throw = false,
) {
}

Expand All @@ -35,7 +36,11 @@ public function highlight(string $codeBlock, ?string $infoLine = null): string
'focusLines' => $definition['focusLines'],
],
);
} catch (Exception) {
} catch (Exception $e) {
if ($this->throw) {
throw $e;
}

$highlightedContents = $codeBlock;
}

Expand Down
47 changes: 45 additions & 2 deletions tests/HighlightCodeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Spatie\CommonMarkShikiHighlighter\HighlightCodeExtension;
use Spatie\ShikiPhp\Shiki;
use Spatie\Snapshots\MatchesSnapshots;
use Symfony\Component\Process\Exception\ProcessFailedException;

class HighlightCodeExtensionTest extends TestCase
{
Expand Down Expand Up @@ -81,11 +82,53 @@ public function can_create_with_a_shiki_instance()

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

$highlightedCode = $commonMarkConverter->convertToHtml($markdown);
$highlightedCode = $commonMarkConverter->convert($markdown);

$this->assertMatchesSnapshot((string) $highlightedCode);
}

/** @test */
public function it_will_not_throw_by_default()
{
$markdown = <<<MD
Here is a piece of fenced PHP code
```phpp
<?php echo "Hello World"; ?>
```
MD;

$environment = (new Environment())
->addExtension(new CommonMarkCoreExtension())
->addExtension(new HighlightCodeExtension(shiki: new Shiki()));

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

$commonMarkConverter->convert($markdown);

$this->expectNotToPerformAssertions();
}

/** @test */
public function can_throw_on_exceptions()
{
$markdown = <<<MD
Here is a piece of fenced PHP code
```phpp
<?php echo "Hello World"; ?>
```
MD;

$environment = (new Environment())
->addExtension(new CommonMarkCoreExtension())
->addExtension(new HighlightCodeExtension(shiki: new Shiki(), throw: true));

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

$this->expectException(ProcessFailedException::class);

$commonMarkConverter->convert($markdown);
}

protected function convertToHtml(string $markdown): string
{
$environment = (new Environment())
Expand All @@ -94,6 +137,6 @@ protected function convertToHtml(string $markdown): string

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

return $commonMarkConverter->convertToHtml($markdown);
return $commonMarkConverter->convert($markdown);
}
}

0 comments on commit 3dd3376

Please sign in to comment.