-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
285 lines (240 loc) · 9.18 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const path = require('path') ;
const chalk = require('chalk') ;
const fs = require('fs-extra') ;
const archiver = require('archiver');
const filenamify = require('filenamify');
const fetch = require('node-fetch');
const cheerio = require('cheerio') ;
const Turndown = require('turndown');
const turndownTransformer = new Turndown({
headingStyle: 'atx',
codeBlockStyle: 'fenced',
});
turndownTransformer.use(require('turndown-plugin-gfm').gfm);
const debug = require('debug');
const sanitizeFilename = (name) => filenamify(name, { replacement: '-', maxLength: 250 });
const sanitizeMarkdownLink = (path) => path.replace(/[!'()* ]/g, (c) => ('%' + c.charCodeAt(0).toString(16)));
const replacePathVariable = (name, { author, username, title }) => {
return [name]
.map((n) => n.replace(/%title%/g, title))
.map((n) => n.replace(/%author%/g, author))
.map((n) => n.replace(/%user%/g, username))
.map((n) => path.normalize(n))[0];
};
const replaceFilenameVariable = (name, { author, username, title }) => {
return sanitizeFilename(replacePathVariable(name, { author, username, title }));
};
const print = (() => {
// ref: https://github.com/parcel-bundler/parcel/blob/v2/packages/reporters/cli/src/emoji.js
const supportsEmoji = process.platform !== 'win32' || process.env.TERM === 'xterm-256color';
return {
log: (...args) => console.log.call(console, chalk.whiteBright(args.join(' '))),
info: (...args) => console.info.call(console, chalk.blueBright([supportsEmoji ? 'ℹ️' : 'ℹ', '', ...args].join(' '))),
warn: (...args) => console.warn.call(console, chalk.yellowBright([supportsEmoji ? '⚠️' : '‼', '', ...args].join(' '))),
error: (...args) => console.error.call(console, chalk.redBright([supportsEmoji ? '🚨' : '×', '', ...args].join(' '))),
success: (...args) => console.log.call(console, chalk.greenBright([supportsEmoji ? '✨' : '√', '', ...args].join(' '))),
progress: (...args) => console.log.call(console, chalk.cyanBright([supportsEmoji ? '⏳' : '∞', '', ...args].join(' '))),
};
})();
const validateIronmanURL = (url) => /^https:\/\/ithelp[.]ithome[.]com[.]tw\/users\/\d+\/ironman\/\d+$/.test(url);
const createReadmeMarkdown = (seriesInfo) => {
const {
profileName,
seriesTitle,
seriesGroup,
seriesDescription,
seriesPageURL,
articlePageInfoList,
} = seriesInfo;
const articleLinks = articlePageInfoList
.map(({ title }) => `1. [${title}](${sanitizeMarkdownLink(title)}.md)`)
.join('\n');
return `
# ${seriesTitle}
* 作者:${profileName}
* 來源:${seriesPageURL}
* 參賽組別:\`${seriesGroup}\`
${seriesDescription}
## 目錄
${articleLinks}
`.trim() + '\n'; // with trailing line
};
const fetchArticlePageInfo = async(articleInfo) => {
debug('fetchArticlePageInfo')('--- start ---');
debug('fetchArticlePageInfo')('articleInfo.href', articleInfo.href);
const resp = await fetch(articleInfo.href).catch((error) => {
print.error('fetch 失敗', error);
debug('fetchArticlePageInfo')('fetch 失敗', error);
});
if (!resp) {
return;
}
if (!resp.ok) {
print.error('回應有錯誤', resp.status);
debug('fetchArticlePageInfo')('resp', resp);
return;
}
const html = await resp.text();
const $ = cheerio.load(html);
const articleTitle = $('.ir-article__title').text().trim();
const articleTime = $('.ir-article-info__time').text().trim();
const yamlFrontmatter = `
---
title: '${articleTitle}'
lang: zh-TW
meta:
- name: last-modified
content: ${articleTime}
---`.trim().replace(/^\s\s/gm, '');
const articleMarkdown = `${yamlFrontmatter}\n\n# ${articleTitle}\n\n` +
turndownTransformer.turndown($('.qa-markdown').html());
debug('fetchArticlePageInfo')('--- end ---');
return Object.assign(articleInfo, {
markdown: articleMarkdown,
});
};
const fetchSeriesPageInfo = async(seriesPageURL, option) => {
debug('fetchSeriesPageInfo')('--- start ---');
const opt = Object.assign({
articleOnly: false,
}, option);
debug('fetchSeriesPageInfo')('seriesPageURL', seriesPageURL);
const resp = await fetch(seriesPageURL).catch((error) => {
print.error('fetch 失敗', error);
debug('fetchSeriesPageInfo')('fetch 失敗', error);
});
if (!resp) {
return;
}
if (!resp.ok) {
print.error('回應有錯誤', resp.status);
debug('fetchSeriesPageInfo')('resp', resp);
return;
}
const result = { seriesPageURL };
const html = await resp.text();
const $ = cheerio.load(html);
if (!opt.articleOnly) {
const profileName = $('.profile-header__name').text().trim();
const $seriesInfoEl = $('.ir-profile-series');
const seriesGroup = $seriesInfoEl.find('.group__badge').text().trim();
const seriesTitle = sanitizeFilename($seriesInfoEl.find('.qa-list__title').text().trim().slice(0, -3));
const seriesDescription = $seriesInfoEl.find('.qa-list__description').text().trim();
Object.assign(result, { profileName, seriesGroup, seriesTitle, seriesDescription });
}
const articleInfoList = $('.qa-list__title-link').toArray().map((el) => {
const title = sanitizeFilename($(el).text().trim());
const href = el.attribs.href.trim();
return { title, href };
});
debug('fetchSeriesPageInfo')('--- end ---');
return Object.assign(result, { articleInfoList });
};
// type ArticlePageInfo = {
// title: String
// href: String
// markdown: String
// }
//
// type SeriesInfo = {
// seriesPageURL: String,
// profileName: String,
// seriesGroup: String,
// seriesTitle: String,
// seriesDescription: String,
// articlePageInfoList: ArticlePageInfo[]
// }
//
// fetchSeriesInfo :: URL -> SeriesInfo
const fetchSeriesInfo = async(seriesPageURL) => {
debug('fetchSeriesInfo')('--- start ---');
const seriesPageInfoPromiseList = ['', '?page=2', '?page=3'].map((search) => {
const option = { articleOnly: Boolean(search) };
return fetchSeriesPageInfo(seriesPageURL + search, option);
});
debug('fetchSeriesInfo')('seriesPageInfoPromiseList', seriesPageInfoPromiseList);
const seriesInfo = await Promise.all(seriesPageInfoPromiseList)
.then((seriesPageInfoList) => seriesPageInfoList.filter(Boolean)
.reduce((prev, curr) => {
prev.articleInfoList.push(...curr.articleInfoList);
return prev;
}))
.catch(print.error);
debug('fetchSeriesInfo')('seriesInfo', seriesInfo);
const { articleInfoList } = seriesInfo;
delete seriesInfo.articleInfoList;
const articlePageInfoList = await Promise.all(articleInfoList.map(fetchArticlePageInfo).filter(Boolean))
.catch(print.error);
debug('fetchSeriesInfo')('articlePageInfoList', articlePageInfoList);
debug('fetchSeriesInfo')('--- end ---');
return Object.assign(seriesInfo, { articlePageInfoList });
};
const exportSeries = async(seriesInfo, cli) => {
const opt = Object.assign({}, cli);
const [author, username] = seriesInfo.profileName.split(/[()\s]/g).filter(Boolean);
const title = seriesInfo.seriesTitle;
const dirName = replacePathVariable(opt.flags.dir, { author, username, title });
const outputName = replaceFilenameVariable(opt.flags.outputName, { author, username, title });
if (!opt.flags.dryRun) {
await fs.mkdirp(dirName);
const readmeMarkdown = createReadmeMarkdown(seriesInfo);
if (opt.command !== 'archive') {
// opt.command === 'download'|'serve'
const underProjectDir = (filename) => path.join(dirName, filename);
fs.writeFileSync(underProjectDir('README.md'), readmeMarkdown);
seriesInfo.articlePageInfoList.forEach(async(articlePageInfo) => {
const { markdown, title } = articlePageInfo;
fs.writeFileSync(underProjectDir(`${title}.md`), markdown);
});
} else {
// opt.command === 'archive'
const output = fs.createWriteStream(outputName)
.on('close', () => {
// console.log(archive.pointer() + ' total bytes');
// console.log('archiver has been finalized and the output file descriptor has closed.');
})
.on('end', () => {
print.success('壓縮完成');
});
const archive = archiver('zip', { zlib: { level: 9 } })
.on('warning', (err) => {
if (err.code === 'ENOENT') {
print.warn(err);
} else {
throw err;
}
})
.on('error', (err) => {
print.error(err);
throw err;
});
archive.pipe(output);
archive.append(Buffer.from(readmeMarkdown), { name: 'README.md' });
const writeStreamPromiseList = seriesInfo.articlePageInfoList.map(async(articlePageInfo) => {
const { markdown, title } = articlePageInfo;
archive.append(Buffer.from(markdown), { name: `${title}.md` });
});
await Promise.all(writeStreamPromiseList);
await archive.finalize();
}
}
const result = { dirName };
if (opt.command === 'archive') {
const src = path.resolve(outputName);
const dst = path.resolve(path.join(dirName, outputName));
if (src !== dst) {
fs.moveSync(src, dst, { overwrite: true });
}
Object.assign(result, { outputName, dstPath: dst });
}
return result;
};
module.exports = {
debug,
print,
exportSeries,
fetchSeriesInfo,
validateIronmanURL,
replacePathVariable,
replaceFilenameVariable,
};