-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 全面支持Github和Gitlab各平台-Vuepress2和Vitepress
- Loading branch information
Showing
20 changed files
with
1,530 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/adaptors/api/gitlab-vitepress/gitlabvitepressApiAdaptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { BlogConfig, PageTypeEnum, Post, YamlConvertAdaptor } from "zhi-blog-api" | ||
import { GitlabvitepressYamlConverterAdaptor } from "~/src/adaptors/api/gitlab-hexo/gitlabvitepressYamlConverterAdaptor.ts" | ||
import { CommonGitlabApiAdaptor } from "~/src/adaptors/api/base/gitlab/commonGitlabApiAdaptor.ts" | ||
import _ from "lodash" | ||
import {YamlUtil} from "zhi-common"; | ||
|
||
/** | ||
* Gitlabvitepress API 适配器 | ||
* | ||
* @author terwer | ||
* @version 1.3.2 | ||
* @since 0.8.1 | ||
*/ | ||
class GitlabvitepressApiAdaptor extends CommonGitlabApiAdaptor { | ||
public override getYamlAdaptor(): YamlConvertAdaptor { | ||
return new GitlabvitepressYamlConverterAdaptor() | ||
} | ||
|
||
public override async preEditPost(post: Post, id?: string, publishCfg?: any): Promise<Post> { | ||
// 公共的属性预处理 | ||
const doc = await super.preEditPost(post, id, publishCfg) | ||
|
||
// HEXO 自带的处理 | ||
const cfg: BlogConfig = publishCfg?.cfg | ||
const updatedPost = _.cloneDeep(doc) as Post | ||
|
||
// 自定义处理 | ||
const md = updatedPost.markdown | ||
this.logger.info("准备处理 Gitlabvitepress 正文") | ||
this.logger.debug("md =>", { md: md }) | ||
const yfm = YamlUtil.extractFrontmatter(md, true) | ||
let updatedMd = YamlUtil.extractMarkdown(md) | ||
|
||
// ====== | ||
// 可修改 updatedMd | ||
// ====== | ||
|
||
updatedPost.markdown = `${yfm}\n${updatedMd}` | ||
this.logger.info("Gitlabvitepress 正文处理完毕") | ||
this.logger.debug("updatedMd =>", { yfm: yfm, updatedMd: updatedMd }) | ||
|
||
// 发布格式 | ||
if (cfg?.pageType == PageTypeEnum.Markdown) { | ||
updatedPost.description = updatedPost.markdown | ||
} else { | ||
updatedPost.description = updatedPost.html | ||
} | ||
|
||
return updatedPost | ||
} | ||
} | ||
|
||
export { GitlabvitepressApiAdaptor } |
69 changes: 69 additions & 0 deletions
69
src/adaptors/api/gitlab-vitepress/gitlabvitepressConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { CategoryTypeEnum, PageTypeEnum, PasswordType } from "zhi-blog-api" | ||
import { VitepressConfig } from "~/src/adaptors/api/vitepress/vitepressConfig.ts" | ||
|
||
/** | ||
* Hexo 配置 | ||
* | ||
* @author terwer | ||
* @since 1.14.0 | ||
*/ | ||
class GitlabvitepressConfig extends VitepressConfig { | ||
constructor( | ||
githubUsername: string, | ||
githubAuthToken: string, | ||
githubRepo: string, | ||
githubBranch: string, | ||
middlewareUrl?: string | ||
) { | ||
super(githubUsername, githubAuthToken, githubRepo, githubBranch, middlewareUrl) | ||
|
||
this.home = "[your-gitlab-home]" | ||
this.apiUrl = "[your-gitlab-api-url]" | ||
this.tokenSettingUrl = "[your-gitlab-host]/-/profile/personal_access_tokens" | ||
this.showTokenTip = true | ||
this.defaultPath = "docs" | ||
this.previewUrl = "/[user]/[repo]/blob/[branch]/[docpath]" | ||
this.previewPostUrl = "/post/[postid].html" | ||
this.mdFilenameRule = "[slug].md" | ||
this.pageType = PageTypeEnum.Markdown | ||
this.passwordType = PasswordType.PasswordType_Token | ||
this.allowPreviewUrlChange = false | ||
this.tagEnabled = true | ||
this.cateEnabled = true | ||
this.allowCateChange = true | ||
this.categoryType = CategoryTypeEnum.CategoryType_Multi | ||
this.knowledgeSpaceEnabled = true | ||
this.allowKnowledgeSpaceChange = false | ||
this.placeholder.knowledgeSpaceReadonlyModeTip = "Gitlabvitepress 平台暂不支持修改发布目录,如需修改,请删除之后重新发布" | ||
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single | ||
this.useMdFilename = false | ||
this.usePathCategory = false | ||
} | ||
} | ||
|
||
export { GitlabvitepressConfig } |
30 changes: 30 additions & 0 deletions
30
src/adaptors/api/gitlab-vitepress/gitlabvitepressPlaceHolder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { VitepressPlaceHolder } from "~/src/adaptors/api/vitepress/vitepressPlaceHolder.ts" | ||
|
||
class GitlabvitepressPlaceHolder extends VitepressPlaceHolder {} | ||
|
||
export { GitlabvitepressPlaceHolder } |
36 changes: 36 additions & 0 deletions
36
src/adaptors/api/gitlab-vitepress/gitlabvitepressYamlConverterAdaptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2022-2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { VitepressYamlConverterAdaptor } from "~/src/adaptors/api/vitepress/vitepressYamlConverterAdaptor.ts" | ||
|
||
/** | ||
* Vitepress 平台的YAML解析器 | ||
* | ||
* @author terwer | ||
* @since 0.8.1 | ||
*/ | ||
class GitlabvitepressYamlConverterAdaptor extends VitepressYamlConverterAdaptor {} | ||
|
||
export { GitlabvitepressYamlConverterAdaptor } |
112 changes: 112 additions & 0 deletions
112
src/adaptors/api/gitlab-vitepress/useGitlabvitepressApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (c) 2023, Terwer . All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Terwer designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Terwer in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com | ||
* or visit www.terwer.space if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
import { createAppLogger } from "~/src/utils/appLogger.ts" | ||
import { PublisherAppInstance } from "~/src/publisherAppInstance.ts" | ||
import { useSettingStore } from "~/src/stores/useSettingStore.ts" | ||
import { JsonUtil, ObjectUtil, StrUtil } from "zhi-common" | ||
import { Utils } from "~/src/utils/utils.ts" | ||
import { getDynPostidKey } from "~/src/platforms/dynamicConfig.ts" | ||
import { CategoryTypeEnum } from "zhi-blog-api" | ||
import { GitlabvitepressConfig } from "~/src/adaptors/api/gitlab-vitepress/gitlabvitepressConfig.ts" | ||
import { GitlabvitepressYamlConverterAdaptor } from "~/src/adaptors/api/gitlab-vitepress/gitlabvitepressYamlConverterAdaptor.ts" | ||
import { GitlabvitepressApiAdaptor } from "~/src/adaptors/api/gitlab-vitepress/gitlabvitepressApiAdaptor.ts" | ||
|
||
const useGitlabvitepressApi = async (key: string, newCfg?: GitlabvitepressConfig) => { | ||
// 创建应用日志记录器 | ||
const logger = createAppLogger("use-gitlab-vitepress-api") | ||
|
||
// 记录开始使用 Hexo API | ||
logger.info("Start using Gitlabvitepress API...") | ||
|
||
// 创建应用实例 | ||
const appInstance = new PublisherAppInstance() | ||
|
||
let cfg: GitlabvitepressConfig | ||
if (newCfg) { | ||
logger.info("Initialize with the latest newCfg passed in...") | ||
cfg = newCfg | ||
} else { | ||
// 从配置中获取数据 | ||
const { getSetting } = useSettingStore() | ||
const setting = await getSetting() | ||
cfg = JsonUtil.safeParse<GitlabvitepressConfig>(setting[key], {} as GitlabvitepressConfig) | ||
|
||
// 如果配置为空,则使用默认的环境变量值,并记录日志 | ||
if (ObjectUtil.isEmptyObject(cfg)) { | ||
// 从环境变量获取 Hexo API 的 URL、认证令牌和其他配置信息 | ||
const githubUsername = Utils.emptyOrDefault(process.env.VITE_GITLAB_USERNAME, "") | ||
const githubAuthToken = Utils.emptyOrDefault(process.env.VITE_GITLAB_AUTH_TOKEN, "") | ||
const githubRepo = Utils.emptyOrDefault(process.env.VITE_GITLAB_REPO, "") | ||
const githubBranch = Utils.emptyOrDefault(process.env.VITE_GITLAB_BRANCH, "main") | ||
const middlewareUrl = Utils.emptyOrDefault( | ||
process.env.VITE_MIDDLEWARE_URL, | ||
"https://api.terwer.space/api/middleware" | ||
) | ||
cfg = new GitlabvitepressConfig(githubUsername, githubAuthToken, githubRepo, githubBranch, middlewareUrl) | ||
logger.info("Configuration is empty, using default environment variables.") | ||
} else { | ||
logger.info("Using configuration from settings...") | ||
} | ||
// 初始化posidKey | ||
if (StrUtil.isEmptyString(cfg.posidKey)) { | ||
// 默认值 | ||
cfg.posidKey = getDynPostidKey(key) | ||
} | ||
} | ||
|
||
// 文件规则 | ||
cfg.mdFilenameRule = "[slug].md" | ||
cfg.useMdFilename = false | ||
cfg.usePathCategory = false | ||
// 标签 | ||
cfg.tagEnabled = true | ||
// 分类 | ||
cfg.cateEnabled = true | ||
cfg.allowCateChange = true | ||
cfg.categoryType = CategoryTypeEnum.CategoryType_Multi | ||
// 知识空间 | ||
cfg.knowledgeSpaceEnabled = true | ||
cfg.knowledgeSpaceTitle = "发布目录" | ||
cfg.allowKnowledgeSpaceChange = false | ||
cfg.placeholder.knowledgeSpaceReadonlyModeTip = | ||
"Gitlabvitepress 平台暂不支持修改发布目录,如需修改,请删除之后重新发布" | ||
cfg.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single | ||
|
||
// 创建 Hexo 的 yamlAdaptor | ||
const yamlAdaptor = new GitlabvitepressYamlConverterAdaptor() | ||
|
||
// 创建 Hexo API 适配器 | ||
const blogApi = new GitlabvitepressApiAdaptor(appInstance, cfg) | ||
logger.info("Gitlabvitepress API created successfully.", cfg) | ||
|
||
return { | ||
cfg, | ||
yamlAdaptor, | ||
blogApi, | ||
} | ||
} | ||
|
||
export { useGitlabvitepressApi } |
Oops, something went wrong.