Skip to content

Commit

Permalink
feat: #133 支持支持Github pages、HUGO、Jekyll、Next.js content、Nuxt.js conte…
Browse files Browse the repository at this point in the history
…nt-文章API适配
  • Loading branch information
terwer committed Nov 30, 2022
1 parent c629d00 commit 269b7db
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/components/publish/tab/main/CommonBlogMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ const initPage = async () => {
formData.postid = meta[commonCfg.posidKey || ""]
// 替换文章链接
previewUrl.value = await api.getPrevireUrl(formData.postid.toString())
previewUrl.value = await api.getPreviewUrl(formData.postid.toString())
if (props.useCat) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/components/publish/tab/main/MetaweblogMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ const initPage = async () => {
formData.postid = meta[metaweblogCfg.posidKey]
// 路径组合
previewUrl.value = await api.getPrevireUrl(formData.postid.toString())
previewUrl.value = await api.getPreviewUrl(formData.postid.toString())
try {
// 如果文章选择了分类,初始化分类
Expand Down
6 changes: 3 additions & 3 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export interface IApi {
* 获取预览链接
* @param postid 文章ID
*/
getPrevireUrl(postid: string): Promise<string>
getPreviewUrl(postid: string): Promise<string>
}

/**
Expand Down Expand Up @@ -240,8 +240,8 @@ export class API implements IApi {
return await this.apiAdaptor.getCategories()
}

async getPrevireUrl(postid: string): Promise<string> {
return await this.apiAdaptor.getPrevireUrl(postid)
async getPreviewUrl(postid: string): Promise<string> {
return await this.apiAdaptor.getPreviewUrl(postid)
}
}

2 changes: 1 addition & 1 deletion src/utils/platform/commonblog/commonblogApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class CommonblogApiAdaptor implements IApi {
return Promise.resolve(0);
}

public async getPrevireUrl(postid: string): Promise<string> {
public async getPreviewUrl(postid: string): Promise<string> {
return Promise.resolve("");
}
}
2 changes: 1 addition & 1 deletion src/utils/platform/commonblog/kms/kmsApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class KmsApiAdaptor extends CommonblogApiAdaptor implements IApi {
return await this.kmsApi.addDoc(post.title, post.description)
}

async getPrevireUrl(postid: string): Promise<string> {
async getPreviewUrl(postid: string): Promise<string> {
let previewUrl

// 替换文章链接
Expand Down
2 changes: 1 addition & 1 deletion src/utils/platform/commonblog/liandi/liandiApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class LiandiApiAdaptor extends CommonblogApiAdaptor implements IApi {
return result || ""
}

async getPrevireUrl(postid: string): Promise<string> {
async getPreviewUrl(postid: string): Promise<string> {
let previewUrl

// 替换文章链接
Expand Down
2 changes: 1 addition & 1 deletion src/utils/platform/commonblog/yuque/yuqueApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class YuqueApiAdaptor extends CommonblogApiAdaptor implements IApi {
return cats;
}

async getPrevireUrl(postid: string): Promise<string> {
async getPreviewUrl(postid: string): Promise<string> {
let previewUrl

// 替换文章链接
Expand Down
35 changes: 30 additions & 5 deletions src/utils/platform/github/githubApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export class GithubApi {
* @param sha 文件的sha,undefined表示新建,更新需要传sha字符串
*/
protected async createOrUpdatePage(docPath: string, mdContent: string, sha: any) {
let data

let res
try {
// const base64 = Buffer.from(mdContent).toString('base64');
Expand Down Expand Up @@ -99,7 +101,11 @@ export class GithubApi {
} catch (e) {
logUtil.logError("createOrUpdatePage error=>", e)
}
return res

if (res) {
data = res.data
}
return data;
}

/**
Expand All @@ -109,6 +115,8 @@ export class GithubApi {
* @param sha 文件的sha,undefined表示新建,更新需要传sha字符串
*/
protected async deletePage(docPath: string, sha: any) {
let data

let res
try {
const route = 'DELETE /repos/' + this.githubCfg.githubUser + '/' + this.githubCfg.githubRepo + '/contents/' + docPath;
Expand All @@ -129,7 +137,11 @@ export class GithubApi {
} catch (e) {
logUtil.logError("deletePage error=>", e)
}
return res

if (res) {
data = res.data
}
return data;
}

// ===========================
Expand All @@ -141,16 +153,29 @@ export class GithubApi {
* @param mdContent Markdown文本
*/
public async publishGithubPage(docPath: string, mdContent: string): Promise<any> {
// 因为是删除,所以这里始终新建,避免路径错误
// https://github.com/terwer/src-sy-post-publisher/issues/21
const sha = undefined // await this.getPageSha(docPath)

const sha = undefined
let res
res = await this.createOrUpdatePage(docPath, mdContent, sha)
logUtil.logInfo("Github publishPage,res=>", res)
return res;
}

/**
* 更新文章到Github
* @param docPath
* @param mdContent
*/
public async updateGithubPage(docPath: string, mdContent: string): Promise<any> {
// https://github.com/terwer/src-sy-post-publisher/issues/21
const sha = await this.getPageSha(docPath)

let res
res = await this.createOrUpdatePage(docPath, mdContent, sha)
logUtil.logInfo("Github updatePage,res=>", res)
return res;
}

/**
* 删除Github发布的文章
* @param docPath 对于根仓库的完整路径,包括文件名和扩展名
Expand Down
2 changes: 1 addition & 1 deletion src/utils/platform/github/githubApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class GithubApiAdaptor implements IApi {
throw new Error("该功能未实现,请在子类重写该方法")
}

async getPrevireUrl(postid: string): Promise<string> {
async getPreviewUrl(postid: string): Promise<string> {
throw new Error("该功能未实现,请在子类重写该方法")
}

Expand Down
21 changes: 17 additions & 4 deletions src/utils/platform/github/githubCfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,32 @@ export interface IGithubCfg {
* 文章别名key
*/
posidKey: string
/**
* 博客首页,预览用
*/
home: string
/**
* 预览规则
*/
previewUrl: string
}

/**
* Github平台通用配置类
*/
export class GithubCfg implements IGithubCfg {
author: string;
githubUser: string;
githubRepo: string;
githubToken: string;

defaultBranch: string;
defaultMsg: string;
defaultPath: string;
author: string;
email: string;
githubRepo: string;
githubToken: string;
githubUser: string;
posidKey: string = ""
home: string
previewUrl: string

constructor() {
this.githubUser = "";
Expand All @@ -63,5 +74,7 @@ export class GithubCfg implements IGithubCfg {
this.defaultMsg = "auto published by sy-post-publisher";
this.author = "terwer";
this.email = "youweics@163.com"
this.home = ""
this.previewUrl = ""
}
}
4 changes: 4 additions & 0 deletions src/utils/platform/github/hugo/hugoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class HugoApi extends GithubApi{
this.hugoCfg = hugoCfg;
}

async getPageData(docPath: string): Promise<any> {
return super.getPageData(docPath);
}

async getGithubPageTreeNode(docPath: string): Promise<Array<any>> {
return super.getGithubPageTreeNode(docPath);
}
Expand Down
58 changes: 40 additions & 18 deletions src/utils/platform/github/hugo/hugoApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {CategoryInfo} from "~/utils/common/categoryInfo";
import {UserBlog} from "~/utils/common/userBlog";
import {HugoCfg} from "~/utils/platform/github/hugo/hugoCfg";
import {pathJoin} from "~/utils/util";
import logUtil from "~/utils/logUtil";
import {Base64} from "js-base64";

/**
* Hugo的Api适配器
Expand All @@ -32,35 +34,55 @@ export class HugoApiAdaptor extends GithubApiAdaptor implements IApi {
return result
}

async deletePost(postid: string): Promise<boolean> {
return super.deletePost(postid);
}
async getPost(postid: string, useSlug?: boolean): Promise<Post> {
const commonPost = new Post();

async editPost(postid: string, post: Post, publish?: boolean): Promise<boolean> {
return super.editPost(postid, post, publish);
}
const page = await this.hugoApi.getPageData(postid)
commonPost.postid = page.path
commonPost.title = page.path
commonPost.description = Base64.fromBase64(page.content)
commonPost.link = page.path
commonPost.permalink = page.html_url
logUtil.logInfo("page=>", page)

async getCategories(): Promise<CategoryInfo[]> {
return super.getCategories();
return commonPost;
}

async getPost(postid: string, useSlug?: boolean): Promise<Post> {
return super.getPost(postid, useSlug);
async newPost(post: Post, publish?: boolean): Promise<string> {
const res = await this.hugoApi.publishGithubPage(post.postid, post.description)
if (!res || !res.content || !res.content.path) {
throw new Error("Hugo调用API异常")
}
return res.content.path;
}

async getPrevireUrl(postid: string): Promise<string> {
return super.getPrevireUrl(postid);
async editPost(postid: string, post: Post, publish?: boolean): Promise<boolean> {
const res = await this.hugoApi.updateGithubPage(post.postid, post.description)
if (!res || !res.content || !res.content.path) {
throw new Error("Hugo调用API异常")
}
return true;
}

async getRecentPosts(numOfPosts: number, page?: number, keyword?: string): Promise<Array<Post>> {
return super.getRecentPosts(numOfPosts, page, keyword);
async deletePost(postid: string): Promise<boolean> {
const res = await this.hugoApi.deleteGithubPage(postid)
if (!res || !res.commit || !res.commit.sha) {
throw new Error("Hugo调用API异常")
}
return true;
}

async getRecentPostsCount(keyword?: string): Promise<number> {
return super.getRecentPostsCount(keyword);
async getCategories(): Promise<CategoryInfo[]> {
return Promise.resolve([]);
}

async newPost(post: Post, publish?: boolean): Promise<string> {
return super.newPost(post, publish);
async getPreviewUrl(postid: string): Promise<string> {
let previewUrl
const newPostid = postid.substring(postid.lastIndexOf("/") + 1).replace(".md", "")
previewUrl = this.cfg.previewUrl.replace("[postid]", newPostid)
// 路径组合
previewUrl = pathJoin(this.cfg.home || "", previewUrl)

return previewUrl
}
}
4 changes: 4 additions & 0 deletions src/utils/platform/github/hugo/hugoCfg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {GithubCfg} from "~/utils/platform/github/githubCfg";
import {POSTID_KEY_CONSTANTS} from "~/utils/constants/postidKeyConstants";

/**
* Hugo配置
Expand All @@ -16,5 +17,8 @@ export class HugoCfg extends GithubCfg {
this.githubToken = githubToken;

this.defaultPath = "content/post"
this.posidKey = POSTID_KEY_CONSTANTS.HUGO_POSTID_KEY
this.home = ""
this.previewUrl = "/post/[postid].html"
}
}
4 changes: 2 additions & 2 deletions src/utils/platform/github/vuepress/vuepressApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class VuepressApiAdaptor extends GithubApiAdaptor implements IApi{
return super.getPost(postid, useSlug);
}

getPrevireUrl(postid: string): Promise<string> {
return super.getPrevireUrl(postid);
getPreviewUrl(postid: string): Promise<string> {
return super.getPreviewUrl(postid);
}

getUsersBlogs(): Promise<Array<UserBlog>> {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/platform/metaweblog/metaWeblogApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class MetaWeblogApiAdaptor implements IApi {
return cats
}

public async getPrevireUrl(postid: string): Promise<string> {
public async getPreviewUrl(postid: string): Promise<string> {
let previewUrl
const postUrl = this.cfg.previewUrl.replace("[postid]", postid)
previewUrl = pathJoin(this.cfg.home || "", postUrl)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/platform/siyuan/siYuanApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class SiYuanApiAdaptor implements IApi {
return Promise.resolve([]);
}

public async getPrevireUrl(postid: string): Promise<string> {
public async getPreviewUrl(postid: string): Promise<string> {
return Promise.resolve("");
}

Expand Down
Loading

0 comments on commit 269b7db

Please sign in to comment.