diff --git a/core/util/index.test.ts b/core/util/index.test.ts index 74ee3ccc86..fc48a49b27 100644 --- a/core/util/index.test.ts +++ b/core/util/index.test.ts @@ -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"); }); diff --git a/core/util/index.ts b/core/util/index.ts index 04dc6d5675..4cb0f4920e 100644 --- a/core/util/index.ts +++ b/core/util/index.ts @@ -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( array: T[],