Skip to content

Commit

Permalink
fix: 支持 Halo
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Sep 20, 2023
1 parent 1d8305a commit 125455e
Show file tree
Hide file tree
Showing 37 changed files with 354 additions and 95 deletions.
28 changes: 23 additions & 5 deletions docs/插件开发.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,38 @@
- Custom
- System

新增大类的时候需要维护
## 新增大类的时候需要维护
- DynamicJsonCfg
- getSubtypeList
- setDynamicJsonCfg
- src/utils/import/pre.ts

## API授权插件开发指南

1. 确定大类。例如:Halo属于Common
2. 新建子类别。例如:src/platforms/dynamicConfig.ts里面的SubPlatformType加一个Common_CSDN
3. 注册子类别,需要修改
- getSubtypeList
- src/utils/import/pre.ts
4. 新增适配器
API授权需要再 src/adaptors/api 新建文件。例如
- HaloApiAdaptor 需要继承 BaseBlogApi
- HaloConfig 需要继承 CommonBlogConfig
- HaloPlaceholder 需要继承 CommonBlogPlaceholder
- useHaloApi 定义 const useHaloApi = async (key: string, newCfg?: HaloConfig) => {}

5. 注册适配器
适配器 src/adaptors/index.ts getCfg
配置 src/adaptors/index.ts getAdaptor
YAML适配器(不一定有)
6. 开启配置页面
在 src/components/set/publish/singleplatform/SingleSettingIndex.vue 注册配置
7. 修改图片上传逻辑,可选

## 网页授权插件开发指南

1. 确定大类。例如:CSDN属于Custom
2. 新建子类别。例如:SubPlatformType加一个Custom_CSDN
2. 新建子类别。例如:src/platforms/dynamicConfig.ts里面的SubPlatformType加一个Custom_CSDN
3. 注册子类别,需要修改
- getSubtypeList
- src/utils/import/pre.ts
Expand All @@ -37,6 +57,4 @@
YAML适配器(不一定有)
6. 开启配置页面
在 src/components/set/publish/singleplatform/SingleSettingIndex.vue 注册配置
7. 修改图片上传逻辑,可选
src/utils/constants.ts

7. 修改图片上传逻辑,可选
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* questions.
*/

import { HexoPlaceHolder } from "~/src/adaptors/api/hexo/hexoPlaceHolder.ts"
import { HexoPlaceholder } from "~/src/adaptors/api/hexo/hexoPlaceholder.ts"

class GitlabhexoPlaceHolder extends HexoPlaceHolder {}
class GitlabhexoPlaceholder extends HexoPlaceholder {}

export { GitlabhexoPlaceHolder }
export { GitlabhexoPlaceholder }
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* questions.
*/

import { HugoPlaceHolder } from "~/src/adaptors/api/hugo/hugoPlaceHolder.ts"
import { HugoPlaceholder } from "~/src/adaptors/api/hugo/hugoPlaceholder.ts"

class GitlabhugoPlaceHolder extends HugoPlaceHolder {}
class GitlabhugoPlaceholder extends HugoPlaceholder {}

export { GitlabhugoPlaceHolder }
export { GitlabhugoPlaceholder }
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* questions.
*/

import { JekyllPlaceHolder } from "~/src/adaptors/api/jekyll/jekyllPlaceHolder.ts"
import { JekyllPlaceholder } from "~/src/adaptors/api/jekyll/jekyllPlaceholder.ts"

class GitlabjekyllPlaceHolder extends JekyllPlaceHolder {}
class GitlabjekyllPlaceholder extends JekyllPlaceholder {}

export { GitlabjekyllPlaceHolder }
export { GitlabjekyllPlaceholder }
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* questions.
*/

import { Vuepress2PlaceHolder } from "~/src/adaptors/api/vuepress2/vuepress2PlaceHolder.ts"
import { VitepressPlaceholder } from "~/src/adaptors/api/vitepress/vitepressPlaceholder.ts"

class Gitlabvuepress2PlaceHolder extends Vuepress2PlaceHolder {}
class GitlabvitepressPlaceholder extends VitepressPlaceholder {}

export { Gitlabvuepress2PlaceHolder }
export { GitlabvitepressPlaceholder }
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* questions.
*/

import { VuepressPlaceHolder } from "~/src/adaptors/api/vuepress/vuepressPlaceHolder.ts"
import { VuepressPlaceholder } from "~/src/adaptors/api/vuepress/vuepressPlaceholder.ts"

class GitlabvuepressPlaceHolder extends VuepressPlaceHolder {}
class GitlabvuepressPlaceholder extends VuepressPlaceholder {}

export { GitlabvuepressPlaceHolder }
export { GitlabvuepressPlaceholder }
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
* questions.
*/

import { VitepressPlaceHolder } from "~/src/adaptors/api/vitepress/vitepressPlaceHolder.ts"
import { Vuepress2Placeholder } from "~/src/adaptors/api/vuepress2/vuepress2Placeholder.ts"

class GitlabvitepressPlaceHolder extends VitepressPlaceHolder {}
class Gitlabvuepress2Placeholder extends Vuepress2Placeholder {}

export { GitlabvitepressPlaceHolder }
export { Gitlabvuepress2Placeholder }
41 changes: 41 additions & 0 deletions src/adaptors/api/halo/HaloApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 { HaloConfig } from "~/src/adaptors/api/halo/HaloConfig.ts"
import { BaseBlogApi } from "~/src/adaptors/api/base/baseBlogApi.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"

