1 Star 0 Fork 0

dongyulongnet/hexo-bridge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
page.js 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
Borche 提交于 2021-02-14 20:45 . Upate package.
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,
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dongyulongnet/hexo-bridge.git
git@gitee.com:dongyulongnet/hexo-bridge.git
dongyulongnet
hexo-bridge
hexo-bridge
dependabot/add-v2-config-file

搜索帮助