diff options
-rw-r--r-- | lib/parse/navigation.js | 10 | ||||
-rw-r--r-- | lib/parse/summary.js | 27 | ||||
-rw-r--r-- | test/fixtures/ALTERNATIVE_SUMMARY.md | 13 | ||||
-rw-r--r-- | test/navigation.js | 17 | ||||
-rw-r--r-- | test/summary.js | 30 | ||||
-rw-r--r-- | theme/templates/includes/book/summary.html | 15 |
6 files changed, 81 insertions, 31 deletions
diff --git a/lib/parse/navigation.js b/lib/parse/navigation.js index 2c783e4..ae4eb9d 100644 --- a/lib/parse/navigation.js +++ b/lib/parse/navigation.js @@ -26,15 +26,9 @@ function navigation(summary, files) { // Support single files as well as list files = _.isArray(files) ? files : (_.isString(files) ? [files] : null); - // Special README nav - var README_NAV = { - path: 'README.md', - title: 'Introduction', - level: '0', - }; - // List of all navNodes - var navNodes = [README_NAV].concat(flattenChapters(summary.chapters)); + // Flatten chapters, then add in default README node if ndeeded etc ... + var navNodes = flattenChapters(summary.chapters); var prevNodes = [null].concat(navNodes.slice(0, -1)); var nextNodes = navNodes.slice(1).concat([null]); diff --git a/lib/parse/summary.js b/lib/parse/summary.js index 1fd5676..7e54df0 100644 --- a/lib/parse/summary.js +++ b/lib/parse/summary.js @@ -94,18 +94,35 @@ function parseChapter(nodes, nums) { }); } +function defaultChapterList(chapterList) { + var first = _.first(chapterList); + + var chapter = parseChapter(first, [0]); + + // Already have README node, we're good to go + if(chapter.path === 'README.md') { + return chapterList; + } + + return [ + [ { type: 'text', text: '[Introduction](README.md)' } ] + ].concat(chapterList); +} + function parseSummary(src) { var nodes = marked.lexer(src); // Get out list of chapters - var chapterList = filterList(nodes); + var chapterList = listSplit( + filterList(nodes), + 'list_item_start', 'list_item_end' + ); // Split out chapter sections - var chapters = _.chain(listSplit(chapterList, 'list_item_start', 'list_item_end')) + var chapters = defaultChapterList(chapterList) .map(function(nodes, i) { - return parseChapter(nodes, [i + 1]); - }) - .value(); + return parseChapter(nodes, [i]); + }); return { chapters: chapters diff --git a/test/fixtures/ALTERNATIVE_SUMMARY.md b/test/fixtures/ALTERNATIVE_SUMMARY.md new file mode 100644 index 0000000..e0d0114 --- /dev/null +++ b/test/fixtures/ALTERNATIVE_SUMMARY.md @@ -0,0 +1,13 @@ +# Summary + +* [Custom name for Introduction](README.md) +* [Chapter 1](chapter-1/README.md) + * [Article 1](chapter-1/ARTICLE1.md) + * [Article 2](chapter-1/ARTICLE2.md) + * [article 1.2.1](chapter-1/ARTICLE-1-2-1.md) + * [article 1.2.2](chapter-1/ARTICLE-1-2-2.md) +* [Chapter 2](chapter-2/README.md) +* [Chapter 3](chapter-3/README.md) +* [Chapter 4](chapter-4/README.md) + * Unfinished article +* Unfinished Chapter diff --git a/test/navigation.js b/test/navigation.js index 0330d74..df29509 100644 --- a/test/navigation.js +++ b/test/navigation.js @@ -7,7 +7,9 @@ var navigation = require('../').parse.navigation; var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/SUMMARY.md'), 'utf8'); +var ALT_CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/ALTERNATIVE_SUMMARY.md'), 'utf8'); var LEXED = summary(CONTENT); +var ALT_LEXED = summary(ALT_CONTENT); describe('Summary navigation', function() { @@ -76,6 +78,21 @@ describe('Summary navigation', function() { assert.equal(nav['chapter-3/README.md'].level, '3'); }); + it('should have a default README node', function() { + var nav = navigation(LEXED); + + assert.equal(nav['README.md'].level, '0'); + assert.equal(nav['README.md'].title, 'Introduction'); + }); + + it('Should allow README node to be customized', function() { + var nav = navigation(ALT_LEXED); + + assert(nav['README.md']); + assert.equal(nav['README.md'].level, '0'); + assert.notEqual(nav['README.md'].title, 'Introduction'); + }); + it('should not accept null paths', function() { var nav = navigation(LEXED); diff --git a/test/summary.js b/test/summary.js index 0316b31..2993817 100644 --- a/test/summary.js +++ b/test/summary.js @@ -12,13 +12,13 @@ var LEXED = summary(CONTENT); describe('Summary parsing', function () { it('should detect chapters', function() { - assert.equal(LEXED.chapters.length, 5); + assert.equal(LEXED.chapters.length, 6); }); it('should support articles', function() { - assert.equal(LEXED.chapters[0].articles.length, 2); - assert.equal(LEXED.chapters[1].articles.length, 0); + assert.equal(LEXED.chapters[1].articles.length, 2); assert.equal(LEXED.chapters[2].articles.length, 0); + assert.equal(LEXED.chapters[3].articles.length, 0); }); it('should detect paths and titles', function() { @@ -26,30 +26,34 @@ describe('Summary parsing', function () { assert(LEXED.chapters[1].path); assert(LEXED.chapters[2].path); assert(LEXED.chapters[3].path); - assert.equal(LEXED.chapters[4].path, null); + assert(LEXED.chapters[4].path); + assert.equal(LEXED.chapters[5].path, null); assert(LEXED.chapters[0].title); assert(LEXED.chapters[1].title); assert(LEXED.chapters[2].title); assert(LEXED.chapters[3].title); assert(LEXED.chapters[4].title); + assert(LEXED.chapters[5].title); }); it('should normalize paths from .md to .html', function() { - assert.equal(LEXED.chapters[0].path,'chapter-1/README.md'); - assert.equal(LEXED.chapters[1].path,'chapter-2/README.md'); - assert.equal(LEXED.chapters[2].path,'chapter-3/README.md'); + assert.equal(LEXED.chapters[0].path,'README.md'); + assert.equal(LEXED.chapters[1].path,'chapter-1/README.md'); + assert.equal(LEXED.chapters[2].path,'chapter-2/README.md'); + assert.equal(LEXED.chapters[3].path,'chapter-3/README.md'); }); it('should detect levels correctly', function() { var c = LEXED.chapters; - assert.equal(c[0].level, '1'); - assert.equal(c[1].level, '2'); - assert.equal(c[2].level, '3'); + assert.equal(c[0].level, '0'); + assert.equal(c[1].level, '1'); + assert.equal(c[2].level, '2'); + assert.equal(c[3].level, '3'); - assert.equal(c[0].articles[0].level, '1.1'); - assert.equal(c[0].articles[1].level, '1.2'); - assert.equal(c[0].articles[1].articles[0].level, '1.2.1'); + assert.equal(c[1].articles[0].level, '1.1'); + assert.equal(c[1].articles[1].level, '1.2'); + assert.equal(c[1].articles[1].articles[0].level, '1.2.1'); }); }); diff --git a/theme/templates/includes/book/summary.html b/theme/templates/includes/book/summary.html index 76ee192..2f9dc3f 100644 --- a/theme/templates/includes/book/summary.html +++ b/theme/templates/includes/book/summary.html @@ -5,11 +5,19 @@ {% if item.path %} {% if !externalLink %} <a href="{{ basePath }}/{{ item.path|mdLink }}"> - <i class="fa fa-check"></i> <b>{{ item.level }}.</b> {{ item.title }} + <i class="fa fa-check"></i> + {% if item.level !== "0" %} + <b>{{ item.level }}.</b> + {% endif %} + {{ item.title }} </a> {% else %} <a target="_blank" href="{{ item.path }}"> - <i class="fa fa-check"></i> <b>{{ item.level }}.</b> {{ item.title }} + <i class="fa fa-check"></i> + {% if item.level !== "0" %} + <b>{{ item.level }}.</b> + {% endif %} + {{ item.title }} </a> {% endif %} {% else %} @@ -55,9 +63,6 @@ <li class="divider"></li> {% endif %} - <li data-level="0" data-path="index.html"> - <a href="{{ basePath }}/"><i class="fa fa-check"></i> Introduction</a> - </li> {{ articles(summary.chapters) }} {% if options.links.gitbook !== false %} |