diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-26 09:41:26 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-26 09:41:26 +0100 |
commit | d3d64f636c859f7f01a64f7774cf70bd8ccdc562 (patch) | |
tree | 4f7731f37c3a793d187b0ab1cd77680e69534c6c /lib/plugins/compatibility.js | |
parent | 4cb9cbb5ae3aa8f9211ffa3ac5e3d34232c0ca4f (diff) | |
parent | eef072693b17526347c37b66078a5059c71caa31 (diff) | |
download | gitbook-d3d64f636c859f7f01a64f7774cf70bd8ccdc562.zip gitbook-d3d64f636c859f7f01a64f7774cf70bd8ccdc562.tar.gz gitbook-d3d64f636c859f7f01a64f7774cf70bd8ccdc562.tar.bz2 |
Merge pull request #1109 from GitbookIO/3.0.0
Version 3.0.0
Diffstat (limited to 'lib/plugins/compatibility.js')
-rw-r--r-- | lib/plugins/compatibility.js | 57 |
1 files changed, 57 insertions, 0 deletions
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 +}; |