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 /lib/api/encodeSummary.js | |
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
Diffstat (limited to 'lib/api/encodeSummary.js')
-rw-r--r-- | lib/api/encodeSummary.js | 51 |
1 files changed, 51 insertions, 0 deletions
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; |