Skip to content

Commit 59be32a

Browse files
committed
Add method .createChildLevel for SummaryArticle
1 parent 4ae7cdb commit 59be32a

File tree

2 files changed

+71
-36
lines changed

2 files changed

+71
-36
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var SummaryArticle = require('../summaryArticle');
2+
3+
describe('SummaryArticle', function() {
4+
describe('createChildLevel', function() {
5+
it('must create the right level', function() {
6+
var article = SummaryArticle.create({}, '1.1');
7+
expect(article.createChildLevel()).toBe('1.1.1');
8+
});
9+
10+
it('must create the right level when has articles', function() {
11+
var article = SummaryArticle.create({
12+
articles: [
13+
{
14+
title: 'Test'
15+
}
16+
]
17+
}, '1.1');
18+
expect(article.createChildLevel()).toBe('1.1.2');
19+
});
20+
});
21+
});
22+
23+

lib/models/summaryArticle.js

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ SummaryArticle.prototype.getArticles = function() {
3030
};
3131

3232
/**
33-
Return how deep the article is.
34-
The README has a depth of 1
35-
36-
@return {Number}
37-
*/
33+
* Return how deep the article is.
34+
* The README has a depth of 1
35+
*
36+
* @return {Number}
37+
*/
3838
SummaryArticle.prototype.getDepth = function() {
3939
return (this.getLevel().split('.').length - 1);
4040
};
4141

4242
/**
43-
Get path (without anchor) to the pointing file
44-
45-
@return {String}
46-
*/
43+
* Get path (without anchor) to the pointing file
44+
*
45+
* @return {String}
46+
*/
4747
SummaryArticle.prototype.getPath = function() {
4848
if (this.isExternal()) {
4949
return undefined;
@@ -63,19 +63,19 @@ SummaryArticle.prototype.getPath = function() {
6363
};
6464

6565
/**
66-
Return url if article is external
67-
68-
@return {String}
69-
*/
66+
* Return url if article is external
67+
*
68+
* @return {String}
69+
*/
7070
SummaryArticle.prototype.getUrl = function() {
7171
return this.isExternal()? this.getRef() : undefined;
7272
};
7373

7474
/**
75-
Get anchor for this article (or undefined)
76-
77-
@return {String}
78-
*/
75+
* Get anchor for this article (or undefined)
76+
*
77+
* @return {String}
78+
*/
7979
SummaryArticle.prototype.getAnchor = function() {
8080
var ref = this.getRef();
8181
var parts = ref.split('#');
@@ -85,29 +85,42 @@ SummaryArticle.prototype.getAnchor = function() {
8585
};
8686

8787
/**
88-
Is article pointing to a page of an absolute url
88+
* Create a new level for a new child article
89+
*
90+
* @return {String}
91+
*/
92+
SummaryArticle.prototype.createChildLevel = function() {
93+
var level = this.getLevel();
94+
var subArticles = this.getArticles();
95+
var childLevel = level + '.' + (subArticles.size + 1);
96+
97+
return childLevel;
98+
};
8999

90-
@return {Boolean}
91-
*/
100+
/**
101+
* Is article pointing to a page of an absolute url
102+
*
103+
* @return {Boolean}
104+
*/
92105
SummaryArticle.prototype.isPage = function() {
93106
return !this.isExternal() && this.getRef();
94107
};
95108

96109
/**
97-
Is article pointing to aan absolute url
98-
99-
@return {Boolean}
100-
*/
110+
* Is article pointing to aan absolute url
111+
*
112+
* @return {Boolean}
113+
*/
101114
SummaryArticle.prototype.isExternal = function() {
102115
return location.isExternal(this.getRef());
103116
};
104117

105118
/**
106-
Create a SummaryArticle
107-
108-
@param {Object} def
109-
@return {SummaryArticle}
110-
*/
119+
* Create a SummaryArticle
120+
*
121+
* @param {Object} def
122+
* @return {SummaryArticle}
123+
*/
111124
SummaryArticle.create = function(def, level) {
112125
var articles = (def.articles || []).map(function(article, i) {
113126
if (article instanceof SummaryArticle) {
@@ -124,14 +137,13 @@ SummaryArticle.create = function(def, level) {
124137
});
125138
};
126139

127-
128140
/**
129-
Find an article from a base one
130-
131-
@param {Article|Part} base
132-
@param {Function(article)} iter
133-
@return {Article}
134-
*/
141+
* Find an article from a base one
142+
*
143+
* @param {Article|Part} base
144+
* @param {Function(article)} iter
145+
* @return {Article}
146+
*/
135147
SummaryArticle.findArticle = function(base, iter) {
136148
var articles = base.getArticles();
137149

0 commit comments

Comments
 (0)