mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-23 02:53:29 +00:00
Fix #1294: multilingual book should extend the book's config
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
var Config = require('../models/config');
|
||||
|
||||
/**
|
||||
Decode changes from a JS API to a config object
|
||||
|
||||
@@ -13,7 +11,7 @@ function decodeGlobal(config, result) {
|
||||
delete values.generator;
|
||||
delete values.output;
|
||||
|
||||
return Config.updateValues(config, values);
|
||||
return config.updateValues(values);
|
||||
}
|
||||
|
||||
module.exports = decodeGlobal;
|
||||
|
||||
+40
-14
@@ -18,6 +18,16 @@ Config.prototype.getValues = function() {
|
||||
return this.get('values');
|
||||
};
|
||||
|
||||
/**
|
||||
Change the file for the configuration
|
||||
|
||||
@param {File} file
|
||||
@return {Config}
|
||||
*/
|
||||
Config.prototype.setFile = function(file) {
|
||||
return this.set('file', file);
|
||||
};
|
||||
|
||||
/**
|
||||
Return a configuration value by its key path
|
||||
|
||||
@@ -40,7 +50,7 @@ Config.prototype.getValue = function(keyPath, def) {
|
||||
|
||||
@param {String} key
|
||||
@param {Mixed} value
|
||||
@return {Mixed}
|
||||
@return {Config}
|
||||
*/
|
||||
Config.prototype.setValue = function(keyPath, value) {
|
||||
keyPath = Config.keyToKeyPath(keyPath);
|
||||
@@ -94,6 +104,35 @@ Config.prototype.setPluginDependencies = function(deps) {
|
||||
return this.setValue('plugins', plugins);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
Update values for an existing configuration
|
||||
|
||||
@param {Object} values
|
||||
@returns {Config}
|
||||
*/
|
||||
Config.prototype.updateValues = function(values) {
|
||||
values = Immutable.fromJS(values);
|
||||
|
||||
return this.set('values', values);
|
||||
};
|
||||
|
||||
/**
|
||||
Update values for an existing configuration
|
||||
|
||||
@param {Config} config
|
||||
@param {Object} values
|
||||
@returns {Config}
|
||||
*/
|
||||
Config.prototype.mergeValues = function(values) {
|
||||
var currentValues = this.getValues();
|
||||
values = Immutable.fromJS(values);
|
||||
|
||||
currentValues = currentValues.mergeDeep(values);
|
||||
|
||||
return this.set('values', currentValues);
|
||||
};
|
||||
|
||||
/**
|
||||
Create a new config for a file
|
||||
|
||||
@@ -120,19 +159,6 @@ Config.createWithValues = function(values) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
Update values for an existing configuration
|
||||
|
||||
@param {Config} config
|
||||
@param {Object} values
|
||||
@returns {Config}
|
||||
*/
|
||||
Config.updateValues = function(config, values) {
|
||||
values = Immutable.fromJS(values);
|
||||
|
||||
return config.set('values', values);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
Convert a keyPath to an array of keys
|
||||
|
||||
@@ -2,7 +2,6 @@ var path = require('path');
|
||||
var Immutable = require('immutable');
|
||||
|
||||
var Output = require('../models/output');
|
||||
var Config = require('../models/config');
|
||||
var Promise = require('../utils/promise');
|
||||
var fs = require('../utils/fs');
|
||||
|
||||
@@ -41,7 +40,7 @@ function processOutput(generator, startOutput) {
|
||||
var book = output.getBook();
|
||||
var config = book.getConfig();
|
||||
|
||||
config = Config.updateValues(config, result);
|
||||
config = config.updateValues(result);
|
||||
book = book.set('config', config);
|
||||
return output.set('book', book);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,41 @@ describe('parseBook', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should extend configuration for multilingual book', function() {
|
||||
var fs = createMockFS({
|
||||
'LANGS.md': '# Languages\n\n* [en](en)\n* [fr](fr)',
|
||||
'book.json': '{ "title": "Test", "author": "GitBook" }',
|
||||
'en': {
|
||||
'README.md': 'Hello',
|
||||
'book.json': '{ "title": "Test EN" }'
|
||||
},
|
||||
'fr': {
|
||||
'README.md': 'Bonjour'
|
||||
}
|
||||
});
|
||||
var book = Book.createForFS(fs);
|
||||
|
||||
return parseBook(book)
|
||||
.then(function(resultBook) {
|
||||
var books = resultBook.getBooks();
|
||||
|
||||
expect(resultBook.isMultilingual()).toBe(true);
|
||||
expect(books.size).toBe(2);
|
||||
|
||||
var en = books.get('en');
|
||||
var fr = books.get('fr');
|
||||
|
||||
var enConfig = en.getConfig();
|
||||
var frConfig = fr.getConfig();
|
||||
|
||||
expect(enConfig.getValue('title')).toBe('Test EN');
|
||||
expect(enConfig.getValue('author')).toBe('GitBook');
|
||||
|
||||
expect(frConfig.getValue('title')).toBe('Test');
|
||||
expect(frConfig.getValue('author')).toBe('GitBook');
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse book in a directory', function() {
|
||||
var fs = createMockFS({
|
||||
'book.json': JSON.stringify({
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
var Promise = require('../utils/promise');
|
||||
var Config = require('../models/config');
|
||||
|
||||
var File = require('../models/file');
|
||||
var validateConfig = require('./validateConfig');
|
||||
var CONFIG_FILES = require('../constants/configFiles');
|
||||
|
||||
@@ -13,6 +11,7 @@ var CONFIG_FILES = require('../constants/configFiles');
|
||||
*/
|
||||
function parseConfig(book) {
|
||||
var fs = book.getFS();
|
||||
var config = book.getConfig();
|
||||
|
||||
return Promise.some(CONFIG_FILES, function(filename) {
|
||||
// Is this file ignored?
|
||||
@@ -38,13 +37,18 @@ function parseConfig(book) {
|
||||
})
|
||||
|
||||
.then(function(result) {
|
||||
var file = result? result.file : File();
|
||||
var values = result? result.values : {};
|
||||
|
||||
values = validateConfig(values);
|
||||
|
||||
var config = Config.create(file, values);
|
||||
return book.set('config', config);
|
||||
// Set the file
|
||||
if (result.file) {
|
||||
config = config.setFile(result.file);
|
||||
}
|
||||
|
||||
// Merge with old values
|
||||
config = config.mergeValues(values);
|
||||
|
||||
return book.setConfig(config);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user