summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/page/index.js9
-rw-r--r--lib/plugins/compatibility.js57
-rw-r--r--lib/plugins/context.js27
-rw-r--r--lib/plugins/plugin.js8
4 files changed, 68 insertions, 33 deletions
diff --git a/lib/page/index.js b/lib/page/index.js
index e8c4de7..8b4fcc9 100644
--- a/lib/page/index.js
+++ b/lib/page/index.js
@@ -7,6 +7,7 @@ var pathUtil = require('../utils/path');
var location = require('../utils/location');
var parsers = require('../parsers');
var gitbook = require('../gitbook');
+var pluginCompatibility = require('../plugins/compatibility');
var HTMLPipeline = require('./html');
/*
@@ -142,10 +143,14 @@ Page.prototype.toHTML = function(output) {
this.log.debug.ln('start parsing file', this.path);
+ // Call a hook in the output
+ // using an utility to "keep" compatibility with gitbook 2
function hook(name) {
- return output.plugins.hook(name, that.getContext().page)
+ return pluginCompatibility.pageHook(that, function(ctx) {
+ return output.plugins.hook(name, ctx);
+ })
.then(function(result) {
- if(result) that.update(result.content);
+ if(_.isString(result)) that.update(result);
});
}
diff --git a/lib/plugins/compatibility.js b/lib/plugins/compatibility.js
new file mode 100644
index 0000000..7ad35a9
--- /dev/null
+++ b/lib/plugins/compatibility.js
@@ -0,0 +1,57 @@
+var _ = require('lodash');
+var error = require('../utils/error');
+
+/*
+ Return the context for a plugin.
+ It tries to keep compatibilities with GitBook v2
+*/
+function pluginCtx(plugin) {
+ var book = plugin.book;
+ var ctx = {
+ config: book.config,
+ log: plugin.log,
+
+ // Paths
+ resolve: book.resolve
+ };
+
+ // Deprecation
+ 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, use "this" directly instead');
+
+ return ctx;
+}
+
+// Call a function "fn" with a context of page similar to the one in GitBook v2
+function pageHook(page, fn) {
+ var ctx = {
+ type: page.type,
+ content: page.content,
+ path: page.path,
+ rawPath: page.rawPath
+ };
+
+ // Deprecate sections
+ error.deprecateField(ctx, 'sections', [
+ { content: ctx.content }
+ ], '"sections" property is deprecated, use page.content instead');
+
+ return fn(ctx)
+ .then(function(result) {
+ if (!result) return undefined;
+ if (result.content) {
+ return result.content;
+ }
+
+ if (result.sections) {
+ return _.pluck(result.sections, 'content').join('\n');
+ }
+ });
+}
+
+module.exports = {
+ pluginCtx: pluginCtx,
+ pageHook: pageHook
+};
diff --git a/lib/plugins/context.js b/lib/plugins/context.js
deleted file mode 100644
index 17cefb6..0000000
--- a/lib/plugins/context.js
+++ /dev/null
@@ -1,27 +0,0 @@
-var error = require('../utils/error');
-
-/*
- Return the context for a plugin.
- It tries to keep compatibilities with GitBook v2
-*/
-
-function pluginCtx(plugin) {
- var book = plugin.book;
- var ctx = {
- config: book.config,
- log: plugin.log,
-
- // Paths
- resolve: book.resolve
- };
-
- // Deprecation
- 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, use "this" directly instead');
-
- return ctx;
-}
-
-module.exports = pluginCtx;
diff --git a/lib/plugins/plugin.js b/lib/plugins/plugin.js
index 5329c66..f678111 100644
--- a/lib/plugins/plugin.js
+++ b/lib/plugins/plugin.js
@@ -10,7 +10,7 @@ var Promise = require('../utils/promise');
var error = require('../utils/error');
var gitbook = require('../gitbook');
var registry = require('./registry');
-var pluginCtx = require('./context');
+var compatibility = require('./compatibility');
var HOOKS = [
'init', 'finish', 'finish:before', 'config', 'page', 'page:before'
@@ -48,7 +48,7 @@ BookPlugin.prototype.isLoaded = function() {
// Bind a function to the plugin's context
BookPlugin.prototype.bind = function(fn) {
- return fn.bind(pluginCtx(this));
+ return fn.bind(compatibility.pluginCtx(this));
};
// Load this plugin
@@ -270,7 +270,7 @@ BookPlugin.prototype.getFilters = function() {
return _.mapValues(this.content.filters || {}, function(fn, filter) {
return function() {
- var ctx = _.extend(pluginCtx(that), this);
+ var ctx = _.extend(compatibility.pluginCtx(that), this);
return fn.apply(ctx, arguments);
};
@@ -286,7 +286,7 @@ BookPlugin.prototype.getBlocks = function() {
var fn = block.process;
block.process = function() {
- var ctx = _.extend(pluginCtx(that), this);
+ var ctx = _.extend(compatibility.pluginCtx(that), this);
return fn.apply(ctx, arguments);
};