Skip to content

Commit 3d8b6e5

Browse files
committed
Merge pull request GitbookIO#319 from GitbookIO/enchancement/custom_intro
Enchancement/custom intro
2 parents 3245d7e + 4c3dbca commit 3d8b6e5

File tree

6 files changed

+81
-31
lines changed

6 files changed

+81
-31
lines changed

lib/parse/navigation.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ function navigation(summary, files) {
2626
// Support single files as well as list
2727
files = _.isArray(files) ? files : (_.isString(files) ? [files] : null);
2828

29-
// Special README nav
30-
var README_NAV = {
31-
path: 'README.md',
32-
title: 'Introduction',
33-
level: '0',
34-
};
35-
3629
// List of all navNodes
37-
var navNodes = [README_NAV].concat(flattenChapters(summary.chapters));
30+
// Flatten chapters, then add in default README node if ndeeded etc ...
31+
var navNodes = flattenChapters(summary.chapters);
3832
var prevNodes = [null].concat(navNodes.slice(0, -1));
3933
var nextNodes = navNodes.slice(1).concat([null]);
4034

lib/parse/summary.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,35 @@ function parseChapter(nodes, nums) {
9494
});
9595
}
9696

97+
function defaultChapterList(chapterList) {
98+
var first = _.first(chapterList);
99+
100+
var chapter = parseChapter(first, [0]);
101+
102+
// Already have README node, we're good to go
103+
if(chapter.path === 'README.md') {
104+
return chapterList;
105+
}
106+
107+
return [
108+
[ { type: 'text', text: '[Introduction](README.md)' } ]
109+
].concat(chapterList);
110+
}
111+
97112
function parseSummary(src) {
98113
var nodes = marked.lexer(src);
99114

100115
// Get out list of chapters
101-
var chapterList = filterList(nodes);
116+
var chapterList = listSplit(
117+
filterList(nodes),
118+
'list_item_start', 'list_item_end'
119+
);
102120

103121
// Split out chapter sections
104-
var chapters = _.chain(listSplit(chapterList, 'list_item_start', 'list_item_end'))
122+
var chapters = defaultChapterList(chapterList)
105123
.map(function(nodes, i) {
106-
return parseChapter(nodes, [i + 1]);
107-
})
108-
.value();
124+
return parseChapter(nodes, [i]);
125+
});
109126

