diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-22 12:31:02 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-22 12:31:02 +0100 |
commit | 395bd62663614c79181e3e27049345231186339b (patch) | |
tree | 638ffc52ad38918280d481d8af1264c5c3606ac9 /lib/plugins | |
parent | d8fd6430ed5e15d0dee33f730fb6e8e8346c866a (diff) | |
download | gitbook-395bd62663614c79181e3e27049345231186339b.zip gitbook-395bd62663614c79181e3e27049345231186339b.tar.gz gitbook-395bd62663614c79181e3e27049345231186339b.tar.bz2 |
Provide "plugins" context to theme templates
Diffstat (limited to 'lib/plugins')
-rw-r--r-- | lib/plugins/index.js | 11 | ||||
-rw-r--r-- | lib/plugins/plugin.js | 47 |
2 files changed, 42 insertions, 16 deletions
diff --git a/lib/plugins/index.js b/lib/plugins/index.js index ed3aa0a..bed4488 100644 --- a/lib/plugins/index.js +++ b/lib/plugins/index.js @@ -107,8 +107,17 @@ PluginsManager.prototype.hook = function(name, input) { }; // Extract all resources for a namespace -PluginsManager.prototype.resources = function(namespace) { +PluginsManager.prototype.getResources = function(namespace) { + return Promise.reduce(this.plugins, function(out, plugin) { + return plugin.getResources(namespace) + .then(function(pluginResources) { + _.each(BookPlugin.RESOURCES, function(resourceType) { + out[resourceType] = (out[resourceType] || []).concat(pluginResources[resourceType] || []); + }); + return out; + }); + }, {}); }; // Copy all resources for a plugin diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js index 094c82d..7fb44b2 100644 --- a/lib/plugins/plugin.js +++ b/lib/plugins/plugin.js @@ -34,6 +34,9 @@ function BookPlugin(book, pluginId) { this.packageInfos = undefined; this.content = undefined; + // Cache for resources + this._resources = {}; + _.bindAll(this); } @@ -191,25 +194,37 @@ BookPlugin.prototype.hook = function(name, input) { // Return resources without normalization BookPlugin.prototype._getResources = function(base) { - base = base; - var book = this.content[base]; + var that = this; + + return Promise() + .then(function() { + if (that._resources[base]) return that._resources[base]; - // Compatibility with version 1.x.x - if (base == 'website') book = book || this.content.book; + base = base; + var book = that.content[base]; - // Nothing specified, fallback to default - if (!book) { - return Promise({}); - } + // Compatibility with version 1.x.x + if (base == 'website') book = book || that.content.book; - // Dynamic function - if(typeof book === 'function') { - // Call giving it the context of our book - return Promise().then(book.bind(this.book)); - } + // Nothing specified, fallback to default + if (!book) { + return Promise({}); + } - // Plain data object - return Promise(_.cloneDeep(book)); + // Dynamic function + if(typeof book === 'function') { + // Call giving it the context of our book + return book.call(that.book); + } + + // Plain data object + return book; + }) + + .then(function(resources) { + that._resources[base] = resources; + return _.cloneDeep(resources); + }); }; // Normalize a specific resource @@ -256,3 +271,5 @@ BookPlugin.prototype.getBlocks = function() { }; module.exports = BookPlugin; +module.exports.RESOURCES = RESOURCES; + |