Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ var getConfig = function(lineString, lineCount) {
return typeof val === 'number' && !isNaN(val);
})
;

if (range[0] > current + padding) {
var collapseEnd = (range[0] - 1 - padding);
if (collapseEnd !== current) {
collapse.push(current + '-' + collapseEnd);
}
}

current = (range[1] || range[0]) + padding + 1;
}

Expand Down Expand Up @@ -106,8 +106,11 @@ module.exports = function() {

var preBlock = findPreviousSibling(highlight.parentElement, 'pre');
var codeBlock = preBlock.childNodes.item(0);

var total = codeBlock.innerHTML.split('\n').length - 1;
//Without trimming, additional characters can create new lines
//this happens when PrismJS is not applied (in testing)
//setting total to length - 1 makes the last not collapsed
codeBlock.textContent = codeBlock.textContent.trim();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best place to do the trimming, or should it be done wherever codeBlock.textContent is initially created/set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it could be trimmed initially in bit-docs-prettify

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chasenlehara bit-docs-prettify is not invoked in testing, so I changed the fix to be the same as the needed part to remove the newline at the end.

var total = codeBlock.innerHTML.split('\n').length;
var config = getConfig(highlight.getAttribute('line-highlight'), total);

if (preBlock) {
Expand Down
2 changes: 1 addition & 1 deletion prism-collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function collapseLines(pre, config) {
});

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

code.splice(line, 0, inserts[i][1]);
numbers[line] += inserts[i][2];
Expand Down
15 changes: 15 additions & 0 deletions test-collapse-last-line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```js
Todo.List = DefineList.extend("TodoList",{
"#": Todo,
completeAll(){
return this.forEach((todo) => { todo.complete = true; });
}
});

const todoConnection = restModel({
Map: Todo,
List: Todo.List,
url: "/api/todos/{id}"
});
```
<span line-highlight='5-7,only'/>
35 changes: 34 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,40 @@ describe("bit-docs-html-highlight-line", function() {
close();
done();
}, done);
}, done)
}, done);

});

it("Collapse last line", function(done) {
this.timeout(60000);

var docMap = Promise.resolve({
index: {
name: "index",
demo: "path/to/demo.html",
body: fs.readFileSync(__dirname+"/test-collapse-last-line.md", "utf8")
}
});

generate(docMap, {
html: {
dependencies: {
"bit-docs-html-highlight-line": __dirname
}
},
dest: path.join(__dirname, "temp"),
parent: "index",
forceBuild: true,
debug: true,
minifyBuild: false
}).then(function() {
open("index.html",function(browser, close) {
var doc = browser.window.document;
var collapseCode = doc.querySelectorAll('pre[data-collapse="11-12"]');
assert.ok(collapseCode);
close();
done();
}, done);
}, done);
});
});