-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.js
69 lines (55 loc) · 2.02 KB
/
gatsby-node.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
/* eslint-disable import/no-extraneous-dependencies */
const cheerio = require('cheerio');
const cloudscraper = require('cloudscraper').defaults({ resolveWithFullResponse: true });
const mainDocsURL = 'https://ionicframework.com';
const downloadedContent = [];
const getLinksList = (responseBody) => {
const $ = cheerio.load(responseBody);
const linksSet = new Set();
$('ul li a').each(function () {
linksSet.add($(this).attr('href'));
});
if (linksSet.size < 1) throw new Error('No links found');
return Array.from(linksSet).filter(str => str.charAt(0) === '/');
};
const getCSSVars = (responseBody) => {
const $ = cheerio.load(responseBody);
const cssVars = [];
$('#css-custom-properties + .table-wrapper > table > tbody > tr').each(function () {
cssVars.push({
cssVar: $(this).children(':first-child').text().trim(),
cssDesc: $(this).children(':nth-child(2)').text().trim(),
});
});
return cssVars;
};
const getPageContent = link => cloudscraper.get(mainDocsURL + link)
.then((response) => {
const $ = cheerio.load(response.body);
downloadedContent.push({
title: $('h1').text().trim(),
url: mainDocsURL + link,
cssVars: getCSSVars(response.body),
});
});
exports.onPreBootstrap = () => cloudscraper.get(`${mainDocsURL}/docs/api`)
.then((response) => {
const linksList = getLinksList(response.body);
const promises = linksList.map(link => getPageContent(link));
return Promise.all(promises);
});
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions;
deletePage(page);
const isEmpty = downloadedContent.filter(data => data.cssVars.length > 0);
if (isEmpty.length < 1) throw new Error('No CSS variables to be displayed');
const dateNow = new Date();
createPage({
...page,
context: {
...page.context,
downloadedContent: downloadedContent.sort((a, b) => a.title.localeCompare(b.title)),
buildDate: `${dateNow.getDate()}/${dateNow.getMonth() + 1}/${dateNow.getFullYear()}`,
},
});
};