mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-24 19:32:07 +00:00
Add base for calling hook on page/global
This commit is contained in:
+14
-1
@@ -77,6 +77,19 @@ 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
|
||||
@@ -87,6 +100,6 @@ Config.createWithValues = function(values) {
|
||||
Config.keyToKeyPath = function(keyPath) {
|
||||
if (is.string(keyPath)) keyPath = keyPath.split('.');
|
||||
return keyPath;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Config;
|
||||
|
||||
+27
-3
@@ -1,15 +1,39 @@
|
||||
var Promise = require('../utils/promise');
|
||||
|
||||
function defaultGetArgument() {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function defaultHandleResult(output, result) {
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
Call a "global" hook for an output
|
||||
|
||||
@param {String} name
|
||||
@param {Function(Output) -> Mixed} getArgument
|
||||
@param {Function(Output, result) -> Output} handleResult
|
||||
@param {Output} output
|
||||
@return {Promise<Output>}
|
||||
*/
|
||||
function callHook(name, output) {
|
||||
function callHook(name, getArgument, handleResult, output) {
|
||||
getArgument = getArgument || defaultGetArgument;
|
||||
handleResult = handleResult || defaultHandleResult;
|
||||
|
||||
var plugins = output.getPlugins();
|
||||
|
||||
return Promise(getArgument(output))
|
||||
.then(function(arg) {
|
||||
return Promise.reduce(plugins, function(prev, plugin) {
|
||||
var hook = plugin.getHook(name);
|
||||
|
||||
return hook(prev);
|
||||
}, arg);
|
||||
})
|
||||
.then(function(result) {
|
||||
return handleResult(output, result);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = callHook;
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
var callHook = require('./callHook');
|
||||
|
||||
/**
|
||||
Call a hook for a specific page
|
||||
|
||||
@param {String} name
|
||||
@param {Output} output
|
||||
@param {Page} page
|
||||
@return {Page}
|
||||
*/
|
||||
function callPageHook(name, output, page) {
|
||||
return callHook(
|
||||
name,
|
||||
|
||||
function(out) {
|
||||
|
||||
|
||||
},
|
||||
|
||||
function(result) {
|
||||
|
||||
|
||||
},
|
||||
|
||||
output
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = callPageHook;
|
||||
@@ -1,4 +1,5 @@
|
||||
var Output = require('../models/output');
|
||||
var Config = require('../models/config');
|
||||
var Promise = require('../utils/promise');
|
||||
|
||||
var callHook = require('./callHook');
|
||||
@@ -28,7 +29,7 @@ var generatePages = require('./generatePages');
|
||||
@param {Book} book
|
||||
@param {Object} options
|
||||
|
||||
@return {Promise}
|
||||
@return {Promise<Output>}
|
||||
*/
|
||||
function generateBook(generator, book, options) {
|
||||
options = generator.Options(options);
|
||||
@@ -39,8 +40,50 @@ function generateBook(generator, book, options) {
|
||||
.then(preparePlugins)
|
||||
.then(preparePages)
|
||||
.then(prepareAssets)
|
||||
|
||||
.then(
|
||||
callHook.bind(null,
|
||||
'config',
|
||||
function(output) {
|
||||
var book = output.getBook();
|
||||
var config = book.getConfig();
|
||||
var values = config.getValues();
|
||||
|
||||
return values.toJS();
|
||||
},
|
||||
function(output, result) {
|
||||
var book = output.getBook();
|
||||
var config = book.getConfig();
|
||||
|
||||
config = Config.updateValues(config, result);
|
||||
book = book.set('config', config);
|
||||
return output.set('book', book);
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
.then(callHook.bind(null, 'init'))
|
||||
|
||||
.then(function(output) {
|
||||
if (!generator.onInit) {
|
||||
return output;
|
||||
}
|
||||
|
||||
return generator.onInit(output);
|
||||
})
|
||||
|
||||
.then(generateAssets.bind(null, generator))
|
||||
.then(generatePages.bind(null, generator));
|
||||
.then(generatePages.bind(null, generator))
|
||||
|
||||
.then(callHook.bind(null, 'finish:before'))
|
||||
|
||||
.then(function(output) {
|
||||
if (!generator.onFinish) {
|
||||
return output;
|
||||
}
|
||||
|
||||
return generator.onFinish(output);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = generateBook;
|
||||
|
||||
Reference in New Issue
Block a user