mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-10-05 21:03:09 +00:00
Complete method getByPath of Summary
This commit is contained in:
@@ -24,7 +24,9 @@ Summary.prototype.getParts = function() {
|
||||
@return {Part|Article}
|
||||
*/
|
||||
Summary.prototype.getByLevel = function(level) {
|
||||
return SummaryArticle.getByLevel(this, level, 'getParts');
|
||||
return SummaryArticle.findArticle(this, function(article) {
|
||||
return (article.getLevel() === level);
|
||||
}, 'getParts');
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -34,9 +36,9 @@ Summary.prototype.getByLevel = function(level) {
|
||||
@return {Part|Article}
|
||||
*/
|
||||
Summary.prototype.getByPath = function(filePath) {
|
||||
// todo
|
||||
|
||||
return undefined;
|
||||
return SummaryArticle.findArticle(this, function(article) {
|
||||
return (article.getPath() === filePath);
|
||||
}, 'getParts');
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,16 +38,6 @@ SummaryArticle.prototype.getDepth = function() {
|
||||
return this.getLevel().split('.').length;
|
||||
};
|
||||
|
||||
/**
|
||||
Return an article by its level
|
||||
|
||||
@param {String} level
|
||||
@return {Article}
|
||||
*/
|
||||
SummaryArticle.prototype.getByLevel = function(level) {
|
||||
return SummaryArticle.getByLevel(this, level);
|
||||
};
|
||||
|
||||
/**
|
||||
Get path (without anchor) to the pointing file
|
||||
|
||||
@@ -131,31 +121,27 @@ SummaryArticle.create = function(def, level) {
|
||||
|
||||
|
||||
/**
|
||||
Return an article by its level
|
||||
Find an article from a base one
|
||||
|
||||
@param {Article|Part} base
|
||||
@param {String} level
|
||||
@param {Function(article)} iter
|
||||
@param {String} method
|
||||
@return {Article}
|
||||
*/
|
||||
SummaryArticle.getByLevel = function(base, level, method) {
|
||||
SummaryArticle.findArticle = function(base, iter, method) {
|
||||
method = method || 'getArticles';
|
||||
var articles = base[method]();
|
||||
var levelParts = level.split('.');
|
||||
var baseLevel = levelParts.shift();
|
||||
|
||||
var result = articles.find(function(a) {
|
||||
return a.getLevel() === baseLevel;
|
||||
});
|
||||
return articles.reduce(function(result, article) {
|
||||
if (result) return result;
|
||||
|
||||
if (!result) {
|
||||
return undefined;
|
||||
}
|
||||
if (levelParts.length === 0) {
|
||||
return result;
|
||||
}
|
||||
if (iter(article)) {
|
||||
return article;
|
||||
}
|
||||
|
||||
return SummaryArticle.getByLevel(result, levelParts.join('.'));
|
||||
return SummaryArticle.findArticle(article, iter);
|
||||
}, null);
|
||||
};
|
||||
|
||||
|
||||
module.exports = SummaryArticle;
|
||||
|
||||
@@ -24,16 +24,6 @@ SummaryPart.prototype.getArticles = function() {
|
||||
return this.get('articles');
|
||||
};
|
||||
|
||||
/**
|
||||
Return an article by its level
|
||||
|
||||
@param {String} level
|
||||
@return {Article}
|
||||
*/
|
||||
SummaryPart.prototype.getByLevel = function(level) {
|
||||
return SummaryArticle.getByLevel(this, level);
|
||||
};
|
||||
|
||||
/**
|
||||
Create a SummaryPart
|
||||
|
||||
|
||||
Reference in New Issue
Block a user