summaryrefslogtreecommitdiffstats
path: root/lib/plugins
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-02-17 21:43:54 +0100
committerSamy Pesse <samypesse@gmail.com>2016-02-17 21:43:54 +0100
commit004ac8851622cb4583b5569ae3480344855a7880 (patch)
treea3f5ee57686b971e493403988504780ea18164e3 /lib/plugins
parent3d1eac234a1633f4822fd019464434124875b7b1 (diff)
downloadgitbook-004ac8851622cb4583b5569ae3480344855a7880.zip
gitbook-004ac8851622cb4583b5569ae3480344855a7880.tar.gz
gitbook-004ac8851622cb4583b5569ae3480344855a7880.tar.bz2
Add method .hook to plugins
Diffstat (limited to 'lib/plugins')
-rw-r--r--lib/plugins/index.js6
-rw-r--r--lib/plugins/plugin.js25
2 files changed, 31 insertions, 0 deletions
diff --git a/lib/plugins/index.js b/lib/plugins/index.js
index 6eb788b..343c569 100644
--- a/lib/plugins/index.js
+++ b/lib/plugins/index.js
@@ -95,7 +95,13 @@ PluginsManager.prototype.install = function() {
return registry.install(that.book, plugin.name, plugin.version);
})
.thenResolve(plugins.length);
+};
+// Call a hook on all plugins to transform an input
+PluginsManager.prototype.hook = function(name, input) {
+ return Promise.reduce(this.plugins, function(current, plugin) {
+ return plugin.hook(name, current);
+ }, input);
};
module.exports = PluginsManager;
diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js
index ae8886c..9f9cc35 100644
--- a/lib/plugins/plugin.js
+++ b/lib/plugins/plugin.js
@@ -7,6 +7,10 @@ var error = require('../utils/error');
var gitbook = require('../gitbook');
var registry = require('./registry');
+var HOOKS = [
+ 'init', 'finish', 'finish:before', 'config', 'page', 'page:before'
+];
+
// Return true if an error is a "module not found"
// Wait on https://github.com/substack/node-resolve/pull/81 to be merged
function isModuleNotFound(err) {
@@ -109,4 +113,25 @@ BookPlugin.prototype.validate = function() {
}
};
+// Call a hook and returns its result
+BookPlugin.prototype.hook = function(name, input) {
+ // Our book will be the context to apply
+ var context = this.book;
+
+ var hookFunc = this.content.hooks? this.content.hooks[name] : null;
+ input = input || {};
+
+ if (!hookFunc) return Promise(input);
+
+ this.book.log.debug.ln('call hook "' + name + '" for plugin "' + this.id + '"');
+ if (!_.contains(HOOKS, name)) {
+ this.book.log.warn.ln('hook "'+name+'" used by plugin "'+this.name+'" is deprecated, and will be removed in the coming versions');
+ }
+
+ return Promise()
+ .then(function() {
+ return hookFunc.apply(context, [input]);
+ });
+};
+
module.exports = BookPlugin;