diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-05-08 22:28:15 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-05-08 22:28:15 +0200 |
commit | 13bc89ff4490abb9ea6ea6bd98bf86a9ad08325b (patch) | |
tree | 826940362c4b17047cae6790e8f72dae90b026f3 | |
parent | e576fdea1405f53a83f4225abb3e2a29cafe8f4a (diff) | |
parent | bb910f984edd8080e5fe1d1b34a7dfec7ac9d785 (diff) | |
download | gitbook-13bc89ff4490abb9ea6ea6bd98bf86a9ad08325b.zip gitbook-13bc89ff4490abb9ea6ea6bd98bf86a9ad08325b.tar.gz gitbook-13bc89ff4490abb9ea6ea6bd98bf86a9ad08325b.tar.bz2 |
Merge pull request #1285 from GitbookIO/api/summary
[WIP] Api for plugins to access summary
-rw-r--r-- | lib/api/encodeGlobal.js | 7 | ||||
-rw-r--r-- | lib/api/encodeSummary.js | 51 | ||||
-rw-r--r-- | lib/json/encodeSummaryArticle.js | 1 |
3 files changed, 57 insertions, 2 deletions
diff --git a/lib/api/encodeGlobal.js b/lib/api/encodeGlobal.js index 1e7df36..8e3456a 100644 --- a/lib/api/encodeGlobal.js +++ b/lib/api/encodeGlobal.js @@ -5,12 +5,14 @@ var fs = require('../utils/fs'); var Plugins = require('../plugins'); var deprecate = require('./deprecate'); -var encodeConfig = require('./encodeConfig'); -var encodeNavigation = require('./encodeNavigation'); var fileToURL = require('../output/helper/fileToURL'); var defaultBlocks = require('../constants/defaultBlocks'); var gitbook = require('../gitbook'); +var encodeConfig = require('./encodeConfig'); +var encodeSummary = require('./encodeSummary'); +var encodeNavigation = require('./encodeNavigation'); + /** Encode a global context into a JS object It's the context for page's hook, etc @@ -29,6 +31,7 @@ function encodeGlobal(output) { var result = { log: logger, config: encodeConfig(output, book.getConfig()), + summary: encodeSummary(output. book.getSummary()), /** Check if the book is a multilingual book diff --git a/lib/api/encodeSummary.js b/lib/api/encodeSummary.js new file mode 100644 index 0000000..0d66ded --- /dev/null +++ b/lib/api/encodeSummary.js @@ -0,0 +1,51 @@ +var encodeSummaryArticle = require('../json/encodeSummaryArticle'); + +/** + Encode summary to provide an API to plugin + + @param {Output} output + @param {Config} config + @return {Object} +*/ +function encodeSummary(output, summary) { + var result = { + /** + Iterate over the summary, it stops when the "iter" returns false + + @param {Function} iter + */ + walk: function (iter) { + summary.getArticle(function(article) { + var jsonArticle = encodeSummaryArticle(article, false); + + return iter(jsonArticle); + }); + }, + + /** + Get an article by its level + + @param {String} level + @return {Object} + */ + getArticleByLevel: function(level) { + var article = summary.getByLevel(level); + return (article? encodeSummaryArticle(article) : undefined); + }, + + /** + Get an article by its path + + @param {String} level + @return {Object} + */ + getArticleByPath: function(level) { + var article = summary.getByPath(level); + return (article? encodeSummaryArticle(article) : undefined); + } + }; + + return result; +} + +module.exports = encodeSummary; diff --git a/lib/json/encodeSummaryArticle.js b/lib/json/encodeSummaryArticle.js index 987e44a..2fc5144 100644 --- a/lib/json/encodeSummaryArticle.js +++ b/lib/json/encodeSummaryArticle.js @@ -20,6 +20,7 @@ function encodeSummaryArticle(article, recursive) { anchor: article.getAnchor(), url: article.getUrl(), path: article.getPath(), + ref: article.getRef(), articles: articles }; } |