summaryrefslogtreecommitdiffstats
path: root/lib/plugins/compatibility.js
blob: 9f80c38a18a49afd560b0656577a4ebc0a4adad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 = book;

    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
};