diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-19 13:14:52 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 12:32:13 +0100 |
commit | 9e99b5850fd866fc2f9196993a0ae7e342311558 (patch) | |
tree | e430d039378e159f9046095e6f35285ecf6497fe /packages/gitbook-html/lib/dom.js | |
parent | eb0bf31baa6cb903ba4242ae5a3866ff67aeb97c (diff) | |
download | gitbook-9e99b5850fd866fc2f9196993a0ae7e342311558.zip gitbook-9e99b5850fd866fc2f9196993a0ae7e342311558.tar.gz gitbook-9e99b5850fd866fc2f9196993a0ae7e342311558.tar.bz2 |
Base code
Diffstat (limited to 'packages/gitbook-html/lib/dom.js')
-rw-r--r-- | packages/gitbook-html/lib/dom.js | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/packages/gitbook-html/lib/dom.js b/packages/gitbook-html/lib/dom.js new file mode 100644 index 0000000..2c2eaf7 --- /dev/null +++ b/packages/gitbook-html/lib/dom.js @@ -0,0 +1,23 @@ +var _ = require('lodash'); +var cheerio = require('cheerio'); + +// Parse an HTML string and return its content +function parse(html) { + var $ = cheerio.load('<div>'+html+'</div>'); + var $el = $('html, body').first(); + + return $el.length > 0? $el : $; +} + +// Return text node of an element +function textNode($el) { + return _.reduce($el.children, function(text, e) { + if (e.type == 'text') text += e.data; + return text; + }, ''); +} + +module.exports = { + parse: parse, + textNode: textNode +}; |