62159
Sy
symb / files
symb / internal / highlight / language.go
1.8 KB
1package highlight 2 3import ( 4 "path/filepath" 5 "strings" 6) 7 8// DetectLanguage returns the Chroma language identifier based on file extension. 9func DetectLanguage(path string) string { 10 // Map common extensions to Chroma language identifiers 11 languageMap := map[string]string{ 12 ".go": "go", 13 ".py": "python", 14 ".js": "javascript", 15 ".ts": "typescript", 16 ".jsx": "jsx", 17 ".tsx": "tsx", 18 ".java": "java", 19 ".c": "c", 20 ".cpp": "cpp", 21 ".cc": "cpp", 22 ".h": "c", 23 ".hpp": "cpp", 24 ".cs": "csharp", 25 ".rb": "ruby", 26 ".php": "php", 27 ".rs": "rust", 28 ".swift": "swift", 29 ".kt": "kotlin", 30 ".scala": "scala", 31 ".sh": "bash", 32 ".bash": "bash", 33 ".zsh": "zsh", 34 ".fish": "fish", 35 ".ps1": "powershell", 36 ".r": "r", 37 ".sql": "sql", 38 ".html": "html", 39 ".htm": "html", 40 ".xml": "xml", 41 ".css": "css", 42 ".scss": "scss", 43 ".sass": "sass", 44 ".less": "less", 45 ".json": "json", 46 ".yaml": "yaml", 47 ".yml": "yaml", 48 ".toml": "toml", 49 ".ini": "ini", 50 ".conf": "nginx", 51 ".md": "markdown", 52 ".markdown": "markdown", 53 ".tex": "tex", 54 ".vim": "vim", 55 ".lua": "lua", 56 ".perl": "perl", 57 ".pl": "perl", 58 ".dockerfile": "docker", 59 ".proto": "protobuf", 60 } 61 62 ext := strings.ToLower(filepath.Ext(path)) 63 if lang, ok := languageMap[ext]; ok { 64 return lang 65 } 66 67 // Check for specific filenames 68 base := strings.ToLower(filepath.Base(path)) 69 switch base { 70 case "dockerfile": 71 return "docker" 72 case "makefile": 73 return "make" 74 case "gemfile": 75 return "ruby" 76 case "rakefile": 77 return "ruby" 78 } 79 80 return "text" // Default fallback 81}