-
Notifications
You must be signed in to change notification settings - Fork 3
/
element.js
33 lines (27 loc) · 968 Bytes
/
element.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
window.onload = function() {
var codeElements = document.querySelectorAll('code')
codeElements.forEach(function(codeElement, i) {
formatCode(codeElement)
})
var xmp = document.querySelectorAll('xmp')
xmp.forEach(function(xmp, i) {
var pattern = xmp.innerHTML.match(/\s*\n[\t\s]*/)
var newHtml = xmp.innerHTML.replace(new RegExp(pattern, "g"),'\n')
var code = document.createElement('code')
if (typeof hljs !== 'undefined') {
code.innerHTML = hljs.highlightAuto(newHtml).value
xmp.parentNode.insertBefore(code, xmp.nextSibling)
xmp.parentNode.removeChild(xmp)
} else {
xmp.innerHTML = newHtml
}
})
}
function formatCode(obj) {
if(obj.parentNode.nodeName !== 'P') {
var pattern = obj.innerHTML.match(/\s*\n[\t\s]*/)
obj.innerHTML = obj.innerHTML.replace(new RegExp(pattern, "g"),'\n')
if (typeof hljs !== 'undefined')
obj.innerHTML = hljs.highlightAuto(obj.innerHTML).value
}
}