-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (56 loc) · 1.7 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
import "nodejsscript";
/**
* @typedef Article_object
* @type {{ title: string, link: string, description: string, updated: string, github_file: string }}
* */
let webDocUrls;
/** @returns {Promise<Article_object>} */
export async function randomMDN(){
if(!webDocUrls)
webDocUrls= await getWebDocUrls();
const webDocUrls_len= webDocUrls.length;
let candidate= {};
while(!candidate.title)
candidate= await pipe(
()=> webDocUrls_len * Math.random(),
random_number=> Math.floor(random_number),
random_integer=> webDocUrls[random_integer],
parseArticle
)();
return candidate;
}
import { url_sitemap, url_web } from './consts.js';
async function getWebDocUrls(){
const sitemap= await fetch(url_sitemap, {
responseType: 'buffer',
headers: {
'accept-encoding': 'gzip',
},
}).then(res=> res.text());
return Array.from(sitemap.matchAll(/<loc>(.*?)<\/loc>/g))
.map(([ _, url ])=> url)
.filter(url=> url.startsWith(url_web));
}
/** @param {string} link @returns {Promise<Article_object | { title: null }>} */
async function parseArticle(link){
const jsonEmpty= ()=> ({ isActive: false });
const json= await fetch(link+"/index.json")
.then(res=> res.json())
.then(json=> json.doc || jsonEmpty())
.catch(jsonEmpty);
if(!json.isActive || isDeprecated(json)) return {};
const pluck= (key, o= json)=> o[key] || "";
return {
title: pluck("title"),
link,
description: pluck("summary").replace(/\n */g, " "),
updated: pluck("modified"),
github_file: pluck("github_url", json.source || {})
};
}
function isDeprecated({ body: [ opening ] }){
if(!opening) return true;
const { content }= opening?.value || {};
if(!content) return true;
return /class="notecard deprecated"/.test(content);
}