gitbook-html
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -6,2 +6,4 @@ var _ = require('lodash'); | ||
var SELECTOR_LINK = '> a, p > a'; | ||
var SELECTOR_PART = 'h2, h3, h4'; | ||
var BL = '\n'; | ||
@@ -19,3 +21,3 @@ | ||
return $parent.children('ul, ol'); | ||
return $parent.children(SELECTOR_LIST); | ||
} | ||
@@ -45,3 +47,3 @@ | ||
article.title = $a.first().text(); | ||
article.ref = $a.attr('href').replace(/\\/g, '/').replace(/^\/+/, '') | ||
article.ref = $a.attr('href').replace(/\\/g, '/').replace(/^\/+/, ''); | ||
} | ||
@@ -61,2 +63,67 @@ | ||
/** | ||
Find all parts and their corresponding lists | ||
@param {cheerio.Node} | ||
@param {cheerio.DOM} | ||
@return {Array<{title: String, list: cheerio.Node}>} | ||
*/ | ||
function findParts($parent, $) { | ||
// Find parts and lists | ||
// TODO asciidoc compatibility | ||
var partsAndLists = $parent.children(SELECTOR_LIST + ', ' + SELECTOR_PART); | ||
// Group each part with the list after | ||
var parts = []; | ||
var previousPart = null; | ||
partsAndLists.each(function (i, el) { | ||
if (isPartNode(el)) { | ||
if (previousPart !== null) { | ||
// The previous part was empty | ||
parts.push(previousPart); | ||
} | ||
previousPart = { | ||
title: getPartTitle(el, $), | ||
list: null | ||
}; | ||
} else { // It is a list | ||
if (previousPart !== null) { | ||
previousPart.list = el; | ||
} else { | ||
previousPart = { | ||
title: '', | ||
list: el | ||
}; | ||
} | ||
parts.push(previousPart); | ||
previousPart = null; | ||
} | ||
}); | ||
return parts; | ||
} | ||
/** | ||
True if the element is a part | ||
@param el | ||
@return {Boolean} | ||
*/ | ||
function isPartNode(el) { | ||
return SELECTOR_PART.indexOf(el.name) !== -1; | ||
} | ||
/** | ||
Parse the title of a part element | ||
@param el | ||
@param {cheerio.DOM} $ | ||
@return {String} | ||
*/ | ||
function getPartTitle(el, $) { | ||
return $(el).text().trim(); | ||
} | ||
/** | ||
Parse an HTML content into a tree of articles/parts | ||
@@ -71,17 +138,17 @@ | ||
var $lists = findList($root); | ||
var parts = []; | ||
var parts = findParts($root, $); | ||
$lists.each(function() { | ||
var $list = $(this); | ||
var $title = $list.prevUntil(SELECTOR_LIST, 'h2, h3, h4').first(); | ||
parts.push({ | ||
title: $title.text().trim(), | ||
articles: parseList($list, $) | ||
// Parse each list | ||
var parsedParts = []; | ||
var part; | ||
for (var i = 0; i < parts.length; ++i) { | ||
part = parts[i]; | ||
parsedParts.push({ | ||
title: part.title, | ||
articles: parseList($(part.list), $) | ||
}); | ||
}); | ||
} | ||
return { | ||
parts: parts | ||
parts: parsedParts | ||
}; | ||
@@ -88,0 +155,0 @@ } |
{ | ||
"name": "gitbook-html", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"homepage": "https://www.gitbook.com", | ||
@@ -5,0 +5,0 @@ "description": "Parse HTML content for gitbook", |
@@ -11,5 +11,7 @@ var fs = require('fs'); | ||
before(function() { | ||
var CONTENT = fs.readFileSync(path.join(__dirname, './fixtures/SUMMARY.html'), 'utf8'); | ||
var CONTENT = fs.readFileSync( | ||
path.join(__dirname, './fixtures/SUMMARY.html'), 'utf8'); | ||
LEXED = summary(CONTENT); | ||
PART = LEXED.parts[0]; | ||
}); | ||
@@ -27,2 +29,11 @@ | ||
}); | ||
it('should detect empty parts', function() { | ||
var CONTENT_EMPTY = fs.readFileSync( | ||
path.join(__dirname, './fixtures/SUMMARY-EMPTY.html'), 'utf8'); | ||
var LEXED_EMPTY = summary(CONTENT_EMPTY); | ||
assert.equal(LEXED_EMPTY.parts.length, 4); | ||
assert.equal(LEXED_EMPTY.parts[2].title, 'Empty part'); | ||
}); | ||
}); | ||
@@ -29,0 +40,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21065
22
543