Skip to content

Commit

Permalink
feat: 全面支持Github和Gitlab各平台-Vuepress2
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Sep 14, 2023
1 parent 485f2ff commit 92b54c6
Show file tree
Hide file tree
Showing 11 changed files with 390 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
*/

import { BlogConfig, PageTypeEnum, Post, YamlConvertAdaptor } from "zhi-blog-api"
import { GitlabvitepressYamlConverterAdaptor } from "~/src/adaptors/api/gitlab-hexo/gitlabvitepressYamlConverterAdaptor.ts"
import { GitlabvitepressYamlConverterAdaptor } from "~/src/adaptors/api/gitlab-vitepress/gitlabvitepressYamlConverterAdaptor.ts"
import { CommonGitlabApiAdaptor } from "~/src/adaptors/api/base/gitlab/commonGitlabApiAdaptor.ts"
import _ from "lodash"
import {YamlUtil} from "zhi-common";
import { YamlUtil } from "zhi-common"

/**
* Gitlabvitepress API 适配器
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ class Gitlabvuepress2ApiAdaptor extends CommonGitlabApiAdaptor {
let updatedMd = YamlUtil.extractMarkdown(md)

// ======
// 可修改 updatedMd
// 摘要
const shortDesc = updatedPost.shortDesc ?? ""
const descRegex = /(\n{2}<!-- more -->\n{2})/
if (!descRegex.test(updatedMd)) {
updatedMd = `${shortDesc}\n\n<!-- more -->\n\n${updatedMd}`
}
// ======

updatedPost.markdown = `${yfm}\n${updatedMd}`
Expand Down
7 changes: 6 additions & 1 deletion src/adaptors/api/vuepress2/vuepress2ApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ class Vuepress2ApiAdaptor extends CommonGithubApiAdaptor {
let updatedMd = YamlUtil.extractMarkdown(md)

// ======
// 可修改 updatedMd
// 摘要
const shortDesc = updatedPost.shortDesc ?? ""
const descRegex = /(\n{2}<!-- more -->\n{2})/
if (!descRegex.test(updatedMd)) {
updatedMd = `${shortDesc}\n\n<!-- more -->\n\n${updatedMd}`
}
// ======

updatedPost.markdown = `${yfm}\n${updatedMd}`
Expand Down
112 changes: 55 additions & 57 deletions src/adaptors/api/vuepress2/vuepress2YamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,77 +39,54 @@ import { toRaw } from "vue"
class Vuepress2YamlConverterAdaptor extends YamlConvertAdaptor {
private readonly logger = createAppLogger("vuepress2-yaml-converter-adaptor")

/**
* 将文章转换为YAML格式对象
*
* @param post - 要转换的文章对象
* @param cfg - 博客配置(可选)
* @returns 返回YAML格式对象
*/
public convertToYaml(post: Post, cfg?: BlogConfig): YamlFormatObj {
this.logger.debug("您正在使用 Vuepress2 Yaml Converter", { post: toRaw(post) })
let yamlFormatObj: YamlFormatObj = new YamlFormatObj()
// title
yamlFormatObj.yamlObj.title = post.title

// date
yamlFormatObj.yamlObj.date = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true)
// short_title
yamlFormatObj.yamlObj.short_title = ""

// updated
if (!post.dateUpdated) {
post.dateUpdated = new Date()
}
yamlFormatObj.yamlObj.updated = DateUtil.formatIsoToZh(post.dateUpdated.toISOString(), true)
// date
yamlFormatObj.yamlObj.date = post.dateCreated

// excerpt
// description
if (!StrUtil.isEmptyString(post.shortDesc)) {
yamlFormatObj.yamlObj.excerpt = post.shortDesc
yamlFormatObj.yamlObj.description = post.shortDesc
}

// tags
// tag
if (!StrUtil.isEmptyString(post.mt_keywords)) {
const tags = post.mt_keywords.split(",")
yamlFormatObj.yamlObj.tags = tags
const tag = post.mt_keywords.split(",")
yamlFormatObj.yamlObj.tag = tag
}

