|
1 |
| -require("./static/styles/highlight-line.less"); |
2 |
| -var $ = require("jquery"); |
| 1 | +var $ = e("jquery"); |
3 | 2 |
|
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 | + }); |
27 | 9 |
|
| 10 | + var only = false; |
| 11 | + var index = lines.indexOf('only'); |
| 12 | + if (index > -1) { |
| 13 | + only = true; |
| 14 | + lines.splice(index, 1); |
28 | 15 | }
|
29 |
| - return result; |
30 |
| -}; |
31 | 16 |
|
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 | +}; |
52 | 22 |
|
| 23 | +module.exports = function() { |
53 | 24 | $('span[line-highlight]').each(function(i, el) {
|
54 | 25 | 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'); |
64 | 29 |
|
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); |
68 | 32 |
|
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); |
83 | 35 | }
|
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 |
| - }); |
140 | 36 | }
|
141 |
| - |
142 | 37 | });
|
143 |
| -} |
144 |
| - |
145 |
| -module.exports = addHighlights; |
| 38 | +}; |
0 commit comments