Skip to content

Commit 5d4bde3

Browse files
committed
Add autodetect plugin
1 parent 3d38a64 commit 5d4bde3

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

code-input.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ var codeInput = {
5757
// Update code
5858
result_element.innerHTML = this.escape_html(text);
5959
this.plugin_evt("beforeHighlight");
60-
61-
if(this.autodetect) { // Autodetection
62-
result_element.className = ""; // CODE
63-
result_element.parentElement.className = ""; // PRE
64-
}
60+
6561
// Syntax Highlight
6662
if(this.template.includeCodeInputInHighlightFunc) this.template.highlight(result_element, this);
6763
else this.template.highlight(result_element);

plugins/autodetect.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
codeInput.plugins.Test = class extends codeInput.Plugin {
2-
/* Runs before code is highlighted; Params: codeInput element) */
1+
/**
2+
* Autodetect the language live and change the `lang` attribute using the syntax highlighter's
3+
* autodetect capabilities. Works with highlight.js.
4+
*/
5+
codeInput.plugins.Autodetect = class extends codeInput.Plugin {
6+
/* Remove previous language class */
37
beforeHighlight(codeInput) {
4-
console.log(codeInput, "before highlight");
8+
let result_element = codeInput.querySelector("pre code");
9+
result_element.className = ""; // CODE
10+
result_element.parentElement.className = ""; // PRE
511
}
6-
/* Runs after code is highlighted; Params: codeInput element) */
12+
/* Get new language class and set `lang` attribute */
713
afterHighlight(codeInput) {
8-
console.log(codeInput, "after highlight");
14+
let result_element = codeInput.querySelector("pre code");
15+
let lang_class = result_element.className || result_element.parentElement.className;
16+
let lang = lang_class.match(/lang(\w|-)*/i)[0]; // Get word starting with lang...; Get outer bracket
17+
lang = lang.split("-")[1];
18+
codeInput.setAttribute("lang", lang);
919
}
10-
/* Runs before elements are added into a `code-input`; Params: codeInput element) */
11-
beforeElementsAdded(codeInput) {
12-
console.log(codeInput, "before elements added");
13-
}
14-
/* Runs after elements are added into a `code-input` (useful for adding events to the textarea); Params: codeInput element) */
15-
afterElementsAdded(codeInput) {
16-
console.log(codeInput, "after elements added");
17-
}
18-
/* Runs when an attribute of a `code-input` is changed (you must add the attribute name to observedAttributes); Params: codeInput element, name attribute name, oldValue previous value of attribute, newValue changed value of attribute) */
19-
attributeChanged(codeInput, name, oldValue, newValue) {
20-
if(name == "testattr") {
21-
console.log(codeInput, "testattr:", oldValue, ">", newValue);
22-
}
23-
}
24-
observedAttributes = ["testattr"]
20+
observedAttributes = []
2521
}

0 commit comments

Comments
 (0)