Add method .createChildLevel for SummaryArticle

This commit is contained in:
Samy Pessé
2016-06-10 18:08:55 +02:00
parent 4ae7cdb5d1
commit 59be32a54f
2 changed files with 71 additions and 36 deletions
+23
View File
@@ -0,0 +1,23 @@
var SummaryArticle = require('../summaryArticle');
describe('SummaryArticle', function() {
describe('createChildLevel', function() {
it('must create the right level', function() {
var article = SummaryArticle.create({}, '1.1');
expect(article.createChildLevel()).toBe('1.1.1');
});
it('must create the right level when has articles', function() {
var article = SummaryArticle.create({
articles: [
{
title: 'Test'
}
]
}, '1.1');
expect(article.createChildLevel()).toBe('1.1.2');
});
});
});
+48 -36
View File
@@ -30,20 +30,20 @@ SummaryArticle.prototype.getArticles = function() {
};
/**
Return how deep the article is.
The README has a depth of 1
@return {Number}
*/
* Return how deep the article is.
* The README has a depth of 1
*
* @return {Number}
*/
SummaryArticle.prototype.getDepth = function() {
return (this.getLevel().split('.').length - 1);
};
/**
Get path (without anchor) to the pointing file
@return {String}
*/
* Get path (without anchor) to the pointing file
*
* @return {String}
*/
SummaryArticle.prototype.getPath = function() {
if (this.isExternal()) {
return undefined;
@@ -63,19 +63,19 @@ SummaryArticle.prototype.getPath = function() {
};
/**
Return url if article is external
@return {String}
*/
* Return url if article is external
*
* @return {String}
*/
SummaryArticle.prototype.getUrl = function() {
return this.isExternal()? this.getRef() : undefined;
};
/**
Get anchor for this article (or undefined)
@return {String}
*/
* Get anchor for this article (or undefined)
*
* @return {String}
*/
SummaryArticle.prototype.getAnchor = function() {
var ref = this.getRef();
var parts = ref.split('#');
@@ -85,29 +85,42 @@ SummaryArticle.prototype.getAnchor = function() {
};
/**
Is article pointing to a page of an absolute url
* Create a new level for a new child article
*
* @return {String}
*/
SummaryArticle.prototype.createChildLevel = function() {
var level = this.getLevel();
var subArticles = this.getArticles();
var childLevel = level + '.' + (subArticles.size + 1);
@return {Boolean}
*/
return childLevel;
};
/**
* Is article pointing to a page of an absolute url
*
* @return {Boolean}
*/
SummaryArticle.prototype.isPage = function() {
return !this.isExternal() && this.getRef();
};
/**
Is article pointing to aan absolute url
@return {Boolean}
*/
* Is article pointing to aan absolute url
*
* @return {Boolean}
*/
SummaryArticle.prototype.isExternal = function() {
return location.isExternal(this.getRef());
};
/**
Create a SummaryArticle
@param {Object} def
@return {SummaryArticle}
*/
* Create a SummaryArticle
*
* @param {Object} def
* @return {SummaryArticle}
*/
SummaryArticle.create = function(def, level) {
var articles = (def.articles || []).map(function(article, i) {
if (article instanceof SummaryArticle) {
@@ -124,14 +137,13 @@ SummaryArticle.create = function(def, level) {
});
};
/**
Find an article from a base one
@param {Article|Part} base
@param {Function(article)} iter
@return {Article}
*/
* Find an article from a base one
*
* @param {Article|Part} base
* @param {Function(article)} iter
* @return {Article}
*/
SummaryArticle.findArticle = function(base, iter) {
var articles = base.getArticles();