|
|
@ -3,23 +3,14 @@ |
|
|
|
<script> |
|
|
|
<script> |
|
|
|
// TODO: this feels more like a mutation observer |
|
|
|
// TODO: this feels more like a mutation observer |
|
|
|
addEventListener('DOMContentLoaded', function () { |
|
|
|
addEventListener('DOMContentLoaded', function () { |
|
|
|
var hlbaseUri = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/"; |
|
|
|
var hlbaseUri = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/"; |
|
|
|
var x = document.querySelectorAll("code[class^='language-']"); |
|
|
|
var lb = document.querySelectorAll("code[class^='language-']"); |
|
|
|
if (x.length > 0) { |
|
|
|
|
|
|
|
// We have blocks to be highlighted, so we load css + js |
|
|
|
|
|
|
|
var st = document.createElement('link'); |
|
|
|
|
|
|
|
st.rel = "stylesheet"; |
|
|
|
|
|
|
|
st.href = hlbaseUri + "styles/atom-one-light.min.css"; |
|
|
|
|
|
|
|
document.getElementsByTagName('head')[0].appendChild(st); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var sc = document.createElement('script'); |
|
|
|
|
|
|
|
sc.type = "text/javascript"; |
|
|
|
|
|
|
|
sc.src = hlbaseUri + "highlight.min.js"; |
|
|
|
|
|
|
|
sc.async = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Here's the crux, we need to react on load event for this new element to make it work. |
|
|
|
// Set langs to the langs that are included by default (for now: 'common set' on CDN) |
|
|
|
sc.onload = () => { highlight(x) } |
|
|
|
var langs = ["apache", "bash", "coffeescript", "cpp", "cs", "css", "diff", |
|
|
|
document.getElementsByTagName('head')[0].appendChild(sc); |
|
|
|
"http", "ini", "java", "javascript", "json", "makefile", "xml", |
|
|
|
|
|
|
|
"markdown", "nginx", "objectivec", "perl", "php", "properties", |
|
|
|
|
|
|
|
"python", "ruby", "shell", "sql"]; |
|
|
|
|
|
|
|
|
|
|
|
// Given a set of nodes, run highlighting on them |
|
|
|
// Given a set of nodes, run highlighting on them |
|
|
|
function highlight(nodes) { |
|
|
|
function highlight(nodes) { |
|
|
@ -27,6 +18,38 @@ |
|
|
|
hljs.highlightBlock(nodes[i]); |
|
|
|
hljs.highlightBlock(nodes[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Given a array of URIs, load them in order |
|
|
|
|
|
|
|
function loadLanguages(uris, callback) { |
|
|
|
|
|
|
|
uris.forEach(function(uri) { |
|
|
|
|
|
|
|
var sc=document.createElement('script'); |
|
|
|
|
|
|
|
sc.src=uri; sc.async=false; // critical? |
|
|
|
|
|
|
|
if( uris.indexOf(uri) == uris.length-1) { |
|
|
|
|
|
|
|
sc.onload = callback; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
document.head.appendChild(sc); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We don't have to do anything if there are no language blocks |
|
|
|
|
|
|
|
if (lb.length > 0) { |
|
|
|
|
|
|
|
// We have blocks to be highlighted, so we load css |
|
|
|
|
|
|
|
var st = document.createElement('link'); |
|
|
|
|
|
|
|
st.rel = "stylesheet"; |
|
|
|
|
|
|
|
st.href = hlbaseUri + "styles/atom-one-light.min.css"; |
|
|
|
|
|
|
|
document.head.appendChild(st); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Construct set of files to load, in order |
|
|
|
|
|
|
|
var jss = [hlbaseUri + "highlight.min.js"]; |
|
|
|
|
|
|
|
// Check what we need to load |
|
|
|
|
|
|
|
for (i=0; i < lb.length; i++) { |
|
|
|
|
|
|
|
lang = lb[i].className.replace('language-',''); |
|
|
|
|
|
|
|
if (!langs.includes(lang)) { |
|
|
|
|
|
|
|
jss.push(hlbaseUri + "languages/" + lang + ".min.js"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Load files in order, higlight on last load |
|
|
|
|
|
|
|
loadLanguages(jss, () => {highlight(lb)}); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
</script> |
|
|
|
</script> |
|
|
|