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 6, 2023
1 parent dccf414 commit 97a13ee
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 46 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Support features such as fast publishing, image bed management, platform expansi

![](./help/help_2.png)

(4) If a platform configuration is incomplete, you can disable the platform configuration, then delete the platform and add it again.

If you still can't use this step after this step, [New issue](https://github.com/terwer/siyuan-plugin-publisher/issues/new) is welcome.

**This tip will be removed after the stable release.**
Expand Down
2 changes: 2 additions & 0 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

![](./help/help_2.png)

(4)如果某个平台配置不完整,可以禁用平台配置、然后删除这个平台并重新添加。

若此步骤之后还是无法使用,或者您有任何建议和需求,欢迎 [新建issue](https://github.com/terwer/siyuan-plugin-publisher/issues/new)

**本提示会在稳定版发布后移除。**
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@
"vue-router": "^4.2.4",
"xmlbuilder2": "^3.1.1",
"zhi-blog-api": "^1.20.11",
"zhi-common": "^1.11.10",
"zhi-common": "^1.12.1",
"zhi-device": "^2.3.0",
"zhi-fetch-middleware": "^0.2.10",
"zhi-github-middleware": "^0.1.7",
"zhi-fetch-middleware": "^0.2.12",
"zhi-github-middleware": "^0.1.9",
"zhi-lib-base": "^0.4.4",
"zhi-notion-markdown": "^0.1.1",
"zhi-siyuan-api": "^2.0.1",
"zhi-xmlrpc-middleware": "^0.4.5"
"zhi-siyuan-api": "^2.0.3",
"zhi-xmlrpc-middleware": "^0.4.7"
}
}
51 changes: 25 additions & 26 deletions pnpm-lock.yaml

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

112 changes: 112 additions & 0 deletions src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2022-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.
*/

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 { DateUtil, StrUtil, YamlUtil } from "zhi-common"
import { PostForm } from "~/src/models/postForm.ts"

/**
* Hexo平台的YAML解析器
*
* @author terwer
* @since 0.8.1
*/
export class HexoYamlConverterAdaptor extends YamlConvertAdaptor {
private readonly logger = createAppLogger("hexo-yaml-converter-adaptor")

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

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

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

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

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

// 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]/, "")
}
}
yamlFormatObj.yamlObj.permalink = link

// comments
yamlFormatObj.yamlObj.comments = true

// toc
yamlFormatObj.yamlObj.toc = true

// formatter
let yaml = YamlUtil.obj2Yaml(yamlFormatObj.yamlObj)
// 修复yaml的ISO日期格式(js-yaml转换的才需要)
yaml = DateUtil.formatIsoToZhDate(yaml)
// this.logger.debug("yaml=>", yaml)

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

return yamlFormatObj
}

convertToAttr(yamlFormatObj: YamlFormatObj, githubCfg?: CommonGithubConfig): PostForm {
return super.convertToAttr(yamlFormatObj, githubCfg)
}
}
5 changes: 5 additions & 0 deletions src/adaptors/api/hexo/useHexoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Utils } from "~/src/utils/utils.ts"
import { getDynPostidKey } from "~/src/platforms/dynamicConfig.ts"
import { HexoConfig } from "~/src/adaptors/api/hexo/hexoConfig.ts"
import { HexoApiAdaptor } from "~/src/adaptors/api/hexo/hexoApiAdaptor.ts"
import { HexoYamlConverterAdaptor } from "~/src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts"

const useHexoApi = async (key: string, newCfg?: HexoConfig) => {
// 创建应用日志记录器
Expand Down Expand Up @@ -75,12 +76,16 @@ const useHexoApi = async (key: string, newCfg?: HexoConfig) => {
}
}

// 创建 Hexo 的 yamlAdaptor
const yamlAdaptor = new HexoYamlConverterAdaptor()

// 创建 Hexo API 适配器
const blogApi = new HexoApiAdaptor(appInstance, cfg)
logger.info("Hexo API created successfully.", cfg)

