Add "level" to TOCArticle

This commit is contained in:
Samy Pessé
2016-02-18 11:50:33 +01:00
parent 52206ce9d0
commit 58a930e456
2 changed files with 20 additions and 15 deletions
+19 -15
View File
@@ -11,29 +11,35 @@ var BackboneFile = require('./file');
An article represent an entry in the Summary.
It's defined by a title, a reference, and children articles, the reference (ref) can be a filename + anchor or an external file (optional)
*/
function TOCArticle(summary, title, ref, articles, parent) {
this.summary = summary;
this.title = title;
function TOCArticle(def, parent) {
// Title
this.title = def.title;
// Parent TOCPart or TOCArticle
this.parent = parent;
if (ref && location.isExternal(ref)) {
// As string indicating the overall position
// ex: '1.0.0'
this.level = def.level;
if (def.path && location.isExternal(def.path)) {
throw error.ParsingError(new Error('SUMMARY can only contains relative locations'));
}
if (!title) {
if (!def.title) {
throw error.ParsingError(new Error('SUMMARY entries should have an non-empty title'));
}
var parts = url.parse(ref);
this.ref = ref;
var parts = url.parse(def.path);
this.ref = def.path;
if (!this.isExternal()) {
this.path = parts.pathname;
this.anchor = parts.hash;
}
this.articles = _.map(articles || [], function(article) {
this.articles = _.map(def.articles || [], function(article) {
if (article instanceof TOCArticle) return article;
return new TOCArticle(article.title, article.ref, article.articles, this);
return new TOCArticle(article, this);
}, this);
}
@@ -48,6 +54,7 @@ TOCArticle.prototype.walk = function(iter) {
// Return templating context for an article
TOCArticle.prototype.getContext = function() {
return {
level: this.level,
title: this.title,
path: this.isExternal()? undefined : this.path,
anchor: this.isExternal()? undefined : this.anchor,
@@ -118,12 +125,9 @@ TOCArticle.prototype.map = function(iter) {
/*
A part of a ToC is a composed of a tree of articles.
*/
function TOCPart(summary, part) {
var that = this;
this.summary = summary;
function TOCPart(part) {
this.articles = _.map(part.articles || part.chapters, function(article) {
return new TOCArticle(that.summary, article.title, article.path, article.articles, this);
return new TOCArticle(article, this);
}, this);
}
@@ -164,7 +168,7 @@ Summary.prototype.parse = function(content) {
// TODO: update GitBook's parsers to return a list of parts
.then(function(part) {
that.parts = [new TOCPart(that, part)];
that.parts = [new TOCPart(part)];
// Update count of articles
that._length = 0;
+1
View File
@@ -102,6 +102,7 @@ Page.prototype.getContext = function() {
title: article? article.title : null,
next: next? next.getContext() : null,
previous: prev? prev.getContext() : null,
level: article? article.level : null,
content: this.content
}
};