diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-12-22 13:12:16 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 13:12:16 +0100 |
commit | 97f2c333a87b9d939b5a7dc2884590c971b53291 (patch) | |
tree | a22824b02d84a89e59c458c8af7d3494561d43f6 /packages/gitbook-html/src/dom.js | |
parent | 627e6dd866f77ff497a21f0b706490b82e40ea0e (diff) | |
download | gitbook-97f2c333a87b9d939b5a7dc2884590c971b53291.zip gitbook-97f2c333a87b9d939b5a7dc2884590c971b53291.tar.gz gitbook-97f2c333a87b9d939b5a7dc2884590c971b53291.tar.bz2 |
Import and adapt gitbook-html
Refactor to remove lodash and q as dependencies
Diffstat (limited to 'packages/gitbook-html/src/dom.js')
-rw-r--r-- | packages/gitbook-html/src/dom.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/packages/gitbook-html/src/dom.js b/packages/gitbook-html/src/dom.js new file mode 100644 index 0000000..9c5e070 --- /dev/null +++ b/packages/gitbook-html/src/dom.js @@ -0,0 +1,62 @@ +const cheerio = require('cheerio'); + +/** + * Parse an HTML string and return its content. + * @param {String} + * @return {cheerio.DOM} + */ +function parse(html) { + const $ = cheerio.load(html); + const $el = $('html, body').first(); + + return $el.length > 0 ? $el : $; +} + +/** + * Return main element for a DOM. + * @param {cheerio.DOM} + * @return {cheerio.Node} + */ +function root($) { + const $el = $('html, body, > div').first(); + return $el.length > 0 ? $el : $.root(); +} + +/** + * Return text node of an element. + * @param {cheerio.Node} + * @return {String} + */ +function textNode($el) { + return $el.children.reduce( + (text, e) => { + if (e.type == 'text') text += e.data; + return text; + }, + '' + ); +} + +/** + * Cleanup a DOM by removing all useless divs. + * @param {cheerio.Node} + * @param {cheerio.DOM} + * @return {cheerio.Node} + */ +function cleanup($el, $) { + $el.find('div').each(function() { + const $div = $(this); + cleanup($div, $); + + $div.replaceWith($div.html()); + }); + + return $el; +} + +module.exports = { + parse, + textNode, + root, + cleanup +}; |