110127
return {
111128
chapters: chapters
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Summary
2+
3+
* [Custom name for Introduction](README.md)
4+
* [Chapter 1](chapter-1/README.md)
5+
* [Article 1](chapter-1/ARTICLE1.md)
6+
* [Article 2](chapter-1/ARTICLE2.md)
7+
* [article 1.2.1](chapter-1/ARTICLE-1-2-1.md)
8+
* [article 1.2.2](chapter-1/ARTICLE-1-2-2.md)
9+
* [Chapter 2](chapter-2/README.md)
10+
* [Chapter 3](chapter-3/README.md)
11+
* [Chapter 4](chapter-4/README.md)
12+
* Unfinished article
13+
* Unfinished Chapter

test/navigation.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ var navigation = require('../').parse.navigation;
77

88

99
var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/SUMMARY.md'), 'utf8');
10+
var ALT_CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/ALTERNATIVE_SUMMARY.md'), 'utf8');
1011
var LEXED = summary(CONTENT);
12+
var ALT_LEXED = summary(ALT_CONTENT);
1113

1214

1315
describe('Summary navigation', function() {
@@ -76,6 +78,21 @@ describe('Summary navigation', function() {
7678
assert.equal(nav['chapter-3/README.md'].level, '3');
7779
});
7880

81+
it('should have a default README node', function() {
82+
var nav = navigation(LEXED);
83+
84+
assert.equal(nav['README.md'].level, '0');
85+
assert.equal(nav['README.md'].title, 'Introduction');
86+
});
87+
88+
it('Should allow README node to be customized', function() {
89+
var nav = navigation(ALT_LEXED);
90+
91+
assert(nav['README.md']);
92+
assert.equal(nav['README.md'].level, '0');
93+
assert.notEqual(nav['README.md'].title, 'Introduction');
94+
});
95+
7996
it('should not accept null paths', function() {
8097
var nav = navigation(LEXED);
8198

test/summary.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,48 @@ var LEXED = summary(CONTENT);
1212
describe('Summary parsing', function () {
1313

1414
it('should detect chapters', function() {
15-
assert.equal(LEXED.chapters.length, 5);
15+
assert.equal(LEXED.chapters.length, 6);
1616
});
1717

1818
it('should support articles', function() {
19-
assert.equal(LEXED.chapters[0].articles.length, 2);
20-
assert.equal(LEXED.chapters[1].articles.length, 0);
19+
assert.equal(LEXED.chapters[1].articles.length, 2);
2120
assert.equal(LEXED.chapters[2].articles.length, 0);
21+
assert.equal(LEXED.chapters[3].articles.length, 0);
2222
});
2323

2424
it('should detect paths and titles', function() {
2525
assert(LEXED.chapters[0].path);
2626
assert(LEXED.chapters[1].path);
2727
assert(LEXED.chapters[2].path);
2828
assert(LEXED.chapters[3].path);
29-
assert.equal(LEXED.chapters[4].path, null);
29+
assert(LEXED.chapters[4].path);
30+
assert.equal(LEXED.chapters[5].path, null);
3031

3132
assert(LEXED.chapters[0].title);
3233
assert(LEXED.chapters[1].title);
3334
assert(LEXED.chapters[2].title);
3435
assert(LEXED.chapters[3].title);
3536
assert(LEXED.chapters[4].title);
37+
assert(LEXED.chapters[5].title);
3638
});
3739

3840
it('should normalize paths from .md to .html', function() {
39-
assert.equal(LEXED.chapters[0].path,'chapter-1/README.md');
40-
assert.equal(LEXED.chapters[1].path,'chapter-2/README.md');
41-
assert.equal(LEXED.chapters[2].path,'chapter-3/README.md');
41+
assert.equal(LEXED.chapters[0].path,'README.md');
42+
assert.equal(LEXED.chapters[1].path,'chapter-1/README.md');
43+
assert.equal(LEXED.chapters[2].path,'chapter-2/README.md');
44+
assert.equal(LEXED.chapters[3].path,'chapter-3/README.md');
4245
});
4346

4447
it('should detect levels correctly', function() {
4548
var c = LEXED.chapters;
4649

47-
assert.equal(c[0].level, '1');
48-
assert.equal(c[1].level, '2');
49-
assert.equal(c[2].level, '3');
50+
assert.equal(c[0].level, '0');
51+
assert.equal(c[1].level, '1');
52+
assert.equal(c[2].level, '2');
53+
assert.equal(c[3].level, '3');
5054

51-
assert.equal(c[0].articles[0].level, '1.1');
52-
assert.equal(c[0].articles[1].level, '1.2');
53-
assert.equal(c[0].articles[1].articles[0].level, '1.2.1');
55+
assert.equal(c[1].articles[0].level, '1.1');
56+
assert.equal(c[1].articles[1].level, '1.2');
57+
assert.equal(c[1].articles[1].articles[0].level, '1.2.1');
5458
});
5559
});

theme/templates/includes/book/summary.html

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
{% if item.path %}
66
{% if !externalLink %}
77
<a href="{{ basePath }}/{{ item.path|mdLink }}">
8-
<i class="fa fa-check"></i> <b>{{ item.level }}.</b> {{ item.title }}
8+
<i class="fa fa-check"></i>
9+
{% if item.level !== "0" %}
10+
<b>{{ item.level }}.</b>
11+
{% endif %}
12+
{{ item.title }}
913
</a>
1014
{% else %}
1115
<a target="_blank" href="{{ item.path }}">
12-
<i class="fa fa-check"></i> <b>{{ item.level }}.</b> {{ item.title }}
16+
<i class="fa fa-check"></i>
17+
{% if item.level !== "0" %}
18+
<b>{{ item.level }}.</b>
19+
{% endif %}
20+
{{ item.title }}
1321
</a>
1422
{% endif %}
1523
{% else %}
@@ -55,9 +63,6 @@
5563
<li class="divider"></li>
5664
{% endif %}
5765

58-
<li data-level="0" data-path="index.html">
59-
<a href="{{ basePath }}/"><i class="fa fa-check"></i> Introduction</a>
60-
</li>
6166
{{ articles(summary.chapters) }}
6267

6368
{% if options.links.gitbook !== false %}

0 commit comments

Comments
 (0)