summaryrefslogtreecommitdiffstats
path: root/packages/gitbook/src/json/encodePage.js
blob: 0671721a29b8c0e0e91197bb33f5fde1075cac15 (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
const encodeSummaryArticle = require('./encodeSummaryArticle');

/**
 * Return a JSON representation of a page.
 *
 * @param  {Page} page
 * @param  {Summary} summary
 * @param  {URIIndex} urls
 * @return {JSON} json
 */
function encodePage(page, summary, urls) {
    const file = page.getFile();
    const attributes = page.getAttributes();
    const article = summary.getByPath(file.getPath());

    const result = {
        content:    page.getContent(),
        dir:        page.getDir(),
        attributes: attributes.toJS()
    };

    if (article) {
        result.title = article.getTitle();
        result.level = article.getLevel();
        result.depth = article.getDepth();

        const nextArticle = summary.getNextArticle(article);
        if (nextArticle) {
            result.next = encodeSummaryArticle(nextArticle, urls, false);
        }

        const prevArticle = summary.getPrevArticle(article);
        if (prevArticle) {
            result.previous = encodeSummaryArticle(prevArticle, urls, false);
        }
    }

    return result;
}

module.exports = encodePage;