diff options
-rw-r--r-- | lib/plugins/plugin.js | 26 | ||||
-rw-r--r-- | test/node_modules/gitbook-plugin-test-resources/index.js | 12 | ||||
-rw-r--r-- | test/node_modules/gitbook-plugin-test-resources/package.json | 7 | ||||
-rw-r--r-- | test/plugins.js | 24 |
4 files changed, 66 insertions, 3 deletions
diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js index df79184..0d69cd9 100644 --- a/lib/plugins/plugin.js +++ b/lib/plugins/plugin.js @@ -1,5 +1,6 @@ var _ = require('lodash'); var path = require('path'); +var url = require('url'); var resolve = require('resolve'); var mergeDefaults = require('merge-defaults'); var jsonschema = require('jsonschema'); @@ -191,10 +192,10 @@ BookPlugin.prototype.hook = function(name, input) { // Return resources without normalization BookPlugin.prototype._getResources = function(base) { base = base; - var book = this.infos[base]; + var book = this.content[base]; // Compatibility with version 1.x.x - if (base == 'website') book = book || this.infos.book; + if (base == 'website') book = book || this.content.book; // Nothing specified, fallback to default if (!book) { @@ -211,6 +212,25 @@ BookPlugin.prototype._getResources = function(base) { return Promise(_.cloneDeep(book)); }; +// Normalize a specific resource +BookPlugin.prototype.normalizeResource = function(resource) { + // Parse the resource path + var parsed = url.parse(resource); + + // This is a remote resource + // so we will simply link to using it's URL + if (parsed.protocol) { + return { + 'url': resource + }; + } + + // This will be copied over from disk + // and shipped with the book's build + return { 'path': this.npmId+'/'+resource }; +}; + + // Normalize resources and return them BookPlugin.prototype.getResources = function(base) { var that = this; @@ -219,7 +239,7 @@ BookPlugin.prototype.getResources = function(base) { .then(function(resources) { _.each(RESOURCES, function(resourceType) { - resources[resourceType] = (resources[resourceType] || []).map(that.normalizeResource); + resources[resourceType] = _.map(resources[resourceType] || [], that.normalizeResource); }); return resources; diff --git a/test/node_modules/gitbook-plugin-test-resources/index.js b/test/node_modules/gitbook-plugin-test-resources/index.js new file mode 100644 index 0000000..e95e411 --- /dev/null +++ b/test/node_modules/gitbook-plugin-test-resources/index.js @@ -0,0 +1,12 @@ +module.exports = { + book: { + assets: './assets', + js: [ + 'myfile.js', + 'https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js' + ], + css: [ + 'myfile.css' + ] + } +}; diff --git a/test/node_modules/gitbook-plugin-test-resources/package.json b/test/node_modules/gitbook-plugin-test-resources/package.json new file mode 100644 index 0000000..606de31 --- /dev/null +++ b/test/node_modules/gitbook-plugin-test-resources/package.json @@ -0,0 +1,7 @@ +{ + "name": "gitbook-plugin-test-resources", + "version": "1.0.0", + "engines": { + "gitbook": "*" + } +}
\ No newline at end of file diff --git a/test/plugins.js b/test/plugins.js index 4fd59d4..576c96b 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -100,5 +100,29 @@ describe('Plugins', function() { }); }); }); + + describe('Resources', function() { + var plugin; + + before(function() { + plugin = new BookPlugin(book, 'test-resources'); + return plugin.load(PLUGINS_ROOT); + }); + + + it('should list all resources for website', function() { + return plugin.getResources('website') + .then(function(resources) { + resources.assets.should.equal('./assets'); + + resources.js.should.have.lengthOf(2); + resources.js[0].path.should.equal('gitbook-plugin-test-resources/myfile.js'); + resources.js[1].url.should.equal('https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js'); + + resources.css.should.have.lengthOf(1); + resources.css[0].path.should.equal('gitbook-plugin-test-resources/myfile.css'); + }); + }); + }); }); |