forked from iansinnott/date-fns-dash-docset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
77 lines (62 loc) · 2.17 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const puppeteer = require('puppeteer');
const { Observable } = require('rxjs');
const fromReadableStream = (stream) => Observable.create(obs => {
const next = chunk => obs.next(chunk);
const error = err => obs.error(err);
const complete = () => obs.complete();
// Set UTF-8 so we don't get Buffer instances read through
stream.setEncoding('utf8');
// Setup
stream.on('data', next);
stream.on('error', error);
stream.on('end', complete);
// Cleanup
return () => {
stream.removeAllListeners();
};
});
const stripNavigation = () => {
document.querySelector('.docs-finder').remove();
document.querySelector('.docs_nav_bar').remove();
// correct paddings
document.querySelector('.docs-content').style.paddingLeft = 0;
document.querySelector('.docs').style.paddingTop = 0;
}
const getDocumentHTML = () => {
const serializer = new XMLSerializer();
return serializer.serializeToString(window.document);
};
/**
* Load a URL in headless chrome. This could easily be accomplished with curl or
* any request library excpet using chrome allows us to wait until all network
* requests on the page have finished and let javascript run. This is super
* important for react based sites like date-fns since they don't contain body
* markup on initial load.
*
* NOTE: By default the headless browser will wait 1s after all network traffic
* has ceased to declare the wait over. However, there is no guarantee the app
* will actually be rendered at this point. It also means that even if the app
* renders before then it will still wait. So this could be improved.
*/
const loadURL = async (url, preserveNavigation = false) => {
const browser = await puppeteer.launch({
args: [
'--disable-web-security',
]
});
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle', networkIdleTimeout: 5000 }); // See NOTE
if (!preserveNavigation) {
await page.evaluate(stripNavigation);
}
const content = await page.evaluate(getDocumentHTML);
browser.close();
return content;
};
const isDebugging = () => Boolean(process.env.DEBUG);
module.exports = {
getDocumentHTML,
fromReadableStream,
loadURL,
isDebugging,
};