Skip to content

Commit 80bf769

Browse files
author
Christopher Baker
committed
add collapse support
1 parent 358abb3 commit 80bf769

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

index.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,60 @@
11
var $ = require("jquery");
22

3-
var getConfig = function(lineString) {
3+
var getConfig = function(lineString, lineCount) {
44
var lines = lineString
55
.split(',')
66
.map(function(data) {
77
return data.trim();
8-
});
8+
})
9+
.filter(function(data) {
10+
return data;
11+
})
12+
;
913

10-
var only = false;
14+
var collapse = [];
1115
var index = lines.indexOf('only');
1216
if (index > -1) {
13-
only = true;
1417
lines.splice(index, 1);
18+
19+
var current = 1;
20+
for (var i = 0; i < lines.length; i++) {
21+
var range = lines[i]
22+
.split('-')
23+
.map(function(val) {
24+
return parseInt(val);
25+
})
26+
.filter(function(val) {
27+
return typeof val === 'number' && !isNaN(val);
28+
})
29+
;
30+
31+
if (range[0] > current + padding) {
32+
collapse.push(current + '-' + (range[0] - 1 - padding));
33+
}
34+
35+
current = (range[1] || range[0]) + padding + 1;
36+
}
37+
38+
if (current < lineCount) {
39+
collapse.push(current + '-' + lineCount);
40+
}
1541
}
1642

1743
return {
18-
lines: lines.join(','),
19-
collapse: false,
44+
lines: lines.length ? lines.join(',') : false,
45+
collapse: collapse.length ? collapse.join(',') : false,
2046
};
2147
};
2248

2349
module.exports = function() {
2450
$('span[line-highlight]').each(function(i, el) {
2551
var $el = $(el);
26-
var config = getConfig($el.attr('line-highlight'));
2752
var preBlock = $el.parent().prev('pre');
2853
var codeBlock = preBlock.children('code');
2954

55+
var total = codeBlock.text().split('\n').length - 1;
56+
var config = getConfig($el.attr('line-highlight'), total);
57+
3058
if (preBlock) {
3159
preBlock.attr('data-line', config.lines);
3260

0 commit comments

Comments
 (0)