mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-22 10:33:22 +00:00
Use hook to allow plugins to edit page content during generation (Fixes #176)
This commit is contained in:
@@ -12,8 +12,8 @@ var BaseGenerator = function(options) {
|
||||
this.plugins = [];
|
||||
};
|
||||
|
||||
BaseGenerator.prototype.callHook = function(name) {
|
||||
return this.plugins.hook(name, this);
|
||||
BaseGenerator.prototype.callHook = function(name, data) {
|
||||
return this.plugins.hook(name, this, data);
|
||||
};
|
||||
|
||||
BaseGenerator.prototype.loadPlugins = function() {
|
||||
|
||||
@@ -74,15 +74,15 @@ Plugin.prototype.resolveFile = function(filename) {
|
||||
};
|
||||
|
||||
// Resolve file path
|
||||
Plugin.prototype.callHook = function(name, args) {
|
||||
Plugin.prototype.callHook = function(name, context, data) {
|
||||
var hookFunc = this.infos.hooks? this.infos.hooks[name] : null;
|
||||
args = _.isArray(args) ? args : [args];
|
||||
data = data || {};
|
||||
|
||||
if (!hookFunc) return Q();
|
||||
if (!hookFunc) return Q(data);
|
||||
|
||||
return Q()
|
||||
.then(function() {
|
||||
return hookFunc.apply(null, args);
|
||||
return hookFunc.apply(context, [data]);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -159,12 +159,12 @@ Plugin.fromList = function(names, root) {
|
||||
return Q({
|
||||
'list': plugins,
|
||||
'resources': resources,
|
||||
'hook': function(name, args) {
|
||||
'hook': function(name, context, data) {
|
||||
return _.reduce(plugins, function(prev, plugin) {
|
||||
return prev.then(function() {
|
||||
return plugin.callHook(name, args);
|
||||
return prev.then(function(ret) {
|
||||
return plugin.callHook(name, context, ret);
|
||||
})
|
||||
}, Q());
|
||||
}, Q(data));
|
||||
},
|
||||
'template': function(name) {
|
||||
var withTpl = _.find(plugins, function(plugin) {
|
||||
|
||||
@@ -124,13 +124,20 @@ Generator.prototype.convertFile = function(content, _input) {
|
||||
});
|
||||
})
|
||||
.then(function(sections) {
|
||||
// Use plugin hook
|
||||
return that.callHook("page", {
|
||||
sections: sections,
|
||||
progress: progress
|
||||
})
|
||||
})
|
||||
.then(function(_page) {
|
||||
that.manifest.add("CACHE", _output);
|
||||
|
||||
return that._writeTemplate(that.template, {
|
||||
progress: progress,
|
||||
progress: _page.progress,
|
||||
|
||||
_input: _input,
|
||||
content: sections,
|
||||
content: _page.sections,
|
||||
|
||||
basePath: basePath,
|
||||
staticBase: path.join(basePath, "gitbook"),
|
||||
|
||||
Reference in New Issue
Block a user