Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downgrade to php7.4 #14

Merged
merged 5 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
],
"require": {
"php": "^8.0"
"php": "^7.4|^8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^v3.0",
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 21 additions & 16 deletions src/Shiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class Shiki
{
protected string $defaultTheme;

private static ?string $customWorkingDirPath = null;

public static function setCustomWorkingDirPath(?string $path)
Expand All @@ -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 ?? [],
]);
}

Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down
27 changes: 12 additions & 15 deletions tests/ShikiCustomRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -36,15 +36,15 @@
@endif
blade;

$highlightedCode = Shiki::highlight($code, language: 'blade', theme: 'github-light');
$highlightedCode = Shiki::highlight($code, 'blade', 'github-light');

assertMatchesSnapshot($highlightedCode);
});

it('can highlight antlers', function () {
$code = '{{ if }} Hi there {{ /if }}';

$highlightedCode = Shiki::highlight($code, language: 'antlers');
$highlightedCode = Shiki::highlight($code, 'antlers');

assertMatchesSnapshot($highlightedCode);
});
Expand All @@ -60,7 +60,7 @@
it('can mark lines as highlighted', function () {
$code = '<?php echo "Hello World"; ?>';

$highlightedCode = Shiki::highlight($code, highlightLines: [1]);
$highlightedCode = Shiki::highlight($code, null, null, [1]);

assertMatchesSnapshot($highlightedCode);
});
Expand All @@ -72,64 +72,61 @@
return null;
";

$highlightedCode = Shiki::highlight($code, highlightLines: ['1', '2-4']);
$highlightedCode = Shiki::highlight($code, null, null, ['1', '2-4']);

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight($code, addLines: [1]);
$highlightedCode = Shiki::highlight($code, null, null, null, [1]);

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight($code, deleteLines: [1]);
$highlightedCode = Shiki::highlight($code, null, null, null, null, [1]);

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight($code, focusLines: [1]);
$highlightedCode = Shiki::highlight($code, null, null, null, null, null, [1]);

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight(
$code,
theme: __DIR__ . '/testfiles/ayu-light.json'
);
$highlightedCode = Shiki::highlight($code, null, __DIR__ . '/testfiles/ayu-light.json');

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight($code, theme: 'github-light');
$highlightedCode = Shiki::highlight($code, null, 'github-light');

assertMatchesSnapshot($highlightedCode);
});

it('throws on invalid theme', function () {
$code = '<?php echo "Hello World"; ?>';

Shiki::highlight($code, theme: 'invalid-theme');
Shiki::highlight($code, null, 'invalid-theme');
})->throws(Exception::class);

it('throws on invalid language', function () {
$code = '<?php echo "Hello World"; ?>';

Shiki::highlight($code, language: 'invalid-language');
Shiki::highlight($code, 'invalid-language');
})->throws(Exception::class);

it('can get all available themes', function () {
Expand Down
27 changes: 12 additions & 15 deletions tests/ShikiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -33,15 +33,15 @@
@endif
blade;

$highlightedCode = Shiki::highlight($code, language: 'blade', theme: 'github-light');
$highlightedCode = Shiki::highlight($code, 'blade', 'github-light');

assertMatchesSnapshot($highlightedCode);
});

it('can highlight antlers', function () {
$code = '{{ if }} Hi there {{ /if }}';

$highlightedCode = Shiki::highlight($code, language: 'antlers');
$highlightedCode = Shiki::highlight($code, 'antlers');

assertMatchesSnapshot($highlightedCode);
});
Expand All @@ -57,7 +57,7 @@
it('can mark lines as highlighted', function () {
$code = '<?php echo "Hello World"; ?>';

$highlightedCode = Shiki::highlight($code, highlightLines: [1]);
$highlightedCode = Shiki::highlight($code, null, null, [1]);

assertMatchesSnapshot($highlightedCode);
});
Expand All @@ -69,64 +69,61 @@
return null;
";

$highlightedCode = Shiki::highlight($code, highlightLines: ['1', '2-4']);
$highlightedCode = Shiki::highlight($code, null, null, ['1', '2-4']);

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight($code, addLines: [1]);
$highlightedCode = Shiki::highlight($code, null, null, null, [1]);

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight($code, deleteLines: [1]);
$highlightedCode = Shiki::highlight($code, 'php', null, null, null, [1]);

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight($code, focusLines: [1]);
$highlightedCode = Shiki::highlight($code, 'php', null, null, null, null, [1]);

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight(
$code,
theme: __DIR__ . '/testfiles/ayu-light.json'
);
$highlightedCode = Shiki::highlight($code, null, __DIR__ . '/testfiles/ayu-light.json');

assertMatchesSnapshot($highlightedCode);
});

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

$highlightedCode = Shiki::highlight($code, theme: 'github-light');
$highlightedCode = Shiki::highlight($code, null, 'github-light');

assertMatchesSnapshot($highlightedCode);
});

it('throws on invalid theme', function () {
$code = '<?php echo "Hello World"; ?>';

Shiki::highlight($code, theme: 'invalid-theme');
Shiki::highlight($code, 'php', 'invalid-theme');
})->throws(Exception::class);

it('throws on invalid language', function () {
$code = '<?php echo "Hello World"; ?>';

Shiki::highlight($code, language: 'invalid-language');
Shiki::highlight($code, 'invalid-language');
})->throws(Exception::class);

it('can get all available themes', function () {
Expand Down