Skip to content

Commit

Permalink
feat: 属性以YAML格式存储到文档自定义属性
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Aug 21, 2023
1 parent 515e3eb commit 9e8b17b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
5 changes: 2 additions & 3 deletions src/components/publish/BatchPublishIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ const handlePublish = async () => {
formData.successBatchResults = []
for (const key of formData.dynList) {
if (sysKeys.includes(key)) {
logger.info(`[${key}] 系统内置平台`)
logger.info(`开始发布 [${key}] 系统内置平台`)
} else {
logger.info(`[${key}] 自定义平台`)
// 文章发布数据不同平台共享
logger.info(`开始发布 [${key}] 自定义平台`)
// 平台相关的配置,需要各自重新获取
const publishCfg = await getPublishCfg(key)
formData.publishCfg = publishCfg
Expand Down
60 changes: 47 additions & 13 deletions src/composables/usePublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { reactive, toRaw } from "vue"
import { SypConfig } from "~/syp.config.ts"
import { AliasTranslator, ObjectUtil, StrUtil } from "zhi-common"
import { BlogAdaptor, PageTypeEnum, Post, PostStatusEnum } from "zhi-blog-api"
import { AliasTranslator, ObjectUtil, StrUtil, YamlUtil } from "zhi-common"
import { BlogAdaptor, PageTypeEnum, Post, PostStatusEnum, YamlConvertAdaptor, YamlFormatObj } from "zhi-blog-api"
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { useSettingStore } from "~/src/stores/useSettingStore.ts"
import { useSiyuanApi } from "~/src/composables/useSiyuanApi.ts"
import { pre } from "~/src/utils/import/pre.ts"
import { MethodEnum } from "~/src/models/methodEnum.ts"
import { DynamicConfig } from "~/src/platforms/dynamicConfig.ts"
import { DynamicConfig, getDynYamlKey } 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 { ElMessage } from "element-plus"
import Adaptors from "~/src/adaptors"
import { SiyuanAttr } from "~/src/constants/siyuanAttr.ts"

/**
* 通用发布组件
Expand Down Expand Up @@ -138,26 +140,61 @@ const usePublish = () => {
const posidKey = cfg.posidKey
const postMeta = ObjectUtil.getProperty(setting, id, {})
postMeta[posidKey] = postid
postMeta["custom-slug"] = post.wp_slug
postMeta[SiyuanAttr.Custom_Slug] = post.wp_slug
setting[id] = postMeta
await updateSetting(setting)

logger.info("new post=>", result)
logger.info("文章发布成功")
} else {
logger.info("文章已发布,准备更新")

// result 正常情况下是 true
const result = await api.editPost(postid, post)

// 写入属性到配置
// 这里更新 slug 的原因是历史文章有可能没有生成过别名
const postMeta = ObjectUtil.getProperty(setting, id, {})
postMeta["custom-slug"] = post.wp_slug
setting[id] = postMeta
await updateSetting(setting)
if (!postMeta.hasOwnProperty(SiyuanAttr.Custom_Slug)) {
logger.info("检测到未生成过别名,准备更新别名")
postMeta[SiyuanAttr.Custom_Slug] = post.wp_slug
setting[id] = postMeta
await updateSetting(setting)
} else {
// 确保别名不被修改
post.wp_slug = postMeta[SiyuanAttr.Custom_Slug]
}

logger.info("edit post=>", result)
logger.info("文章更新成功")
}

logger.info("发布完成,准备处理文章属性")
// 保存属性用于初始化
if (isSys) {
const yamlKey = SiyuanAttr.Custom_Yaml
const yaml = YamlUtil.obj2Yaml(post.toYamlObj())
await kernelApi.setSingleBlockAttr(id, yamlKey, yaml)
logger.info("内置平台,保存公共的YAML")
} else {
const yamlAdaptor: YamlConvertAdaptor = await Adaptors.getYamlAdaptor(key, cfg)
if (null !== yamlAdaptor) {
const yamlKey = getDynYamlKey(key)
// 先生成对应平台的yaml
const yamlObj: YamlFormatObj = yamlAdaptor.convertToYaml(post, cfg)
const yaml = yamlObj.formatter
await kernelApi.setSingleBlockAttr(id, yamlKey, yaml)
logger.info("使用自定义的YAML适配器")
} else {
const yamlKey = getDynYamlKey(key)
const yaml = YamlUtil.obj2Yaml(post.toYamlObj())
await kernelApi.setSingleBlockAttr(id, yamlKey, yaml)
logger.warn("未找到YAML适配器,使用公共的YAML模型")
}
}

logger.info("文章属性处理完成")

// 更新预览链接
postPreviewUrl = await getPostPreviewUrl(api, postid, cfg)

Expand Down Expand Up @@ -219,8 +256,8 @@ const usePublish = () => {
if (updatedPostMeta.hasOwnProperty(posidKey)) {
delete updatedPostMeta[posidKey]
}
if (updatedPostMeta.hasOwnProperty("custom-slug")) {
delete updatedPostMeta["custom-slug"]
if (updatedPostMeta.hasOwnProperty(SiyuanAttr.Custom_Slug)) {
delete updatedPostMeta[SiyuanAttr.Custom_Slug]
}

setting[id] = updatedPostMeta
Expand Down Expand Up @@ -269,9 +306,6 @@ const usePublish = () => {
delete updatedPostMeta[posidKey]
}
// 别名不能删除,因为别的平台可能还用
// if (updatedPostMeta.hasOwnProperty("custom-slug")) {
// delete updatedPostMeta["custom-slug"]
// }

setting[id] = updatedPostMeta
await updateSetting(setting)
Expand Down Expand Up @@ -312,7 +346,7 @@ const usePublish = () => {
const postMeta = ObjectUtil.getProperty(setting, id, {})

// 别名
const slug = ObjectUtil.getProperty(postMeta, "custom-slug", post.wp_slug)
const slug = ObjectUtil.getProperty(postMeta, SiyuanAttr.Custom_Slug, post.wp_slug)
if (!StrUtil.isEmptyString(slug)) {
post.wp_slug = slug
logger.info("Using existing siyuan note slug")
Expand Down
4 changes: 2 additions & 2 deletions src/constants/siyuanAttr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ enum SiyuanAttr {
Custom_Slug = "custom-slug",

/**
* 自定义categories属性
* YAML属性
*/
Custom_Categories = "custom-categories",
Custom_Yaml = "custom-yaml",
}

export { SiyuanAttr }
2 changes: 1 addition & 1 deletion src/platforms/dynamicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,5 +421,5 @@ export function getDynPostidKey(platformKey: string): string {
* @param platformKey
*/
export function getDynYamlKey(platformKey: string): string {
return "custom-" + platformKey + "-yaml"
return "custom-" + platformKey.replace(/_/g, "-") + "-yaml"
}

0 comments on commit 9e8b17b

Please sign in to comment.