diff options
-rw-r--r-- | lib/plugins/context.js | 2 | ||||
-rw-r--r-- | lib/plugins/plugin.js | 10 | ||||
-rw-r--r-- | test/node_modules/gitbook-plugin-test-filters/index.js | 21 | ||||
-rw-r--r-- | test/node_modules/gitbook-plugin-test-filters/package.json | 7 | ||||
-rw-r--r-- | test/plugins.js | 27 |
5 files changed, 58 insertions, 9 deletions
diff --git a/lib/plugins/context.js b/lib/plugins/context.js index a192b7b..17cefb6 100644 --- a/lib/plugins/context.js +++ b/lib/plugins/context.js @@ -19,7 +19,7 @@ function pluginCtx(plugin) { error.deprecateField(ctx, 'options', book.config.dump(), '"options" property is deprecated, use config.get(key) instead'); // Loop for template filters/blocks - error.deprecateField(ctx, 'book', ctx, '"book" property is deprecated, this directly instead'); + error.deprecateField(ctx, 'book', ctx, '"book" property is deprecated, use "this" directly instead'); return ctx; } diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js index 0fbbab1..4901579 100644 --- a/lib/plugins/plugin.js +++ b/lib/plugins/plugin.js @@ -270,10 +270,7 @@ BookPlugin.prototype.getFilters = function() { return _.mapValues(this.content.filters || {}, function(fn, filter) { return function() { - var ctx = _.extend( - this, - pluginCtx(that) - ); + var ctx = _.extend(pluginCtx(that), this); return fn.apply(ctx, arguments); }; @@ -289,10 +286,7 @@ BookPlugin.prototype.getBlocks = function() { var fn = block.exec; block.exec = function() { - var ctx = _.extend( - this, - pluginCtx(that) - ); + var ctx = _.extend(pluginCtx(that), this); return fn.apply(ctx, arguments); }; diff --git a/test/node_modules/gitbook-plugin-test-filters/index.js b/test/node_modules/gitbook-plugin-test-filters/index.js new file mode 100644 index 0000000..71f2752 --- /dev/null +++ b/test/node_modules/gitbook-plugin-test-filters/index.js @@ -0,0 +1,21 @@ +var should = require('should'); + +module.exports = { + filters: { + // Simple test + hello: function(s) { + return 'Hello ' + s + '!'; + }, + + // Test using the conetxt "this" + testContext: function(s) { + this.should.have.property('config'); + this.should.have.property('log'); + this.should.have.property('options'); + this.should.have.property('resolve').which.is.a.function; + this.should.have.property('book').which.equal(this); + + return 'Hello ' + s + '!'; + } + } +}; diff --git a/test/node_modules/gitbook-plugin-test-filters/package.json b/test/node_modules/gitbook-plugin-test-filters/package.json new file mode 100644 index 0000000..d555d06 --- /dev/null +++ b/test/node_modules/gitbook-plugin-test-filters/package.json @@ -0,0 +1,7 @@ +{ + "name": "gitbook-plugin-test-filters", + "version": "1.0.0", + "engines": { + "gitbook": "*" + } +}
\ No newline at end of file diff --git a/test/plugins.js b/test/plugins.js index 576c96b..03bbc26 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -1,3 +1,4 @@ +var _ = require('lodash'); var path = require('path'); var mock = require('./mock'); @@ -124,5 +125,31 @@ describe('Plugins', function() { }); }); }); + + describe('Filters', function() { + var plugin, filters; + + before(function() { + plugin = new BookPlugin(book, 'test-filters'); + return plugin.load(PLUGINS_ROOT) + + .then(function() { + filters = plugin.getFilters(); + }); + }); + + + it('should list all resources for website', function() { + _.size(filters).should.equal(2); + }); + + it('should correctly execute a filter', function() { + filters.hello('World').should.equal('Hello World!'); + }); + + it('should correctly set contexts for filter', function() { + filters.testContext('Hello'); + }); + }); }); |