-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (106 loc) · 3.02 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
const fs = require('hexo-fs');
const moment = require('moment');
const pinyin = require('pinyin');
const _ = require('lodash');
function getPinYin(value, type) {
const STYLE_NORMAL = { style: pinyin.STYLE_NORMAL };
if (type === 'slug') {
return pinyin(value.replace(/\s+/g, '-'), STYLE_NORMAL).join('-').replace(/-+/g, '-');
} else if (type === 'user') {
return pinyin(value, STYLE_NORMAL).join('');
} else {
return pinyin(value, STYLE_NORMAL);
}
}
function getPosts(posts, pages) {
const data = [].concat(posts, pages);
if (!data.length) { return []; }
data.sort((a, b) => a.date - b.date);
return data.map((item, index) => {
const author_id = getPinYin(item.author, 'user');
return {
id: item._id,
title: item.title,
html: item._content.replace(/\/assets\//g, '/content/images/hexo/'),
featured: 0,
page: item.layout === 'post' ? 0 : 1,
status: 'published',
visibility: 'public',
author_id: author_id,
created_at: item.date.format(),
created_by: author_id,
updated_at: item.updated.format(),
updated_by: author_id,
published_at: item.date.format(),
published_by: author_id,
};
});
}
function getUsers(data) {
if (!data.length) { return []; }
const users = [];
const users_id = [];
data.forEach((item) => {
const author = item.author || 'autohome-fe';
const author_id = getPinYin(author, 'user');
if (users_id.indexOf(author_id) !== -1) {
return;
}
users_id.push(author_id);
users.push({
id: author_id,
name: author,
slug: author_id,
email: `${author_id}@autohome.com.cn`,
status: 'active',
visibility: 'public',
});
});
return users;
}
function getTags(tags, categories) {
const data = Object.assign({}, tags, categories);
if (!data || !Object.keys(data).length) { return []; }
return Object.keys(data).map(key => {
return {
id: data[key]._id,
name: data[key].name,
slug: getPinYin(data[key].name, 'slug'),
visibility: 'public',
};
});
}
function getPostsTags(data) {
if (!data.length) { return []; }
const tags = [];
data.forEach(item => {
const data = [].concat(item.tags.data, item.categories.data);
_.uniq(data, 'name').forEach(tag => {
tags.push({
"post_id": item._id,
"tag_id": tag._id,
});
});
});
return tags;
}
hexo.extend.generator.register('datatrans', locals => {
const ghostData = {
"meta": {
"exported_on": Date.now(),
"version": "1.19.0"
},
data: {
posts: getPosts(locals.posts.data, locals.pages.data),
users: getUsers(locals.posts.data),
tags: getTags(locals.tags.data, locals.categories.data),
posts_tags: getPostsTags(locals.posts.data),
},
};
// console.log(locals);
// console.log(ghostData);
const path = `hexo.export.${moment().format('YYYYMMDD')}.json`;
fs.writeFile(path, JSON.stringify(ghostData, null, '\t'), () => {
console.log('Data export completed =>', path);
});
});