-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
110 lines (101 loc) · 3.62 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env node
var prism = require('prismjs');
var fs = require('fs');
var supportsColor = require('supports-color');
function highjackRenderer (ansi_mapping) {
(function(Token) {
if (!ansi_mapping) {
if (fs.existsSync('~/.prismrc')) {
ansi_mapping = require('~/.prismrc');
} else {
if (supportsColor.has256) {
ansi_mapping = {
'function': '\x1b[37m',
'comment': '\x1b[38;5;241m',
'keyword': '\x1b[38;5;31m',
'string': '\x1b[38;5;28m',
'regex': '\x1b[38;166m',
'punctuation': '',
'operator': '',
'number': '\x1b[38;5;160m',
};
} else {
ansi_mapping = {
'function': '\x1b[1;37m',
'comment': '\x1b[36m',
'keyword': '\x1b[01;34m',
'string': '\x1b[1;32m',
'regex': '\x1b[1;33m',
'punctuation': '',
'operator': '',
'number': '\x1b[01;31m'
};
}
}
}
var _ = prism;
_.Token = function(type, content, alias, matchedStr, greedy) {
Token.apply(this, [].slice.call(arguments));
};
_.Token.stringify = function(o, language, parent, newlines) {
if (typeof o == 'string') {
return o;
}
if (_.util.type(o) === 'Array') {
return o.map(function(element) {
return _.Token.stringify(element, language, o, newlines);
}).join('');
}
var env = {
type: o.type,
content: _.Token.stringify(o.content, language, parent, newlines),
tag: 'span',
classes: ['token', o.type],
attributes: {},
language: language,
parent: parent
};
_.hooks.run('wrap', env);
function format(string) {
return ansi_mapping[env.type] + string + '\x1b[0m';
}
if (typeof ansi_mapping[env.type] != 'undefined') {
if (newlines) {
return env.content.split('\n').map(format).join('\n');
} else {
return format(env.content);
}
} else {
return env.content;
}
};
})(prism.Token);
}
function higlight(text, language, { html, newlines, grammar, colors } = {}) {
if (!html) {
highjackRenderer(colors);
}
if (grammar) {
prism.languages[language] = grammar;
} else {
grammar = prism.languages[language];
}
if (!grammar) {
try {
require('prismjs/components/prism-' + language + '.min.js');
grammar = prism.languages[language];
} catch(e) {
var languages = Object.keys(require('prismjs/components.js').languages);
throw new Error('Unknown language ' + language +
', available languages are ' +
languages.join(', '));
}
}
var tokens = prism.tokenize(text, grammar);
var string = prism.Token.stringify(tokens, language, undefined, newlines);
if (html) {
string = '<pre class="language-' + language + '">' + string + '</pre>';
}
return string;
}
module.exports = higlight;