Merge pull request #3708 from fbricon/fix-syntaxhighlighting

Fix syntax highlighting for .class files
This commit is contained in:
Nate Sesti 2025-02-05 13:06:22 -08:00 • committed by GitHub
commit 956e2a9757
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 5 deletions

View file

@ -207,7 +207,7 @@ World
it("should handle strings with only whitespace", () => {
const result = dedent`
`;
expect(result).toBe("");
});
@ -249,9 +249,9 @@ World
it("should handle a string with only one non-empty line", () => {
const result = dedent`
Hello World!
`;
expect(result).toBe("Hello World!");
});
@ -373,11 +373,16 @@ describe("getMarkdownLanguageTagForFile", () => {
expect(getMarkdownLanguageTagForFile("test.py")).toBe("python");
expect(getMarkdownLanguageTagForFile("test.tsx")).toBe("tsx");
expect(getMarkdownLanguageTagForFile("test.java")).toBe("java");
expect(getMarkdownLanguageTagForFile("test.class")).toBe("java");
expect(getMarkdownLanguageTagForFile("test.md")).toBe("markdown");
expect(getMarkdownLanguageTagForFile("test.sh")).toBe("shell");
expect(getMarkdownLanguageTagForFile("test.sql")).toBe("sql");
});
it("should sanitize ranges from the extension", () => {
expect(getMarkdownLanguageTagForFile("test.java (1-5)")).toBe("java");
});
it("should return the extension if not in the known list", () => {
expect(getMarkdownLanguageTagForFile("file.unknownext")).toBe("unknownext");
});

View file

@ -74,6 +74,7 @@ export function getMarkdownLanguageTagForFile(filepath: string): string {
tsx: "tsx",
ts: "typescript",
java: "java",
class:"java", //.class files decompile to Java
go: "go",
rb: "ruby",
rs: "rust",
@ -96,17 +97,28 @@ export function getMarkdownLanguageTagForFile(filepath: string): string {
ps1: "powershell",
};
const ext = filepath.split(".").pop();
const ext = sanitizeExtension(filepath.split(".").pop());
return ext ? (extToLangMap[ext] ?? ext) : "";
}
function sanitizeExtension(ext?: string): string|undefined {
if (ext) {
//ignore ranges in extension eg. "java (11-23)"
const match = ext.match(/^(\S+)\s*(\(.*\))?$/);
if (match) {
ext = match[1];
}
}
return ext;
}
export function copyOf(obj: any): any {
if (obj === null || obj === undefined) {
return obj;
}
return JSON.parse(JSON.stringify(obj));
}
``;
export function deduplicateArray<T>(
array: T[],