-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver.js
executable file
·62 lines (55 loc) · 1.75 KB
/
server.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
#!/usr/bin/env node
var hljs = require('highlight.js'); hljs.configure({classPrefix: ''});
var cheerio = require('cheerio');
var util = require('slap-util');
function highlight (text, language) {
if (language === false) return [];
var highlighted;
if (language) {
try { highlighted = hljs.highlight(language, text, true); } catch (e) {}
}
if (!highlighted) highlighted = hljs.highlightAuto(text);
var $ = cheerio.load(highlighted.value);
var ranges = [];
do {
var lastElCount = elCount;
var elCount = $('*:not(:has(*))').replaceWith(function () {
var $el = $(this);
var text = '';
[this].concat($el.parents().get(), [$.root()]).reverse().reduce(function (parent, el) {
$(parent).contents().each(function () {
var $sibling = $(this);
if ($sibling.is(el)) return false;
text += $sibling.text();
});
return el;
});
var lines = util.text.splitLines(text);
var linesPlusEl = util.text.splitLines(text + $el.text());
ranges.push({
range: [
[lines .length - 1, lines[lines.length - 1] .length],
[linesPlusEl.length - 1, linesPlusEl[linesPlusEl.length - 1].length]
],
properties: {
type: 'syntax',
syntax: ($el.attr('class') || '').match(/\S+/g) || []
}
});
return $el.text();
}).length;
} while (lastElCount !== elCount);
return ranges;
};
process.on('message', function (message) {
switch (message.type) {
case 'highlight':
process.send({
ranges: highlight(message.text, message.language),
revision: message.revision,
bucket: message.bucket
});
break;
case 'logger': util.logger(message.options); break;
}
});