diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-05-08 10:33:45 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-05-08 10:33:45 +0200 |
commit | 54956de59f1a3a5796b4a3665d36c08cba91bf4d (patch) | |
tree | c24b86d40950fa47b94dd9d6746e21c4fc5515e9 | |
parent | 2cbd30d1f22ab2c886c05b8e33631b9f1f175160 (diff) | |
download | gitbook-54956de59f1a3a5796b4a3665d36c08cba91bf4d.zip gitbook-54956de59f1a3a5796b4a3665d36c08cba91bf4d.tar.gz gitbook-54956de59f1a3a5796b4a3665d36c08cba91bf4d.tar.bz2 |
Start API for plugins to access the summary
-rw-r--r-- | lib/api/encodeGlobal.js | 7 | ||||
-rw-r--r-- | lib/api/encodeSummary.js | 40 | ||||
-rw-r--r-- | lib/json/encodeSummaryArticle.js | 1 |
3 files changed, 46 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..2bb8d11 --- /dev/null +++ b/lib/api/encodeSummary.js @@ -0,0 +1,40 @@ +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 + + @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); + } + }; + + 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 }; } |