return {
cfg,
yamlAdaptor,
blogApi,
}
}
Expand Down
50 changes: 35 additions & 15 deletions src/adaptors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@
* questions.
*/

import { BlogAdaptor, WebAdaptor } from "zhi-blog-api"
import { getSubPlatformTypeByKey, SubPlatformType } from "~/src/platforms/dynamicConfig.ts"
import { useCnblogsApi } from "~/src/adaptors/api/cnblogs/useCnblogsApi.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { useWordpressApi } from "~/src/adaptors/api/wordpress/useWordpressApi.ts"
import { useTypechoApi } from "~/src/adaptors/api/typecho/useTypechoApi.ts"
import { useYuqueApi } from "~/src/adaptors/api/yuque/useYuqueApi.ts"
import { useZhihuWeb } from "~/src/adaptors/web/zhihu/useZhihuWeb.ts"
import { useCsdnWeb } from "~/src/adaptors/web/csdn/useCsdnWeb.ts"
import { useJianshuWeb } from "~/src/adaptors/web/jianshu/useJianshuWeb.ts"
import { useJuejinWeb } from "~/src/adaptors/web/juejin/useJuejinWeb.ts"
import { useWechatWeb } from "~/src/adaptors/web/wechat/useWechatWeb.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { useMetaweblogApi } from "~/src/adaptors/api/metaweblog/useMetaweblogApi.ts"
import { useNotionApi } from "~/src/adaptors/api/notion/useNotionApi.ts"
import {BlogAdaptor, WebAdaptor} from "zhi-blog-api"
import {getSubPlatformTypeByKey, SubPlatformType} from "~/src/platforms/dynamicConfig.ts"
import {useCnblogsApi} from "~/src/adaptors/api/cnblogs/useCnblogsApi.ts"
import {createAppLogger} from "~/src/utils/appLogger.ts"
import {useWordpressApi} from "~/src/adaptors/api/wordpress/useWordpressApi.ts"
import {useTypechoApi} from "~/src/adaptors/api/typecho/useTypechoApi.ts"
import {useYuqueApi} from "~/src/adaptors/api/yuque/useYuqueApi.ts"
import {useZhihuWeb} from "~/src/adaptors/web/zhihu/useZhihuWeb.ts"
import {useCsdnWeb} from "~/src/adaptors/web/csdn/useCsdnWeb.ts"
import {useJianshuWeb} from "~/src/adaptors/web/jianshu/useJianshuWeb.ts"
import {useJuejinWeb} from "~/src/adaptors/web/juejin/useJuejinWeb.ts"
import {useWechatWeb} from "~/src/adaptors/web/wechat/useWechatWeb.ts"
import {useSiyuanApi} from "~/src/composables/useSiyuanApi.ts"
import {useMetaweblogApi} from "~/src/adaptors/api/metaweblog/useMetaweblogApi.ts"
import {useNotionApi} from "~/src/adaptors/api/notion/useNotionApi.ts"
import {YamlConvertAdaptor} from "~/src/platforms/yamlConvertAdaptor.ts"

/**
* 适配器统一入口
Expand Down Expand Up @@ -126,6 +127,25 @@ class Adaptors {
this.logger.debug(`get blogAdaptor from key ${key}=>`, blogAdaptor)
return blogAdaptor
}

/**
* 根据平台key查找YAML适配器
*
* @param key
* @param newCfg
*/
public static async getYamlAdaptor(key: string, newCfg?: any): Promise<YamlConvertAdaptor> {
let yamlAdaptor = null
const type: SubPlatformType = getSubPlatformTypeByKey(key)

switch (type) {
default: {
break
}
}
this.logger.debug(`get yamlAdaptor from key ${key}=>`, yamlAdaptor)
return yamlAdaptor
}
}

export default Adaptors

0 comments on commit 97a13ee

Please sign in to comment.