Skip to content

Commit 34fe614

Browse files
Downgrade to php7.4 (#14)
* Add PHP 7.4 support * Fix styling * Allow to pass null * Fix styling Co-authored-by: jonassiewertsen <[email protected]>
1 parent 13d5241 commit 34fe614

File tree

7 files changed

+86
-49
lines changed

7 files changed

+86
-49
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ubuntu-latest]
12-
php: [8.1, 8.0]
12+
php: [7.4, 8.1, 8.0]
1313
stability: [prefer-lowest, prefer-stable]
1414

1515
name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,40 @@ Shiki::highlight(
117117

118118
You can then target these classes in your own CSS to color these lines how you want.
119119

120+
## PHP 7.4 support
121+
122+
Shiki has a nice and easy syntax in combination with at least PHP 8.
123+
124+
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.
125+
126+
```php
127+
// As reference
128+
highlight(
129+
string $code,
130+
?string $language = 'php',
131+
?string $theme = 'nord',
132+
?array $highlightLines = [],
133+
?array $addLines = [],
134+
?array $deleteLines = [],
135+
?array $focusLines = []
136+
)
137+
138+
// Instead of PHP 8 syntax
139+
Shiki::highlight(
140+
code: $code,
141+
language: 'php',
142+
deleteLines: [1],
143+
);
144+
145+
// You need to follow PHP 7.4 syntax
146+
Shiki::highlight(
147+
$code,
148+
'php',
149+
null,
150+
null,
151+
[1],
152+
);
153+
120154
## Determining available languages
121155

122156
To get an array with [all languages that Shiki supports](https://github.com/shikijs/shiki/blob/master/docs/languages.md), call `getAvailableLanguages`

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121
],
2222
"require": {
23-
"php": "^8.0"
23+
"php": "^7.4|^8.0"
2424
},
2525
"require-dev": {
2626
"friendsofphp/php-cs-fixer": "^v3.0",
@@ -45,7 +45,10 @@
4545
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
4646
},
4747
"config": {
48-
"sort-packages": true
48+
"sort-packages": true,
49+
"allow-plugins": {
50+
"pestphp/pest-plugin": true
51+
}
4952
},
5053
"minimum-stability": "dev",
5154
"prefer-stable": true

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Shiki.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
class Shiki
1010
{
11+
protected string $defaultTheme;
12+
1113
private static ?string $customWorkingDirPath = null;
1214

1315
public static function setCustomWorkingDirPath(?string $path)
@@ -17,18 +19,21 @@ public static function setCustomWorkingDirPath(?string $path)
1719

1820
public static function highlight(
1921
string $code,
20-
string $language = 'php',
21-
string $theme = 'nord',
22-
array $highlightLines = [],
23-
array $addLines = [],
24-
array $deleteLines = [],
25-
array $focusLines = [],
22+
?string $language = null,
23+
?string $theme = null,
24+
?array $highlightLines = null,
25+
?array $addLines = null,
26+
?array $deleteLines = null,
27+
?array $focusLines = null
2628
): string {
29+
$language = $language ?? 'php';
30+
$theme = $theme ?? 'nord';
31+
2732
return (new static())->highlightCode($code, $language, $theme, [
28-
'highlightLines' => $highlightLines,
29-
'addLines' => $addLines,
30-
'deleteLines' => $deleteLines,
31-
'focusLines' => $focusLines,
33+
'highlightLines' => $highlightLines ?? [],
34+
'addLines' => $addLines ?? [],
35+
'deleteLines' => $deleteLines ?? [],
36+
'focusLines' => $focusLines ?? [],
3237
]);
3338
}
3439

@@ -48,9 +53,9 @@ public function getAvailableLanguages(): array
4853
return $languages;
4954
}
5055

51-
public function __construct(
52-
protected string $defaultTheme = 'nord'
53-
) {
56+
public function __construct(string $defaultTheme = 'nord')
57+
{
58+
$this->defaultTheme = $defaultTheme;
5459
}
5560

5661
public function getAvailableThemes(): array
@@ -98,9 +103,9 @@ protected function callShiki(...$arguments): string
98103
];
99104

100105
$process = new Process(
101-
command: $command,
102-
cwd: $this->getWorkingDirPath(),
103-
timeout: null,
106+
$command,
107+
$this->getWorkingDirPath(),
108+
null,
104109
);
105110

106111
$process->run();

tests/ShikiCustomRenderTest.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
it('can highlight blade', function () {
2525
$code = '@if(true) {{ "Hello world" }} @endif';
2626

27-
$highlightedCode = Shiki::highlight($code, language: 'blade');
27+
$highlightedCode = Shiki::highlight($code, 'blade');
2828

2929
assertMatchesSnapshot($highlightedCode);
3030
});
@@ -36,15 +36,15 @@
3636
@endif
3737
blade;
3838

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

4141
assertMatchesSnapshot($highlightedCode);
4242
});
4343

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

47-
$highlightedCode = Shiki::highlight($code, language: 'antlers');
47+
$highlightedCode = Shiki::highlight($code, 'antlers');
4848

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

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

6565
assertMatchesSnapshot($highlightedCode);
6666
});
@@ -72,64 +72,61 @@
7272
return null;
7373
";
7474

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

7777
assertMatchesSnapshot($highlightedCode);
7878
});
7979

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

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

8585
assertMatchesSnapshot($highlightedCode);
8686
});
8787

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

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

9393
assertMatchesSnapshot($highlightedCode);
9494
});
9595

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

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

101101
assertMatchesSnapshot($highlightedCode);
102102
});
103103

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

107-
$highlightedCode = Shiki::highlight(
108-
$code,
109-
theme: __DIR__ . '/testfiles/ayu-light.json'
110-
);
107+
$highlightedCode = Shiki::highlight($code, null, __DIR__ . '/testfiles/ayu-light.json');
111108

112109
assertMatchesSnapshot($highlightedCode);
113110
});
114111

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

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

120117
assertMatchesSnapshot($highlightedCode);
121118
});
122119

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

126-
Shiki::highlight($code, theme: 'invalid-theme');
123+
Shiki::highlight($code, null, 'invalid-theme');
127124
})->throws(Exception::class);
128125

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

132-
Shiki::highlight($code, language: 'invalid-language');
129+
Shiki::highlight($code, 'invalid-language');
133130
})->throws(Exception::class);
134131

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

tests/ShikiTest.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
it('can highlight blade', function () {
2222
$code = '@if(true) {{ "Hello world" }} @endif';
2323

24-
$highlightedCode = Shiki::highlight($code, language: 'blade');
24+
$highlightedCode = Shiki::highlight($code, 'blade');
2525

2626
assertMatchesSnapshot($highlightedCode);
2727
});
@@ -33,15 +33,15 @@
3333
@endif
3434
blade;
3535

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

3838
assertMatchesSnapshot($highlightedCode);
3939
});
4040

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

44-
$highlightedCode = Shiki::highlight($code, language: 'antlers');
44+
$highlightedCode = Shiki::highlight($code, 'antlers');
4545

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

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

6262
assertMatchesSnapshot($highlightedCode);
6363
});
@@ -69,64 +69,61 @@
6969
return null;
7070
";
7171

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

7474
assertMatchesSnapshot($highlightedCode);
7575
});
7676

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

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

8282
assertMatchesSnapshot($highlightedCode);
8383
});
8484

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

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

9090
assertMatchesSnapshot($highlightedCode);
9191
});
9292

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

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

9898
assertMatchesSnapshot($highlightedCode);
9999
});
100100

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

104-
$highlightedCode = Shiki::highlight(
105-
$code,
106-
theme: __DIR__ . '/testfiles/ayu-light.json'
107-
);
104+
$highlightedCode = Shiki::highlight($code, null, __DIR__ . '/testfiles/ayu-light.json');
108105

109106
assertMatchesSnapshot($highlightedCode);
110107
});
111108

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

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

117114
assertMatchesSnapshot($highlightedCode);
118115
});
119116

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

123-
Shiki::highlight($code, theme: 'invalid-theme');
120+
Shiki::highlight($code, 'php', 'invalid-theme');
124121
})->throws(Exception::class);
125122

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

129-
Shiki::highlight($code, language: 'invalid-language');
126+
Shiki::highlight($code, 'invalid-language');
130127
})->throws(Exception::class);
131128

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

0 commit comments

Comments
 (0)