-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
47 lines (42 loc) · 1.32 KB
/
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
var split = require('split'),
cardinal = require('cardinal');
module.exports = function(str) {
var highlightjs = false,
code = '';
return (str !== undefined) ?
// sync
str.toString()
.split('\n')
.map(format)
.filter(nonnull)
.join('\n') :
// stream
split(function(d) {
var f = format(d.toString());
return f === null ? '' : f + '\n';
});
function nonnull(_) { console.log(nonnull); return typeof _ !== 'null'; }
function format(str) {
if (str.match(/^```(js$|javascript)/)) {
highlightjs = true;
code = '';
return null;
} else if (str.trim() === '```' && highlightjs) {
highlightjs = false;
try {
return cardinal.highlight(code);
} finally {
code = '';
}
} else if (highlightjs) {
code += str + '\n';
return null;
}
return str
.replace(/^[\#]+\s+(.+)/, '\x1B[1m$1\x1B[22m')
.replace(/\`(.+?)\`/g, '\x1B[36m$1\x1B[39m')
.replace(/\*\*(.+?)\*\*/g, '\x1B[1m$1\x1B[22m')
.replace(/__(.+?)__/g, '\x1B[3m$1\x1B[23m')
.replace(/\*(.+?)\*/g, '\x1B[90m$1\x1B[39m');
}
};