Skip to content

Commit

Permalink
Merge pull request #1477 from terwer/dev
Browse files Browse the repository at this point in the history
feat: zhihu is ok now
  • Loading branch information
terwer authored Nov 26, 2024
2 parents fad28a4 + c144e9e commit 3fe983d
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 295 deletions.
119 changes: 64 additions & 55 deletions src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import { createAppLogger } from "~/src/utils/appLogger.ts"
import { BlogConfig, Post, YamlConvertAdaptor, YamlFormatObj } from "zhi-blog-api"
import { DateUtil, StrUtil, YamlUtil } from "zhi-common"
import { DateUtil, JsonUtil, ObjectUtil, StrUtil, YamlUtil } from "zhi-common"
import { CommonGithubConfig } from "~/src/adaptors/api/base/github/commonGithubConfig.ts"
import { toRaw } from "vue"

Expand All @@ -44,70 +44,79 @@ class HexoYamlConverterAdaptor extends YamlConvertAdaptor {
// 没有的情况默认初始化一个
if (!yamlFormatObj) {
yamlFormatObj = new YamlFormatObj()
}

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

// date
yamlFormatObj.yamlObj.date = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true)
// 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)
// updated
if (!post.dateUpdated) {
post.dateUpdated = new Date()
}
yamlFormatObj.yamlObj.updated = DateUtil.formatIsoToZh(post.dateUpdated.toISOString(), true)

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

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

// categories
if (post.categories?.length > 0) {
yamlFormatObj.yamlObj.categories = post.categories
}
// 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]/, "")
// 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
}
yamlFormatObj.yamlObj.permalink = link
}

// comments
yamlFormatObj.yamlObj.comments = true

// toc
yamlFormatObj.yamlObj.toc = true
// 上面是固定配置。下面是个性配置
const dynYamlCfg = JsonUtil.safeParse<any>(cfg?.dynYamlCfg ?? "{}", {})
if (ObjectUtil.isEmptyObject(dynYamlCfg)) {
// comments
yamlFormatObj.yamlObj.comments = true

// toc
yamlFormatObj.yamlObj.toc = true
} else {
Object.keys(dynYamlCfg).forEach((key) => {
yamlFormatObj.yamlObj[key] = dynYamlCfg[key]
})
}
} else {
this.logger.info("yaml 已保存,不使用预设", { post: toRaw(post) })
}

// formatter
let yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)
Expand Down
151 changes: 80 additions & 71 deletions src/adaptors/api/hugo/hugoYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import { createAppLogger } from "~/src/utils/appLogger.ts"
import { BlogConfig, Post, YamlConvertAdaptor, YamlFormatObj } from "zhi-blog-api"
import { DateUtil, StrUtil, YamlUtil } from "zhi-common"
import { DateUtil, JsonUtil, ObjectUtil, StrUtil, YamlUtil } from "zhi-common"
import { toRaw } from "vue"

/**
Expand All @@ -43,78 +43,87 @@ class HugoYamlConverterAdaptor extends YamlConvertAdaptor {
// 没有的情况默认初始化一个
if (!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
let tzstr = "+00:00"
const tz = new Date().getTimezoneOffset() / -60
const sign = tz > 0 ? "+" : "-"
if (tz.toString().length < 2) {
tzstr = `${sign}0${tz}:00`
// 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
let tzstr = "+00:00"
const tz = new Date().getTimezoneOffset() / -60
const sign = tz > 0 ? "+" : "-"
if (tz.toString().length < 2) {
tzstr = `${sign}0${tz}:00`
} else {
tzstr = `${sign}${tz}:00`
}
yamlFormatObj.yamlObj.date = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true) + tzstr

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

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

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

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

// // 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,
// },
// }
// }
// }

// 上面是固定配置。下面是个性配置
const dynYamlCfg = JsonUtil.safeParse<any>(cfg?.dynYamlCfg ?? "{}", {})
if (ObjectUtil.isEmptyObject(dynYamlCfg)) {
// toc
yamlFormatObj.yamlObj.toc = true

// isCJKLanguage
yamlFormatObj.yamlObj.isCJKLanguage = true
} else {
Object.keys(dynYamlCfg).forEach((key) => {
yamlFormatObj.yamlObj[key] = dynYamlCfg[key]
})
}
} else {
tzstr = `${sign}${tz}:00`
}
yamlFormatObj.yamlObj.date = DateUtil.formatIsoToZh(post.dateCreated.toISOString(), true) + tzstr

// lastmod
if (!post.dateUpdated) {
post.dateUpdated = new Date()
this.logger.info("yaml 已保存,不使用预设", { post: toRaw(post) })
}
yamlFormatObj.yamlObj.lastmod = DateUtil.formatIsoToZh(post.dateUpdated.toISOString(), true) + tzstr

// toc
yamlFormatObj.yamlObj.toc = true

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

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

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

// 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 Down
Loading

0 comments on commit 3fe983d

Please sign in to comment.