From ca9d183197ea7d880fbbcf765263ebf4c4728b40 Mon Sep 17 00:00:00 2001 From: terwer Date: Tue, 31 Oct 2023 16:51:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20#388=20=E4=BF=9D=E5=AD=98=E5=90=84?= =?UTF-8?q?=E4=B8=AA=E5=B9=B3=E5=8F=B0=E7=9A=84=E6=A0=87=E7=AD=BE=E5=92=8C?= =?UTF-8?q?=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/composables/usePublish.ts | 13 ++- src/models/platformMetadata.ts | 65 +++++++++++ src/stores/usePlatformMetadataStore.ts | 143 +++++++++++++++++++++++++ 3 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 src/models/platformMetadata.ts create mode 100644 src/stores/usePlatformMetadataStore.ts diff --git a/src/composables/usePublish.ts b/src/composables/usePublish.ts index ae860953..c7ac5c94 100644 --- a/src/composables/usePublish.ts +++ b/src/composables/usePublish.ts @@ -48,6 +48,7 @@ import { ElMessage } from "element-plus" import { SiyuanAttr } from "zhi-siyuan-api" import _ from "lodash" import Adaptors from "~/src/adaptors" +import { usePlatformMetadataStore } from "~/src/stores/usePlatformMetadataStore.ts" /** * 通用发布组件 @@ -64,6 +65,7 @@ const usePublish = () => { const { updateSetting } = usePublishSettingStore() const { kernelApi, blogApi } = useSiyuanApi() const { getPublishApi } = usePublishConfig() + const { updatePlatformMetadata } = usePlatformMetadataStore() // datas const singleFormData = reactive({ @@ -158,6 +160,7 @@ const usePublish = () => { // 写入属性到配置 // 这里更新 slug 的原因是历史文章有可能没有生成过别名 const postMeta = ObjectUtil.getProperty(setting, id, {}) + // eslint-disable-next-line no-prototype-builtins if (!postMeta.hasOwnProperty(SiyuanAttr.Custom_slug)) { logger.info("检测到未生成过别名,准备更新别名") postMeta[SiyuanAttr.Custom_slug] = finalPost.wp_slug @@ -172,13 +175,21 @@ const usePublish = () => { logger.info("文章更新成功") } - logger.info("发布完成,准备处理文章属性") + logger.info("发布完成,准备处理文章属性、元数据") // 保存属性用于初始化 if (isSys) { logger.info("内置平台,忽略保存属性") } else { + // 保存属性 const yamlKey = getDynYamlKey(key) await kernelApi.setSingleBlockAttr(id, yamlKey, finalPost.yaml) + + // 保存元数据 + const metadataPost = _.cloneDeep(finalPost) as Post + const tags = metadataPost.mt_keywords.split(",") + const cates = metadataPost.categories + const templates = [] + updatePlatformMetadata(key, tags, cates, templates) } logger.info("文章属性处理完成") diff --git a/src/models/platformMetadata.ts b/src/models/platformMetadata.ts new file mode 100644 index 00000000..3e6911c4 --- /dev/null +++ b/src/models/platformMetadata.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 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 1.18.0 + */ +class PlatformMetadata { + metadata: Record +} + +/** + * 表示元数据项 + * + * @author terwer + * @since 1.18.0 + */ +class MetadataItem { + /** + * 标签列表 + */ + public tags: string[] + + /** + * 分类列表 + */ + public categories: string[] + + /** + * 模板列表 + */ + public templates: string[] + + constructor() { + this.tags = [] + this.categories = [] + this.templates = [] + } +} + +export { PlatformMetadata, MetadataItem } diff --git a/src/stores/usePlatformMetadataStore.ts b/src/stores/usePlatformMetadataStore.ts new file mode 100644 index 00000000..b5c25b49 --- /dev/null +++ b/src/stores/usePlatformMetadataStore.ts @@ -0,0 +1,143 @@ +/* + * Copyright (c) 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 { createAppLogger } from "~/src/utils/appLogger.ts" +import { RemovableRef, StorageSerializers } from "@vueuse/core" +import { MetadataItem, PlatformMetadata } from "~/src/models/platformMetadata.ts" +import useCommonLocalStorage from "~/src/stores/common/useCommonLocalStorage.ts" +import { readonly } from "vue" + +/** + * 使用平台元数据存储的自定义钩子 + * + * @returns 包含获取平台元数据和只读平台元数据的函数 + */ +const usePlatformMetadataStore = () => { + // 存储键 + const filePath = "storage/syp/platform-metadata.json" + const storageKey = "platform-metadata" + const logger = createAppLogger("use-platform-metadata") + + /** + * 获取平台元数据 + * + * @returns 包含平台元数据的可移除引用 + */ + const getAllPlatformMetadata = (): RemovableRef => { + const initialValue = new PlatformMetadata() + const metadata = useCommonLocalStorage(filePath, storageKey, initialValue, { + serializer: StorageSerializers.object, + }) + return metadata + } + + /** + * 获取只读平台元数据 + * + * @returns 只读平台元数据的引用 + */ + const getReadOnlyAllPlatformMetadata = () => { + const metadataRef = getAllPlatformMetadata() + const readOnlyMetadataRef = readonly(metadataRef) + return readOnlyMetadataRef + } + + const getPlatformMetadata = (platformKey: string) => { + const tags = [] + const categories = [] + const templates = [] + + const metadataRef = getReadOnlyAllPlatformMetadata() + const metadata = metadataRef.value + if (metadata.metadata) { + // eslint-disable-next-line no-prototype-builtins + if (metadata.metadata.hasOwnProperty(platformKey)) { + const datas = metadata.metadata[platformKey] + tags.push(...datas.tags) + categories.push(...datas.categories) + templates.push(...datas.templates) + } + } + + return { + tags: tags, + categories: categories, + templates: templates, + } + } + + /** + * 更新平台元数据 + * + * @param platformKey 平台 key + * @param tags 新的标签数组 + * @param categories 新的分类数组 + * @param templates 新的模板数组 + */ + const updatePlatformMetadata = (platformKey: string, tags: string[], categories: string[], templates: string[]) => { + if (tags.length === 0 && categories.length === 0 && templates.length === 0) { + // 如果所有数组都是空数组,不需要进行任何操作 + return + } + + const metadataRef = getAllPlatformMetadata() + const metadata = metadataRef.value + metadata.metadata = metadata.metadata ?? {} + // eslint-disable-next-line no-prototype-builtins + if (!metadata.metadata.hasOwnProperty(platformKey)) { + metadata.metadata[platformKey] = new MetadataItem() + } + + const datas = metadata.metadata[platformKey] + // 去除 datas 中的空字符串并进行 trim 操作 + datas.tags = datas.tags.filter((tag) => tag.trim() !== "") + datas.categories = datas.categories.filter((category) => category.trim() !== "") + datas.templates = datas.templates.filter((template) => template.trim() !== "") + // 合并新的标签数组并去重 + if (tags.length > 0) { + datas.tags = Array.from(new Set([...datas.tags, ...tags.filter((tag) => tag.trim() !== "")])) + } + // 合并新的分类数组并去重 + if (categories.length > 0) { + datas.categories = Array.from( + new Set([...datas.categories, ...categories.filter((category) => category.trim() !== "")]) + ) + } + // 合并新的模板数组并去重 + if (templates.length > 0) { + datas.templates = Array.from( + new Set([...datas.templates, ...templates.filter((template) => template.trim() !== "")]) + ) + } + metadata.metadata[platformKey] = datas + + // 更新元数据 + metadataRef.value = metadata + } + + return { getPlatformMetadata, updatePlatformMetadata } +} + +export { usePlatformMetadataStore }