diff options
-rw-r--r-- | lib/parse/summary.js | 27 | ||||
-rwxr-xr-x | test/bin/lex.js | 16 | ||||
-rwxr-xr-x | test/bin/summary.js | 16 | ||||
-rw-r--r-- | test/fixtures/SUMMARY_WHITESPACE.md | 15 | ||||
-rw-r--r-- | test/summary.js | 18 |
5 files changed, 85 insertions, 7 deletions
diff --git a/lib/parse/summary.js b/lib/parse/summary.js index 21eadd1..2fdec8a 100644 --- a/lib/parse/summary.js +++ b/lib/parse/summary.js @@ -60,6 +60,26 @@ function filterList(nodes) { .value().slice(1, -1); } +function skipSpace(nodes) { + return _.filter(nodes, function(node) { + return node && node.type != 'space'; + }); +} + +function correctLoose(nodes) { + return _.map(nodes, function(node) { + // Return normal nodes + if(!node || node.type != 'loose_item_start') { + return node + } + + // Correct loose items + node.type = 'list_item_start'; + + return node; + }) +} + // Parses an Article or Chapter title // supports extracting links function parseTitle(src, nums) { @@ -123,18 +143,17 @@ function listGroups(src) { // Get out groups of lists return listSplit( - filterList(nodes), + filterList(correctLoose(skipSpace(nodes))), 'list_item_start', 'list_item_end' ); } function parseSummary(src) { // Split out chapter sections - var chapters = defaultChapterList(listGroups(src)) - .map(parseChapter); + var chapters = defaultChapterList(listGroups(src)); return { - chapters: chapters + chapters: chapters.map(parseChapter) }; } diff --git a/test/bin/lex.js b/test/bin/lex.js new file mode 100755 index 0000000..c35d399 --- /dev/null +++ b/test/bin/lex.js @@ -0,0 +1,16 @@ +#!/usr/bin/env node + +var fs = require('fs'); + +var gitbook = require('../../'); + +if(process.argv < 3) { + console.error('Please specify a filename'); + process.exit(1); +} + +var content = fs.readFileSync(process.argv[2], 'utf8'); + +var lexed = gitbook.parse.lex(content); + +console.log(JSON.stringify(lexed, null, 2)); diff --git a/test/bin/summary.js b/test/bin/summary.js new file mode 100755 index 0000000..78c20dc --- /dev/null +++ b/test/bin/summary.js @@ -0,0 +1,16 @@ +#!/usr/bin/env node + +var fs = require('fs'); + +var gitbook = require('../../'); + +if(process.argv < 3) { + console.error('Please specify a filename'); + process.exit(1); +} + +var content = fs.readFileSync(process.argv[2], 'utf8'); + +var lexed = gitbook.parse.summary(content); + +console.log(JSON.stringify(lexed, null, 2)); diff --git a/test/fixtures/SUMMARY_WHITESPACE.md b/test/fixtures/SUMMARY_WHITESPACE.md new file mode 100644 index 0000000..10a7100 --- /dev/null +++ b/test/fixtures/SUMMARY_WHITESPACE.md @@ -0,0 +1,15 @@ +# Summary + +* [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/summary.js b/test/summary.js index 2993817..616b983 100644 --- a/test/summary.js +++ b/test/summary.js @@ -4,10 +4,16 @@ var assert = require('assert'); var summary = require('../').parse.summary; +function lex(fixtureFile) { + return summary( + fs.readFileSync( + path.join(__dirname, 'fixtures', fixtureFile), + 'utf8' + ) + ); +} -var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/SUMMARY.md'), 'utf8'); -var LEXED = summary(CONTENT); - +var LEXED = lex('SUMMARY.md'); describe('Summary parsing', function () { @@ -56,4 +62,10 @@ describe('Summary parsing', function () { assert.equal(c[1].articles[1].level, '1.2'); assert.equal(c[1].articles[1].articles[0].level, '1.2.1'); }); + + it('should allow lists separated by whitespace', function() { + var l = lex('SUMMARY_WHITESPACE.md'); + + assert.equal(l.chapters.length, 6); + }); }); |