/**
* Halo API 适配器
* @see [Halo API](https://github.com/halo-sigs/vscode-extension-halo/blob/main/src/service/index.ts)
*/
class HaloApiAdaptor extends BaseBlogApi {
constructor(appInstance: any, cfg: HaloConfig) {
super(appInstance, cfg)
this.logger = createAppLogger("halo-api-adaptor")
}
}

export { HaloApiAdaptor }
58 changes: 58 additions & 0 deletions src/adaptors/api/halo/HaloConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 { CommonBlogConfig } from "~/src/adaptors/api/base/commonBlogConfig.ts"
import { CategoryTypeEnum, PageTypeEnum } from "zhi-blog-api"

/**
* Halo 配置
*/
class HaloConfig extends CommonBlogConfig {
/**
* Halo 配置项
*
* @param username 用户名
* @param password 密码
* @param middlewareUrl 代理地址
*/
constructor(username: string, password: string, middlewareUrl?: string) {
super("", "", username, password, middlewareUrl)

this.home = "[your-halo-home]"
this.apiUrl = "[your-halo-api-url]"
this.previewUrl = "/?p=[postid]"
this.pageType = PageTypeEnum.Html
this.usernameEnabled = true
this.showTokenTip = false
this.allowPreviewUrlChange = true
this.tagEnabled = true
this.cateEnabled = true
this.categoryType = CategoryTypeEnum.CategoryType_Multi
this.allowCateChange = true
this.knowledgeSpaceEnabled = false
}
}

export { HaloConfig }
33 changes: 33 additions & 0 deletions src/adaptors/api/halo/HaloPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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/commonBlogPlaceholder.ts"

/**
* Halo 配置提示
*/
class HaloPlaceholder extends CommonBlogPlaceholder {}

export { HaloPlaceholder }
97 changes: 97 additions & 0 deletions src/adaptors/api/halo/useHaloApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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 { HaloConfig } from "~/src/adaptors/api/halo/HaloConfig.ts"
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 { HaloApiAdaptor } from "~/src/adaptors/api/halo/HaloApiAdaptor.ts"

const useHaloApi = async (key: string, newCfg?: HaloConfig) => {
// 创建应用日志记录器
const logger = createAppLogger("use-halo-api")

// 记录开始使用 Halo API
logger.info("Start using Halo API...")

// 创建应用实例
const appInstance = new PublisherAppInstance()

let cfg: HaloConfig
if (newCfg) {
logger.info("Initialize with the latest newCfg passed in...")
cfg = newCfg
} else {
// 从配置中获取数据
const { getSetting } = useSettingStore()
const setting = await getSetting()
cfg = JsonUtil.safeParse<HaloConfig>(setting[key], {} as HaloConfig)

// 如果配置为空,则使用默认的环境变量值,并记录日志
if (ObjectUtil.isEmptyObject(cfg)) {
// 从环境变量获取 Halo API 的 URL、认证令牌和其他配置信息
const haloUsername = Utils.emptyOrDefault(process.env.VITE_YUQUE_USERNAME, "")
const haloAuthToken = Utils.emptyOrDefault(process.env.VITE_YUQUE_AUTH_TOKEN, "")
const middlewareUrl = Utils.emptyOrDefault(
process.env.VITE_MIDDLEWARE_URL,
"https://api.terwer.space/api/middleware"
)
cfg = new HaloConfig(haloUsername, haloAuthToken, 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.tagEnabled = false
// Halo 使用单选分类作为知识空间
cfg.cateEnabled = false
cfg.knowledgeSpaceEnabled = true
cfg.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Single
cfg.allowKnowledgeSpaceChange = false
cfg.placeholder.knowledgeSpaceReadonlyModeTip =
"由于语雀平台的限制,暂时不支持编辑所属知识库。如果您想移动文档,请先点击取消删除该文档,然后重新选择新的知识库发布"

// 创建 Halo API 适配器
const blogApi = new HaloApiAdaptor(appInstance, cfg)
logger.info("Halo API created successfully.", cfg)

return {
cfg,
blogApi,
}
}

export { useHaloApi }
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@

import { CommonGithubPlaceholder } from "~/src/adaptors/api/base/github/commonGithubPlaceholder.ts"

class HugoPlaceHolder extends CommonGithubPlaceholder {}
class HexoPlaceholder extends CommonGithubPlaceholder {}

export { HugoPlaceHolder }
export { HexoPlaceholder }
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@

import { CommonGithubPlaceholder } from "~/src/adaptors/api/base/github/commonGithubPlaceholder.ts"

class HexoPlaceHolder extends CommonGithubPlaceholder {}
class HugoPlaceholder extends CommonGithubPlaceholder {}

export { HexoPlaceHolder }
export { HugoPlaceholder }
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@

import { CommonGithubPlaceholder } from "~/src/adaptors/api/base/github/commonGithubPlaceholder.ts"

class JekyllPlaceHolder extends CommonGithubPlaceholder {}
class JekyllPlaceholder extends CommonGithubPlaceholder {}

export { JekyllPlaceHolder }
export { JekyllPlaceholder }
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ import { CommonBlogPlaceholder } from "~/src/adaptors/api/base/commonBlogPlaceho
/**
* Notion 配置提示
*/
class NotionPlaceHolder extends CommonBlogPlaceholder {}
class NotionPlaceholder extends CommonBlogPlaceholder {}

export { NotionPlaceHolder }
export { NotionPlaceholder }
Loading

0 comments on commit 125455e

Please sign in to comment.