Skip to content

Commit 36e2236

Browse files
committed
Fix GitbookIO#765: in init handle correctly empty entries
Add tests for it, fix GitbookIO#769
1 parent 72df665 commit 36e2236

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

lib/book.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -789,20 +789,19 @@ Book.prototype.normError = function(err, opts, defs) {
789789
};
790790

791791
// Init and return a book
792-
Book.init = function(root) {
793-
var book = new Book(root);
792+
Book.init = function(root, opts) {
793+
var book = new Book(root, opts);
794794
var extensionToUse = ".md";
795795

796796
var chaptersPaths = function(chapters) {
797797
return _.reduce(chapters || [], function(accu, chapter) {
798-
if (!chapter.path) return accu;
798+
var o = {
799+
title: chapter.title
800+
};
801+
if (chapter.path) o.path = chapter.path;
802+
799803
return accu.concat(
800-
_.filter([
801-
{
802-
title: chapter.title,
803-
path: chapter.path
804-
}
805-
].concat(chaptersPaths(chapter.articles)))
804+
[o].concat(chaptersPaths(chapter.articles))
806805
);
807806
}, []);
808807
};
@@ -839,6 +838,7 @@ Book.init = function(root) {
839838
.then(function(chapters) {
840839
// Create files that don't exist
841840
return Q.all(_.map(chapters, function(chapter) {
841+
if (!chapter.path) return Q();
842842
var absolutePath = path.resolve(book.root, chapter.path);
843843

844844
return fs.exists(absolutePath)

test/books/init/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!SUMMARY.md
3+
!.gitignore

test/books/init/SUMMARY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Summary
2+
3+
* [Hello](hello.md)
4+
* [Hello 2](hello2.md)
5+
* Hello 3
6+
* [Hello 4](hello3/hello4.md)

test/init.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var should = require('should');
4+
5+
var Book = require('../').Book;
6+
var LOG_LEVELS = require('../').LOG_LEVELS;
7+
8+
describe('Init Books', function () {
9+
var initRoot;
10+
11+
before(function() {
12+
initRoot = path.resolve(__dirname, "books/init");
13+
return Book.init(initRoot, {
14+
logLevel: LOG_LEVELS.DISABLED,
15+
});
16+
});
17+
18+
it('should create all chapters', function() {
19+
should(fs.existsSync(path.resolve(initRoot, "hello.md"))).be.ok;
20+
should(fs.existsSync(path.resolve(initRoot, "hello2.md"))).be.ok;
21+
should(fs.existsSync(path.resolve(initRoot, "hello3/hello4.md"))).be.ok;
22+
});
23+
});

0 commit comments

Comments
 (0)