-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
102 lines (90 loc) · 2.98 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
var github = require('octonode');
var matter = require('hexo-front-matter');
var log = hexo.log,
post = hexo.post,
pagesn = 1,
publish_mode,
source,
repo;
hexo.extend.migrator.register('github-issue', function(args, callback){
source = args._.shift();
// load --publish option
const { publish } = args;
publish_mode = publish;
if (!source){
var help = [
'Usage: hexo migrate github-issue <owner/repo>',
'',
'For more help, you can check the docs: http://hexo.io/docs/migration.html'
];
console.log(help.join('\n'));
return callback();
}
log.i('Migrate from %s...', source);
repo = github.client().repo(source);
nextpage(callback);
});
function nextpage(cb) {
var category_prefix = 'category_';
// refrence from https://github.com/hexojs/hexo-front-matter/blob/69516870249e91ba3e77e5b2e395645b3991d97a/lib/front_matter.js#L5
var regexp = /^(-{3,})(\r\n)([\s\S]+?)\r\n\1\r\n?([\s\S]*)/;
repo.issues(pagesn, function(err, body, headers) {
if (!err) {
if (body && body.length) {
for(var i in body) {
var issue = body[i];
var categories = [];
var tags = [];
var data = {};
var published_tag = false;
for (var i in issue.labels) {
var name = issue.labels[i].name;
if (name.indexOf(category_prefix) != -1) {
name = name.substr(category_prefix.length);
categories.push(name);
} else if (name.toLowerCase() == "draft") {
data.layout = "draft"
} else if (name.toLowerCase() == "publish") {
published_tag = true
} else {
tags.push(name);
}
}
// parse front-matter
var match = issue.body.match(regexp);
if (match) {
// replace CRLF with LF before parse
var separator = match[1];
var frontMatterData = match[3].replace(/\r\n/g, '\n');
var content = match[4];
var issueBody = separator + '\n' + frontMatterData + '\n' + separator + '\n' + content;
} else {
var issueBody = issue.body;
}
var { _content, ...meta } = matter.parse(issueBody);
data.title = (meta.title ? meta.title : issue.title).replace(/\"/g,"");
// if you migrate with --publish option, will skip unpublished issue
if (publish_mode && (!published_tag) ) {
log.i('skip unpublished post: ' + data.title);
continue;
}
data.content = _content;
data.date = issue.created_at;
data.tags = tags;
data.categories = categories;
data.number = issue.number;
data = Object.assign(data, meta);
post.create(data, true);
log.i('create post: ' + data.title);
}
pagesn++;
nextpage(cb);
return;
};
cb();
} else {
log.i('cannot get post: ' + err);
cb();
}
});
}