diff options
Diffstat (limited to 'lib/api/encodeNavigation.js')
-rw-r--r-- | lib/api/encodeNavigation.js | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/lib/api/encodeNavigation.js b/lib/api/encodeNavigation.js index fa9eb16..c48ba50 100644 --- a/lib/api/encodeNavigation.js +++ b/lib/api/encodeNavigation.js @@ -1,14 +1,58 @@ +/** + Encode an article for next/prev + + @param {Map<String:Page>} + @param {Article} + @return {Object} +*/ +function encodeArticle(pages, article) { + var articlePath = article.getPath(); + + return { + path: articlePath, + title: article.getTitle(), + level: article.getLevel(), + exists: (articlePath && pages.has(articlePath)), + external: article.isExternal() + }; +} /** - page.navigation is a deprecated property from GitBook v2 + this.navigation is a deprecated property from GitBook v2 @param {Output} @return {Object} */ function encodeNavigation(output) { - // todo - return {}; + var book = output.getBook(); + var summary = book.getSummary(); + var articles = summary.getArticlesAsList(); + + return articles + .map(function(article, i) { + var ref = article.getRef(); + if (ref) { + return undefined; + } + + var prev = articles.get(i - 1); + var next = articles.get(i + 1); + + return [ + ref, + { + index: i, + title: article.getTitle(), + introduction: (i === 0), + prev: prev? encodeArticle(prev) : undefined, + next: next? encodeArticle(next) : undefined, + level: article.getLevel() + } + ]; + }) + .toMap() + .toJS(); } module.exports = encodeNavigation; |