diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-22 13:54:52 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-22 13:54:52 +0100 |
commit | 71144db09c150f6499d977863dbccf12ce05638b (patch) | |
tree | 690d9c6fff23e3a2b33493be6f5d16602240a9c1 /lib/plugins | |
parent | 395bd62663614c79181e3e27049345231186339b (diff) | |
download | gitbook-71144db09c150f6499d977863dbccf12ce05638b.zip gitbook-71144db09c150f6499d977863dbccf12ce05638b.tar.gz gitbook-71144db09c150f6499d977863dbccf12ce05638b.tar.bz2 |
Normalize context for plugins
Diffstat (limited to 'lib/plugins')
-rw-r--r-- | lib/plugins/context.js | 24 | ||||
-rw-r--r-- | lib/plugins/plugin.js | 16 |
2 files changed, 34 insertions, 6 deletions
diff --git a/lib/plugins/context.js b/lib/plugins/context.js new file mode 100644 index 0000000..51649aa --- /dev/null +++ b/lib/plugins/context.js @@ -0,0 +1,24 @@ +var error = require('../utils/error'); + +/* + Return the context for a plugin. + It tries to keep compatibilities with GitBook v2 +*/ + +function pluginCtx(plugin) { + var book = plugin.book; + var ctx = { + config: book.config, + log: plugin.log, + + // Paths + resolve: book.resolve + }; + + // Deprecation + error.deprecateField(ctx, 'options', book.config.dump(), '"options" property is deprecated, use config.get(key) instead'); + + return ctx; +} + +module.exports = pluginCtx; 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 |