Skip to content

Commit

Permalink
feat: 全面支持Github和Gitlab各平台-Jekyll
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Sep 8, 2023
1 parent 5042c13 commit 1bb4cbe
Show file tree
Hide file tree
Showing 20 changed files with 908 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/adaptors/api/gitlab-hugo/gitlabhugoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class GitlabhugoConfig extends HugoConfig {
this.categoryType = CategoryTypeEnum.CategoryType_Multi
this.knowledgeSpaceEnabled = true
this.allowKnowledgeSpaceChange = false
this.placeholder.knowledgeSpaceReadonlyModeTip = "Githubhexo 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.placeholder.knowledgeSpaceReadonlyModeTip = "Githubhugo 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
}
}
Expand Down
76 changes: 76 additions & 0 deletions src/adaptors/api/gitlab-jekyll/gitlabjekyllApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 { GitlabHexoYamlConverterAdaptor } from "~/src/adaptors/api/gitlab-hexo/gitlabHexoYamlConverterAdaptor.ts"
import { CommonGitlabApiAdaptor } from "~/src/adaptors/api/base/gitlab/commonGitlabApiAdaptor.ts"
import _ from "lodash"

/**
* Jekyll API 适配器
*
* @author terwer
* @version 1.14.0
* @since 1.14.0
*/
class GitlabjekyllApiAdaptor extends CommonGitlabApiAdaptor {
public override getYamlAdaptor(): YamlConvertAdaptor {
return new GitlabHexoYamlConverterAdaptor()
}

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("准备处理 Gitlabjekyll 正文")
this.logger.debug("md =>", { md: md })
let updatedMd = md

// MD暂时无法处理标记,先搁置
// 处理MD

updatedPost.markdown = updatedMd
this.logger.info("Gitlabjekyll 正文处理完毕")
this.logger.debug("updatedMd =>", { updatedMd: updatedMd })

// 发布格式
if (cfg?.pageType == PageTypeEnum.Markdown) {
post.description = post.markdown
} else {
post.description = post.html
}

return updatedPost
}
}

export { GitlabjekyllApiAdaptor }
68 changes: 68 additions & 0 deletions src/adaptors/api/gitlab-jekyll/gitlabjekyllConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 { JekyllConfig } from "~/src/adaptors/api/jekyll/jekyllConfig.ts"

/**
* Hexo 配置
*
* @author terwer
* @since 1.14.0
*/
class GitlabjekyllConfig extends JekyllConfig {
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 = "_posts"
this.previewUrl = "/[user]/[repo]/blob/[branch]/[docpath]"
// this.previewPostUrl = "/[cats]/[yyyy]/[mm]/[dd]/[postid].html"
this.previewPostUrl = "/post/[postid].html"
this.mdFilenameRule = "[yyyy]-[mm]-[dd]-[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 = "Gitlabjekyll 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
this.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single
}
}

export { GitlabjekyllConfig }
30 changes: 30 additions & 0 deletions src/adaptors/api/gitlab-jekyll/gitlabjekyllPlaceHolder.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 { JekyllPlaceHolder } from "~/src/adaptors/api/jekyll/jekyllPlaceHolder.ts"

class GitlabjekyllPlaceHolder extends JekyllPlaceHolder {}

export { GitlabjekyllPlaceHolder }
36 changes: 36 additions & 0 deletions src/adaptors/api/gitlab-jekyll/gitlabjekyllYamlConverterAdaptor.ts
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 { JekyllYamlConverterAdaptor } from "~/src/adaptors/api/jekyll/jekyllYamlConverterAdaptor.ts"

/**
* Jekyll平台的YAML解析器
*
* @author terwer
* @since 1.14.0
*/
class GitlabjekyllYamlConverterAdaptor extends JekyllYamlConverterAdaptor {}

export { GitlabjekyllYamlConverterAdaptor }
107 changes: 107 additions & 0 deletions src/adaptors/api/gitlab-jekyll/useGitlabjekyllApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* 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 { GitlabjekyllConfig } from "~/src/adaptors/api/gitlab-jekyll/gitlabjekyllConfig.ts"
import { GitlabjekyllYamlConverterAdaptor } from "~/src/adaptors/api/gitlab-jekyll/gitlabjekyllYamlConverterAdaptor.ts"
import { GitlabjekyllApiAdaptor } from "~/src/adaptors/api/gitlab-jekyll/gitlabjekyllApiAdaptor.ts"

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

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

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

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

// 如果配置为空,则使用默认的环境变量值,并记录日志
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 GitlabjekyllConfig(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.tagEnabled = true
// 分类
cfg.cateEnabled = true
cfg.allowCateChange = true
cfg.categoryType = CategoryTypeEnum.CategoryType_Multi
// 知识空间
cfg.knowledgeSpaceEnabled = true
cfg.knowledgeSpaceTitle = "发布目录"
cfg.allowKnowledgeSpaceChange = false
cfg.placeholder.knowledgeSpaceReadonlyModeTip = "Gitlabjekyll 平台暂不支持修改发布目录,如需修改,请删除之后重新发布"
cfg.knowledgeSpaceType = CategoryTypeEnum.CategoryType_Tree_Single

// 创建 Hexo 的 yamlAdaptor
const yamlAdaptor = new GitlabjekyllYamlConverterAdaptor()

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

return {
cfg,
yamlAdaptor,
blogApi,
}
}

export { useGitlabjekyllApi }
3 changes: 2 additions & 1 deletion src/adaptors/api/hugo/hugoYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ import { toRaw } from "vue"
* @since 0.8.1
*/
class HugoYamlConverterAdaptor extends YamlConvertAdaptor {
private readonly logger = createAppLogger("hexo-yaml-converter-adaptor")
private readonly logger = createAppLogger("hugo-yaml-converter-adaptor")

public convertToYaml(post: Post, cfg?: BlogConfig): YamlFormatObj {
this.logger.debug("您正在使用 Hugo Yaml Converter", { post: toRaw(post) })
let yamlFormatObj: YamlFormatObj = new YamlFormatObj()

// title
yamlFormatObj.yamlObj.title = post.title

Expand Down
Loading

0 comments on commit 1bb4cbe

Please sign in to comment.