summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/parse/navigation.js19
1 files changed, 12 insertions, 7 deletions
diff --git a/lib/parse/navigation.js b/lib/parse/navigation.js
index 8d39a4e..52f3028 100644
--- a/lib/parse/navigation.js
+++ b/lib/parse/navigation.js
@@ -3,7 +3,7 @@ var _ = require('lodash');
// Cleans up an article/chapter object
// remove 'articles' and '_path' attributes
function clean(obj) {
- return _.omit(obj, ['articles', '_path']);
+ return obj && _.omit(obj, ['articles', '_path']);
}
// Returns a map of
@@ -26,13 +26,18 @@ function navigation(summary, files) {
// Walk the chapter/article tree and create navigation entires
_.each(summary.chapters, function(chapter, idx, chapters) {
var currentChapter = clean(chapter);
- var prevChapter = (idx-1 < 0) ? null : clean(chapters[idx-1]);
- var nextChapter = (idx+1 >= chapters.length) ? null : clean(chapters[idx+1]);
+ var prevChapter = (idx-1 < 0) ? null : chapters[idx-1];
+ var nextChapter = (idx+1 >= chapters.length) ? null : chapters[idx+1];
+
+ var prev = (!prevChapter || _.isEmpty(prevChapter.articles)) ?
+ prevChapter : _.last(prevChapter.articles);
+ var next = (!chapter || _.isEmpty(chapter.articles)) ?
+ nextChapter : _.first(chapter.articles);
// Add chapter mapping
mapping[chapter.path] = {
- prev: prevChapter,
- next: nextChapter,
+ prev: clean(prev),
+ next: clean(next),
};
// Check a chapter's articles
@@ -42,8 +47,8 @@ function navigation(summary, files) {
var next = (_idx+1 >= articles.length) ? nextChapter : clean(articles[_idx+1]);
mapping[article.path] = {
- prev: prev,
- next: next,
+ prev: clean(prev),
+ next: clean(next),
};
});
});