From 91ce36cc40cddcc00cc8e1bf4d94404b5e756b93 Mon Sep 17 00:00:00 2001 From: terwer Date: Sun, 6 Aug 2023 16:51:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=9F=E4=B8=80=E6=95=B4=E5=90=88?= =?UTF-8?q?=E5=90=84=E5=B9=B3=E5=8F=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/adaptor/CommonGithubApiAdaptor.ts | 4 +- .../base/github/config/CommonGithubConfig.ts | 43 ++++++++++++- .../github/config/CommonGithubPlaceholder.ts | 32 ++++++++++ .../api/hexo/adaptor/hexoApiAdaptor.ts | 37 +++++++++++ src/adaptors/api/hexo/config/hexoConfig.ts | 55 ++++++++++++++++ .../api/hexo/config/hexoPlaceHolder.ts | 30 +++++++++ src/adaptors/api/hexo/useHexoApi.ts | 63 +++++++++++++++++++ .../singleplatform/CommonGithubSetting.vue | 50 +++++++++++++++ .../singleplatform/MetaweblogSetting.vue | 2 +- .../publish/singleplatform/SettingEntry.vue | 1 + .../singleplatform/github/HexoSetting.vue | 57 +++++++++++++++++ 11 files changed, 370 insertions(+), 4 deletions(-) create mode 100644 src/adaptors/api/base/github/config/CommonGithubPlaceholder.ts create mode 100644 src/adaptors/api/hexo/adaptor/hexoApiAdaptor.ts create mode 100644 src/adaptors/api/hexo/config/hexoConfig.ts create mode 100644 src/adaptors/api/hexo/config/hexoPlaceHolder.ts create mode 100644 src/components/set/publish/singleplatform/CommonGithubSetting.vue create mode 100644 src/components/set/publish/singleplatform/github/HexoSetting.vue diff --git a/src/adaptors/api/base/github/adaptor/CommonGithubApiAdaptor.ts b/src/adaptors/api/base/github/adaptor/CommonGithubApiAdaptor.ts index bf4db7ac..fd2fe057 100644 --- a/src/adaptors/api/base/github/adaptor/CommonGithubApiAdaptor.ts +++ b/src/adaptors/api/base/github/adaptor/CommonGithubApiAdaptor.ts @@ -23,7 +23,7 @@ * questions. */ -import { BlogApi } from "zhi-blog-api" +import { BaseBlogApi } from "~/src/adaptors/api/base/baseBlogApi.ts" /** * Github API 适配器 @@ -32,7 +32,7 @@ import { BlogApi } from "zhi-blog-api" * @version 0.9.0 * @since 0.9.0 */ -class CommonGithubApiAdaptor extends BlogApi { +class CommonGithubApiAdaptor extends BaseBlogApi { } diff --git a/src/adaptors/api/base/github/config/CommonGithubConfig.ts b/src/adaptors/api/base/github/config/CommonGithubConfig.ts index bc01844a..c513ed13 100644 --- a/src/adaptors/api/base/github/config/CommonGithubConfig.ts +++ b/src/adaptors/api/base/github/config/CommonGithubConfig.ts @@ -23,6 +23,47 @@ * questions. */ -class CommonGithubConfig {} +import { PasswordType } from "zhi-blog-api" +import { CommonblogConfig } from "~/src/adaptors/api/base/commonblog/config/CommonblogConfig.ts" + +/** + * CommonGithubConfig 类用于存储 GitHub 相关配置信息 + */ +class CommonGithubConfig extends CommonblogConfig { + public githubRepo: string + public githubBranch: string + public defaultPath: string + + /** + * 构造函数 + * + * @param {string} githubUsername - GitHub 用户名 + * @param {string} githubAuthToken - GitHub 访问令牌 + * @param {string} githubRepo - GitHub 仓库 + * @param {string} githubBranch - GitHub 分支 + * @param {string} middlewareUrl - 跨域代理 URL + */ + constructor( + githubUsername: string, + githubAuthToken: string, + githubRepo: string, + githubBranch: string, + middlewareUrl?: string + ) { + super("https://github.com", "", githubUsername, githubAuthToken, middlewareUrl) + + this.username = githubUsername + this.password = githubAuthToken + this.usernameEnabled = true + this.passwordType = PasswordType.PasswordType_Token + this.githubRepo = githubRepo + this.githubBranch = githubBranch + this.defaultPath = "/" + this.blogid = "" + this.blogName = "" + + this.middlewareUrl = middlewareUrl + } +} export { CommonGithubConfig } diff --git a/src/adaptors/api/base/github/config/CommonGithubPlaceholder.ts b/src/adaptors/api/base/github/config/CommonGithubPlaceholder.ts new file mode 100644 index 00000000..6432792d --- /dev/null +++ b/src/adaptors/api/base/github/config/CommonGithubPlaceholder.ts @@ -0,0 +1,32 @@ +/* + * 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 { CommonblogPlaceholder } from "~/src/adaptors/api/base/commonblog/config/CommonblogPlaceholder.ts" + +/** + * Github 操作提示 + */ +class CommonGithubPlaceholder extends CommonblogPlaceholder {} +export { CommonGithubPlaceholder } diff --git a/src/adaptors/api/hexo/adaptor/hexoApiAdaptor.ts b/src/adaptors/api/hexo/adaptor/hexoApiAdaptor.ts new file mode 100644 index 00000000..32af99ae --- /dev/null +++ b/src/adaptors/api/hexo/adaptor/hexoApiAdaptor.ts @@ -0,0 +1,37 @@ +/* + * 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 { CommonGithubApiAdaptor } from "~/src/adaptors/api/base/github/adaptor/CommonGithubApiAdaptor.ts" + +/** + * Hexo API 适配器 + * + * @author terwer + * @version 1.3.2 + * @since 0.8.1 + */ +class HexoApiAdaptor extends CommonGithubApiAdaptor {} + +export { HexoApiAdaptor } diff --git a/src/adaptors/api/hexo/config/hexoConfig.ts b/src/adaptors/api/hexo/config/hexoConfig.ts new file mode 100644 index 00000000..1db62841 --- /dev/null +++ b/src/adaptors/api/hexo/config/hexoConfig.ts @@ -0,0 +1,55 @@ +/* + * 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 { CommonGithubConfig } from "~/src/adaptors/api/base/github/config/CommonGithubConfig.ts" +import { PageTypeEnum, PasswordType } from "zhi-blog-api" + +/** + * 博 Hexo 配置 + * + * @author terwer + * @since 1.3.2 + */ +class HexoConfig extends CommonGithubConfig { + constructor( + githubUsername: string, + githubAuthToken: string, + githubRepo: string, + githubBranch: string, + middlewareUrl?: string + ) { + super(githubUsername, githubAuthToken, githubRepo, githubBranch, middlewareUrl) + + this.tokenSettingUrl = "https://github.com/settings/tokens" + this.defaultPath = "source/_posts" + 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 + } +} + +export { HexoConfig } diff --git a/src/adaptors/api/hexo/config/hexoPlaceHolder.ts b/src/adaptors/api/hexo/config/hexoPlaceHolder.ts new file mode 100644 index 00000000..54a08a46 --- /dev/null +++ b/src/adaptors/api/hexo/config/hexoPlaceHolder.ts @@ -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 { CommonGithubPlaceholder } from "~/src/adaptors/api/base/github/config/CommonGithubPlaceholder.ts" + +class HexoPlaceHolder extends CommonGithubPlaceholder {} + +export { HexoPlaceHolder } diff --git a/src/adaptors/api/hexo/useHexoApi.ts b/src/adaptors/api/hexo/useHexoApi.ts index 04633007..a4e7cf47 100644 --- a/src/adaptors/api/hexo/useHexoApi.ts +++ b/src/adaptors/api/hexo/useHexoApi.ts @@ -23,3 +23,66 @@ * questions. */ +import { createAppLogger } from "~/src/utils/appLogger.ts" +import { AppInstance } from "~/src/appInstance.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/components/set/publish/platform/dynamicConfig.ts" +import { HexoConfig } from "~/src/adaptors/api/hexo/config/hexoConfig.ts" +import { HexoApiAdaptor } from "~/src/adaptors/api/hexo/adaptor/hexoApiAdaptor.ts" + +const useHexoApi = async (key: string, newCfg?: HexoConfig) => { + // 创建应用日志记录器 + const logger = createAppLogger("use-hexo-api") + + // 记录开始使用 Hexo API + logger.info("Start using Hexo API...") + + // 创建应用实例 + const appInstance = new AppInstance() + + let cfg: HexoConfig + if (newCfg) { + logger.info("Initialize with the latest newCfg passed in...") + cfg = newCfg + } else { + // 从配置中获取数据 + const { getSetting } = useSettingStore() + const setting = await getSetting() + cfg = JsonUtil.safeParse(setting[key], {} as HexoConfig) + + // 如果配置为空,则使用默认的环境变量值,并记录日志 + if (ObjectUtil.isEmptyObject(cfg)) { + // 从环境变量获取 Hexo API 的 URL、认证令牌和其他配置信息 + const githubUsername = Utils.emptyOrDefault(process.env.VITE_GITHUB_USERNAME, "") + const githubAuthToken = Utils.emptyOrDefault(process.env.VITE_GITHUB_AUTH_TOKEN, "") + const githubRepo = Utils.emptyOrDefault(process.env.VITE_GITHUB_REPO, "") + const githubBranch = Utils.emptyOrDefault(process.env.VITE_GITHUB_BRANCH, "main") + const middlewareUrl = Utils.emptyOrDefault( + process.env.VITE_MIDDLEWARE_URL, + "https://api.terwer.space/api/middleware" + ) + cfg = new HexoConfig(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) + } + } + + // 创建 Hexo API 适配器 + const blogApi = new HexoApiAdaptor(appInstance, cfg) + logger.info("Hexo API created successfully.", cfg) + + return { + cfg, + blogApi, + } +} + +export { useHexoApi } diff --git a/src/components/set/publish/singleplatform/CommonGithubSetting.vue b/src/components/set/publish/singleplatform/CommonGithubSetting.vue new file mode 100644 index 00000000..d19c88c2 --- /dev/null +++ b/src/components/set/publish/singleplatform/CommonGithubSetting.vue @@ -0,0 +1,50 @@ + + + + + + + diff --git a/src/components/set/publish/singleplatform/MetaweblogSetting.vue b/src/components/set/publish/singleplatform/MetaweblogSetting.vue index d6b9675d..61e17d78 100644 --- a/src/components/set/publish/singleplatform/MetaweblogSetting.vue +++ b/src/components/set/publish/singleplatform/MetaweblogSetting.vue @@ -89,8 +89,8 @@ const valiConf = async () => { formData.cfg.blogid = userBlog.blogid formData.cfg.blogName = userBlog.blogName + formData.cfg.apiStatus = true - formData.dynCfg.isAuth = true } else { formData.cfg.apiStatus = false } diff --git a/src/components/set/publish/singleplatform/SettingEntry.vue b/src/components/set/publish/singleplatform/SettingEntry.vue index 41349bd8..da56c48d 100644 --- a/src/components/set/publish/singleplatform/SettingEntry.vue +++ b/src/components/set/publish/singleplatform/SettingEntry.vue @@ -48,6 +48,7 @@ const subtype = getSubPlatformTypeByKey(apiType) + diff --git a/src/components/set/publish/singleplatform/github/HexoSetting.vue b/src/components/set/publish/singleplatform/github/HexoSetting.vue new file mode 100644 index 00000000..e54f53de --- /dev/null +++ b/src/components/set/publish/singleplatform/github/HexoSetting.vue @@ -0,0 +1,57 @@ + + + + + + +