diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-28 14:12:21 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-28 14:12:21 +0200 |
commit | 5059e8df3466bf33c379f3ed0626f48904e9cbcc (patch) | |
tree | f39b0e7254ab66e942af27cda3fc5e6f9fd631ab /lib/modifiers/summary/editArticle.js | |
parent | 3d405a1ac56e6c0e63276e0918ab8bd8fae82142 (diff) | |
download | gitbook-5059e8df3466bf33c379f3ed0626f48904e9cbcc.zip gitbook-5059e8df3466bf33c379f3ed0626f48904e9cbcc.tar.gz gitbook-5059e8df3466bf33c379f3ed0626f48904e9cbcc.tar.bz2 |
Add modifiers to edit article and insert in summary
Diffstat (limited to 'lib/modifiers/summary/editArticle.js')
-rw-r--r-- | lib/modifiers/summary/editArticle.js | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/modifiers/summary/editArticle.js b/lib/modifiers/summary/editArticle.js new file mode 100644 index 0000000..1625398 --- /dev/null +++ b/lib/modifiers/summary/editArticle.js @@ -0,0 +1,70 @@ + +/** + Edit a list of articles + + @param {List<Article>} articles + @param {String} level + @param {Article} newArticle + @return {List<Article>} +*/ +function editArticleInList(articles, level, newArticle) { + return articles.map(function(article) { + var articleLevel = article.getLevel(); + + if (articleLevel == level) { + return article.merge(newArticle); + } + + if (level.indexOf(articleLevel) === 0) { + var articles = editArticleInList(article.getArticles(), level, newArticle); + return article.set('articles', articles); + } + + return article; + }); +} + + +/** + Edit an article in a part + + @param {Part} part + @param {String} level + @param {Article} newArticle + @return {Part} +*/ +function editArticleInPart(part, level, newArticle) { + var articles = part.getArticles(); + articles = editArticleInList(articles); + + return part.set('articles', articles); +} + + +/** + Edit an article in a summary + + @param {Summary} summary + @param {String} level + @param {Article} newArticle + @return {Summary} +*/ +function editArticle(summary, level, newArticle) { + var parts = summary.getParts(); + + var levelParts = level.split('.'); + var partIndex = Number(levelParts[0]); + + var part = parts.get(partIndex); + if (!part) { + return summary; + } + + part = editArticleInPart(part, level, newArticle); + parts = parts.set(partIndex, part); + + return summary.set('parts', parts); +} + + +module.exports = editArticle; |