diff options
Diffstat (limited to 'lib/plugins/plugin.js')
-rw-r--r-- | lib/plugins/plugin.js | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js index 7fb44b2..c262887 100644 --- a/lib/plugins/plugin.js +++ b/lib/plugins/plugin.js @@ -10,6 +10,7 @@ var Promise = require('../utils/promise'); var error = require('../utils/error'); var gitbook = require('../gitbook'); var registry = require('./registry'); +var pluginCtx = require('./context'); var HOOKS = [ 'init', 'finish', 'finish:before', 'config', 'page', 'page:before' @@ -25,7 +26,7 @@ function isModuleNotFound(err) { function BookPlugin(book, pluginId) { this.book = book; - this.log = this.book.log; + this.log = this.book.log.prefix(pluginId); this.id = pluginId; this.npmId = registry.npmId(pluginId); @@ -45,6 +46,11 @@ BookPlugin.prototype.isLoaded = function() { return Boolean(this.packageInfos && this.content); }; +// Bind a function to the plugin's context +BookPlugin.prototype.bind = function(fn) { + return fn.bind(pluginCtx(this)); +}; + // Load this plugin // An optional folder to search in can be passed BookPlugin.prototype.load = function(folder) { @@ -173,9 +179,7 @@ BookPlugin.prototype.getConfigKey = 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 that = this; var hookFunc = this.content.hooks? this.content.hooks[name] : null; input = input || {}; @@ -188,7 +192,7 @@ BookPlugin.prototype.hook = function(name, input) { return Promise() .then(function() { - return hookFunc.apply(context, [input]); + return that.bind(hookFunc)(input); }); }; @@ -214,7 +218,7 @@ BookPlugin.prototype._getResources = function(base) { // Dynamic function if(typeof book === 'function') { // Call giving it the context of our book - return book.call(that.book); + return that.bind(book)(); } // Plain data object |