mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Add property depth to article and pages
This commit is contained in:
@@ -74,6 +74,7 @@ TOCArticle.prototype.getContext = function() {
|
||||
return {
|
||||
level: this.level,
|
||||
title: this.title,
|
||||
depth: this.depth(),
|
||||
path: this.isExternal()? undefined : this.path,
|
||||
anchor: this.isExternal()? undefined : this.anchor,
|
||||
url: this.isExternal()? this.ref : undefined
|
||||
@@ -105,6 +106,11 @@ TOCArticle.prototype.hasParent = function() {
|
||||
return !(this.parent instanceof TOCPart);
|
||||
};
|
||||
|
||||
// Return depth of this article
|
||||
TOCArticle.prototype.depth = function() {
|
||||
return this.level.split('.').length;
|
||||
};
|
||||
|
||||
// Return next article in the TOC
|
||||
TOCArticle.prototype.next = function() {
|
||||
return this._next;
|
||||
|
||||
@@ -122,6 +122,7 @@ Page.prototype.getContext = function() {
|
||||
next: next? next.getContext() : null,
|
||||
previous: prev? prev.getContext() : null,
|
||||
level: article? article.level : null,
|
||||
depth: article? article.depth : 0,
|
||||
content: this.content,
|
||||
dir: dir
|
||||
}
|
||||
|
||||
@@ -105,6 +105,12 @@ describe('Summary / Table of contents', function() {
|
||||
book.summary.getArticleByLevel('2.1').title.should.equal('Hello 3');
|
||||
book.summary.getArticleByLevel('2.1.1').title.should.equal('Hello 4');
|
||||
});
|
||||
|
||||
it('should correctly calcul depth', function() {
|
||||
book.summary.getArticleByLevel('0').depth().should.equal(1);
|
||||
book.summary.getArticleByLevel('1.1').depth().should.equal(2);
|
||||
book.summary.getArticleByLevel('1.1.1').depth().should.equal(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('External', function() {
|
||||
|
||||
Reference in New Issue
Block a user