Skip to content

Commit

Permalink
feat: Hexo 支持 YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 7, 2023
1 parent 8a8bf8d commit a230ff0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 110 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@
"vue-i18n": "^9.2.2",
"vue-router": "^4.2.4",
"xmlbuilder2": "^3.1.1",
"zhi-blog-api": "^1.20.12",
"zhi-blog-api": "^1.20.13",
"zhi-common": "^1.14.0",
"zhi-device": "^2.3.0",
"zhi-fetch-middleware": "^0.2.18",
"zhi-github-middleware": "^0.2.0",
"zhi-lib-base": "^0.4.4",
"zhi-notion-markdown": "^0.1.2",
"zhi-siyuan-api": "^2.0.13",
"zhi-siyuan-api": "^2.0.14",
"zhi-xmlrpc-middleware": "^0.4.13"
}
}
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 40 additions & 38 deletions src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
*/

import { YamlConvertAdaptor } from "~/src/platforms/yamlConvertAdaptor.ts"
import { CommonGithubConfig } from "~/src/adaptors/api/base/github/CommonGithubConfig.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { YamlFormatObj } from "~/src/models/yamlFormatObj.ts"
import { Post } from "zhi-blog-api"
import { CommonblogConfig } from "~/src/adaptors/api/base/CommonblogConfig.ts"
import { DateUtil, StrUtil, YamlUtil } from "zhi-common"
import { PostForm } from "~/src/models/postForm.ts"
import { CommonGithubConfig } from "~/src/adaptors/api/base/github/CommonGithubConfig.ts"

/**
* Hexo平台的YAML解析器
Expand All @@ -39,49 +40,51 @@ import { PostForm } from "~/src/models/postForm.ts"
export class HexoYamlConverterAdaptor extends YamlConvertAdaptor {
private readonly logger = createAppLogger("hexo-yaml-converter-adaptor")

convertToYaml(postForm: PostForm, githubCfg?: CommonGithubConfig): YamlFormatObj {
convertToYaml(post: Post, cfg?: CommonblogConfig): YamlFormatObj {
let yamlFormatObj: YamlFormatObj = new YamlFormatObj()
this.logger.debug("您正在使用 Hexo Yaml Converter", postForm)
this.logger.debug("您正在使用 Hexo Yaml Converter", post)

// title
yamlFormatObj.yamlObj.title = postForm.formData.title
yamlFormatObj.yamlObj.title = post.title

// date
// yamlFormatObj.yamlObj.date = postForm.formData.created
// yamlFormatObj.yamlObj.date = post.dateCreated

// updated
yamlFormatObj.yamlObj.updated = DateUtil.formatIsoToZhDate(new Date().toISOString())

// excerpt
yamlFormatObj.yamlObj.excerpt = postForm.formData.desc

// tags
yamlFormatObj.yamlObj.tags = postForm.formData.tag.dynamicTags
yamlFormatObj.yamlObj.excerpt = post.shortDesc

// categories
yamlFormatObj.yamlObj.categories = postForm.formData.categories
// // tags
// yamlFormatObj.yamlObj.tags = postForm.formData.tag.dynamicTags
//
// // categories
// yamlFormatObj.yamlObj.categories = postForm.formData.categories

// permalink
let link = "/post/" + postForm.formData.customSlug + ".html"
if (githubCfg && !StrUtil.isEmptyString(githubCfg.previewUrl)) {
link = githubCfg.previewUrl.replace("[postid]", postForm.formData.customSlug)

const created = postForm.formData.created
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]/, "")
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 = post.dateCreated
// 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
Expand All @@ -96,17 +99,16 @@ export class HexoYamlConverterAdaptor extends YamlConvertAdaptor {
let yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)
// 修复yaml的ISO日期格式(js-yaml转换的才需要)
yaml = DateUtil.formatIsoToZhDate(yaml)
// this.logger.debug("yaml=>", yaml)
this.logger.debug("yaml=>", yaml)

yamlFormatObj.formatter = yaml
yamlFormatObj.mdContent = postForm.formData.mdContent
yamlFormatObj.mdContent = post.markdown
yamlFormatObj.mdFullContent = yamlFormatObj.formatter + "\n\n" + yamlFormatObj.mdContent
yamlFormatObj.htmlContent = postForm.formData.htmlContent

yamlFormatObj.htmlContent = post.html
return yamlFormatObj
}

convertToAttr(yamlFormatObj: YamlFormatObj, githubCfg?: CommonGithubConfig): PostForm {
return super.convertToAttr(yamlFormatObj, githubCfg)
convertToAttr(yamlFormatObj: YamlFormatObj, cfg?: CommonblogConfig): Post {
return super.convertToAttr(yamlFormatObj, cfg)
}
}
11 changes: 8 additions & 3 deletions src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,21 @@ class Adaptors {
* @param newCfg
*/
public static async getYamlAdaptor(key: string, newCfg?: any): Promise<YamlConvertAdaptor> {
let yamlAdaptor = null
let yamlAdp = null
const type: SubPlatformType = getSubPlatformTypeByKey(key)

switch (type) {
case SubPlatformType.Github_Hexo: {
const { yamlAdaptor } = await useHexoApi(key, newCfg)
yamlAdp = yamlAdaptor
break
}
default: {
break
}
}
this.logger.debug(`get yamlAdaptor from key ${key}=>`, yamlAdaptor)
return yamlAdaptor
this.logger.debug(`get yamlAdaptor from key ${key}=>`, yamlAdp)
return yamlAdp
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/composables/usePublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

import { createAppLogger } from "~/src/utils/appLogger.ts"
import { isReactive, isRef, reactive, toRaw } from "vue"
import { reactive, toRaw } from "vue"
import { SypConfig } from "~/syp.config.ts"
import { AliasTranslator, ObjectUtil, StrUtil } from "zhi-common"
import { BlogAdaptor, BlogConfig, PageTypeEnum, Post, PostStatusEnum } from "zhi-blog-api"
Expand All @@ -37,6 +37,8 @@ import { DynamicConfig } from "~/src/platforms/dynamicConfig.ts"
import { CommonblogConfig } from "~/src/adaptors/api/base/CommonblogConfig.ts"
import { IPublishCfg } from "~/src/types/IPublishCfg.ts"
import { usePublishConfig } from "~/src/composables/usePublishConfig.ts"
import { YamlConvertAdaptor } from "~/src/platforms/yamlConvertAdaptor.ts"
import { YamlFormatObj } from "~/src/models/yamlFormatObj.ts"

/**
* 通用发布组件
Expand All @@ -52,7 +54,7 @@ const usePublish = () => {
const { t } = useVueI18n()
const { getSetting, updateSetting } = useSettingStore()
const { kernelApi, blogApi } = useSiyuanApi()
const { getPublishApi } = usePublishConfig()
const { getPublishApi, getYamlApi } = usePublishConfig()

// datas
const singleFormData = reactive({
Expand Down Expand Up @@ -102,6 +104,14 @@ const usePublish = () => {
logger.debug(`after preHandlePost, doc=>`, toRaw(post))

// 平台相关的预处理
const yamlApi: YamlConvertAdaptor = await getYamlApi(key, cfg)
if (yamlApi instanceof YamlConvertAdaptor) {
const yamlObj: YamlFormatObj = yamlApi.convertToYaml(post, cfg)
post.description = yamlObj.mdFullContent
logger.info("handled yaml using YamlConvertAdaptor")
} else {
logger.info("yaml adaptor not found, ignore convert")
}

// 初始化API
const api = await getPublishApi(key, cfg)
Expand Down Expand Up @@ -267,6 +277,8 @@ const usePublish = () => {
// 发布格式
if (cfg?.pageType == PageTypeEnum.Markdown) {
post.description = post.markdown
} else {
post.description = post.html
}
return post
}
Expand Down
13 changes: 13 additions & 0 deletions src/composables/usePublishConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,22 @@ const usePublishConfig = () => {
return api
}

/**
* 获取YAML API
*
* @param key - 平台配置的键值
* @param newCfg - 可选参数,用于指定新的配置
* @returns 返回一个Promise,包含YAML适配器
*/
const getYamlApi = async (key: string, newCfg?: any) => {
const yamlAdaptor = await Adaptors.getYamlAdaptor(key, newCfg)
return yamlAdaptor
}

return {
getPublishCfg,
getPublishApi,
getYamlApi,
}
}

