-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairbnb-readme-parser.js
52 lines (44 loc) · 1.36 KB
/
airbnb-readme-parser.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
import https from 'https';
const AIRBNB_JS_README_URL = 'https://raw.githubusercontent.com/airbnb/javascript/master/README.md';
const SEARCH_TERM = 'eslint.org/docs/rules/';
const getReadme = () => new Promise((resolve, reject) => {
let README = '';
https.get(AIRBNB_JS_README_URL, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
README += chunk;
});
res.on('end', () => {
resolve(README);
});
});
});
const parseReadme = (README) => {
const parsed = {};
const indexes = [];
let index = 0;
while (index < README.length && index !== -1) {
const foundIndex = README.indexOf(SEARCH_TERM, index);
index = foundIndex === -1 ? -1 : foundIndex + 1;
if (foundIndex !== -1) {
indexes.push(foundIndex);
}
}
indexes.forEach((index) => {
const id = README.slice(index + SEARCH_TERM.length, README.indexOf(')', index)).replace('.html', '');
const anchorEnd = README.lastIndexOf('"></a><a name', index);
const anchorStart = README.lastIndexOf('="', anchorEnd);
const anchor = README.slice(anchorStart + 2, anchorEnd);
parsed[id] = {
anchor
};
});
return parsed;
};
const getReadmeData = () => new Promise((resolve, reject) => {
getReadme().then((README) => {
const parsedRules = parseReadme(README);
resolve(parsedRules);
});
});
export default getReadmeData;