1+ /**
2+ * Language detection utility for syntax highlighting
3+ * Maps file extensions to Monaco Editor and highlight.js language identifiers
4+ */
5+
6+ /**
7+ * Get the appropriate language identifier for syntax highlighting
8+ * @param {string } filename - The filename or file path
9+ * @returns {Object } Object containing language identifiers for both Monaco and highlight.js
10+ */
11+ function detectLanguage ( filename ) {
12+ if ( ! filename || typeof filename !== 'string' ) {
13+ return {
14+ monaco : 'javascript' ,
15+ highlightjs : 'javascript'
16+ } ;
17+ }
18+
19+ // Extract file extension
20+ const extension = filename . split ( '.' ) . pop ( ) . toLowerCase ( ) ;
21+
22+ // Map extensions to language identifiers
23+ const languageMap = {
24+ // JavaScript and related
25+ 'js' : { monaco : 'javascript' , highlightjs : 'javascript' } ,
26+ 'mjs' : { monaco : 'javascript' , highlightjs : 'javascript' } ,
27+ 'jsx' : { monaco : 'javascript' , highlightjs : 'javascript' } ,
28+
29+ // TypeScript
30+ 'ts' : { monaco : 'typescript' , highlightjs : 'typescript' } ,
31+ 'tsx' : { monaco : 'typescript' , highlightjs : 'typescript' } ,
32+
33+ // Data formats
34+ 'json' : { monaco : 'json' , highlightjs : 'json' } ,
35+ 'jsonc' : { monaco : 'json' , highlightjs : 'json' } ,
36+
37+ // Configuration files
38+ 'yaml' : { monaco : 'yaml' , highlightjs : 'yaml' } ,
39+ 'yml' : { monaco : 'yaml' , highlightjs : 'yaml' } ,
40+ 'toml' : { monaco : 'ini' , highlightjs : 'ini' } ,
41+ 'ini' : { monaco : 'ini' , highlightjs : 'ini' } ,
42+ 'conf' : { monaco : 'ini' , highlightjs : 'ini' } ,
43+
44+ // Markup and web
45+ 'html' : { monaco : 'html' , highlightjs : 'html' } ,
46+ 'htm' : { monaco : 'html' , highlightjs : 'html' } ,
47+ 'xml' : { monaco : 'xml' , highlightjs : 'xml' } ,
48+ 'css' : { monaco : 'css' , highlightjs : 'css' } ,
49+ 'scss' : { monaco : 'scss' , highlightjs : 'scss' } ,
50+ 'sass' : { monaco : 'sass' , highlightjs : 'sass' } ,
51+ 'less' : { monaco : 'less' , highlightjs : 'less' } ,
52+
53+ // Documentation
54+ 'md' : { monaco : 'markdown' , highlightjs : 'markdown' } ,
55+ 'markdown' : { monaco : 'markdown' , highlightjs : 'markdown' } ,
56+ 'txt' : { monaco : 'plaintext' , highlightjs : 'plaintext' } ,
57+
58+ // Testing frameworks
59+ 'feature' : { monaco : 'gherkin' , highlightjs : 'gherkin' } ,
60+ 'gherkin' : { monaco : 'gherkin' , highlightjs : 'gherkin' } ,
61+
62+ // Shell and scripts
63+ 'sh' : { monaco : 'shell' , highlightjs : 'bash' } ,
64+ 'bash' : { monaco : 'shell' , highlightjs : 'bash' } ,
65+ 'zsh' : { monaco : 'shell' , highlightjs : 'bash' } ,
66+ 'fish' : { monaco : 'shell' , highlightjs : 'bash' } ,
67+ 'ps1' : { monaco : 'powershell' , highlightjs : 'powershell' } ,
68+
69+ // Other programming languages
70+ 'py' : { monaco : 'python' , highlightjs : 'python' } ,
71+ 'rb' : { monaco : 'ruby' , highlightjs : 'ruby' } ,
72+ 'php' : { monaco : 'php' , highlightjs : 'php' } ,
73+ 'java' : { monaco : 'java' , highlightjs : 'java' } ,
74+ 'c' : { monaco : 'c' , highlightjs : 'c' } ,
75+ 'cpp' : { monaco : 'cpp' , highlightjs : 'cpp' } ,
76+ 'cs' : { monaco : 'csharp' , highlightjs : 'csharp' } ,
77+ 'go' : { monaco : 'go' , highlightjs : 'go' } ,
78+ 'rs' : { monaco : 'rust' , highlightjs : 'rust' } ,
79+ 'swift' : { monaco : 'swift' , highlightjs : 'swift' } ,
80+ 'kt' : { monaco : 'kotlin' , highlightjs : 'kotlin' } ,
81+ 'scala' : { monaco : 'scala' , highlightjs : 'scala' } ,
82+
83+ // SQL
84+ 'sql' : { monaco : 'sql' , highlightjs : 'sql' }
85+ } ;
86+
87+ // Return mapped language or default to JavaScript
88+ return languageMap [ extension ] || {
89+ monaco : 'javascript' ,
90+ highlightjs : 'javascript'
91+ } ;
92+ }
93+
94+ /**
95+ * Get user-friendly language name for display
96+ * @param {string } filename - The filename or file path
97+ * @returns {string } Human-readable language name
98+ */
99+ function getLanguageDisplayName ( filename ) {
100+ const languages = detectLanguage ( filename ) ;
101+
102+ const displayNames = {
103+ 'javascript' : 'JavaScript' ,
104+ 'typescript' : 'TypeScript' ,
105+ 'json' : 'JSON' ,
106+ 'yaml' : 'YAML' ,
107+ 'html' : 'HTML' ,
108+ 'css' : 'CSS' ,
109+ 'scss' : 'SCSS' ,
110+ 'sass' : 'Sass' ,
111+ 'less' : 'Less' ,
112+ 'markdown' : 'Markdown' ,
113+ 'gherkin' : 'Gherkin (BDD)' ,
114+ 'shell' : 'Shell Script' ,
115+ 'bash' : 'Bash' ,
116+ 'powershell' : 'PowerShell' ,
117+ 'python' : 'Python' ,
118+ 'ruby' : 'Ruby' ,
119+ 'php' : 'PHP' ,
120+ 'java' : 'Java' ,
121+ 'c' : 'C' ,
122+ 'cpp' : 'C++' ,
123+ 'csharp' : 'C#' ,
124+ 'go' : 'Go' ,
125+ 'rust' : 'Rust' ,
126+ 'swift' : 'Swift' ,
127+ 'kotlin' : 'Kotlin' ,
128+ 'scala' : 'Scala' ,
129+ 'sql' : 'SQL' ,
130+ 'ini' : 'Configuration' ,
131+ 'xml' : 'XML' ,
132+ 'plaintext' : 'Plain Text'
133+ } ;
134+
135+ return displayNames [ languages . monaco ] || 'JavaScript' ;
136+ }
137+
138+ /**
139+ * Check if a language is supported by Monaco Editor
140+ * @param {string } language - Monaco language identifier
141+ * @returns {boolean } Whether the language is supported
142+ */
143+ function isMonacoLanguageSupported ( language ) {
144+ // Monaco Editor built-in languages
145+ const supportedLanguages = [
146+ 'javascript' , 'typescript' , 'json' , 'html' , 'css' , 'scss' , 'less' ,
147+ 'markdown' , 'yaml' , 'xml' , 'shell' , 'powershell' , 'python' , 'ruby' ,
148+ 'php' , 'java' , 'c' , 'cpp' , 'csharp' , 'go' , 'rust' , 'swift' , 'kotlin' ,
149+ 'scala' , 'sql' , 'ini' , 'plaintext'
150+ ] ;
151+
152+ return supportedLanguages . includes ( language ) ;
153+ }
154+
155+ // Export for CommonJS (Node.js tests) and ES modules (Vue.js components)
156+ module . exports = {
157+ detectLanguage,
158+ getLanguageDisplayName,
159+ isMonacoLanguageSupported
160+ } ;
0 commit comments