-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
207 lines (194 loc) · 6.46 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
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
// TODO: rewrite with TS
const http = require('http');
const https = require('https');
const path = require('path');
const fs = require('fs/promises');
if (process.argv.length < 3) {
console.error('output directory not specified');
console.error(
`Usage: ${process.argv[0]} ${process.argv[1]} <output_directory>`
);
return process.exit(1);
}
const outputDir = process.argv[2];
const listsDirName = 'lists';
const listsDir = path.join(outputDir, listsDirName);
const rootRepo = {
owner: 'sindresorhus',
name: 'awesome',
};
/** @type {(options: https.RequestOptions) => Promise<http.IncomingMessage>} */
const request = (options) => {
return new Promise((resolve, reject) => {
// use native https.request in order to make this script dependency-less
const req = https.request(
{
headers: { 'user-agent': 'node' },
...options,
},
(res) => {
res.data = Buffer.alloc(0);
res.on('data', (chunk) => (res.data += chunk));
res.on('end', () => resolve(res));
}
);
req.on('error', reject);
req.end();
});
};
/**
* @param {string[][]} components
* @return {string[]}
*/
const combination = (components) => {
if (components.length < 1) {
return [];
}
/** @type {string[]} */
const result = [];
/**
* @param {string} prefix
* @param {number} index
*/
const recurse = (prefix, index) => {
if (index >= components.length) {
result.push(prefix);
return;
}
for (const element of components[index]) {
recurse(prefix+element, index+1)
}
};
recurse('', 0)
return result;
};
const readmeCombination = combination([['', 'docs/'], ['README', 'readme', 'Readme'], ['.md', '.MD']]);
/**
* @param {{ owner: string, name: string, branch: string? }}
* @return {Promise<{ owner: string, name: string, branch: string, filename: string, markdown: string }>}
*/
const getReadme = async ({ owner, name, branch = null }) => {
if (!branch) {
try {
// If the default branch is main, request on branch master
// will get the result, the opposite is not.
// So we try on branch main first to get the correct branch name.
return await getReadme({ owner, name, branch: 'main' });
} catch {
return await getReadme({ owner, name, branch: 'master' });
}
}
for (const filename of readmeCombination) {
try {
const { data, statusCode } = await request({
hostname: 'raw.githubusercontent.com',
path: `/${owner}/${name}/${branch}/${filename}`,
});
if (statusCode != 200) continue;
return { owner, name, branch, filename, markdown: data.toString() };
} catch {}
}
throw new Error('README.md not found');
};
/** @param {{ owner: string, name: string, branch: string, filename: string, markdown: string }} */
const wrapReadmeObject = ({ owner, name, branch, filename, markdown }) => ({
owner,
name,
branch,
filename,
markdown,
fix() {
this.markdown = this.markdown.replace(/^---\n/s, '---\n---\n');
return this;
},
replaceHtmlImage() {
this.markdown = this.markdown.replace(
// best effort matching
/(<img\s+.*?src\s*=\s*(('|")?))([^\s>]+|.*?)(\2.*?\/?>)/gi,
(match, g1, g2, g3, g4, g5) =>
g4 ? `${g1}${this.getResourceAbsoluteURL(g4)}${g5}` : match
);
return this;
},
replaceMarkdownImage() {
this.markdown = this.markdown.replace(
// syntax: ![alt](src "title")
/(!\[(?:[^\\\]]|\\.)*?\]\(\s*)([^\s]*?)(\s*(\s((["'])(?:[^\6\\]|\\.)*?\6|\((?:[^\\)]|\\.)*?\)))?\))/gi,
(match, g1, g2, g3) =>
g2 ? `${g1}${this.getResourceAbsoluteURL(g2)}${g3}` : match
);
this.markdown = this.markdown.replace(
// syntax: [key]: href "title"
/(\[(?:[^\\\]]|\\.)*?\]:\s)([^\s]*)/gi,
(match, g1, g2) =>
g2 ? `${g1}${this.getResourceAbsoluteURL(g2)}` : match
);
return this;
},
addGithubBadges() {
this.markdown = this.markdown.replace(
// syntax: [text](href "title")
/(?<!!)(\[(?:[^\\\]]|\\.)*?)(\]\((((https?:)?\/\/)?github\.com\/([^\s\/?#)]+?)\/([^\s\/?#)]+)([^\s)]*?))\s*(\s((["'])(?:[^\11\\]|\\.)*?\11|\((?:[^\\)]|\\.)*?\)))?\))/gi,
"$1 ![GitHub Repo Stars](https://img.shields.io/github/stars/$6/$7) ![GitHub last commit](https://img.shields.io/github/last-commit/$6/$7)$2"
);
return this;
},
/** @type {(src: string) => string)} */
getResourceAbsoluteURL(src) {
const url = new URL(src, "protocol://hostname");
if (url.hostname != "hostname") return src;
return new URL(
url.pathname.replace(
/^\/*/,
`https://github.com/${this.owner}/${this.name}/raw/${this.branch}/`
)
).href;
},
});
(async () => {
try {
await fs.mkdir(outputDir, { recursive: true });
const rootReadme = wrapReadmeObject(await getReadme(rootRepo));
const repositories = Array.from(
rootReadme.markdown.matchAll(
/(?<!!)(\[(?:[^\\\]]|\\.)*?)(\]\((((https?:)?\/\/)?github\.com\/((?!topics)[^\s\/?#)]+?)\/([^\s\/?#)]+)([^\s)]*?))\s*(\s((["'])(?:[^\11\\]|\\.)*?\11|\((?:[^\\)]|\\.)*?\)))?\))/gi
),
(match) => ({ owner: match[6], name: match[7] })
);
await fs.writeFile(
`${outputDir}/README.md`,
rootReadme
.replaceHtmlImage()
.replaceMarkdownImage()
.markdown
.replace(
/(?<!!)(\[(?:[^\\\]]|\\.)*?)(\]\((((https?:)?\/\/)?github\.com\/((?!topics)[^\s\/?#)]+?)\/([^\s\/?#)]+)([^\s)]*?))\s*(\s((["'])(?:[^\11\\]|\\.)*?\11|\((?:[^\\)]|\\.)*?\)))?\))/gi,
`$1 ![GitHub Repo Stars](https://img.shields.io/github/stars/$6/$7) ![GitHub last commit](https://img.shields.io/github/last-commit/$6/$7)](./${listsDirName}/$6-$7.md) [*Origin*]($3)`
)
);
console.log("README.md");
console.log(`${repositories.length} awesome repositories`);
await fs.mkdir(listsDir, { recursive: true });
await Promise.all(
repositories.map(async (repository) => {
try {
await fs.writeFile(
`${listsDir}/${repository.owner}-${repository.name}.md`,
wrapReadmeObject(await getReadme(repository))
.fix()
.replaceHtmlImage()
.replaceMarkdownImage()
.addGithubBadges().markdown
);
console.log(`${repository.owner}-${repository.name}.md`);
} catch (err) {
return console.error(
`failed to get README.md of ${repository.owner}/${repository.name}: ${err}`
);
}
})
);
} catch (err) {
console.error(err);
}
})();