forked from GitbookIO/plugin-highlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (48 loc) · 972 Bytes
/
index.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var path = require('path');
var hljs = require('algorithmarchive-highlight.js');
var MAP = {
'py': 'python',
'js': 'javascript',
'json': 'javascript',
'rb': 'ruby',
'csharp': 'cs',
};
function normalize(lang) {
if(!lang) { return null; }
var lower = lang.toLowerCase();
return MAP[lower] || lower;
}
function highlight(lang, code) {
if(!lang) return {
body: code,
html: false
};
// Normalize lang
lang = normalize(lang);
try {
return hljs.highlight(lang, code).value;
} catch(e) { }
return {
body: code,
html: false
};
}
module.exports = {
book: {
assets: './css',
css: [
'website.css'
]
},
ebook: {
assets: './css',
css: [
'ebook.css'
]
},
blocks: {
code: function(block) {
return highlight(block.kwargs.language, block.body);
}
}
};