Skip to content

Commit

Permalink
feat: 全面支持Github和Gitlab各平台-Hugo
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Sep 8, 2023
1 parent 683e655 commit 0396d77
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/adaptors/api/gitlab-hexo/gitlabhexoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import { CategoryTypeEnum, PageTypeEnum, PasswordType } from "zhi-blog-api"
import { HexoConfig } from "~/src/adaptors/api/hexo/hexoConfig.ts"

/**
* Hexo 配置
* Hexo 配置
*
* @author terwer
* @since 1.3.2
* @since 1.14.0
*/
class GitlabhexoConfig extends HexoConfig {
constructor(
Expand Down
6 changes: 3 additions & 3 deletions src/adaptors/api/gitlab-hugo/gitlabhugoApiAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import { CommonGitlabApiAdaptor } from "~/src/adaptors/api/base/gitlab/commonGit
import _ from "lodash"

/**
* Hexo API 适配器
* Hugo API 适配器
*
* @author terwer
* @version 1.3.2
* @since 0.8.1
* @version 1.14.0
* @since 1.14.0
*/
class GitlabhugoApiAdaptor extends CommonGitlabApiAdaptor {
public override getYamlAdaptor(): YamlConvertAdaptor {
Expand Down
4 changes: 2 additions & 2 deletions src/adaptors/api/gitlab-hugo/gitlabhugoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import { CategoryTypeEnum, PageTypeEnum, PasswordType } from "zhi-blog-api"
import { HugoConfig } from "~/src/adaptors/api/hugo/hugoConfig.ts"

/**
* Hexo 配置
* Hexo 配置
*
* @author terwer
* @since 1.3.2
* @since 1.14.0
*/
class GitlabhugoConfig extends HugoConfig {
constructor(
Expand Down
1 change: 1 addition & 0 deletions src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { toRaw } from "vue"
/**
* Hexo平台的YAML解析器
*
* @see {https://hexo.io/docs/front-matter front-tmatter}
* @author terwer
* @since 0.8.1
*/
Expand Down
88 changes: 44 additions & 44 deletions src/adaptors/api/hugo/hugoYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,43 @@
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { BlogConfig, Post, YamlConvertAdaptor, YamlFormatObj } from "zhi-blog-api"
import { DateUtil, StrUtil, YamlUtil } from "zhi-common"
import { CommonGithubConfig } from "~/src/adaptors/api/base/github/commonGithubConfig.ts"
import { toRaw } from "vue"

/**
* Hexo平台的YAML解析器
* Hugo平台的YAML解析器
*
* @see {https://gohugo.io/content-management/front-matter/ front-tmatter}
* @author terwer
* @since 0.8.1
*/
class HugoYamlConverterAdaptor extends YamlConvertAdaptor {
private readonly logger = createAppLogger("hexo-yaml-converter-adaptor")

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

// slug
yamlFormatObj.yamlObj.slug = post.wp_slug

// url
if (cfg.yamlLinkEnabled) {
yamlFormatObj.yamlObj.url = "/post/" + post.wp_slug + ".html"
}

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

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

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

// tags
if (!StrUtil.isEmptyString(post.mt_keywords)) {
Expand All @@ -69,40 +75,34 @@ class HugoYamlConverterAdaptor extends YamlConvertAdaptor {
yamlFormatObj.yamlObj.categories = 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
// seo
if (!StrUtil.isEmptyString(post.mt_keywords)) {
yamlFormatObj.yamlObj.keywords = post.mt_keywords
}
if (!StrUtil.isEmptyString(post.shortDesc)) {
yamlFormatObj.yamlObj.description = post.shortDesc
}

// comments
yamlFormatObj.yamlObj.comments = true

// toc
yamlFormatObj.yamlObj.toc = true
// isCJKLanguage
yamlFormatObj.yamlObj.isCJKLanguage = true

// // linkTitle
// const linkTitle = post.linkTitle
// // weight
// const weight = parseInt(post.weight.toString())
// if (weight > 0) {
// yamlFormatObj.yamlObj.weight = weight
// }
// if (!StrUtil.isEmptyString(linkTitle)) {
// yamlFormatObj.yamlObj.linkTitle = linkTitle
// if (weight > 0) {
// yamlFormatObj.yamlObj.menu = {
// main: {
// weight: weight,
// },
// }
// }
// }

// formatter
let yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)
Expand All @@ -129,12 +129,12 @@ class HugoYamlConverterAdaptor extends YamlConvertAdaptor {
if (yamlFormatObj.yamlObj?.date) {
post.dateCreated = DateUtil.convertStringToDate(yamlFormatObj.yamlObj?.date)
}
if (yamlFormatObj.yamlObj?.updated) {
post.dateUpdated = DateUtil.convertStringToDate(yamlFormatObj.yamlObj?.updated)
if (yamlFormatObj.yamlObj?.lastmod) {
post.dateUpdated = DateUtil.convertStringToDate(yamlFormatObj.yamlObj?.lastmod)
}

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

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

0 comments on commit 0396d77

Please sign in to comment.