diff --git a/package.json b/package.json index a672593e..f0ff94fd 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0fa13a96..642a7d81 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -51,8 +51,8 @@ dependencies: specifier: ^3.1.1 version: 3.1.1 zhi-blog-api: - specifier: ^1.20.12 - version: 1.20.12 + specifier: ^1.20.13 + version: 1.20.13 zhi-common: specifier: ^1.14.0 version: 1.14.0 @@ -72,8 +72,8 @@ dependencies: specifier: ^0.1.2 version: 0.1.2 zhi-siyuan-api: - specifier: ^2.0.13 - version: 2.0.13 + specifier: ^2.0.14 + version: 2.0.14 zhi-xmlrpc-middleware: specifier: ^0.4.13 version: 0.4.13 @@ -7188,8 +7188,8 @@ packages: engines: {node: '>=12.20'} dev: true - /zhi-blog-api@1.20.12: - resolution: {integrity: sha512-NRpxpEzVf8xn083VBHk5yu7nhptyzrIRRWbcg/eAp1z06dXpvkq7bJ2iIUZwqZ6b2zmxuU7KIaqfwdAd1sXCjg==} + /zhi-blog-api@1.20.13: + resolution: {integrity: sha512-2G9jOfa7MIUgoA7vBOsU5nJgrbtjrBx9Y4siWaOp/g7tlD3xdLs79HCnUpMSTOA4YAwbHRAO/1Rszutw57tTCA==} dependencies: zhi-lib-base: 0.4.4 dev: false @@ -7239,10 +7239,10 @@ packages: - supports-color dev: false - /zhi-siyuan-api@2.0.13: - resolution: {integrity: sha512-d8KXiLqUpF9OrCE/WYBmthC9Y8f67FT8HMw2ltx0WMhURQUP80l4Y+jc3mtAA7PpZ1+a5fL/L6jSLSL09jaGkw==} + /zhi-siyuan-api@2.0.14: + resolution: {integrity: sha512-m1CtKfv26idaginV83doLrlv7xLVVeifSJZwPI6LJcBpg66ZPeTJceUeKBJIFQNfkokXUj2aVVisPIy/ruod0g==} dependencies: - zhi-blog-api: 1.20.12 + zhi-blog-api: 1.20.13 zhi-common: 1.14.0 zhi-lib-base: 0.4.4 dev: false diff --git a/src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts b/src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts index a21c0f4f..25f208b9 100644 --- a/src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts +++ b/src/adaptors/api/hexo/hexoYamlConverterAdaptor.ts @@ -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解析器 @@ -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 @@ -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) } } diff --git a/src/adaptors/index.ts b/src/adaptors/index.ts index f519879f..a18eb174 100644 --- a/src/adaptors/index.ts +++ b/src/adaptors/index.ts @@ -141,16 +141,21 @@ class Adaptors { * @param newCfg */ public static async getYamlAdaptor(key: string, newCfg?: any): Promise { - 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 } } diff --git a/src/composables/usePublish.ts b/src/composables/usePublish.ts index af9b107a..1244d70b 100644 --- a/src/composables/usePublish.ts +++ b/src/composables/usePublish.ts @@ -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" @@ -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" /** * 通用发布组件 @@ -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({ @@ -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) @@ -267,6 +277,8 @@ const usePublish = () => { // 发布格式 if (cfg?.pageType == PageTypeEnum.Markdown) { post.description = post.markdown + } else { + post.description = post.html } return post } diff --git a/src/composables/usePublishConfig.ts b/src/composables/usePublishConfig.ts index 31da0496..39973b89 100644 --- a/src/composables/usePublishConfig.ts +++ b/src/composables/usePublishConfig.ts @@ -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, } } diff --git a/src/models/postForm.ts b/src/models/postForm.ts deleted file mode 100644 index 36ab8968..00000000 --- a/src/models/postForm.ts +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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. - */ - -/** - * 通用发布表单数据定义 - * @author terwer - * @since 0.1.0 - */ -export class PostForm { - formData = { - title: "", - customSlug: "", - desc: "", - created: "", - tag: { - inputValue: "", - dynamicTags: [], - inputVisible: false, - }, - customPath: "", - categories: ["默认分类"], - mdContent: "", - htmlContent: "", - usePermalink: true, - useDate: true, - linkTitle: "", - weight: 0, - } -} diff --git a/src/platforms/yamlConvertAdaptor.ts b/src/platforms/yamlConvertAdaptor.ts index 7bccd7d9..3531c3d3 100644 --- a/src/platforms/yamlConvertAdaptor.ts +++ b/src/platforms/yamlConvertAdaptor.ts @@ -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: 该功能未实现,请在子类重写该方法") } }