58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
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
|
|
};
|