-
Notifications
You must be signed in to change notification settings - Fork 0
/
languageChooser.js
56 lines (48 loc) · 1.51 KB
/
languageChooser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const highlightJS = require('highlight.js');
// codemirror language -> highlight.js language map
CODEMIRROR_TO_HL = {
"clike": "cpp",
"commonlisp": "lisp"
}
// get the code_mirror language names by extension
const ghLanguages = require('./languages.json');
const langKeys = {};
const extensionToCMLanguage = {};
Object.keys(ghLanguages).forEach(function(key) {
let lang = ghLanguages[key];
if (lang.codemirror_mode) {
langKeys[lang.codemirror_mode] = true;
if (lang.extensions) {
lang.extensions.forEach(function(extension) {
extensionToCMLanguage[extension] = lang.codemirror_mode;
})
}
}
});
// array of CM language names
const cmLanguages = Object.keys(langKeys);
// array of HL language names
const hlLangs = highlightJS.listLanguages();
/**
* @param {String} filename
* @returns {String|undefined} The highlight.js language extension, or undefined if unknown
*/
function chooseLanguage(filename) {
const splitFn = filename.split(".");
let extension = splitFn[splitFn.length - 1];
extension = "." + extension;
const cmLanguageByExtension = extensionToCMLanguage[extension];
if (!cmLanguageByExtension) {
return;
}
if (CODEMIRROR_TO_HL[cmLanguageByExtension]) {
return CODEMIRROR_TO_HL[cmLanguageByExtension];
}
else if (hlLangs.indexOf(cmLanguageByExtension) !== -1) {
return cmLanguageByExtension;
}
}
module.exports = chooseLanguage;
// for debug purposes
chooseLanguage.extensionToCMLanguage = extensionToCMLanguage;
chooseLanguage.hlLangs = hlLangs;