// categories
// category
if (post.categories?.length > 0) {
yamlFormatObj.yamlObj.categories = post.categories
yamlFormatObj.yamlObj.category = post.categories
}

// permalink
if (cfg.yamlLinkEnabled) {
let link = "/post/" + post.wp_slug + ".html"
if (cfg instanceof CommonGithubConfig) {
const githubCfg = cfg as CommonGithubConfig
if (!StrUtil.isEmptyString(cfg.previewPostUrl)) {
link = githubCfg.previewPostUrl.replace("[postid]", post.wp_slug)
const created = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true)
const datearr = created.split(" ")[0]
const numarr = datearr.split("-")
this.logger.debug("created numarr=>", numarr)
const y = numarr[0]
const m = numarr[1]
const d = numarr[2]
link = link.replace(/\[yyyy]/g, y)
link = link.replace(/\[MM]/g, m)
link = link.replace(/\[mm]/g, m)
link = link.replace(/\[dd]/g, d)

if (yamlFormatObj.yamlObj.categories?.length > 0) {
link = link.replace(/\[cats]/, yamlFormatObj.yamlObj.categories.join("/"))
} else {
link = link.replace(/\/\[cats]/, "")
}
}
}
yamlFormatObj.yamlObj.permalink = link
}
// article
yamlFormatObj.yamlObj.article = true

// comments
yamlFormatObj.yamlObj.comments = true
// timeline
yamlFormatObj.yamlObj.timeline = false

// toc
yamlFormatObj.yamlObj.toc = true
// isOriginal
yamlFormatObj.yamlObj.isOriginal = true

// formatter
let yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)
this.logger.debug("yaml=>", yaml)
const yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)

yamlFormatObj.formatter = yaml
yamlFormatObj.formatter = this.removeTZ(yaml)
yamlFormatObj.mdContent = post.markdown
yamlFormatObj.mdFullContent = YamlUtil.addYamlToMd(yamlFormatObj.formatter, yamlFormatObj.mdContent)
yamlFormatObj.htmlContent = post.html
Expand All @@ -118,6 +95,14 @@ class Vuepress2YamlConverterAdaptor extends YamlConvertAdaptor {
return yamlFormatObj
}

/**
* 将文章转换为属性
*
* @param post - 要转换的文章对象
* @param yamlFormatObj - YAML 格式对象
* @param cfg - 博客配置(可选)
* @returns 转换后的文章对象
*/
public convertToAttr(post: Post, yamlFormatObj: YamlFormatObj, cfg?: BlogConfig): Post {
this.logger.debug("开始转换YAML到Post", yamlFormatObj)

Expand All @@ -128,27 +113,40 @@ class Vuepress2YamlConverterAdaptor extends YamlConvertAdaptor {

// 发布时间
if (yamlFormatObj.yamlObj?.date) {
post.dateCreated = DateUtil.convertStringToDate(yamlFormatObj.yamlObj?.date)
}
if (yamlFormatObj.yamlObj?.updated) {
post.dateUpdated = DateUtil.convertStringToDate(yamlFormatObj.yamlObj?.updated)
post.dateCreated = yamlFormatObj.yamlObj?.date
}

// 摘要
post.shortDesc = yamlFormatObj.yamlObj?.excerpt
post.shortDesc = yamlFormatObj.yamlObj?.description

// 标签
post.mt_keywords = yamlFormatObj.yamlObj?.tags?.join(",")
post.mt_keywords = yamlFormatObj.yamlObj?.tag?.join(",")

// 分类
post.categories = yamlFormatObj.yamlObj?.categories
post.categories = yamlFormatObj.yamlObj?.category

// 添加新的YAML
post.yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)
const yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)
post.yaml = this.removeTZ(yaml)

this.logger.debug("转换完成,post =>", post)
return post
}

