summaryrefslogtreecommitdiffstats
path: root/lib/models
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-06-10 18:08:55 +0200
committerSamy Pessé <samypesse@gmail.com>2016-06-10 18:08:56 +0200
commit59be32a54f86d047a19d64eaa084bcc61f11ee2f (patch)
treec988789a13e216deeabdbd391f97136859798be5 /lib/models
parent4ae7cdb5d1598824c36e0fdb4f98ea3ac8152511 (diff)
downloadgitbook-59be32a54f86d047a19d64eaa084bcc61f11ee2f.zip
gitbook-59be32a54f86d047a19d64eaa084bcc61f11ee2f.tar.gz
gitbook-59be32a54f86d047a19d64eaa084bcc61f11ee2f.tar.bz2
Add method .createChildLevel for SummaryArticle
Diffstat (limited to 'lib/models')
-rw-r--r--lib/models/__tests__/summaryArticle.js23
-rw-r--r--lib/models/summaryArticle.js84
2 files changed, 71 insertions, 36 deletions
diff --git a/lib/models/__tests__/summaryArticle.js b/lib/models/__tests__/summaryArticle.js
new file mode 100644
index 0000000..7c4bc57
--- /dev/null
+++ b/lib/models/__tests__/summaryArticle.js
@@ -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');
+ });
+ });
+});
+
+
diff --git a/lib/models/summaryArticle.js b/lib/models/summaryArticle.js
index f072e7b..9b5b653 100644
--- a/lib/models/summaryArticle.js
+++ b/lib/models/summaryArticle.js
@@ -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 childLevel;
+};
- @return {Boolean}
-*/
+/**
+ * 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();