Skip to content

Commit 7a8e624

Browse files
authored
only one line after expand button (#15)
* Trim code to prevent creating new line * More fixes * fix tests * Debugging * Debug for travis * This test only * Checkout without the fix * Update the fix to make the build pass * Fix changes * Just use trim * Pared down the markdown test file * Reduce markdown test file
1 parent c1bc03e commit 7a8e624

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

Diff for: index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ var getConfig = function(lineString, lineCount) {
6666
return typeof val === 'number' && !isNaN(val);
6767
})
6868
;
69-
69+
7070
if (range[0] > current + padding) {
7171
var collapseEnd = (range[0] - 1 - padding);
7272
if (collapseEnd !== current) {
7373
collapse.push(current + '-' + collapseEnd);
7474
}
7575
}
76-
76+
7777
current = (range[1] || range[0]) + padding + 1;
7878
}
7979

@@ -115,8 +115,11 @@ module.exports = function() {
115115

116116
var preBlock = findPreviousSibling(highlight, 'pre');
117117
var codeBlock = preBlock.childNodes.item(0);
118-
119-
var total = codeBlock.innerHTML.split('\n').length - 1;
118+
//Without trimming, additional characters can create new lines
119+
//this happens when PrismJS is not applied (in testing)
120+
//setting total to length - 1 makes the last not collapsed
121+
codeBlock.textContent = codeBlock.textContent.trim();
122+
var total = codeBlock.innerHTML.split('\n').length;
120123
var config = getConfig(highlight.getAttribute('line-highlight'), total);
121124

122125
if (preBlock) {

Diff for: prism-collapse.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function collapseLines(pre, config) {
100100
});
101101

102102
for (var i = 0; i < inserts.length; i++) {
103-
var line = Math.min(code.length - 1, inserts[i][0] - 1);
103+
var line = Math.min(code.length, inserts[i][0] - 1);
104104

105105
code.splice(line, 0, inserts[i][1]);
106106
numbers[line] += inserts[i][2];

Diff for: test-collapse-last-line.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
```js
2+
Todo.List = DefineList.extend("TodoList",{
3+
"#": Todo,
4+
completeAll(){
5+
return this.forEach((todo) => { todo.complete = true; });
6+
}
7+
});
8+
9+
const todoConnection = restModel({
10+
Map: Todo,
11+
List: Todo.List,
12+
url: "/api/todos/{id}"
13+
});
14+
```
15+
<span line-highlight='5-7,only'/>

Diff for: test.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,40 @@ describe("bit-docs-html-highlight-line", function() {
8989
close();
9090
done();
9191
}, done);
92-
}, done)
92+
}, done);
93+
94+
});
95+
96+
it("Collapse last line", function(done) {
97+
this.timeout(60000);
9398

99+
var docMap = Promise.resolve({
100+
index: {
101+
name: "index",
102+
demo: "path/to/demo.html",
103+
body: fs.readFileSync(__dirname+"/test-collapse-last-line.md", "utf8")
104+
}
105+
});
106+
107+
generate(docMap, {
108+
html: {
109+
dependencies: {
110+
"bit-docs-html-highlight-line": __dirname
111+
}
112+
},
113+
dest: path.join(__dirname, "temp"),
114+
parent: "index",
115+
forceBuild: true,
116+
debug: true,
117+
minifyBuild: false
118+
}).then(function() {
119+
open("index.html",function(browser, close) {
120+
var doc = browser.window.document;
121+
var collapseCode = doc.querySelectorAll('pre[data-collapse="11-12"]');
122+
assert.ok(collapseCode);
123+
close();
124+
done();
125+
}, done);
126+
}, done);
94127
});
95128
});

0 commit comments

Comments
 (0)