-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
151 lines (134 loc) · 5.24 KB
/
api.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
'use strict';
var http = require('./http');
var constants = require('./constants');
var fs = require('fs');
module.exports = function() {
var getUrl = function(path, baseUrl) {
return baseUrl + "/" + path;
}
var getHeaderToken = function(key, token, bearer) {
var headers = [];
var bearer;
if (bearer) {
bearer = 'Bearer ';
token = bearer + token;
};
headers.push({ key: key, value: token });
return headers;
}
var getContent = function(path, config, token) {
var url = getUrl(path, config.url);
var headerWithToken = getHeaderToken('auth', token, true);
return http.get(url, headerWithToken, []);
}
var addIsDraft = function(post) {
post.isDraft = post.source.indexOf('_draft') === 0;
post.isDiscarded = post.source.indexOf('_discarded') === 0;
return post;
}
var formatPost = function (post) {
var newPost = {};
post.itemModel.fields.forEach(function (field) {
if (field.fieldType.slug === constants.title) {
newPost.title = post.content[field.slug].data || '';
}
if (field.fieldType.slug === (constants.content[0] || constants.content[0])) {
newPost.content = post.content[field.slug].data || '';
}
if (field.fieldType.slug === (constants.summary)) {
newPost.summary = post.content[field.slug].data || '';
}
if (field.fieldType.slug === (constants.image)) {
if (post.content[field.slug]) {
newPost.featured_image = post.content[field.slug].data || '';
}
}
if (field.fieldType.slug === (constants.categories)) {
if (!post.content[field.slug]) return;
if (post.content[field.slug].data && post.content[field.slug].data.length) {
newPost.categories = [];
post.content[field.slug].data.forEach(function (category) {
newPost.categories.push(category.text);
});
}
}
if (field.fieldType.slug === (constants.tags)) {
if (!post.content[field.slug]) return;
if (post.content[field.slug].data && post.content[field.slug].data.length) {
newPost.tags = [];
post.content[field.slug].data.forEach(function (tag) {
newPost.tags.push(tag.text);
});
}
}
if (field.fieldType.slug === (constants.metatag)) {
newPost[constants.metatag+'_title'] = post.content[field.slug+'_title'].data || '';
newPost[constants.metatag+'_description'] = post.content[field.slug+'_description'].data || '';
}
});
newPost.date = post.createDate;
newPost.slug = post.slug;
if (post.itemModel.themeLayout) {
newPost.selected_layout = post.itemModel.themeLayout.archive_name;
}
if (!post.itemModel.multiple) {
newPost.layout = 'page';
}
return newPost;
}
var createNewItem = function(hexo, post) {
return hexo.post.create(formatPost(post))
.error(function(err) {
return new Error("Failed to create page");
})
.then(function(file) {
var source = file.path.slice(hexo.source_dir.length);
return hexo.source.process([source]);
});
}
var listLocalPosts = function(hexo) {
return hexo.model('Post').sort('-date').toArray();
}
var listLocalPages = function(hexo) {
return hexo.model('Page').toArray();
}
var auth = function (config) {
var url = getUrl('auth/#BySite', config.url);
var headerWithToken = getHeaderToken('token', config.readApiToken);
return http.post(url, headerWithToken, []);
}
var removeAllLocalPosts = function(hexo, posts) {
posts.forEach(function (post) {
fs.unlinkSync(hexo.source_dir + post.source);
})
}
var removeAllLocalPages = function(hexo, pages) {
pages.forEach(function (page) {
fs.unlinkSync(hexo.source_dir + page.source);
})
}
var sync = function(hexo, config) {
return auth(config).then(function (data) {
var token = data.data.token;
var siteHash = data.data.hashid;
return getContent('api/item?siteId='+siteHash, config, token).then(function(data) {
if (data.data.length) {
removeAllLocalPosts(hexo, listLocalPosts(hexo));
removeAllLocalPosts(hexo, listLocalPages(hexo));
data.data = JSON.parse(data.data);
return data.data.forEach(function(post) {
createNewItem(hexo, post);
});
} else {
removeAllLocalPosts(hexo, listLocalPosts(hexo));
}
});
}).catch(function (err) {
console.log(err);
});
}
return {
getContent: getContent,
sync: sync
}
}