Skip to content

Commit

Permalink
fix: create shiki with dual themes (#27)
Browse files Browse the repository at this point in the history
* fix: create shiki with dual themes

* fix: type
  • Loading branch information
Barbapapazes authored Nov 5, 2024
1 parent 2ec5273 commit fab7828
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/Shiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

class Shiki
{
protected string $defaultTheme;
/**
* @var string|array<string, string> $defaultTheme Can be a single theme or an array with a light and a dark theme.
*/
protected mixed $defaultTheme;

private static ?string $customWorkingDirPath = null;

Expand Down Expand Up @@ -51,7 +54,10 @@ public function getAvailableLanguages(): array
return $languages;
}

public function __construct(string $defaultTheme = 'nord')
/**
* @param string|array<string, string> $defaultTheme Can be a single theme or an array with a light and a dark theme.
*/
public function __construct(mixed $defaultTheme = 'nord')
{
$this->defaultTheme = $defaultTheme;
}
Expand Down
25 changes: 24 additions & 1 deletion tests/ShikiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,36 @@

use function Spatie\Snapshots\assertMatchesSnapshot;

beforeAll(fn () => Shiki::setCustomWorkingDirPath(null));
beforeAll(fn() => Shiki::setCustomWorkingDirPath(null));

it('can get the default workingDirPath', function () {
expect((new Shiki())->getWorkingDirPath())
->toBeString()->toBe(dirname(__DIR__) . '/bin');
});

it('can accept a single theme', function () {
$code = '<?php echo "Hello World"; ?>';

$highlighter = new Shiki('github-light');

$highlightedCode = $highlighter->highlightCode($code, 'php');

assertMatchesSnapshot($highlightedCode);
});

it('can accept a light and a dark theme', function () {
$code = '<?php echo "Hello World"; ?>';

$highlighter = new Shiki([
'light' => 'github-light',
'dark' => 'github-dark',
]);

$highlightedCode = $highlighter->highlightCode($code, 'php');

assertMatchesSnapshot($highlightedCode);
});

it('can highlight php', function () {
$code = '<?php echo "Hello World"; ?>';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<pre class="shiki shiki-themes github-light github-dark" style="background-color:#fff;--shiki-dark-bg:#24292e;color:#24292e;--shiki-dark:#e1e4e8;"><code><span class="line"><span style="color:#D73A49;--shiki-dark:#F97583">&lt;?</span><span style="color:#005CC5;--shiki-dark:#79B8FF">php</span><span style="color:#24292E;--shiki-dark:#E1E4E8"> </span><span style="color:#005CC5;--shiki-dark:#79B8FF">echo</span><span style="color:#24292E;--shiki-dark:#E1E4E8"> </span><span style="color:#032F62;--shiki-dark:#9ECBFF">&quot;Hello World&quot;</span><span style="color:#24292E;--shiki-dark:#E1E4E8">; </span><span style="color:#D73A49;--shiki-dark:#F97583">?&gt;</span></span></code></pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<pre class="shiki" style="background-color: #fff"><code><span class="line"><span style="color: #D73A49">&lt;?</span><span style="color: #005CC5">php</span><span style="color: #24292E"> </span><span style="color: #005CC5">echo</span><span style="color: #24292E"> </span><span style="color: #032F62">&quot;Hello World&quot;</span><span style="color: #24292E">; </span><span style="color: #D73A49">?&gt;</span></span></code></pre>

0 comments on commit fab7828

Please sign in to comment.