-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
316 additions
and
11,456 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,73 @@ | ||
/** | ||
* Implement Gatsby's Node APIs in this file. | ||
* | ||
* See: https://www.gatsbyjs.org/docs/node-apis/ | ||
*/ | ||
const cheerio = require('cheerio'); | ||
const cloudscraper = require('cloudscraper').defaults({ resolveWithFullResponse: true }); | ||
|
||
// You can delete this file if you're not using it | ||
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 > 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) => { | ||
return 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 = () => { | ||
return 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() | ||
} | ||
}); | ||
} |
Oops, something went wrong.