diff options
Diffstat (limited to 'lib/parse/summary.js')
-rw-r--r-- | lib/parse/summary.js | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/lib/parse/summary.js b/lib/parse/summary.js index 7e54df0..fd300bc 100644 --- a/lib/parse/summary.js +++ b/lib/parse/summary.js @@ -87,6 +87,9 @@ function parseTitle(src, nums) { } function parseChapter(nodes, nums) { + // Convert single number to an array + nums = _.isArray(nums) ? nums : [nums]; + return _.extend(parseTitle(_.first(nodes).text, nums), { articles: _.map(listSplit(filterList(nodes), 'list_item_start', 'list_item_end'), function(nodes, i) { return parseChapter(nodes, nums.concat(i + 1)); @@ -97,11 +100,13 @@ function parseChapter(nodes, nums) { function defaultChapterList(chapterList) { var first = _.first(chapterList); - var chapter = parseChapter(first, [0]); + if (first) { + var chapter = parseChapter(first, [0]); - // Already have README node, we're good to go - if(chapter.path === 'README.md') { - return chapterList; + // Already have README node, we're good to go + if(chapter.path === 'README.md') { + return chapterList; + } } return [ @@ -109,26 +114,31 @@ function defaultChapterList(chapterList) { ].concat(chapterList); } -function parseSummary(src) { +function listGroups(src) { var nodes = marked.lexer(src); - // Get out list of chapters - var chapterList = listSplit( + // Get out groups of lists + return listSplit( filterList(nodes), 'list_item_start', 'list_item_end' ); +} +function parseSummary(src) { // Split out chapter sections - var chapters = defaultChapterList(chapterList) - .map(function(nodes, i) { - return parseChapter(nodes, [i]); - }); + var chapters = defaultChapterList(listGroups(src)) + .map(parseChapter); return { chapters: chapters }; } +function parseEntries (src) { + return listGroups(src).map(parseChapter); +} + // Exports module.exports = parseSummary; +module.exports.entries = parseEntries; |