Skip to content

Commit

Permalink
Re-add getAvailableLanguages, getAvailableThemes, `languageIsAvai…
Browse files Browse the repository at this point in the history
…lable` 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]>
  • Loading branch information
s3ththompson and riasvdv authored Sep 29, 2024
1 parent 24ff797 commit dc2305e
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 3 deletions.
25 changes: 25 additions & 0 deletions bin/shiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ async function main(args) {

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

if (args[0] === 'languages') {
process.stdout.write(
JSON.stringify([
...Object.keys(shiki.bundledLanguagesBase),
...Object.keys(customLanguages),
])
);
return;
}

if (args[0] === 'aliases') {
process.stdout.write(
JSON.stringify([
...Object.keys(shiki.bundledLanguages),
...Object.keys(customLanguages),
])
);
return;
}

if (args[0] === 'themes') {
process.stdout.write(JSON.stringify(Object.keys(shiki.bundledThemes)));
return;
}

const { theme: theme$ } = highlighter.setTheme(theme)

const result = highlighter.codeToTokens(args[0], {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"php": "^7.4|^8.0",
"ext-json": "*",
"symfony/process": "^5.0|^6.0|^7.0"
"symfony/process": "^5.4|^6.4|^7.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^v3.0",
Expand Down
32 changes: 32 additions & 0 deletions src/Shiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,43 @@ public static function highlight(
]);
}

public function getAvailableLanguages(): array
{
$shikiResult = $this->callShiki('languages');

$languages = json_decode($shikiResult, true);

sort($languages);

return $languages;
}

public function __construct(string $defaultTheme = 'nord')
{
$this->defaultTheme = $defaultTheme;
}

public function getAvailableThemes(): array
{
$shikiResult = $this->callShiki('themes');

return json_decode($shikiResult, true);
}

public function languageIsAvailable(string $language): bool
{
$shikiResult = $this->callShiki('aliases');

$aliases = json_decode($shikiResult, true);

return in_array($language, $aliases);
}

public function themeIsAvailable(string $theme): bool
{
return in_array($theme, $this->getAvailableThemes());
}

public function highlightCode(string $code, string $language, ?string $theme = null, ?array $options = []): string
{
$theme = $theme ?? $this->defaultTheme;
Expand Down
32 changes: 32 additions & 0 deletions tests/ShikiCustomRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,35 @@

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

it('can get all available themes', function () {
$availableThemes = (new Shiki())->getAvailableThemes();

expect($availableThemes)->not()->toBeEmpty();
});

it('can get all available languages', function () {
$availableLanguages = (new Shiki())->getAvailableLanguages();

expect($availableLanguages)->not()->toBeEmpty();
expect($availableLanguages)->toContain('javascript');
// should not include aliases
expect($availableLanguages)->not()->toContain('js');
});

it('can determine that a theme is available', function () {
$shiki = (new Shiki());

expect($shiki->themeIsAvailable('nord'))->toBeTrue();
expect($shiki->themeIsAvailable('non-existing-theme'))->toBeFalse();
});

it('can determine that a language is available', function () {
$shiki = (new Shiki());

expect($shiki->languageIsAvailable('php'))->toBeTrue();
expect($shiki->languageIsAvailable('non-existing-language'))->toBeFalse();
expect($shiki->languageIsAvailable('javascript'))->toBeTrue();
// should match aliases
expect($shiki->languageIsAvailable('js'))->toBeTrue();
});
32 changes: 32 additions & 0 deletions tests/ShikiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,35 @@

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

it('can get all available themes', function () {
$availableThemes = (new Shiki())->getAvailableThemes();

expect($availableThemes)->not()->toBeEmpty();
});

it('can get all available languages', function () {
$availableLanguages = (new Shiki())->getAvailableLanguages();

expect($availableLanguages)->not()->toBeEmpty();
expect($availableLanguages)->toContain('javascript');
// should not include aliases
expect($availableLanguages)->not()->toContain('js');
});

it('can determine that a theme is available', function () {
$shiki = (new Shiki());

expect($shiki->themeIsAvailable('nord'))->toBeTrue();
expect($shiki->themeIsAvailable('non-existing-theme'))->toBeFalse();
});

it('can determine that a language is available', function () {
$shiki = (new Shiki());

expect($shiki->languageIsAvailable('php'))->toBeTrue();
expect($shiki->languageIsAvailable('non-existing-language'))->toBeFalse();
expect($shiki->languageIsAvailable('javascript'))->toBeTrue();
// should match aliases
expect($shiki->languageIsAvailable('js'))->toBeTrue();
});
19 changes: 17 additions & 2 deletions tests/testfiles/alt-bin/shiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,27 @@ async function main(args) {
if (!customLanguages[language]) await highlighter.loadLanguage(language);

if (args[0] === 'languages') {
process.stdout.write(JSON.stringify(highlighter.getLoadedLanguages()));
process.stdout.write(
JSON.stringify([
...Object.keys(shiki.bundledLanguagesBase),
...Object.keys(customLanguages),
])
);
return;
}

if (args[0] === 'aliases') {
process.stdout.write(
JSON.stringify([
...Object.keys(shiki.bundledLanguages),
...Object.keys(customLanguages),
])
);
return;
}

if (args[0] === 'themes') {
process.stdout.write(JSON.stringify(highlighter.getLoadedThemes()));
process.stdout.write(JSON.stringify(Object.keys(shiki.bundledThemes)));
return;
}

Expand Down

0 comments on commit dc2305e

Please sign in to comment.