-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
133 lines (111 loc) · 3.42 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
require("bit-docs-prettify");
require("prismjs/plugins/line-highlight/prism-line-highlight");
require("prismjs/plugins/line-highlight/prism-line-highlight.css");
require("./prism-collapse");
require("./prism-collapse.less");
/**
* Get node for provided line number
* Copied from prism-line-numbers.js and modified to support nested spans
* Original version assumed all line number spans were inside .line-numbers-rows
* but now they may be may be nested inside collapsed sections
*
* @param {Element} element pre element
* @param {Number} number line number
* @return {Element|undefined}
*/
Prism.plugins.lineNumbers.getLine = function (element, number) {
if (element.tagName !== 'PRE' || !element.classList.contains('line-numbers')) {
return;
}
var lineNumberRows = element.querySelector('.line-numbers-rows');
var lineNumbers = lineNumberRows.querySelectorAll('span'); // added
var lineNumberStart = parseInt(element.getAttribute('data-start'), 10) || 1;
var lineNumberEnd = lineNumberStart + (lineNumbers.length - 1);
if (number < lineNumberStart) {
number = lineNumberStart;
}
if (number > lineNumberEnd) {
number = lineNumberEnd;
}
var lineIndex = number - lineNumberStart;
return lineNumbers[lineIndex];
};
var padding = 3;
var getConfig = function(lineString, lineCount) {
var lines = lineString
.split(',')
.map(function(data) {
return data.trim();
})
.filter(function(data) {
return data;
})
;
var collapse = [];
var index = lines.indexOf('only');
if (index > -1) {
lines.splice(index, 1);
var current = 1;
for (var i = 0; i < lines.length; i++) {
var range = lines[i]
.split('-')
.map(function(val) {
return parseInt(val);
})
.filter(function(val) {
return typeof val === 'number' && !isNaN(val);
})
;
if (range[0] > current + padding) {
var collapseEnd = (range[0] - 1 - padding);
if (collapseEnd !== current) {
collapse.push(current + '-' + collapseEnd);
}
}
current = (range[1] || range[0]) + padding + 1;
}
if (current < lineCount) {
collapse.push(current + '-' + lineCount);
}
}
return {
lines: lines.length ? lines.join(',') : false,
collapse: collapse.length ? collapse.join(',') : false,
};
};
function findPreviousSibling(start, tag) {
tag = tag.toUpperCase();
while(start) {
if(start.nodeName === tag) {
return start;
}
if(start.querySelector) {
var pre = start.querySelector(tag);
if(pre) {
return pre;
}
}
// needs to be previousSibling for zombie
start = start.previousSibling;
}
}
module.exports = function() {
var highlights = document.querySelectorAll('div[line-highlight]');
for (var i = 0; i < highlights.length; i++) {
var highlight = highlights[i];
var preBlock = findPreviousSibling(highlight, 'pre');
var codeBlock = preBlock.childNodes.item(0);
//Without trimming, additional characters can create new lines
//this happens when PrismJS is not applied (in testing)
//setting total to length - 1 makes the last not collapsed
codeBlock.textContent = codeBlock.textContent.trim();
var total = codeBlock.innerHTML.split('\n').length;
var config = getConfig(highlight.getAttribute('line-highlight'), total);
if (preBlock) {
preBlock.setAttribute('data-line', config.lines);
if (config.collapse) {
preBlock.setAttribute('data-collapse', config.collapse);
}
}
};
};