Skip to content

Commit

Permalink
feat: 统一整合各平台配置
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 6, 2023
1 parent f0ba519 commit 91ce36c
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* questions.
*/

import { BlogApi } from "zhi-blog-api"
import { BaseBlogApi } from "~/src/adaptors/api/base/baseBlogApi.ts"

/**
* Github API 适配器
Expand All @@ -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 {

}

Expand Down
43 changes: 42 additions & 1 deletion src/adaptors/api/base/github/config/CommonGithubConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
32 changes: 32 additions & 0 deletions src/adaptors/api/base/github/config/CommonGithubPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -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 }
37 changes: 37 additions & 0 deletions src/adaptors/api/hexo/adaptor/hexoApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -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 }
55 changes: 55 additions & 0 deletions src/adaptors/api/hexo/config/hexoConfig.ts
Original file line number Diff line number Diff line change
@@ -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 }
30 changes: 30 additions & 0 deletions src/adaptors/api/hexo/config/hexoPlaceHolder.ts
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 { CommonGithubPlaceholder } from "~/src/adaptors/api/base/github/config/CommonGithubPlaceholder.ts"

class HexoPlaceHolder extends CommonGithubPlaceholder {}

export { HexoPlaceHolder }
63 changes: 63 additions & 0 deletions src/adaptors/api/hexo/useHexoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HexoConfig>(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 }
50 changes: 50 additions & 0 deletions src/components/set/publish/singleplatform/CommonGithubSetting.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!--
- 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.
-->

<script setup lang="ts">
import { CommonGithubConfig } from "~/src/adaptors/api/base/github/config/CommonGithubConfig.ts"
// props
const props = defineProps({
apiType: {
type: String,
default: "",
},
cfg: {
type: CommonGithubConfig,
default: null,
},
})
</script>

<template>
<common-blog-setting :api-type="props.apiType" :cfg="props.cfg" />
</template>

<style scoped lang="stylus">
.top-tip
margin 10px 0
padding-left 0
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions src/components/set/publish/singleplatform/SettingEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const subtype = getSubPlatformTypeByKey(apiType)
<back-page :title="t('setting.entry.title') + apiType">
<yuque-setting v-if="subtype === SubPlatformType.Common_Yuque" :api-type="apiType" />
<notion-setting v-else-if="subtype === SubPlatformType.Common_Notion" :api-type="apiType" />
<hexo-setting v-else-if="subtype === SubPlatformType.Github_Hexo" :api-type="apiType" />
<othermeta-setting v-else-if="subtype === SubPlatformType.Metaweblog_Metaweblog" :api-type="apiType" />
<cnblogs-setting v-else-if="subtype === SubPlatformType.Metaweblog_Cnblogs" :api-type="apiType" />
<wordpress-setting v-else-if="subtype === SubPlatformType.Wordpress_Wordpress" :api-type="apiType" />
Expand Down
Loading

0 comments on commit 91ce36c

Please sign in to comment.