writefreely/templates/include/post-render.tmpl

105 lines
3.0 KiB
Cheetah
Raw Normal View History

<!-- Miscelaneous render related template parts we use multiple times -->
{{define "collection-meta"}}
{{if .Monetization -}}
<meta name="monetization" content="{{.DisplayMonetization}}" />
{{- end}}
{{end}}
{{define "highlighting"}}
2018-11-23 18:12:47 +01:00
<script>
// TODO: this feels more like a mutation observer
addEventListener('DOMContentLoaded', function () {
2018-12-07 22:37:14 +01:00
var hlbaseUri = "/js/";
var lb = document.querySelectorAll("code[class^='language-']");
// Custom aliasmap
var aliasmap = {
"elisp" : "lisp",
"emacs-lisp" : "lisp",
"c" : "cpp",
"cc" : "cpp",
"h" : "cpp",
"c++" : "cpp",
"h++" : "cpp",
"hpp" : "cpp",
"hh" : "cpp",
"hxx" : "cpp",
"cxx" : "cpp",
"sh" : "bash",
"js" : "javascript",
"jsx" : "javascript",
"html" : "xml"
};
// 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) {
2018-12-03 18:18:04 +01:00
var sc = document.createElement('script');
sc.src = uri;
sc.async = false; // critical?
// Set callback on last script
2018-12-03 18:18:04 +01:00
if (uris.indexOf(uri) == uris.length-1) {
// Set callback regardless
// so we're sure it will run if last element had error
// (we only know after loading, so we've had load time already)
sc.onload = callback; sc.onerror = callback;
2018-12-03 18:18:04 +01:00
}
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
2018-11-23 18:12:47 +01:00
var st = document.createElement('link');
st.rel = "stylesheet";
2018-12-07 22:37:14 +01:00
st.href = "/css/lib/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-','').toLowerCase();
// Support the aliases specified above
if (aliasmap[lang]) lang = aliasmap[lang];
2018-12-07 22:37:14 +01:00
lurl = hlbaseUri + "highlightjs/" + lang + ".min.js";
if (!jss.includes(lurl)) {
2018-12-03 18:18:04 +01:00
jss.push(lurl);
}
2018-11-23 18:12:47 +01:00
}
// Load files in order, highlight on last load
loadLanguages(jss, () => {highlight(lb)});
2018-11-23 18:12:47 +01:00
}
});
</script>
{{end}}
<!-- Include mathjax configuration -->
{{define "mathjax"}}
2019-09-11 23:04:13 +02:00
<script>
MathJax = {
tex: {
inlineMath: [
["\\(", "\\)"],
['$', '$'],
],
displayMath: [
['$$', '$$'],
['\\[', '\\]'],
],
},
};
</script>
<script type="text/javascript" id="MathJax-script" src="/js/mathjax/tex-svg-full.js" async>
</script>
{{end}}