diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 614ffd5..d7ac0ab 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,7 +9,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [8.1, 8.0] + php: [7.4, 8.1, 8.0] stability: [prefer-lowest, prefer-stable] name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/README.md b/README.md index 498dd31..d4034ae 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,40 @@ Shiki::highlight( You can then target these classes in your own CSS to color these lines how you want. +## PHP 7.4 support + +Shiki has a nice and easy syntax in combination with at least PHP 8. + +It does support PHP 7.4, but does loose a little bit of it's nice syntax if using it with PHP7.4, as you need to follow the order of the variables. + +```php +// As reference +highlight( + string $code, + ?string $language = 'php', + ?string $theme = 'nord', + ?array $highlightLines = [], + ?array $addLines = [], + ?array $deleteLines = [], + ?array $focusLines = [] +) + +// Instead of PHP 8 syntax +Shiki::highlight( + code: $code, + language: 'php', + deleteLines: [1], +); + +// You need to follow PHP 7.4 syntax +Shiki::highlight( + $code, + 'php', + null, + null, + [1], +); + ## Determining available languages To get an array with [all languages that Shiki supports](https://github.com/shikijs/shiki/blob/master/docs/languages.md), call `getAvailableLanguages` diff --git a/composer.json b/composer.json index 3d06d9a..1914928 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ } ], "require": { - "php": "^8.0" + "php": "^7.4|^8.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^v3.0", @@ -45,7 +45,10 @@ "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true + } }, "minimum-stability": "dev", "prefer-stable": true diff --git a/package-lock.json b/package-lock.json index 5631b03..638c2ff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "shiki-php", "license": "MIT", "dependencies": { "shiki": "^0.9.5" diff --git a/src/Shiki.php b/src/Shiki.php index 1fa223c..ebcf3ef 100644 --- a/src/Shiki.php +++ b/src/Shiki.php @@ -8,6 +8,8 @@ class Shiki { + protected string $defaultTheme; + private static ?string $customWorkingDirPath = null; public static function setCustomWorkingDirPath(?string $path) @@ -17,18 +19,21 @@ public static function setCustomWorkingDirPath(?string $path) public static function highlight( string $code, - string $language = 'php', - string $theme = 'nord', - array $highlightLines = [], - array $addLines = [], - array $deleteLines = [], - array $focusLines = [], + ?string $language = null, + ?string $theme = null, + ?array $highlightLines = null, + ?array $addLines = null, + ?array $deleteLines = null, + ?array $focusLines = null ): string { + $language = $language ?? 'php'; + $theme = $theme ?? 'nord'; + return (new static())->highlightCode($code, $language, $theme, [ - 'highlightLines' => $highlightLines, - 'addLines' => $addLines, - 'deleteLines' => $deleteLines, - 'focusLines' => $focusLines, + 'highlightLines' => $highlightLines ?? [], + 'addLines' => $addLines ?? [], + 'deleteLines' => $deleteLines ?? [], + 'focusLines' => $focusLines ?? [], ]); } @@ -48,9 +53,9 @@ public function getAvailableLanguages(): array return $languages; } - public function __construct( - protected string $defaultTheme = 'nord' - ) { + public function __construct(string $defaultTheme = 'nord') + { + $this->defaultTheme = $defaultTheme; } public function getAvailableThemes(): array @@ -98,9 +103,9 @@ protected function callShiki(...$arguments): string ]; $process = new Process( - command: $command, - cwd: $this->getWorkingDirPath(), - timeout: null, + $command, + $this->getWorkingDirPath(), + null, ); $process->run(); diff --git a/tests/ShikiCustomRenderTest.php b/tests/ShikiCustomRenderTest.php index c46cc66..e5f22e8 100644 --- a/tests/ShikiCustomRenderTest.php +++ b/tests/ShikiCustomRenderTest.php @@ -24,7 +24,7 @@ it('can highlight blade', function () { $code = '@if(true) {{ "Hello world" }} @endif'; - $highlightedCode = Shiki::highlight($code, language: 'blade'); + $highlightedCode = Shiki::highlight($code, 'blade'); assertMatchesSnapshot($highlightedCode); }); @@ -36,7 +36,7 @@ @endif blade; - $highlightedCode = Shiki::highlight($code, language: 'blade', theme: 'github-light'); + $highlightedCode = Shiki::highlight($code, 'blade', 'github-light'); assertMatchesSnapshot($highlightedCode); }); @@ -44,7 +44,7 @@ it('can highlight antlers', function () { $code = '{{ if }} Hi there {{ /if }}'; - $highlightedCode = Shiki::highlight($code, language: 'antlers'); + $highlightedCode = Shiki::highlight($code, 'antlers'); assertMatchesSnapshot($highlightedCode); }); @@ -60,7 +60,7 @@ it('can mark lines as highlighted', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, highlightLines: [1]); + $highlightedCode = Shiki::highlight($code, null, null, [1]); assertMatchesSnapshot($highlightedCode); }); @@ -72,7 +72,7 @@ return null; "; - $highlightedCode = Shiki::highlight($code, highlightLines: ['1', '2-4']); + $highlightedCode = Shiki::highlight($code, null, null, ['1', '2-4']); assertMatchesSnapshot($highlightedCode); }); @@ -80,7 +80,7 @@ it('can mark lines as added', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, addLines: [1]); + $highlightedCode = Shiki::highlight($code, null, null, null, [1]); assertMatchesSnapshot($highlightedCode); }); @@ -88,7 +88,7 @@ it('can mark lines as deleted', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, deleteLines: [1]); + $highlightedCode = Shiki::highlight($code, null, null, null, null, [1]); assertMatchesSnapshot($highlightedCode); }); @@ -96,7 +96,7 @@ it('can mark lines as focus', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, focusLines: [1]); + $highlightedCode = Shiki::highlight($code, null, null, null, null, null, [1]); assertMatchesSnapshot($highlightedCode); }); @@ -104,10 +104,7 @@ it('can receive a custom theme', function () { $code = ''; - $highlightedCode = Shiki::highlight( - $code, - theme: __DIR__ . '/testfiles/ayu-light.json' - ); + $highlightedCode = Shiki::highlight($code, null, __DIR__ . '/testfiles/ayu-light.json'); assertMatchesSnapshot($highlightedCode); }); @@ -115,7 +112,7 @@ it('can accept different themes', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, theme: 'github-light'); + $highlightedCode = Shiki::highlight($code, null, 'github-light'); assertMatchesSnapshot($highlightedCode); }); @@ -123,13 +120,13 @@ it('throws on invalid theme', function () { $code = ''; - Shiki::highlight($code, theme: 'invalid-theme'); + Shiki::highlight($code, null, 'invalid-theme'); })->throws(Exception::class); it('throws on invalid language', function () { $code = ''; - Shiki::highlight($code, language: 'invalid-language'); + Shiki::highlight($code, 'invalid-language'); })->throws(Exception::class); it('can get all available themes', function () { diff --git a/tests/ShikiTest.php b/tests/ShikiTest.php index eb80564..bda078f 100644 --- a/tests/ShikiTest.php +++ b/tests/ShikiTest.php @@ -21,7 +21,7 @@ it('can highlight blade', function () { $code = '@if(true) {{ "Hello world" }} @endif'; - $highlightedCode = Shiki::highlight($code, language: 'blade'); + $highlightedCode = Shiki::highlight($code, 'blade'); assertMatchesSnapshot($highlightedCode); }); @@ -33,7 +33,7 @@ @endif blade; - $highlightedCode = Shiki::highlight($code, language: 'blade', theme: 'github-light'); + $highlightedCode = Shiki::highlight($code, 'blade', 'github-light'); assertMatchesSnapshot($highlightedCode); }); @@ -41,7 +41,7 @@ it('can highlight antlers', function () { $code = '{{ if }} Hi there {{ /if }}'; - $highlightedCode = Shiki::highlight($code, language: 'antlers'); + $highlightedCode = Shiki::highlight($code, 'antlers'); assertMatchesSnapshot($highlightedCode); }); @@ -57,7 +57,7 @@ it('can mark lines as highlighted', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, highlightLines: [1]); + $highlightedCode = Shiki::highlight($code, null, null, [1]); assertMatchesSnapshot($highlightedCode); }); @@ -69,7 +69,7 @@ return null; "; - $highlightedCode = Shiki::highlight($code, highlightLines: ['1', '2-4']); + $highlightedCode = Shiki::highlight($code, null, null, ['1', '2-4']); assertMatchesSnapshot($highlightedCode); }); @@ -77,7 +77,7 @@ it('can mark lines as added', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, addLines: [1]); + $highlightedCode = Shiki::highlight($code, null, null, null, [1]); assertMatchesSnapshot($highlightedCode); }); @@ -85,7 +85,7 @@ it('can mark lines as deleted', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, deleteLines: [1]); + $highlightedCode = Shiki::highlight($code, 'php', null, null, null, [1]); assertMatchesSnapshot($highlightedCode); }); @@ -93,7 +93,7 @@ it('can mark lines as focus', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, focusLines: [1]); + $highlightedCode = Shiki::highlight($code, 'php', null, null, null, null, [1]); assertMatchesSnapshot($highlightedCode); }); @@ -101,10 +101,7 @@ it('can receive a custom theme', function () { $code = ''; - $highlightedCode = Shiki::highlight( - $code, - theme: __DIR__ . '/testfiles/ayu-light.json' - ); + $highlightedCode = Shiki::highlight($code, null, __DIR__ . '/testfiles/ayu-light.json'); assertMatchesSnapshot($highlightedCode); }); @@ -112,7 +109,7 @@ it('can accept different themes', function () { $code = ''; - $highlightedCode = Shiki::highlight($code, theme: 'github-light'); + $highlightedCode = Shiki::highlight($code, null, 'github-light'); assertMatchesSnapshot($highlightedCode); }); @@ -120,13 +117,13 @@ it('throws on invalid theme', function () { $code = ''; - Shiki::highlight($code, theme: 'invalid-theme'); + Shiki::highlight($code, 'php', 'invalid-theme'); })->throws(Exception::class); it('throws on invalid language', function () { $code = ''; - Shiki::highlight($code, language: 'invalid-language'); + Shiki::highlight($code, 'invalid-language'); })->throws(Exception::class); it('can get all available themes', function () {