summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/lib/dom.js
blob: 2c2eaf76c0276f73d1f3da504025afe11ee43c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
};