diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-20 14:00:28 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 12:32:14 +0100 |
commit | 21fe8961d7ac91a49c49c28052fc2de4f251f409 (patch) | |
tree | 31b5f5b19a70a67ba6c4bd9e9432f31be66084b2 /packages/gitbook-html/lib/summary.js | |
parent | 677032ead533b9d21cb9d8a752cb3671a0cbeb52 (diff) | |
download | gitbook-21fe8961d7ac91a49c49c28052fc2de4f251f409.zip gitbook-21fe8961d7ac91a49c49c28052fc2de4f251f409.tar.gz gitbook-21fe8961d7ac91a49c49c28052fc2de4f251f409.tar.bz2 |
Cleanup html before parsing summary
Diffstat (limited to 'packages/gitbook-html/lib/summary.js')
-rwxr-xr-x | packages/gitbook-html/lib/summary.js | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/packages/gitbook-html/lib/summary.js b/packages/gitbook-html/lib/summary.js index e71d6b5..607062e 100755 --- a/packages/gitbook-html/lib/summary.js +++ b/packages/gitbook-html/lib/summary.js @@ -2,11 +2,19 @@ var _ = require('lodash'); var dom = require('./dom'); var SELECTOR_LIST = '.olist > ol, ol, ul'; -var SELECTOR_LINK = 'a, p > a'; +var SELECTOR_LINK = '> a, p > a'; var BL = '\n'; -// parse a ul list and return list of chapters recursvely +// Find a list +function findList($parent) { + var $container = $parent.children('.olist'); + if ($container.length > 0) $parent = $container.first(); + + return $parent.children('ul, ol'); +} + +// Parse a ul list and return list of chapters recursvely function parseList($ul, $) { var articles = []; @@ -15,18 +23,18 @@ function parseList($ul, $) { var $li = $(this); // Get text for the entry - var $p = $li.children('> p'); + var $p = $li.children('p'); article.title = $p.text() || dom.textNode($li.get(0)); // Parse link - var $a = $li.children(SELECTOR_LINK); + var $a = $li.find(SELECTOR_LINK); if ($a.length > 0) { article.title = $a.first().text(); article.path = $a.attr('href').replace(/\\/g, '/').replace(/^\/+/, '') } // Sub articles - var $sub = $li.children(SELECTOR_LIST).first(); + var $sub = findList($li); article.articles = parseList($sub, $); articles.push(article); @@ -38,9 +46,9 @@ function parseList($ul, $) { // HTML -> Summary function parseSummary(html) { var $ = dom.parse(html); - var $root = dom.root($); + var $root = dom.cleanup(dom.root($), $); - var $lists = $root.children(SELECTOR_LIST); + var $lists = findList($root); var parts = []; $lists.each(function() { |