// ================
// private methods
// ================
/**
* 移除YAML字符串中的时间戳信息(TZ)
*
* @param {string} yamlString - 要处理的YAML字符串
* @returns {string} - 移除了时间戳信息的YAML字符串
*/
private removeTZ(yamlString: string): string {
return yamlString.replace(/---([\s\S]*?)---/g, function (match, captureGroup) {
return match.replace(/T/g, " ").replace(/\.\d{3}Z/g, "")
})
}
}

export { Vuepress2YamlConverterAdaptor }
64 changes: 64 additions & 0 deletions src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ import { useJekyllApi } from "~/src/adaptors/api/jekyll/useJekyllApi.ts"
import { useGitlabjekyllApi } from "~/src/adaptors/api/gitlab-jekyll/useGitlabjekyllApi.ts"
import { useVuepressApi } from "~/src/adaptors/api/vuepress/useVuepressApi.ts"
import { useGitlabvuepressApi } from "~/src/adaptors/api/gitlab-vuepress/useGitlabvuepressApi.ts"
import { useVuepress2Api } from "~/src/adaptors/api/vuepress2/useVuepress2Api.ts"
import { useVitepressApi } from "~/src/adaptors/api/vitepress/useVitepressApi.ts"
import { useGitlabvuepress2Api } from "~/src/adaptors/api/gitlab-vuepress2/useGitlabvuepress2Api.ts"
import { useGitlabvitepressApi } from "~/src/adaptors/api/gitlab-vitepress/useGitlabvitepressApi.ts"

