summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-10-13 14:31:11 +0200
committerSamy Pessé <samypesse@gmail.com>2015-10-13 14:31:11 +0200
commitb742b29259aced60ec03d5577da96a2c7f2e1277 (patch)
treed8582baaf807233856fd2958664f7be45a03dc14 /lib
parentdf5d27ab6103389294795a7fada76d46f36fc00f (diff)
downloadgitbook-b742b29259aced60ec03d5577da96a2c7f2e1277.zip
gitbook-b742b29259aced60ec03d5577da96a2c7f2e1277.tar.gz
gitbook-b742b29259aced60ec03d5577da96a2c7f2e1277.tar.bz2
Add tests for plugins config validation
Diffstat (limited to 'lib')
-rw-r--r--lib/configuration.js12
-rw-r--r--lib/plugin.js41
-rw-r--r--lib/pluginslist.js4
3 files changed, 40 insertions, 17 deletions
diff --git a/lib/configuration.js b/lib/configuration.js
index 64d4b17..d514720 100644
--- a/lib/configuration.js
+++ b/lib/configuration.js
@@ -168,7 +168,7 @@ Configuration.prototype.extend = function(options) {
_.extend(this.options, options);
};
-// Replace the configuration
+// Replace the whole configuration
Configuration.prototype.replace = function(options) {
this.options = _.cloneDeep(DEFAULT_CONFIG);
this.options = _.merge(this.options, options || {});
@@ -199,16 +199,6 @@ Configuration.prototype.set = function(key, value) {
return _.set(this.options, key, value);
};
-// Return a configuration for a plugin
-Configuration.prototype.getForPlugin = function(pluginName) {
- return this.get('pluginsConfig.'+pluginName, {});
-};
-
-// Set a configuration for a plugin
-Configuration.prototype.setForPlugin = function(pluginName, config) {
- return this.get('pluginsConfig.'+pluginName, config);
-};
-
// Default configuration
Configuration.DEFAULT = DEFAULT_CONFIG;
diff --git a/lib/plugin.js b/lib/plugin.js
index 45d77b8..4095231 100644
--- a/lib/plugin.js
+++ b/lib/plugin.js
@@ -10,9 +10,18 @@ var jsonSchemaDefaults = require('json-schema-defaults');
var pkg = require('../package.json');
+var PLUGIN_PREFIX = 'gitbook-plugin-';
+
+// Return an absolute name for the plugin (the one on NPM)
+function absoluteName(name) {
+ if (name.indexOf(PLUGIN_PREFIX) === 0) return name;
+ return [PLUGIN_PREFIX, name].join('');
+}
+
+
var Plugin = function(book, name) {
this.book = book;
- this.name = name;
+ this.name = absoluteName(name);
this.packageInfos = {};
this.infos = {};
@@ -20,8 +29,7 @@ var Plugin = function(book, name) {
_.bindAll(this);
_.each([
- 'gitbook-plugin-'+name,
- 'gitbook-'+name,
+ absoluteName(name),
name
], function(_name) {
// Load from the book
@@ -38,6 +46,13 @@ Plugin.HOOKS = [
'init', 'finish', 'finish:before', 'config', 'page', 'page:before'
];
+// Return the reduce name for the plugin
+// "gitbook-plugin-test" -> "test"
+// Return a relative name for the plugin (the one on GitBook)
+Plugin.prototype.reducedName = function() {
+ return this.name.replace(PLUGIN_PREFIX, '');
+};
+
// Load from a name
Plugin.prototype.load = function(name, baseDir) {
try {
@@ -152,11 +167,19 @@ Plugin.prototype.validateConfig = function(config) {
if (!schema) return config;
// Normalize schema
+ schema.id = '/pluginsConfig.'+that.reducedName();
schema.type = 'object';
// Validate and throw if invalid
var v = new jsonschema.Validator();
- v.validate(config, schema);
+ var result = v.validate(config, schema, {
+ propertyName: 'pluginsConfig.'+that.reducedName()
+ });
+
+ // Throw error
+ if (result.errors.length > 0) {
+ throw new Error('Configuration Error: '+result.errors[0].stack);
+ }
// Insert default values
var defaults = jsonSchemaDefaults(schema);
@@ -205,4 +228,14 @@ Plugin.prototype.copyAssets = function(out, base) {
}, _.constant(false));
};
+// Get config from book
+Plugin.prototype.getConfig = function() {
+ return this.book.config.get('pluginsCOnfig.'+this.reducedName());
+};
+
+// Set configuration for this plugin
+Plugin.prototype.setConfig = function(values) {
+ return this.book.config.set('pluginsCOnfig.'+this.reducedName(), values);
+};
+
module.exports = Plugin;
diff --git a/lib/pluginslist.js b/lib/pluginslist.js
index 636eddc..8830950 100644
--- a/lib/pluginslist.js
+++ b/lib/pluginslist.js
@@ -82,12 +82,12 @@ PluginsList.prototype.load = function(plugin) {
// Validate and normalize configuration
.then(function() {
- var config = that.book.config.getForPlugin(plugin.name);
+ var config = plugin.getConfig();
return plugin.validateConfig(config);
})
.then(function(config) {
// Update configuration
- that.book.config.getForPlugin(plugin.name, config);
+ plugin.setConfig(config);
// Extract filters
that.book.template.addFilters(plugin.getFilters());