diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-04-28 22:45:50 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-04-28 22:45:50 +0200 |
commit | 68b9bf7e38867eed8763854a731f15959aaf65b5 (patch) | |
tree | b9e20ba59d355758a64c94eed05fbfdca8b92a7b /lib/api | |
parent | 826f90505c1a70ad2a8cf3715bac6d5efaeba22c (diff) | |
download | gitbook-68b9bf7e38867eed8763854a731f15959aaf65b5.zip gitbook-68b9bf7e38867eed8763854a731f15959aaf65b5.tar.gz gitbook-68b9bf7e38867eed8763854a731f15959aaf65b5.tar.bz2 |
Encode navigation for compatibility with gitbook v2
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/encodeGlobal.js | 6 | ||||
-rw-r--r-- | lib/api/encodeNavigation.js | 50 | ||||
-rw-r--r-- | lib/api/encodePage.js | 8 | ||||
-rw-r--r-- | lib/api/encodeProgress.js | 16 |
4 files changed, 72 insertions, 8 deletions
diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js index bbd693b..78919d3 100644 --- a/lib/api/encodeGlobal.js +++ b/lib/api/encodeGlobal.js @@ -4,6 +4,7 @@ var PathUtils = require('../utils/path'); var deprecate = require('./deprecate'); var encodeConfig = require('./encodeConfig'); +var encodeNavigation = require('./encodeNavigation'); var fileToURL = require('../output/helper/fileToURL'); /** @@ -79,6 +80,11 @@ function encodeGlobal(output) { } }; + // Deprecated properties + + deprecate.field(output, 'this.navigation', result, 'sections', encodeNavigation(output), + '"navigation" property is deprecated'); + deprecate.field(output, 'this.book', result, 'book', result, '"book" property is deprecated, use "this" directly instead'); 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; diff --git a/lib/api/encodePage.js b/lib/api/encodePage.js index b212c29..1465137 100644 --- a/lib/api/encodePage.js +++ b/lib/api/encodePage.js @@ -1,6 +1,6 @@ var JSONUtils = require('../json'); var deprecate = require('./deprecate'); -var encodeNavigation = require('./encodeNavigation'); +var encodeProgress = require('./encodeProgress'); /** Encode a page in a context to a JS API @@ -24,11 +24,9 @@ function encodePage(output, page) { // todo: (as deprecated) // page.progress - // page.navigation - // Deprecated properties - deprecate.field(output, 'page.navigation', result, 'sections', encodeNavigation(output), - '"navigation" property is deprecated'); + deprecate.field(output, 'page.progress', result, 'sections', encodeProgress(output, page), + '"page.progress" property is deprecated'); deprecate.field(output, 'page.sections', result, 'sections', [ { diff --git a/lib/api/encodeProgress.js b/lib/api/encodeProgress.js new file mode 100644 index 0000000..f32c8d8 --- /dev/null +++ b/lib/api/encodeProgress.js @@ -0,0 +1,16 @@ + + +/** + page.progress is a deprecated property from GitBook v2 + + @param {Output} + @param {Page} + @return {Object} +*/ +function encodeProgress(output, page) { + // todo + return {}; +} + +module.exports = encodeProgress; + |