Skip to content

Commit 3c2f3fa

Browse files
authored
fix: Support highlighting language aliases (#1673)
* Fetches language aliases from shiki, fixes #1672 * test cases * fix formatting with prettier --write .
1 parent 56f371c commit 3c2f3fa

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

src/lib/utils/highlighter.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@ import { ok as assert } from "assert";
22
import * as shiki from "shiki";
33
import { unique } from "./array";
44

5-
// This is needed because Shiki includes some "fake" languages
6-
// ts / js are expected by users to be equivalent to typescript / javascript
7-
8-
const aliases = new Map<string, string>([
9-
["ts", "typescript"],
10-
["js", "javascript"],
11-
["bash", "shellscript"],
12-
["sh", "shellscript"],
13-
["shell", "shellscript"],
14-
]);
5+
type MapKey = [string, string[] | undefined];
6+
type RMapKey = [string, string];
7+
8+
function rM_zipIdWithAliases(bl: shiki.ILanguageRegistration): MapKey {
9+
return [bl.id, bl.aliases];
10+
}
11+
12+
function rM_nonEmptyRow(row: MapKey): boolean {
13+
return Boolean(row[1]); // row is empty if second element of a mapkey (aliases) is undefined
14+
}
15+
16+
function rM_remapAliasToId([base, al]: MapKey): RMapKey[] {
17+
return (al || []).map((a) => [a, base]);
18+
}
19+
20+
const reverseMapping: RMapKey[][] = [
21+
[["text", "text"]],
22+
...shiki.BUNDLED_LANGUAGES.map(rM_zipIdWithAliases)
23+
.filter(rM_nonEmptyRow)
24+
.map(rM_remapAliasToId),
25+
];
26+
27+
const aliases = new Map<string, string>(reverseMapping.flat());
1528

1629
const supportedLanguages = unique([
1730
"text",
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { deepStrictEqual as equal } from "assert";
2+
import { isSupportedLanguage } from "../../lib/utils/highlighter";
3+
4+
describe("Language aliases", () => {
5+
describe("Original language aliases", () => {
6+
it("js is present", () => {
7+
equal(isSupportedLanguage("js"), true);
8+
});
9+
it("ts is present", () => {
10+
equal(isSupportedLanguage("ts"), true);
11+
});
12+
it("sh is present", () => {
13+
equal(isSupportedLanguage("sh"), true);
14+
});
15+
it("bash is present", () => {
16+
equal(isSupportedLanguage("bash"), true);
17+
});
18+
it("zsh is present", () => {
19+
equal(isSupportedLanguage("zsh"), true);
20+
});
21+
it("text is present", () => {
22+
equal(isSupportedLanguage("text"), true);
23+
});
24+
});
25+
26+
// non-exhaustive, just shows that some of the uncovered ones are now covered
27+
describe("Extended language aliases", () => {
28+
it("rb is present", () => {
29+
equal(isSupportedLanguage("rb"), true);
30+
});
31+
it("py is present", () => {
32+
equal(isSupportedLanguage("py"), true);
33+
});
34+
it("jssm is present", () => {
35+
equal(isSupportedLanguage("jssm"), true);
36+
});
37+
});
38+
39+
// non-exhaustive, just shows that the basic names are upheld too
40+
describe("Basic ids", () => {
41+
it("ruby is present", () => {
42+
equal(isSupportedLanguage("ruby"), true);
43+
});
44+
it("python is present", () => {
45+
equal(isSupportedLanguage("python"), true);
46+
});
47+
it("javascript is present", () => {
48+
equal(isSupportedLanguage("javascript"), true);
49+
});
50+
it("typescript is present", () => {
51+
equal(isSupportedLanguage("typescript"), true);
52+
});
53+
it("fsl is present", () => {
54+
equal(isSupportedLanguage("fsl"), true);
55+
});
56+
});
57+
58+
describe("Improper language aliases", () => {
59+
it("js2 is not present", () => {
60+
equal(isSupportedLanguage("js2"), false);
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)