Merge pull request #553 from GitbookIO/fix/summary-whitespace

Fix/summary whitespace
This commit is contained in:
Samy Pessé
2015-01-14 22:41:34 +01:00
5 changed files with 85 additions and 7 deletions
+23 -4
View File
@@ -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)
};
}
+16
View File
@@ -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));
+16
View File
@@ -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));
+15
View File
@@ -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
+15 -3
View File
@@ -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);
});
});