-
Notifications
You must be signed in to change notification settings - Fork 24
/
autodetect.js
28 lines (28 loc) · 1.08 KB
/
autodetect.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
/**
* Autodetect the language live and change the `lang` attribute using the syntax highlighter's
* autodetect capabilities. Works with highlight.js only.
* Files: autodetect.js
*/
codeInput.plugins.Autodetect = class extends codeInput.Plugin {
constructor() {
super([]); // No observed attributes
}
/* Remove previous language class */
beforeHighlight(codeInput) {
let resultElement = codeInput.codeElement;
resultElement.className = ""; // CODE
resultElement.parentElement.className = ""; // PRE
}
/* Get new language class and set `language` attribute */
afterHighlight(codeInput) {
let langClass = codeInput.codeElement.className || codeInput.preElement.className;
let lang = langClass.match(/lang(\w|-)*/i)[0]; // Get word starting with lang...; Get outer bracket
lang = lang.split("-")[1];
if(lang == "undefined") {
codeInput.removeAttribute("language");
codeInput.removeAttribute("lang");
} else {
codeInput.setAttribute("language", lang);
}
}
}