summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-html/lib/dom.js
diff options
context:
space:
mode:
Diffstat (limited to 'packages/gitbook-html/lib/dom.js')
-rw-r--r--packages/gitbook-html/lib/dom.js23
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
+};