-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
118 lines (95 loc) · 2.75 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
'use strict';
const fs = require('fs');
const moment = require('moment');
const pinyin = require('pinyin');
const remark = require('remark');
const pangu = require('remark-pangu');
const Mustache = require('mustache');
const rp = require('request-promise');
const debug = require('debug')('githubIssue2Blog');
const markpangu = remark().use(pangu);
function getPinyinTitle(title) {
const pinyinTitle = pinyin(title, {
style: pinyin.STYLE_NORMAL,
});
return pinyinTitle
.map(t => t[0]).join('-')
.replace(/\s/g, '-')
.toLowerCase();
}
function getEnTitleFromBody(body) {
const enTitle = body.match(/<!--\s+en_title:\s*([^\s]+)\s+-->/i);
return enTitle && enTitle[1]
}
exports.renderMDFiles = function renderMDFiles(templateFile, posts) {
const template = fs.readFileSync(templateFile).toString();
Mustache.parse(template);
return posts.map((post) => {
const tags = post.labels.map(l => l.name).join(',');
const date = moment(post.created_at).format('YYYY-MM-DD HH:mm:ss');
const updated = moment(post.updated_at).format('YYYY-MM-DD HH:mm:ss');
const enTitle = getEnTitleFromBody(post.body);
const pinyinTitle = getPinyinTitle(post.title);
debug('Title', post.title, pinyinTitle, enTitle);
post.body = markpangu.processSync(post.body).contents;
const content = Mustache.render(
template, {
date,
updated,
tags,
post,
}
);
return {
title: enTitle || pinyinTitle,
content,
};
});
}
exports.writeFiles = function writeFiles(dir, renderedFiles) {
for (const rendered of renderedFiles) {
fs.writeFileSync(`${dir}/${rendered.title}.md`, rendered.content);
}
}
exports.getIssues = async function getIssues(params) {
const url = `https://api.github.com/repos/${params.user}/${params.repo}/issues`;
const qs = {};
if (!params.allowOtherUsers) {
qs.creator = params.user;
}
if (params.since) {
qs.since = moment(params.since).toISOString();
}
return await _getIssues(url, qs);
};
async function _getIssues(url, qs) {
const response = await rp.get({
url,
qs,
headers: {
'User-Agent': 'node',
'Accept': 'application/vnd.github.v3+json'
},
json: true,
resolveWithFullResponse: true,
});
if (response.statusCode >= 400) {
throw new Error(response.body);
}
let issues = response.body;
if (!response.headers.link) {
return issues;
}
const matches = response.headers.link.match(/<([^>]+)>; rel="(\w+)"/);
let nextUrl;
for (let i = 0; i < matches.length; i++) {
if (matches[i] === 'next') {
nextUrl = matches[i - 1];
}
}
if (nextUrl) {
issues = issues.concat(await _getIssues(nextUrl, qs));
}
debug(`Get issues length: ${issues.length}`);
return issues;
}