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 | |
parent | 677032ead533b9d21cb9d8a752cb3671a0cbeb52 (diff) | |
download | gitbook-21fe8961d7ac91a49c49c28052fc2de4f251f409.zip gitbook-21fe8961d7ac91a49c49c28052fc2de4f251f409.tar.gz gitbook-21fe8961d7ac91a49c49c28052fc2de4f251f409.tar.bz2 |
Cleanup html before parsing summary
-rw-r--r-- | packages/gitbook-html/lib/dom.js | 16 | ||||
-rwxr-xr-x | packages/gitbook-html/lib/summary.js | 22 |
2 files changed, 30 insertions, 8 deletions
diff --git a/packages/gitbook-html/lib/dom.js b/packages/gitbook-html/lib/dom.js index df4de90..d8f7c84 100644 --- a/packages/gitbook-html/lib/dom.js +++ b/packages/gitbook-html/lib/dom.js @@ -23,8 +23,22 @@ function textNode($el) { }, ''); } +// Cleanup a dom +// Remove all divs +function cleanup($el, $) { + $el.find('div').each(function() { + var $div = $(this); + cleanup($div, $); + + $div.replaceWith($div.html()); + }); + + return $el; +} + module.exports = { parse: parse, textNode: textNode, - root: root + root: root, + cleanup: cleanup }; 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() { |