Skip to content

Commit

Permalink
read bundled languages and add support for aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
s3ththompson committed Jul 24, 2024
1 parent c18ea45 commit f160017
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
17 changes: 16 additions & 1 deletion bin/shiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,22 @@ 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;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Shiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public function getAvailableThemes(): array

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

$aliases = json_decode($shikiResult, true);

return in_array($language, $aliases);
}

public function themeIsAvailable(string $theme): bool
Expand Down
6 changes: 6 additions & 0 deletions tests/ShikiCustomRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
$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 () {
Expand All @@ -154,4 +157,7 @@

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();
});
6 changes: 6 additions & 0 deletions tests/ShikiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
$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 () {
Expand All @@ -151,4 +154,7 @@

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();
});
17 changes: 16 additions & 1 deletion tests/testfiles/alt-bin/shiki.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,22 @@ 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;
}

Expand Down

0 comments on commit f160017

Please sign in to comment.