-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
53 lines (39 loc) · 1.4 KB
/
index.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
var needle = require('needle');
var cheerio = require('cheerio');
var fs = require('fs');
const json2md = require("json2md");
var urls = ['https://howhttps.works/', 'https://howhttps.works/episodes/','https://howhttps.works/why-do-we-need-https/', 'https://howhttps.works/the-keys/', 'https://howhttps.works/the-handshake/', 'https://howhttps.works/https-ssl-tls-differences/','https://howhttps.works/certificate-authorities/'];
function parser(url ,index){
const results = [];
needle.get(url, function(err, res){
if (err) throw err;
const $ = cheerio.load(res.body);
$('main *').each(function(i,elem){
if(checkTextElement(elem.name)){
if (elem.name==='span'||elem.name==='text') elem.name='p';
if (elem.children[0].data!==undefined) {results.push(getElementObj(elem.name, elem.children[0].data));}
}
})
const fileName = url.match(/\/([^\/]+)[\/]?$/)[1];
write(results,`./${index}-${fileName}-en.md`);
});
}
urls.forEach(function(item,i) {
parser(item,i);
});
function write(res,filePath){
fs.writeFileSync(filePath, json2md(res));
};
function getElementObj(tagName, value){
const elementObj={};
elementObj[tagName]=value;
// console.log(elementObj);
return elementObj;
}
getElementObj('h1','episodes');
function checkTextElement(element){
const tagNames=['h1','p','span','text'];
return tagNames.some(function(tagName){
return element===tagName;
});
}