mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Correctly setup summary instance when file doesn't exist
This commit is contained in:
@@ -21,6 +21,11 @@ BackboneFile.prototype.parse = function() {
|
||||
// To be implemented by each child
|
||||
};
|
||||
|
||||
// Handle case where file doesn't exists
|
||||
BackboneFile.prototype.parseNotFound = function() {
|
||||
|
||||
};
|
||||
|
||||
// Return true if backbone file exists
|
||||
BackboneFile.prototype.exists = function() {
|
||||
return Boolean(this.path);
|
||||
@@ -48,7 +53,7 @@ BackboneFile.prototype.load = function() {
|
||||
|
||||
return this.locate()
|
||||
.then(function() {
|
||||
if (!that.path) return;
|
||||
if (!that.path) return that.parseNotFound();
|
||||
|
||||
that.log.debug.ln(that.type, 'located at', that.path);
|
||||
|
||||
|
||||
+35
-29
@@ -124,12 +124,10 @@ TOCArticle.prototype.map = function(iter) {
|
||||
/*
|
||||
A part of a ToC is a composed of a tree of articles and an optiona title
|
||||
*/
|
||||
function TOCPart(part, summary) {
|
||||
if (!(this instanceof TOCPart)) return new TOCPart(part);
|
||||
function TOCPart(part, parent) {
|
||||
if (!(this instanceof TOCPart)) return new TOCPart(part, parent);
|
||||
|
||||
TOCArticle.apply(this, arguments);
|
||||
|
||||
this.summary = summary;
|
||||
}
|
||||
util.inherits(TOCPart, TOCArticle);
|
||||
|
||||
@@ -138,7 +136,7 @@ TOCPart.prototype.validate = function() { };
|
||||
|
||||
// Return a sibling (next or prev) of this part
|
||||
TOCPart.prototype.sibling = function(direction) {
|
||||
var parts = this.summary.parts;
|
||||
var parts = this.parent.parts;
|
||||
var pos = _.findIndex(parts, this);
|
||||
|
||||
if (parts[pos + direction]) {
|
||||
@@ -200,6 +198,11 @@ util.inherits(Summary, BackboneFile);
|
||||
|
||||
Summary.prototype.type = 'summary';
|
||||
|
||||
// Prepare summary when non existant
|
||||
Summary.prototype.parseNotFound = function() {
|
||||
this.update([]);
|
||||
};
|
||||
|
||||
// Parse the summary content
|
||||
Summary.prototype.parse = function(content) {
|
||||
var that = this;
|
||||
@@ -207,28 +210,7 @@ Summary.prototype.parse = function(content) {
|
||||
return this.parser.summary(content)
|
||||
|
||||
.then(function(summary) {
|
||||
that.parts = _.map(summary.parts, function(part) {
|
||||
return new TOCPart(part, that);
|
||||
});
|
||||
|
||||
// Create first part if none
|
||||
if (that.parts.length == 0) {
|
||||
that.parts.push(new TOCPart({}, that));
|
||||
}
|
||||
|
||||
// Add README as first entry
|
||||
var firstArticle = that.parts[0].articles[0];
|
||||
if (!firstArticle || firstArticle.path != that.book.readme.path) {
|
||||
that.parts[0].articles.unshift(new TOCArticle({
|
||||
title: 'Introduction',
|
||||
path: that.book.readme.path,
|
||||
isAutoIntro: true
|
||||
}, that.parts[0]));
|
||||
}
|
||||
that.parts[0].articles[0].isIntroduction = true;
|
||||
|
||||
// Update count of articles and create "level"
|
||||
that.update();
|
||||
that.update(summary.parts);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -297,9 +279,33 @@ Summary.prototype.count = function() {
|
||||
return this._length;
|
||||
};
|
||||
|
||||
// Update the count and indexing of "level"
|
||||
Summary.prototype.update = function() {
|
||||
// Prepare the summary
|
||||
Summary.prototype.update = function(parts) {
|
||||
var that = this;
|
||||
|
||||
|
||||
that.parts = _.map(parts, function(part) {
|
||||
return new TOCPart(part, that);
|
||||
});
|
||||
|
||||
// Create first part if none
|
||||
if (that.parts.length == 0) {
|
||||
that.parts.push(new TOCPart({}, that));
|
||||
}
|
||||
|
||||
// Add README as first entry
|
||||
var firstArticle = that.parts[0].articles[0];
|
||||
if (!firstArticle || firstArticle.path != that.book.readme.path) {
|
||||
that.parts[0].articles.unshift(new TOCArticle({
|
||||
title: 'Introduction',
|
||||
path: that.book.readme.path,
|
||||
isAutoIntro: true
|
||||
}, that.parts[0]));
|
||||
}
|
||||
that.parts[0].articles[0].isIntroduction = true;
|
||||
|
||||
|
||||
// Update the count and indexing of "level"
|
||||
var prev = undefined;
|
||||
|
||||
that._length = 0;
|
||||
|
||||
@@ -33,6 +33,32 @@ describe('Summary / Table of contents', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Non-existant summary', function() {
|
||||
var book;
|
||||
|
||||
before(function() {
|
||||
return mock.setupBook({
|
||||
'README.md': 'Hello'
|
||||
})
|
||||
.then(function(_book) {
|
||||
book = _book;
|
||||
|
||||
return book.readme.load()
|
||||
.then(function() {
|
||||
return book.summary.load();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should add README as first entry', function() {
|
||||
should(book.summary.getArticle('README.md')).be.ok();
|
||||
});
|
||||
|
||||
it('should correctly count articles', function() {
|
||||
book.summary.count().should.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Non-empty summary list', function() {
|
||||
var book;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user