Refactors Liquid syntax highlighters to add line highlights.
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
This commit is contained in:
33
_src/HighlightLines.js
Normal file
33
_src/HighlightLines.js
Normal file
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user