Skip to content

Commit

Permalink
fix: Support highlighting language aliases (#1673)
Browse files Browse the repository at this point in the history
* Fetches language aliases from shiki, fixes #1672
* test cases
* fix formatting with prettier --write .
  • Loading branch information
StoneCypher committed Aug 29, 2021
1 parent 56f371c commit 3c2f3fa
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/lib/utils/highlighter.ts
Expand Up @@ -2,16 +2,29 @@ import { ok as assert } from "assert";
import * as shiki from "shiki";
import { unique } from "./array";

// This is needed because Shiki includes some "fake" languages
// ts / js are expected by users to be equivalent to typescript / javascript

const aliases = new Map<string, string>([
["ts", "typescript"],
["js", "javascript"],
["bash", "shellscript"],
["sh", "shellscript"],
["shell", "shellscript"],
]);
type MapKey = [string, string[] | undefined];
type RMapKey = [string, string];

function rM_zipIdWithAliases(bl: shiki.ILanguageRegistration): MapKey {
return [bl.id, bl.aliases];
}

function rM_nonEmptyRow(row: MapKey): boolean {
return Boolean(row[1]); // row is empty if second element of a mapkey (aliases) is undefined
}

function rM_remapAliasToId([base, al]: MapKey): RMapKey[] {
return (al || []).map((a) => [a, base]);
}

const reverseMapping: RMapKey[][] = [
[["text", "text"]],
...shiki.BUNDLED_LANGUAGES.map(rM_zipIdWithAliases)
.filter(rM_nonEmptyRow)
.map(rM_remapAliasToId),
];

const aliases = new Map<string, string>(reverseMapping.flat());

const supportedLanguages = unique([
"text",
Expand Down
63 changes: 63 additions & 0 deletions src/test/utils/languageAliases.test.ts
@@ -0,0 +1,63 @@
import { deepStrictEqual as equal } from "assert";
import { isSupportedLanguage } from "../../lib/utils/highlighter";

describe("Language aliases", () => {
describe("Original language aliases", () => {
it("js is present", () => {
equal(isSupportedLanguage("js"), true);
});
it("ts is present", () => {
equal(isSupportedLanguage("ts"), true);
});
it("sh is present", () => {
equal(isSupportedLanguage("sh"), true);
});
it("bash is present", () => {
equal(isSupportedLanguage("bash"), true);
});
it("zsh is present", () => {
equal(isSupportedLanguage("zsh"), true);
});
it("text is present", () => {
equal(isSupportedLanguage("text"), true);
});
});

// non-exhaustive, just shows that some of the uncovered ones are now covered
describe("Extended language aliases", () => {
it("rb is present", () => {
equal(isSupportedLanguage("rb"), true);
});
it("py is present", () => {
equal(isSupportedLanguage("py"), true);
});
it("jssm is present", () => {
equal(isSupportedLanguage("jssm"), true);
});
});

// non-exhaustive, just shows that the basic names are upheld too
describe("Basic ids", () => {
it("ruby is present", () => {
equal(isSupportedLanguage("ruby"), true);
});
it("python is present", () => {
equal(isSupportedLanguage("python"), true);
});
it("javascript is present", () => {
equal(isSupportedLanguage("javascript"), true);
});
it("typescript is present", () => {
equal(isSupportedLanguage("typescript"), true);
});
it("fsl is present", () => {
equal(isSupportedLanguage("fsl"), true);
});
});

describe("Improper language aliases", () => {
it("js2 is not present", () => {
equal(isSupportedLanguage("js2"), false);
});
});
});

0 comments on commit 3c2f3fa

Please sign in to comment.