Skip to content

Commit

Permalink
feat: 全面支持Github和Gitlab各平台-Vitepress
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Sep 14, 2023
1 parent 2c4d0ab commit a9df74e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 56 deletions.
113 changes: 59 additions & 54 deletions src/adaptors/api/vitepress/vitepressYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@
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"

/**
* Vitepress 平台的YAML解析器
*
* @see {https://vitepress.io/docs/front-matter front-tmatter}
* @see {https://vitepress.dev/guide/frontmatter front-tmatter}
* @author terwer
* @since 0.8.1
*/
Expand All @@ -45,65 +44,63 @@ class VitepressYamlConverterAdaptor extends YamlConvertAdaptor {
// title
yamlFormatObj.yamlObj.title = post.title

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

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

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

// tags
if (!StrUtil.isEmptyString(post.mt_keywords)) {
const tags = post.mt_keywords.split(",")
yamlFormatObj.yamlObj.tags = tags
}
// date
yamlFormatObj.yamlObj.date = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true)

// head
yamlFormatObj.yamlObj.head = [
[
"meta",
{
name: "description",
content: post?.shortDesc ?? "",
},
"meta",
{
name: "keywords",
content: post?.mt_keywords?.split(",").join(" "),
},
],
]

// categories
if (post.categories?.length > 0) {
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
}
// layout
// https://vitepress.dev/reference/frontmatter-config#layout
// yamlFormatObj.yamlObj.layout = "doc"

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

// toc
yamlFormatObj.yamlObj.toc = true
// outline
yamlFormatObj.yamlObj.outline = "deep"

// navbar
// yamlFormatObj.yamlObj.navbar = true

// sidebar
yamlFormatObj.yamlObj.sidebar = false

// prev && next
yamlFormatObj.yamlObj.prev = false
yamlFormatObj.yamlObj.next = false

// lastUpdated
// yamlFormatObj.yamlObj.lastUpdated= true

// editLink
// yamlFormatObj.yamlObj.editLink = true

// formatter
let yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)
Expand All @@ -130,15 +127,23 @@ class VitepressYamlConverterAdaptor 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.shortDesc = yamlFormatObj.yamlObj?.excerpt
if (yamlFormatObj.yamlObj?.description) {
post.shortDesc = yamlFormatObj.yamlObj.description
}

// 标签
post.mt_keywords = yamlFormatObj.yamlObj?.tags?.join(",")
const head = yamlFormatObj.yamlObj?.head
if (head && head.length == 4) {
for (let i = 0; i < head.length; i++) {
const m = head[i]
if (m?.name === "keywords" && !StrUtil.isEmptyString(m.content)) {
post.mt_keywords = m.content?.split(" ")
break
}
}
}

// 分类
post.categories = yamlFormatObj.yamlObj?.categories
Expand Down
4 changes: 2 additions & 2 deletions src/adaptors/api/vuepress/vuepressYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class VuepressYamlConverterAdaptor extends YamlConvertAdaptor {
yamlFormatObj.yamlObj.meta = [
{
name: "keywords",
content: post.mt_keywords,
content: post.mt_keywords.split(",").join(" "),
},
{
name: "description",
content: post.shortDesc,
content: post.shortDesc ?? "",
},
]

Expand Down

0 comments on commit a9df74e

Please sign in to comment.