Only load js when there are blocks to highlight
Crux was the load event for the script tag inserted.
This commit is contained in:
parent
8144c9581d
commit
add06ee56c
|
@ -2,15 +2,29 @@
|
||||||
{{define "highlighting"}}
|
{{define "highlighting"}}
|
||||||
<!-- TODO: make this conditional on presence of code blocks -->
|
<!-- TODO: make this conditional on presence of code blocks -->
|
||||||
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/atom-one-light.min.css"/>
|
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/atom-one-light.min.css"/>
|
||||||
<script type="text/javascript"
|
|
||||||
src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js" async></script>
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
addEventListener('load', function () {
|
// TODO: this feels more like a mutation observer
|
||||||
var x = document.querySelectorAll("code[class^='language-']");
|
addEventListener('DOMContentLoaded', function () {
|
||||||
for (i=0; i<x.length; i++) {
|
var hlbaseUri="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/";
|
||||||
hljs.highlightBlock(x[i]);
|
var x = document.querySelectorAll("code[class^='language-']");
|
||||||
}});
|
if (x.length > 0 ) {
|
||||||
|
// We have blocks to be highlighted, stuff, so we load css + js
|
||||||
|
var s=document.createElement('script');
|
||||||
|
s.type="text/javascript"; s.src= hlbaseUri + "highlight.min.js"; s.async=true;
|
||||||
|
|
||||||
|
// Here's the crux, we need to react on load event for this new element to make it work.
|
||||||
|
s.onload = () => { highlight(x) }
|
||||||
|
document.getElementsByTagName('head')[0].appendChild(s);
|
||||||
|
|
||||||
|
// Gives a set of nodes, run highlighting on them
|
||||||
|
function highlight(nodes) {
|
||||||
|
for (i=0; i<nodes.length; i++) {
|
||||||
|
hljs.highlightBlock(nodes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue