Skip to content

Commit dc2305e

Browse files
Re-add getAvailableLanguages, getAvailableThemes, languageIsAvailable and themeIsAvailable (#23)
* add symfony/process dependency * Revert "Remove methods" This reverts commit cc1e021. * read bundled languages and add support for aliases * read bundled themes * fix symfony/process version range --------- Co-authored-by: Rias <[email protected]>
1 parent 24ff797 commit dc2305e

File tree

6 files changed

+139
-3
lines changed

6 files changed

+139
-3
lines changed

bin/shiki.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ async function main(args) {
3737

3838
if (!customLanguages[language]) await highlighter.loadLanguage(language);
3939

40+
if (args[0] === 'languages') {
41+
process.stdout.write(
42+
JSON.stringify([
43+
...Object.keys(shiki.bundledLanguagesBase),
44+
...Object.keys(customLanguages),
45+
])
46+
);
47+
return;
48+
}
49+
50+
if (args[0] === 'aliases') {
51+
process.stdout.write(
52+
JSON.stringify([
53+
...Object.keys(shiki.bundledLanguages),
54+
...Object.keys(customLanguages),
55+
])
56+
);
57+
return;
58+
}
59+
60+
if (args[0] === 'themes') {
61+
process.stdout.write(JSON.stringify(Object.keys(shiki.bundledThemes)));
62+
return;
63+
}
64+
4065
const { theme: theme$ } = highlighter.setTheme(theme)
4166

4267
const result = highlighter.codeToTokens(args[0], {

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"require": {
2323
"php": "^7.4|^8.0",
2424
"ext-json": "*",
25-
"symfony/process": "^5.0|^6.0|^7.0"
25+
"symfony/process": "^5.4|^6.4|^7.1"
2626
},
2727
"require-dev": {
2828
"friendsofphp/php-cs-fixer": "^v3.0",

src/Shiki.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,43 @@ public static function highlight(
3737
]);
3838
}
3939

40+
public function getAvailableLanguages(): array
41+
{
42+
$shikiResult = $this->callShiki('languages');
43+
44+
$languages = json_decode($shikiResult, true);
45+
46+
sort($languages);
47+
48+
return $languages;
49+
}
50+
4051
public function __construct(string $defaultTheme = 'nord')
4152
{
4253
$this->defaultTheme = $defaultTheme;
4354
}
4455

56+
public function getAvailableThemes(): array
57+
{
58+
$shikiResult = $this->callShiki('themes');
59+
60+
return json_decode($shikiResult, true);
61+
}
62+
63+
public function languageIsAvailable(string $language): bool
64+
{
65+
$shikiResult = $this->callShiki('aliases');
66+
67+
$aliases = json_decode($shikiResult, true);
68+
69+
return in_array($language, $aliases);
70+
}
71+
72+
public function themeIsAvailable(string $theme): bool
73+
{
74+
return in_array($theme, $this->getAvailableThemes());
75+
}
76+
4577
public function highlightCode(string $code, string $language, ?string $theme = null, ?array $options = []): string
4678
{
4779
$theme = $theme ?? $this->defaultTheme;

tests/ShikiCustomRenderTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,35 @@
129129

130130
Shiki::highlight($code, 'invalid-language');
131131
})->throws(Exception::class);
132+
133+
it('can get all available themes', function () {
134+
$availableThemes = (new Shiki())->getAvailableThemes();
135+
136+
expect($availableThemes)->not()->toBeEmpty();
137+
});
138+
139+
it('can get all available languages', function () {
140+
$availableLanguages = (new Shiki())->getAvailableLanguages();
141+
142+
expect($availableLanguages)->not()->toBeEmpty();
143+
expect($availableLanguages)->toContain('javascript');
144+
// should not include aliases
145+
expect($availableLanguages)->not()->toContain('js');
146+
});
147+
148+
it('can determine that a theme is available', function () {
149+
$shiki = (new Shiki());
150+
151+
expect($shiki->themeIsAvailable('nord'))->toBeTrue();
152+
expect($shiki->themeIsAvailable('non-existing-theme'))->toBeFalse();
153+
});
154+
155+
it('can determine that a language is available', function () {
156+
$shiki = (new Shiki());
157+
158+
expect($shiki->languageIsAvailable('php'))->toBeTrue();
159+
expect($shiki->languageIsAvailable('non-existing-language'))->toBeFalse();
160+
expect($shiki->languageIsAvailable('javascript'))->toBeTrue();
161+
// should match aliases
162+
expect($shiki->languageIsAvailable('js'))->toBeTrue();
163+
});

tests/ShikiTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,35 @@
126126

127127
Shiki::highlight($code, 'invalid-language');
128128
})->throws(Exception::class);
129+
130+
it('can get all available themes', function () {
131+
$availableThemes = (new Shiki())->getAvailableThemes();
132+
133+
expect($availableThemes)->not()->toBeEmpty();
134+
});
135+
136+
it('can get all available languages', function () {
137+
$availableLanguages = (new Shiki())->getAvailableLanguages();
138+
139+
expect($availableLanguages)->not()->toBeEmpty();
140+
expect($availableLanguages)->toContain('javascript');
141+
// should not include aliases
142+
expect($availableLanguages)->not()->toContain('js');
143+
});
144+
145+
it('can determine that a theme is available', function () {
146+
$shiki = (new Shiki());
147+
148+
expect($shiki->themeIsAvailable('nord'))->toBeTrue();
149+
expect($shiki->themeIsAvailable('non-existing-theme'))->toBeFalse();
150+
});
151+
152+
it('can determine that a language is available', function () {
153+
$shiki = (new Shiki());
154+
155+
expect($shiki->languageIsAvailable('php'))->toBeTrue();
156+
expect($shiki->languageIsAvailable('non-existing-language'))->toBeFalse();
157+
expect($shiki->languageIsAvailable('javascript'))->toBeTrue();
158+
// should match aliases
159+
expect($shiki->languageIsAvailable('js'))->toBeTrue();
160+
});

tests/testfiles/alt-bin/shiki.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,27 @@ async function main(args) {
3838
if (!customLanguages[language]) await highlighter.loadLanguage(language);
3939

4040
if (args[0] === 'languages') {
41-
process.stdout.write(JSON.stringify(highlighter.getLoadedLanguages()));
41+
process.stdout.write(
42+
JSON.stringify([
43+
...Object.keys(shiki.bundledLanguagesBase),
44+
...Object.keys(customLanguages),
45+
])
46+
);
47+
return;
48+
}
49+
50+
if (args[0] === 'aliases') {
51+
process.stdout.write(
52+
JSON.stringify([
53+
...Object.keys(shiki.bundledLanguages),
54+
...Object.keys(customLanguages),
55+
])
56+
);
4257
return;
4358
}
4459

4560
if (args[0] === 'themes') {
46-
process.stdout.write(JSON.stringify(highlighter.getLoadedThemes()));
61+
process.stdout.write(JSON.stringify(Object.keys(shiki.bundledThemes)));
4762
return;
4863
}
4964

0 commit comments

Comments
 (0)