diff options
Diffstat (limited to 'lib/plugins/plugin.js')
-rw-r--r-- | lib/plugins/plugin.js | 25 |
1 files changed, 25 insertions, 0 deletions
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; |