-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (118 loc) · 5.54 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const request = require("node-superfetch");
const cheerio = require("cheerio");
const html2md = require('html-markdown');
class MDNDocs {
static async search(query = null) {
if (!query) return "No query specified.";
const $ = cheerio.load((await request.get(`https://developer.mozilla.org/en-US/search?q=${encodeURIComponent(query)}&topic=js`)).text);
let resText = $("div[class=\"search-pane search-results-explanation\"]");
resText = resText.children("p").text().split("\n").map(t => t.trim()).filter(t => t !== "").join("\n");
if (resText === `0 documents found for "${query}" in English (US).`) return null;
let resultUrl = $("li[class=\"result-1\"]")
.children("div[class=\"column-container\"]")
.children("div[class=\"column-5 result-list-item\"]")
.children("h4").children("a").attr("href");
return resultUrl;
}
static async load(result) {
return new MDNDocResult((await request.get(result)).text);
}
}
class MDNDocResult {
constructor(text) {
this.text = text;
this.$ = cheerio.load(text);
}
get name() {
return this.$("meta[property=\"og:title\"]").attr("content");
}
get description() {
const desc = this.$("p").first().html();
return md(desc);
}
get url() {
return this.$("meta[property=\"og:url\"]").attr("content");
}
get examples() {
const regex = /<h[1-6] id="Examples">Examples<\/h[1-6]>/;
const indexes = this.text.split("\n").map(t => t.trim().replace(/[\n]+/g, "\n"));
let index = indexes.indexOf(regex.test(this.text) ? regex.exec(this.text)[0] : null);
if (index === -1) return null;
const $ = cheerio.load(indexes.slice(index + 1).join('\n'));
return md($('pre').first().html());
}
get polyfill() {
const regex = /<h[1-6] id="Polyfill">Polyfill<\/h[1-6]>/;
const indexes = this.text.split("\n").map(t => t.trim().replace(/[\n]+/g, "\n"));
let index = indexes.indexOf(regex.test(this.text) ? regex.exec(this.text)[0] : null);
if (index === -1) return null;
const $ = cheerio.load(indexes.slice(index + 1).join('\n'));
return md($('pre').first().html());
}
get syntax() {
const regex = /<h[1-6] id="Syntax">Syntax<\/h[1-6]>/;
const indexes = this.text.split("\n").map(t => t.trim()).filter(t => t !== "");
let index = indexes.indexOf(regex.test(this.text) ? regex.exec(this.text)[0] : null);
if (index === -1) return null;
const $ = cheerio.load(indexes.slice(index + 1).join("\n"));
return md($("pre").first().html().replace(/<[^>]+>/g, ''));
}
get params() {
const regex = /<h[1-6] id="Parameters">Parameters<\/h[1-6]>/;
const indexes = this.text.split("\n").map(t => t.trim()).filter(t => t !== "");
let index = indexes.indexOf(regex.test(this.text) ? regex.exec(this.text)[0] : null);
if (index === -1) return null;
const params = [];
const text = indexes.slice(index + 1).join("\n");
const $ = cheerio.load(text);
$("dl").first().children().map((_, e) => params.push(md($(e).html()).replace(/<[^>]+>/g, '').replace(/(&.*;|
)/g, " ").replace(/Optional/g, ' (Optional)').replace(/Value/g, ' Value').replace(/[\r\n]+/g, '\n')));
return chunk(params, 2);
}
get pageDescription() {
const regex = /<h[1-6] id="Description">Description<\/h[1-6]>/;
const indexes = this.text.split("\n").map(t => t.trim()).filter(t => t !== "");
let index = indexes.indexOf(regex.test(this.text) ? regex.exec(this.text)[0] : null);
if (index === -1) return null;
const text = indexes.slice(index + 1).join("\n");
const $ = cheerio.load(text);
return md($("p").first().html());
}
get seeAlso() {
const regex = /<h[1-6] id="See_also">See also<\/h[1-6]>/;
const indexes = this.text.split("\n").map(t => t.trim()).filter(t => t !== "");
let index = indexes.indexOf(regex.test(this.text) ? regex.exec(this.text)[0] : null);
if (index === -1) return null;
const text = indexes.slice(index + 1).join("\n");
const $ = cheerio.load(text);
return md($("ul").first().html()).replace(/\d+\./g, '- ');
}
get methods() {
const regex = /<h[1-6] id="Methods">Methods<\/h[1-6]>/;
const indexes = this.text.split("\n").map(t => t.trim()).filter(t => t !== "");
let index = indexes.indexOf(regex.test(this.text) ? regex.exec(this.text)[0] : null);
if (index === -1) return null;
const methods = [];
const text = indexes.slice(index + 1).join("\n");
const $ = cheerio.load(text);
$("dl").first().children().map((_, e) => methods.push(md($(e).html()).replace(/<[^>]+>/g, '')));
return chunk(methods, 2);
}
get returnValue() {
const regex = /<h[1-6] id="Return_value">Return value<\/h[1-6]>/;
const indexes = this.text.split("\n").map(t => t.trim()).filter(t => t !== "");
const index = indexes.indexOf(regex.test(this.text) ? regex.exec(this.text)[0] : null);
if (index === -1) return null;
const text = indexes.slice(index + 1).join("\n");
const $ = cheerio.load(text);
return md($("p").first().html());
}
}
function chunk(arr, len) {
const chunked = [];
for (let i = 0; i < arr.length; i += len) chunked.push(arr.slice(i, i + len));
return chunked;
}
function md(html) {
return html2md.html2mdFromString(html);
}
module.exports = MDNDocs;