-
Notifications
You must be signed in to change notification settings - Fork 6
/
post.js
128 lines (113 loc) · 3.68 KB
/
post.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
const fs = require("hexo-fs");
const path = require("path");
const frontMatterHelper = require("hexo-front-matter");
const settings = require("./setting");
let hexo = null;
let Post = null;
let SOURCE_DIR = null;
let POSTS_DIR = null;
let DRAFTS_DIR = null;
let READ_TIMEOUT = 200;
function setup(hexoInstance) {
hexo = hexoInstance;
Post = hexo.model("Post");
settings.setup(hexo);
READ_TIMEOUT = settings.getBridgeConfigAsJson().content_fetch_timeout || 200;
SOURCE_DIR = hexo.source_dir;
POSTS_DIR = path.join(SOURCE_DIR, "_posts");
DRAFTS_DIR = path.join(SOURCE_DIR, "_drafts");
if (!fs.existsSync(DRAFTS_DIR)) {
fs.mkdirsSync(DRAFTS_DIR);
}
if (!fs.existsSync(POSTS_DIR)) {
fs.mkdirsSync(POSTS_DIR);
}
}
function getAllPosts() {
return Post.toArray();
}
async function getSinglePost(id) {
const currentPost = Post.get(id);
if (currentPost) {
return currentPost;
} else {
throw new Error("Invalid post id: " + id);
}
}
async function findNewPost(newPath, timeout = READ_TIMEOUT) {
//TODO: Find a way to do this without setTimeout.
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve((newPost = Post.findOne({ source: newPath.split(SOURCE_DIR)[1] })));
}, timeout);
});
}
async function getNewPost(newPath) {
let newPostReturnInfo = await findNewPost(newPath);
//Search for the new post one more time. Sometimes it takes couple of ms for hexo to update the database.
if (!newPostReturnInfo) {
newPostReturnInfo = await findNewPost(newPath, READ_TIMEOUT + 200);
}
//If post is still not found, throw error.
if (!newPostReturnInfo) {
throw new Error("Sorry, I cannot find the newly created post.");
}
return newPostReturnInfo;
}
async function unpublish(id) {
const currentPost = await getSinglePost(id);
//Create old & new path
const oldPath = currentPost.full_source;
const fileName = path.basename(oldPath);
const newPath = path.join(DRAFTS_DIR, fileName);
// Rename & process the new changes
fs.renameSync(oldPath, newPath);
await hexo.source.process([SOURCE_DIR, POSTS_DIR, DRAFTS_DIR]);
return await getNewPost(newPath);
}
async function publish(id) {
const currentPost = await getSinglePost(id);
//Create old & new path
const oldPath = currentPost.full_source;
const fileName = path.basename(oldPath);
const newPath = path.join(POSTS_DIR, fileName);
// Rename & process the new changes
fs.renameSync(oldPath, newPath);
await hexo.source.process([SOURCE_DIR, POSTS_DIR, DRAFTS_DIR]);
return await getNewPost(newPath);
}
async function save(id, content) {
const currentPost = await getSinglePost(id);
const postPath = currentPost.full_source;
await fs.writeFileSync(postPath, content);
return "Success!";
}
async function updateContent(id, content) {
const currentPost = await getSinglePost(id);
const postPath = currentPost.full_source;
const parsed = frontMatterHelper.parse(currentPost.raw);
const metadata = (({ _content, ...others }) => ({ ...others }))(parsed);
const newContent = frontMatterHelper.stringify(metadata) + content;
await fs.writeFileSync(postPath, newContent);
return "Success!";
}
async function create(title, scaffold) {
const postParameters = { title: title, layout: scaffold };
const newPostInfo = await hexo.post.create(postParameters);
return await getNewPost(newPostInfo.path);
}
async function deletePost(id) {
const currentPost = await getSinglePost(id);
return await fs.unlinkSync(currentPost.full_source);
}
module.exports = {
setup: setup,
getAll: getAllPosts,
getSingle: getSinglePost,
unpublish: unpublish,
publish: publish,
save: save,
updateContent: updateContent,
create: create,
delete: deletePost,
};