summaryrefslogtreecommitdiffstats
path: root/static/hlraw.js
blob: 8d57bb4aa73e780ce59097a9bde8dfdee510befa (plain)
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
34
35
36
37
38
39
(function() {
	'use strict';

	var highlight = function(block, lang) {
		var langs = lang.split('_');
		if (langs.length > 1) {
			lang = langs[langs.length-1];
		}
		if (!lang || typeof hljs.getLanguage(lang) === 'undefined') {
			return;
		}
		var hl = hljs.highlight(lang, block.textContent, true);
		block.innerHTML = hl.value;
	};

	var highlightAll = function() {
		var rawBlocks = document.querySelectorAll('pre.cnm-raw code');
		for (var i=0; i<rawBlocks.length; i++) {
			var code = rawBlocks[i];
			var classes = code.className.split();
			var lang = '';
			for (var j=0; j<classes.length; j++) {
				if (classes[j].startsWith('cnm-raw-')) {
					lang = classes[j].slice('cnm-raw-'.length);
					break;
				}
			}
			if (lang) {
				try {
					highlight(code, lang);
				} catch (e) {
					console.error(e);
				}
			}
		}
	};

	highlightAll();
})();