From ca345d528381781de12df869d98c839d1121526c Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Tue, 5 Nov 2024 11:43:41 +0100 Subject: [PATCH] fix: create shiki with dual themes --- src/Shiki.php | 10 ++++++-- tests/ShikiTest.php | 25 ++++++++++++++++++- ...can_accept_a_light_and_a_dark_theme__1.txt | 1 + ...iTest__it_can_accept_a_single_theme__1.txt | 1 + 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/__snapshots__/ShikiTest__it_can_accept_a_light_and_a_dark_theme__1.txt create mode 100644 tests/__snapshots__/ShikiTest__it_can_accept_a_single_theme__1.txt diff --git a/src/Shiki.php b/src/Shiki.php index d13a39f..7c08703 100644 --- a/src/Shiki.php +++ b/src/Shiki.php @@ -8,7 +8,10 @@ class Shiki { - protected string $defaultTheme; + /** + * @var string|array|null $defaultTheme Can be a single theme or an array with a light and a dark theme. + */ + protected mixed $defaultTheme; private static ?string $customWorkingDirPath = null; @@ -51,7 +54,10 @@ public function getAvailableLanguages(): array return $languages; } - public function __construct(string $defaultTheme = 'nord') + /** + * @param string|array|null $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; } diff --git a/tests/ShikiTest.php b/tests/ShikiTest.php index 5642b94..6ed2015 100644 --- a/tests/ShikiTest.php +++ b/tests/ShikiTest.php @@ -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 = ''; + + $highlighter = new Shiki('github-light'); + + $highlightedCode = $highlighter->highlightCode($code, 'php'); + + assertMatchesSnapshot($highlightedCode); +}); + +it('can accept a light and a dark theme', function () { + $code = ''; + + $highlighter = new Shiki([ + 'light' => 'github-light', + 'dark' => 'github-dark', + ]); + + $highlightedCode = $highlighter->highlightCode($code, 'php'); + + assertMatchesSnapshot($highlightedCode); +}); + it('can highlight php', function () { $code = ''; diff --git a/tests/__snapshots__/ShikiTest__it_can_accept_a_light_and_a_dark_theme__1.txt b/tests/__snapshots__/ShikiTest__it_can_accept_a_light_and_a_dark_theme__1.txt new file mode 100644 index 0000000..5cd73d2 --- /dev/null +++ b/tests/__snapshots__/ShikiTest__it_can_accept_a_light_and_a_dark_theme__1.txt @@ -0,0 +1 @@ +
<?php echo "Hello World"; ?>
\ No newline at end of file diff --git a/tests/__snapshots__/ShikiTest__it_can_accept_a_single_theme__1.txt b/tests/__snapshots__/ShikiTest__it_can_accept_a_single_theme__1.txt new file mode 100644 index 0000000..8844573 --- /dev/null +++ b/tests/__snapshots__/ShikiTest__it_can_accept_a_single_theme__1.txt @@ -0,0 +1 @@ +
<?php echo "Hello World"; ?>
\ No newline at end of file