diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-28 14:28:41 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-28 14:28:41 +0200 |
commit | f946650ddb6b075aa626cc165b7b9ea53924a5b1 (patch) | |
tree | 44a1b2f31ea28ab1cba9c956026fb35f667d14b9 /lib | |
parent | 14fd6bbdce87a8b4c60a9dc571608428d738aafc (diff) | |
download | gitbook-f946650ddb6b075aa626cc165b7b9ea53924a5b1.zip gitbook-f946650ddb6b075aa626cc165b7b9ea53924a5b1.tar.gz gitbook-f946650ddb6b075aa626cc165b7b9ea53924a5b1.tar.bz2 |
Add "next" and "previous" to page JSON repr
Diffstat (limited to 'lib')
-rw-r--r-- | lib/json/encodePage.js | 13 | ||||
-rw-r--r-- | lib/json/encodeSummaryArticle.js | 12 | ||||
-rw-r--r-- | lib/models/summary.js | 41 | ||||
-rw-r--r-- | lib/output/callPageHook.js | 2 |
4 files changed, 61 insertions, 7 deletions
diff --git a/lib/json/encodePage.js b/lib/json/encodePage.js index d876c78..be92117 100644 --- a/lib/json/encodePage.js +++ b/lib/json/encodePage.js @@ -1,3 +1,5 @@ +var encodeSummaryArticle = require('./encodeSummaryArticle'); + /** Return a JSON representation of a page @@ -10,7 +12,6 @@ function encodePage(page, summary) { var attributes = page.getAttributes(); var article = summary.getByPath(file.getPath()); - var result = attributes.toJS(); if (article) { @@ -18,7 +19,15 @@ function encodePage(page, summary) { result.level = article.getLevel(); result.depth = article.getDepth(); - // todo: next and prev + var nextArticle = summary.getNextArticle(article); + if (nextArticle) { + result.next = encodeSummaryArticle(nextArticle); + } + + var prevArticle = summary.getPrevArticle(article); + if (prevArticle) { + result.previous = encodeSummaryArticle(prevArticle); + } } result.content = page.getContent(); diff --git a/lib/json/encodeSummaryArticle.js b/lib/json/encodeSummaryArticle.js index b3f977a..987e44a 100644 --- a/lib/json/encodeSummaryArticle.js +++ b/lib/json/encodeSummaryArticle.js @@ -5,7 +5,14 @@ @param {SummaryArticle} @return {Object} */ -function encodeSummaryArticle(article) { +function encodeSummaryArticle(article, recursive) { + var articles = undefined; + if (recursive !== false) { + articles = article.getArticles() + .map(encodeSummaryArticle) + .toJS(); + } + return { title: article.getTitle(), level: article.getLevel(), @@ -13,8 +20,7 @@ function encodeSummaryArticle(article) { anchor: article.getAnchor(), url: article.getUrl(), path: article.getPath(), - articles: article.getArticles() - .map(encodeSummaryArticle).toJS() + articles: articles }; } diff --git a/lib/models/summary.js b/lib/models/summary.js index b04630f..3b46941 100644 --- a/lib/models/summary.js +++ b/lib/models/summary.js @@ -1,3 +1,4 @@ +var is = require('is'); var Immutable = require('immutable'); var error = require('../utils/error'); @@ -88,6 +89,46 @@ Summary.prototype.getFirstArticle = function() { }; /** + Return next article of an article + + @param {Article} current + @return {Article} +*/ +Summary.prototype.getNextArticle = function(current) { + var level = is.string(current)? current : current.getLevel(); + var wasPrev = false; + + return this.getArticle(function(article) { + if (wasPrev) return true; + + wasPrev = article.getLevel() == level; + return false; + }); +}; + +/** + Return previous article of an article + + @param {Article} current + @return {Article} +*/ +Summary.prototype.getPrevArticle = function(current) { + var level = is.string(current)? current : current.getLevel(); + var prev = undefined; + + this.getArticle(function(article) { + if (article.getLevel() == level) { + return true; + } + + prev = article; + return false; + }); + + return prev; +}; + +/** Render summary as text @return {Promise<String>} diff --git a/lib/output/callPageHook.js b/lib/output/callPageHook.js index 0582da9..c66cef0 100644 --- a/lib/output/callPageHook.js +++ b/lib/output/callPageHook.js @@ -1,8 +1,6 @@ var Api = require('../api'); var callHook = require('./callHook'); -var Promise = require('../utils/promise'); - /** Call a hook for a specific page |