UNPKG

1.27 kBJavaScriptView Raw
1exports.jsdom = function (doc) {
2 var jsdom = require('jsdom').jsdom,
3 htmlparser = require('htmlparser');
4
5 doc.window = jsdom(doc.html, jsdom.defaultLevel, {
6 parser: htmlparser
7 }).createWindow();
8 return doc;
9};
10
11exports.first_paragraph = function (doc) {
12 if (!doc.window) doc = exports.jsdom(doc);
13 var tags = doc.window.document.getElementsByTagName("P");
14 doc.first_paragraph = tags.length ? tags[0].innerHTML: null;
15 return doc;
16};
17
18exports.heading = function (doc) {
19 if (!doc.window) doc = exports.jsdom(doc);
20 var tags = doc.window.document.getElementsByTagName("H1");
21 doc.heading = tags.length ? tags[0].innerHTML: null;
22 return doc;
23};
24
25exports.html_no_heading = function (doc) {
26 var jsdom = require('jsdom').jsdom,
27 htmlparser = require('htmlparser');
28
29 // create new window because we'll be editing the html
30 var window = jsdom(doc.html, jsdom.defaultLevel, {
31 parser: htmlparser
32 }).createWindow();
33 var tags = window.document.getElementsByTagName("H1");
34 if (tags.length) {
35 var elem = tags[0];
36 elem.parentNode.removeChild( elem );
37 doc.html_no_heading = window.document.innerHTML;
38 }
39 else doc.html_no_heading = doc.html;
40 return doc;
41};