summaryrefslogtreecommitdiffstats
path: root/lib/api/encodeNavigation.js
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-04-30 20:15:08 +0200
committerSamy Pesse <samypesse@gmail.com>2016-04-30 20:15:08 +0200
commit36b49c66c6b75515bc84dd678fd52121a313e8d2 (patch)
treebc7e0f703d4557869943ec7f9495cac7a5027d4f /lib/api/encodeNavigation.js
parent87db7cf1d412fa6fbd18e9a7e4f4755f2c0c5547 (diff)
parent80b8e340dadc54377ff40500f86b1de631395806 (diff)
downloadgitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.zip
gitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.tar.gz
gitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.tar.bz2
Merge branch 'fixes'
Diffstat (limited to 'lib/api/encodeNavigation.js')
-rw-r--r--lib/api/encodeNavigation.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/api/encodeNavigation.js b/lib/api/encodeNavigation.js
new file mode 100644
index 0000000..8e329a1
--- /dev/null
+++ b/lib/api/encodeNavigation.js
@@ -0,0 +1,64 @@
+var Immutable = require('immutable');
+
+/**
+ 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()
+ };
+}
+
+/**
+ this.navigation is a deprecated property from GitBook v2
+
+ @param {Output}
+ @return {Object}
+*/
+function encodeNavigation(output) {
+ var book = output.getBook();
+ var pages = output.getPages();
+ var summary = book.getSummary();
+ var articles = summary.getArticlesAsList();
+
+
+ var navigation = 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(pages, prev) : undefined,
+ next: next? encodeArticle(pages, next) : undefined,
+ level: article.getLevel()
+ }
+ ];
+ })
+ .filter(function(e) {
+ return Boolean(e);
+ });
+
+ return Immutable.Map(navigation).toJS();
+}
+
+module.exports = encodeNavigation;