forked from olton/metroui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-code.js
More file actions
43 lines (34 loc) · 879 Bytes
/
pre-code.js
File metadata and controls
43 lines (34 loc) · 879 Bytes
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
/**
* Copyright (c) 2014, Leon Sorokin
* All rights reserved. (MIT Licensed)
*
* preCode.js - painkiller for <pre><code> & <textarea>
*/
(function() {
function preCode(selector) {
var els = Array.prototype.slice.call(document.querySelectorAll(selector), 0);
els.forEach(function(el, idx, arr){
var txt = el.textContent
.replace(/^[\r\n]+/, "") // strip leading newline
.replace(/\s+$/g, "");
if (/^\S/gm.test(txt)) {
el.textContent = txt;
return;
}
var mat, str, re = /^[\t ]+/gm, len, min = 1e3;
while (mat = re.exec(txt)) {
len = mat[0].length;
if (len < min) {
min = len;
str = mat[0];
}
}
if (min == 1e3)
return;
el.textContent = txt.replace(new RegExp("^" + str, 'gm'), "");
});
}
document.addEventListener("DOMContentLoaded", function() {
preCode("pre code, textarea");
}, false);
})();