mirror of
https://github.com/writefreely/writefreely
synced 2025-01-10 14:28:43 +00:00
c6e4967728
- dynamically construct a list of languages to load - fire hightlighting once last file has loaded
73 lines
2.5 KiB
Cheetah
73 lines
2.5 KiB
Cheetah
<!-- Miscelaneous render related template parts we use multiple times -->
|
|
{{define "highlighting"}}
|
|
<script>
|
|
// TODO: this feels more like a mutation observer
|
|
addEventListener('DOMContentLoaded', function () {
|
|
var hlbaseUri = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/";
|
|
var lb = document.querySelectorAll("code[class^='language-']");
|
|
|
|
// Set langs to the langs that are included by default (for now: 'common set' on CDN)
|
|
var langs = ["apache", "bash", "coffeescript", "cpp", "cs", "css", "diff",
|
|
"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
|
|
function highlight(nodes) {
|
|
for (i=0; i < nodes.length; 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>
|
|
{{end}}
|
|
|
|
<!-- Include mathjax configuration -->
|
|
{{define "mathjax"}}
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js"],
|
|
jax: ["input/TeX", "output/HTML-CSS"],
|
|
tex2jax: {
|
|
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
|
|
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
|
|
processEscapes: true
|
|
},
|
|
"HTML-CSS": { fonts: ["TeX"] }
|
|
});
|
|
</script>
|
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
|
|
{{end}}
|