summaryrefslogtreecommitdiffstats
path: root/lib/plugin.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/plugin.js')
-rw-r--r--lib/plugin.js41
1 files changed, 37 insertions, 4 deletions
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;