diff options
author | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-03-31 17:35:59 -0700 |
---|---|---|
committer | Aaron O'Mullan <aaron.omullan@friendco.de> | 2014-03-31 17:36:01 -0700 |
commit | 752b26e963a231a55ffd3fe60e789635d4e9e6d6 (patch) | |
tree | 5b186279acc3066a7fbb60a92d5be58a6018c540 | |
parent | 45574cf7a51cc15daa685be463a116e91f0a13f5 (diff) | |
download | gitbook-752b26e963a231a55ffd3fe60e789635d4e9e6d6.zip gitbook-752b26e963a231a55ffd3fe60e789635d4e9e6d6.tar.gz gitbook-752b26e963a231a55ffd3fe60e789635d4e9e6d6.tar.bz2 |
Fix previous/next chapter navigation
-rw-r--r-- | lib/parse/navigation.js | 19 |
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), }; }); }); |