Usage (ranges are space separated):
{% highlight js 1,4-6 %}
One range
Adds `highlight-line-active` to lines 1,4,5,6
{% highlight js 3-4 -1 %}
Two ranges (add/remove), remove is N/A
Adds `highlight-line-add` to lines 3,4
{% highlight js -1 3-4 %}
Two ranges (add/remove), add is N/A
Adds `highlight-line-remove` to lines 3,4
{% highlight js 3-4 5,8-10 %}
Two ranges, both are used
Adds `highlight-line-add` to lines 3-4
Adds `highlight-line-remove` to lines 5,8,9,10
33 lines
651 B
JavaScript
33 lines
651 B
JavaScript
class HighlightLines {
|
|
constructor(rangeStr) {
|
|
this.highlights = this.convertRangeToHash(rangeStr);
|
|
}
|
|
|
|
convertRangeToHash(rangeStr) {
|
|
let hash = {};
|
|
if( !rangeStr ) {
|
|
return hash;
|
|
}
|
|
|
|
let ranges = rangeStr.split(",").map(function(range) {
|
|
return range.trim();
|
|
});
|
|
|
|
for(let range of ranges) {
|
|
let startFinish = range.split('-');
|
|
let start = parseInt(startFinish[0], 10);
|
|
let end = parseInt(startFinish[1] || start, 10);
|
|
|
|
for( let j = start, k = end; j<=k; j++ ) {
|
|
hash[j] = true;
|
|
}
|
|
}
|
|
return hash;
|
|
}
|
|
|
|
isHighlighted(lineNumber) {
|
|
return !!this.highlights[lineNumber]
|
|
}
|
|
}
|
|
|
|
module.exports = HighlightLines; |