/**
* 适配器统一入口
Expand Down Expand Up @@ -97,6 +101,16 @@ class Adaptors {
conf = cfg
break
}
case SubPlatformType.Github_Vuepress2: {
const { cfg } = await useVuepress2Api(key, newCfg)
conf = cfg
break
}
case SubPlatformType.Github_Vitepress: {
const { cfg } = await useVitepressApi(key, newCfg)
conf = cfg
break
}
case SubPlatformType.Gitlab_Hexo: {
const { cfg } = await useGitlabhexoApi(key, newCfg)
conf = cfg
Expand All @@ -117,6 +131,16 @@ class Adaptors {
conf = cfg
break
}
case SubPlatformType.Gitlab_Vuepress2: {
const { cfg } = await useGitlabvuepress2Api(key, newCfg)
conf = cfg
break
}
case SubPlatformType.Gitlab_Vitepress: {
const { cfg } = await useGitlabvitepressApi(key, newCfg)
conf = cfg
break
}
case SubPlatformType.Metaweblog_Metaweblog: {
const { cfg } = await useMetaweblogApi(key, newCfg)
conf = cfg
Expand Down Expand Up @@ -217,6 +241,16 @@ class Adaptors {
blogAdaptor = blogApi
break
}
case SubPlatformType.Github_Vuepress2: {
const { blogApi } = await useVuepress2Api(key, newCfg)
blogAdaptor = blogApi
break
}
case SubPlatformType.Github_Vitepress: {
const { blogApi } = await useVitepressApi(key, newCfg)
blogAdaptor = blogApi
break
}
case SubPlatformType.Gitlab_Hexo: {
const { blogApi } = await useGitlabhexoApi(key, newCfg)
blogAdaptor = blogApi
Expand All @@ -237,6 +271,16 @@ class Adaptors {
blogAdaptor = blogApi
break
}
case SubPlatformType.Gitlab_Vuepress2: {
const { blogApi } = await useGitlabvuepress2Api(key, newCfg)
blogAdaptor = blogApi
break
}
case SubPlatformType.Gitlab_Vitepress: {
const { blogApi } = await useGitlabvitepressApi(key, newCfg)
blogAdaptor = blogApi
break
}
case SubPlatformType.Metaweblog_Metaweblog: {
const { blogApi } = await useMetaweblogApi(key, newCfg)
blogAdaptor = blogApi
Expand Down Expand Up @@ -326,6 +370,16 @@ class Adaptors {
yamlAdp = yamlAdaptor
break
}
case SubPlatformType.Github_Vuepress2: {
const { yamlAdaptor } = await useVuepress2Api(key, newCfg)
yamlAdp = yamlAdaptor
break
}
case SubPlatformType.Github_Vitepress: {
const { yamlAdaptor } = await useVitepressApi(key, newCfg)
yamlAdp = yamlAdaptor
break
}
case SubPlatformType.Gitlab_Hexo: {
const { yamlAdaptor } = await useGitlabhexoApi(key, newCfg)
yamlAdp = yamlAdaptor
Expand All @@ -346,6 +400,16 @@ class Adaptors {
yamlAdp = yamlAdaptor
break
}
case SubPlatformType.Gitlab_Vuepress2: {
const { yamlAdaptor } = await useGitlabvuepress2Api(key, newCfg)
yamlAdp = yamlAdaptor
break
}
case SubPlatformType.Gitlab_Vitepress: {
const { yamlAdaptor } = await useGitlabvitepressApi(key, newCfg)
yamlAdp = yamlAdaptor
break
}
default: {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ const subtype = getSubPlatformTypeByKey(apiType)
<hugo-setting v-else-if="subtype === SubPlatformType.Github_Hugo" :api-type="apiType" />
<jekyll-setting v-else-if="subtype === SubPlatformType.Github_Jekyll" :api-type="apiType" />
<vuepress-setting v-else-if="subtype === SubPlatformType.Github_Vuepress" :api-type="apiType" />
<vuepress2-setting v-else-if="subtype === SubPlatformType.Github_Vuepress2" :api-type="apiType" />
<vitepress-setting v-else-if="subtype === SubPlatformType.Github_Vitepress" :api-type="apiType" />
<gitlabhexo-setting v-else-if="subtype === SubPlatformType.Gitlab_Hexo" :api-type="apiType" />
<gitlabhugo-setting v-else-if="subtype === SubPlatformType.Gitlab_Hugo" :api-type="apiType" />
<gitlabjekyll-setting v-else-if="subtype === SubPlatformType.Gitlab_Jekyll" :api-type="apiType" />
<gitlabvuepress-setting v-else-if="subtype === SubPlatformType.Gitlab_Vuepress" :api-type="apiType" />
<gitlabvuepress2-setting v-else-if="subtype === SubPlatformType.Gitlab_Vuepress2" :api-type="apiType" />
<gitlabvitepress-setting v-else-if="subtype === SubPlatformType.Gitlab_Vitepress" :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" />
<typecho-setting v-else-if="subtype === SubPlatformType.Metaweblog_Typecho" :api-type="apiType" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!--
- 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 { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { useVitepressApi } from "~/src/adaptors/api/vitepress/useVitepressApi.ts"
import { VitepressConfig } from "~/src/adaptors/api/vitepress/vitepressConfig.ts"
import { VitepressPlaceHolder } from "~/src/adaptors/api/vitepress/vitepressPlaceHolder.ts"
const props = defineProps({
apiType: {
type: String,
default: "",
},
})
const { t } = useVueI18n()
const { cfg } = await useVitepressApi(props.apiType)
const vitepressCfg = cfg as VitepressConfig
const vitepressPlaceholder = new VitepressPlaceHolder()
vitepressPlaceholder.homePlaceholder = t("setting.blog.github.url.tip")
vitepressPlaceholder.usernamePlaceholder = t("setting.blog.type.github.user.tip")
vitepressPlaceholder.passwordPlaceholder = t("setting.blog.type.github.token.tip")
vitepressPlaceholder.apiUrlPlaceholder = t("setting.blog.github.apiurl.tip")
vitepressPlaceholder.previewUrlPlaceholder = t("setting.blog.previewUrl.tip")
vitepressCfg.placeholder = vitepressPlaceholder
</script>

<template>
<common-github-setting :api-type="props.apiType" :cfg="vitepressCfg">
<template #header="header"> </template>
<template #main="main"> </template>
<template #footer="footer"> </template>
</common-github-setting>
</template>
Loading

0 comments on commit 92b54c6

Please sign in to comment.