代码拉取完成,页面将自动刷新
const fs = require("hexo-fs");
const path = require("path");
let hexo = null;
let Page = null;
let SOURCE_DIR = null;
function setup(hexoInstance) {
hexo = hexoInstance;
Page = hexo.model("Page");
SOURCE_DIR = hexo.source_dir;
}
function getAllPages() {
return Page.toArray();
}
async function getSinglePage(id) {
const currentPage = Page.get(id);
if (currentPage) {
return currentPage;
} else {
throw new Error("Invalid page id: " + id);
}
}
async function save(id, content) {
const currentPage = await getSinglePage(id);
const pagePath = currentPage.full_source;
await fs.writeFileSync(pagePath, content);
return "Success!";
}
async function getNewPage(newPath) {
//TODO: Find a way to do this without setTimeout.
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve((newPage = Page.findOne({ source: newPath.split(SOURCE_DIR)[1] })));
}, 200);
});
}
async function create(title) {
const newPageInfo = await hexo.post.create({ title: title, layout: "page" });
return getNewPage(newPageInfo.path);
}
async function deletePage(id) {
const currentPage = await getSinglePage(id);
await fs.unlinkSync(currentPage.full_source);
const pagePath = path.dirname(currentPage.full_source);
const pageFiles = fs.readdirSync(pagePath);
if (pageFiles.length === 0) {
await fs.rmdirSync(pagePath);
}
return "deleted";
}
module.exports = {
setup: setup,
getAll: getAllPages,
getSingle: getSinglePage,
save: save,
delete: deletePage,
create: create,
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。