-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.js
211 lines (178 loc) · 5.6 KB
/
js.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import cheerio from 'cheerio';
import { URL } from 'url';
import { outputFile } from 'fs-extra';
import fetch from 'node-fetch';
/**
* Loadn content from Url
*
* @param {string} url Url of page
* @returns {string} Loaded content
*/
const loadPage = async (url) => {
const rawData = await fetch(url);
const data = await rawData.text();
return data;
}
/**
* Clear content
*
* @param {string} content Content for clear
* @returns {string} Cleared content
*/
const clearContent = (content) => {
const $ = cheerio.load(content, {decodeEntities: false});
$('svg').remove();
$('h1').remove();
$('.bc-table abbr').remove();
$('.bc-table button').remove();
$('.bc-table .bc-icon').remove();
$('.bc-table .bc-icons').remove();
$('.bc-table .bc-browser-name').remove();
$('.bc-github-link').remove();
$('.bc-legend').remove();
$('.hidden').remove();
return $.html();
}
/**
* Get Set of page links
*
* @param {string} content Page content
* @param {string} selector Links selector
* @param {string} root Links root url
* @returns {Map} Map of links
*/
const getLinks = (content, selector, root) => {
const $ = cheerio.load( content, {decodeEntities: false} );
const links = new Map();
$(selector).each((i, el) => {
const href = el.attribs.href;
if ( href.indexOf('#') === -1 ){
const resolvedUrl = new URL(href, root);
if ( resolvedUrl.href.indexOf(root) !== -1 ){
links.set( resolvedUrl.href.substring(root.length), resolvedUrl.href );
}
}
});
return links;
}
/**
* Fix links from root url
*
* @param {string} content Content for fixing links
* @param {string} selector Links selector
* @param {string} root Links root url
* @returns {string} Content with fixed links
*/
const fixLinks = (content, selector, root) => {
const $ = cheerio.load( content, {decodeEntities: false} );
$(selector).each((_i, el) => {
const href = el.attribs.href;
if ( !href.startsWith('#') ){
const resolvedUrl = new URL(href, root);
if ( resolvedUrl.href.startsWith(root) ){
const shortUrl = resolvedUrl.href.substring(root.length);
el.attribs.href = '/js' + shortUrl + '/';
} else {
el.attribs.href = resolvedUrl.href;
}
}
});
return $.html();
}
/**
* Save content to file
*
* @param {string} content Content
* @param {string} selector Selector what part of content to save
* @param {string} filename File name
*/
const saveContent = async (content, selector, filename) => {
const $ = cheerio.load( content, {decodeEntities: false} );
try {
await outputFile(filename, $(selector).html());
} catch (err) {
console.log(err);
};
}
/**
* Get page title
*
* @param {string} content Page content
* @param {string} selector Title selector
* @returns Title of page
*/
const getTitle = ( content, selector = 'head title' ) => {
const $ = cheerio.load( content, {decodeEntities: false} );
const title = $( selector ).text();
return title;
}
//
const START = 'https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects';
const START_EN = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects';
const ROOT = 'https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects';
const ROOT_EN = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects';
const OUTDIR = 'docs/js';
const SELECTOR = 'article a[href]';
const startContent = await loadPage(START);
const startContentEn = await loadPage(START_EN);
const clearedContent = clearContent( startContent );
const clearedContentEn = clearContent( startContentEn );
const fixContent = fixLinks(clearedContent, SELECTOR, ROOT);
const fixContentEn = fixLinks(clearedContentEn, SELECTOR, ROOT_EN);
await saveContent(fixContentEn, 'article', OUTDIR + '/index.html');
await saveContent(fixContent, 'article', OUTDIR + '/index.html');
console.log('Index saved');
const result = new Map();
let links = new Map();
let linksEn = new Map([
['/', START_EN],
...getLinks(clearedContentEn, SELECTOR, ROOT_EN),
])
let linksRu = new Map([
['/', START],
...getLinks(clearedContent, SELECTOR, ROOT),
])
for await (let [_key, link1] of linksEn){
const c1 = await loadPage(link1);
const cc1 = clearContent(c1);
const links1 = getLinks(cc1, SELECTOR, ROOT_EN);
links = new Set([...links, ...links1]); // [...links, ...links1];
console.log('Get links:', links.size);
}
for await (let [_key, link1] of linksRu){
const c1 = await loadPage(link1);
const cc1 = clearContent(c1);
const links1 = getLinks(cc1, SELECTOR, ROOT);
links = new Set([...links, ...links1]); // [...links, ...links1];
console.log('Get links:', links.size);
}
console.log('Prepared links:', links.size);
for await (let [_key, link] of links){
const subDir = _key; //link.substring(ROOT.length);
const c = await loadPage(link);
const cc = clearContent(c);
const title = getTitle(c, 'body h1');
const fc = fixLinks(cc, SELECTOR, ROOT);
const filename = subDir + '/index.html';
await saveContent(fc, 'article', OUTDIR + filename);
console.log('File saved:', filename);
const theme = subDir.split('/')[1];
result.set( filename, {
id: `js${filename}`,
path: `js${filename}`,
title: title,
name: title,
nameIndex: title.toLocaleLowerCase(),
theme: theme,
themeIndex: theme.toLocaleLowerCase(),
lang: 'JS',
langIndex: 'js',
filter: title.toLocaleLowerCase() + ' ' + theme.toLocaleLowerCase() + ' js',
})
}
try {
await outputFile(OUTDIR + '.json', JSON.stringify([...result.values()]));
console.log('JSON saved:', result.size)
} catch (err) {
console.log(err);
};