Expand Down
51 changes: 0 additions & 51 deletions src/models/postForm.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/platforms/yamlConvertAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,26 @@
* questions.
*/

import { PostForm } from "~/src/models/postForm.ts"
import { YamlFormatObj } from "~/src/models/yamlFormatObj.ts"
import { CommonGithubConfig } from "~/src/adaptors/api/base/github/CommonGithubConfig.ts"
import { Post } from "zhi-blog-api"
import { CommonblogConfig } from "~/src/adaptors/api/base/CommonblogConfig.ts"

export interface IYamlConvertAdaptor {
convertToYaml(postForm: PostForm, githubCfg?: CommonGithubConfig): YamlFormatObj
convertToYaml(post: Post, githubCfg?: CommonGithubConfig): YamlFormatObj

convertToAttr(yamlObj: YamlFormatObj, githubCfg?: CommonGithubConfig): PostForm
convertToAttr(yamlObj: YamlFormatObj, githubCfg?: CommonGithubConfig): Post
}

/**
* YAML转换适配器
*/
export class YamlConvertAdaptor implements IYamlConvertAdaptor {
convertToYaml(postForm: PostForm, githubCfg?: CommonGithubConfig): YamlFormatObj {
convertToYaml(post: Post, cfg?: CommonblogConfig): YamlFormatObj {
throw new Error("YamlConvertAdaptor.convertToYaml: 该功能未实现,请在子类重写该方法")
}

convertToAttr(yamlFormatObj: YamlFormatObj, githubCfg?: CommonGithubConfig): PostForm {
convertToAttr(yamlFormatObj: YamlFormatObj, cfg?: CommonblogConfig): Post {
throw new Error("YamlConvertAdaptor.convertToAttr: 该功能未实现,请在子类重写该方法")
}
}

0 comments on commit a230ff0

Please sign in to comment.