Skip to content

Commit 7657845

Browse files
author
Christopher Baker
committed
utilize prism from bit-docs-prettify
1 parent 50678fb commit 7657845

File tree

2 files changed

+26
-171
lines changed

2 files changed

+26
-171
lines changed

index.js

Lines changed: 26 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,38 @@
1-
require("./static/styles/highlight-line.less");
2-
var $ = require("jquery");
1+
var $ = e("jquery");
32

4-
var getLines = function(lineString) {
5-
var lineArray = lineString.split(',');
6-
var result = {};
7-
8-
for (var i = 0; i < lineArray.length; i++) {
9-
var val = lineArray[i];
10-
11-
// Matches any string with 1+ digits dash 1+ digits
12-
// will ignore non matching strings
13-
if (/^([\d]+-[\d]+)$/.test(val)) {
14-
var values = val.split('-'),
15-
start = (values[0] - 1),
16-
finish = (values[1] - 1);
17-
18-
for (var j = start; finish >= j; j++) {
19-
result[j] = true;
20-
}
21-
//matches one or more digits
22-
} else if (/^[\d]+$/.test(val)) {
23-
result[val - 1] = true;
24-
} else {
25-
result[val] = true;
26-
}
3+
var getConfig = function(lineString) {
4+
var lines = lineString
5+
.split(',')
6+
.map(function(data) {
7+
return data.trim();
8+
});
279

10+
var only = false;
11+
var index = lines.indexOf('only');
12+
if (index > -1) {
13+
only = true;
14+
lines.splice(index, 1);
2815
}
29-
return result;
30-
};
3116

32-
/**
33-
* @parent bit-docs-html-highlight-line/static
34-
* @module {function} bit-docs-html-highlight-line/highlight-line.js
35-
*
36-
* Main front end JavaScript file for static portion of this plugin.
37-
*
38-
* @signature `addHighlights()`
39-
*
40-
* Goes through the lines in a `<code>` block and highlights the specified
41-
* ranges.
42-
*
43-
* Finds all `<span highlight-line="..."></span>` elements and uses those as
44-
* directives for what to highlight.
45-
*
46-
* If the `only` option was specified to the
47-
* [bit-docs-html-highlight-line/tags/highlight] tag, then non-highlighted
48-
* lines will be collapsed if they exist greater than three lines away from a
49-
* highlighted line.
50-
*/
51-
function addHighlights() {
17+
return {
18+
lines: lines.join(','),
19+
collapse: false,
20+
};
21+
};
5222

23+
module.exports = function() {
5324
$('span[line-highlight]').each(function(i, el) {
5425
var $el = $(el);
55-
var lines = getLines($el.attr('line-highlight'));
56-
var codeBlock = $el.parent().prev('pre').children('code');
57-
codeBlock.addClass("line-highlight");
58-
59-
var lineMap = [[]];
60-
var k = 0;
61-
codeBlock.children().each(function(i, el) {
62-
var nodeText = $(el).text();
63-
if (/\n/.test(nodeText)) {
26+
var config = getConfig($el.attr('line-highlight'));
27+
var preBlock = $el.parent().prev('pre');
28+
var codeBlock = preBlock.children('code');
6429

65-
var cNames = $(el).attr('class');
66-
var str = nodeText.split('\n');
67-
var l = str.length;
30+
if (preBlock) {
31+
preBlock.attr('data-line', config.lines);
6832

69-
for (var j = 0; j < l; j++) {
70-
var text = j === (l - 1) ? str[j] : str[j] + '\n';
71-
var newNode = document.createElement('span');
72-
newNode.className = cNames;
73-
$(newNode).text(text);
74-
lineMap[k].push(newNode);
75-
76-
if (j !== (l - 1)) {
77-
k++;
78-
lineMap[k] = [];
79-
}
80-
}
81-
} else {
82-
lineMap[k].push(el);
33+
if (config.collapse) {
34+
preBlock.attr('data-collapse', config.collapse);
8335
}
84-
});
85-
86-
codeBlock.empty();
87-
if(lines.only) {
88-
var segments = [];
89-
lineMap.forEach(function(lineNodes, lineNumber){
90-
var visible = lines[lineNumber];
91-
var lineNode = document.createElement('span');
92-
$(lineNode).append(lineNodes);
93-
lineNode.className = lines[lineNumber] ? 'line highlight line-'+lineNumber: 'line line-'+lineNumber ;
94-
95-
var lastSegment = segments[segments.length - 1];
96-
if(!lastSegment || lastSegment.visible !== visible) {
97-
segments.push(lastSegment = {visible: visible, lines: []});
98-
}
99-
lastSegment.lines.push(lineNode);
100-
101-
102-
});
103-
segments.forEach(function(segment, index){
104-
var next = segments[index+1];
105-
106-
if(segment.visible) {
107-
// take 3 lines from next if possible
108-
if(next) {
109-
var first = next.lines.splice(0,3);
110-
segment.lines = segment.lines.concat(first);
111-
}
112-
codeBlock.append(segment.lines);
113-
} else {
114-
// move 3 lines to next if possible
115-
if(next) {
116-
var last = segment.lines.splice(segment.lines.length-3);
117-
next.lines = last.concat(next.lines);
118-
}
119-
if(segment.lines.length > 2) {
120-
var expander = document.createElement('div');
121-
expander.className = "expand";
122-
expander.addEventListener("click", function(){
123-
$(expander).replaceWith(segment.lines);
124-
});
125-
codeBlock.append(expander);
126-
} else {
127-
codeBlock.append(segment.lines);
128-
}
129-
}
130-
});
131-
132-
133-
} else {
134-
lineMap.forEach(function(lineNodes, lineNumber){
135-
var newNode = document.createElement('span');
136-
newNode.className = lines[lineNumber] ? 'line highlight': 'line' ;
137-
$(newNode).append(lineNodes);
138-
codeBlock.append(newNode);
139-
});
14036
}
141-
14237
});
143-
}
144-
145-
module.exports = addHighlights;
38+
};

static/styles/highlight-line.less

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)