Remove .gitbook and add "root" option

This commit is contained in:
Samy Pessé
2016-02-29 17:35:12 +01:00
parent 496f253e69
commit ec353e179d
14 changed files with 78 additions and 74 deletions
-1
View File
@@ -1 +0,0 @@
./docs
+1
View File
@@ -1,6 +1,7 @@
var pkg = require('../package.json');
module.exports = {
root: './docs',
title: 'GitBook Documentation',
plugins: ['theme-official'],
+1
View File
@@ -6,6 +6,7 @@ GitBook allows you to customize your book using a flexible configuration. These
| Variable | Description |
| -------- | ----------- |
| `root` | Path to the root folder containing the content |
| `title` | Title of your book, default value is extracted from the README. On GitBook.com this field is pre-filled. |
| `description` | Description of your book, default value is extracted from the README. On GitBook.com this field is pre-filled. |
| `author` | Name of the author. On GitBook.com this field is pre-filled. |
+6 -4
View File
@@ -45,18 +45,20 @@ bin/*
### Project documentation / Sub-directory {#subdirectory}
For project documentaiton, it sometimes better to use a diretcory (like `docs/`) to store the prject's documentation. You can use a `.gitbook` file to indicate to GitBook in which folder the book is stored:
For software project, it sometimes better to use a diretcory (like `docs/`) to store the project's documentation. You can use the [`root` option](config.md) to indicate to GitBook in which folder the book is stored:
```
.
├── .gitbook
├── book.json
└── docs/
├── README.md
└── SUMMARY.md
```
With `.gitbook` containing:
With `book.json` containing:
```
./docs/
{
"root": "./docs"
}
```
+16 -28
View File
@@ -123,7 +123,16 @@ Book.prototype.getContext = function() {
// Parse and prepare the configuration, fail if invalid
Book.prototype.prepareConfig = function() {
return this.config.load();
var that = this;
return this.config.load()
.then(function() {
var root = that.config.get('root');
if (!root) return;
that.originalRoot = that.root;
that.root = path.resolve(that.root, root);
})
};
// Resolve a path in the book source
@@ -344,35 +353,14 @@ Book.prototype.isInLanguageBook = function(filename) {
});
};
// Locate a book in a folder
// - Read the ".gitbook" is exists
// - Try the folder itself
// - Try a "docs" folder
Book.locate = function(fs, root) {
return fs.readAsString(path.join(root, '.gitbook'))
.then(function(content) {
return path.join(root, content);
}, function() {
// .gitbook doesn't exists, fall back to the root folder
return Promise(root);
});
};
// Locate and setup a book
Book.setup = function(fs, root, opts) {
return Book.locate(fs, root)
.then(function(_root) {
return new Book(_.extend(opts || {}, {
root: _root,
fs: fs
}));
});
};
// Initialize a book
Book.init = function(fs, root, opts) {
return Book.setup(fs, root, opts)
.then(initBook);
var book = new Book(_.extend(opts || {}, {
root: root,
fs: fs
}));
return initBook(book);
};
+5 -4
View File
@@ -42,12 +42,13 @@ var FORMATS = {
function bookCmd(fn) {
return function(args, kwargs) {
var input = path.resolve(args[0] || process.cwd());
return Book.setup(nodeFS, input, {
var book = new Book({
fs: nodeFS,
root: input,
logLevel: kwargs.log
})
.then(function(book) {
return fn(book, args.slice(1), kwargs);
});
return fn(book, args.slice(1), kwargs);
};
}
+19 -18
View File
@@ -131,26 +131,27 @@ module.exports = {
// Generate the book
.then(function() {
return Book.setup(helper.nodeFS, input, {
'logLevel': kwargs.log
})
.then(function(book) {
return book.parse()
.then(function() {
// Add livereload plugin
book.config.set('plugins',
book.config.get('plugins')
.concat([
{ name: 'livereload' }
])
);
var book = new Book({
fs: helper.nodeFS,
root: input,
logLevel: kwargs.log
});
var Out = helper.FORMATS[kwargs.format];
var output = new Out(book);
return book.parse()
.then(function() {
// Add livereload plugin
book.config.set('plugins',
book.config.get('plugins')
.concat([
{ name: 'livereload' }
])
);
return output.generate()
.thenResolve(output);
});
var Out = helper.FORMATS[kwargs.format];
var output = new Out(book);
return output.generate()
.thenResolve(output);
});
})
+4
View File
@@ -4,6 +4,10 @@ module.exports = {
'title': 'GitBook Configuration',
'type': 'object',
'properties': {
'root': {
'type': 'string',
'title': 'Path fro the root folder containing the book\'s content'
},
'title': {
'type': 'string',
'title': 'Title of the book, default is extracted from README'
+3 -3
View File
@@ -46,7 +46,7 @@ describe('Configuration', function() {
return mock.setupDefaultBook()
.then(function(_book) {
book = _book;
return book.config.load();
return book.prepareConfig();
});
});
@@ -64,7 +64,7 @@ describe('Configuration', function() {
})
.then(function(_book) {
book = _book;
return book.config.load();
return book.prepareConfig();
});
});
@@ -82,7 +82,7 @@ describe('Configuration', function() {
})
.then(function(_book) {
book = _book;
return book.config.load();
return book.prepareConfig();
});
});
+2 -2
View File
@@ -7,7 +7,7 @@ describe('Glossary', function() {
'GLOSSARY.md': ''
})
.then(function(book) {
return book.config.load()
return book.prepareConfig()
.then(function() {
return book.glossary.load();
@@ -27,7 +27,7 @@ describe('Glossary', function() {
})
.then(function(_book) {
book = _book;
return book.config.load();
return book.prepareConfig();
})
.then(function() {
return book.glossary.load();
+1 -1
View File
@@ -6,7 +6,7 @@ describe('Langs', function() {
'LANGS.md': ''
})
.then(function(book) {
return book.config.load()
return book.prepareConfig()
.then(function() {
return book.langs.load();
+17 -10
View File
@@ -1,27 +1,34 @@
var path = require('path');
var should = require('should');
var Book = require('../').Book;
var mock = require('./mock');
describe('Locate', function() {
it('should use root folder if no .gitbook', function() {
return mock.setupFS({
return mock.setupBook({
'README.md': '# Hello'
})
.then(function(root) {
return Book.locate(mock.fs, root)
.should.be.fulfilledWith(root);
.then(function(book) {
return book.prepareConfig()
.then(function() {
should(book.originalRoot).not.be.ok();
});
});
});
it('should use resolve using .gitbook', function() {
return mock.setupFS({
it('should use resolve using book.js root property', function() {
return mock.setupBook({
'README.md': '# Hello',
'.gitbook': './docs'
'docs/README.md': '# Hello Book',
'book.json': { root: './docs' }
})
.then(function(root) {
return Book.locate(mock.fs, root)
.should.be.fulfilledWith(path.resolve(root, 'docs'));
.then(function(book) {
return book.prepareConfig()
.then(function() {
should(book.originalRoot).be.ok();
book.root.should.equal(path.resolve(book.originalRoot, 'docs'));
});
});
});
+2 -2
View File
@@ -53,7 +53,7 @@ describe('Plugins', function() {
}
})
.then(function(book) {
return book.config.load()
return book.prepareConfig()
.then(function() {
var plugins = new PluginsManager(book);
return plugins.install();
@@ -90,7 +90,7 @@ describe('Plugins', function() {
}
})
.then(function(book2) {
return book2.config.load()
return book2.prepareConfig()
.then(function() {
var plugin = new BookPlugin(book2, 'test-config');
return plugin.load(PLUGINS_ROOT);
+1 -1
View File
@@ -6,7 +6,7 @@ describe('Readme', function() {
'README.md': ''
})
.then(function(book) {
return book.config.load()
return book.prepareConfig()
.then(function() {
return book.readme.load();