Skip to content

Commit

Permalink
fix: #388 保存各个平台的标签和分类
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Oct 31, 2023
1 parent c1b646a commit ca9d183
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/composables/usePublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

/**
* 通用发布组件
Expand All @@ -64,6 +65,7 @@ const usePublish = () => {
const { updateSetting } = usePublishSettingStore()
const { kernelApi, blogApi } = useSiyuanApi()
const { getPublishApi } = usePublishConfig()
const { updatePlatformMetadata } = usePlatformMetadataStore()

// datas
const singleFormData = reactive({
Expand Down Expand Up @@ -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
Expand All @@ -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("文章属性处理完成")

Expand Down
65 changes: 65 additions & 0 deletions src/models/platformMetadata.ts
Original file line number Diff line number Diff line change
@@ -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<string, MetadataItem>
}

/**
* 表示元数据项
*
* @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 }
143 changes: 143 additions & 0 deletions src/stores/usePlatformMetadataStore.ts
Original file line number Diff line number Diff line change
@@ -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<PlatformMetadata> => {
const initialValue = new PlatformMetadata()
const metadata = useCommonLocalStorage<PlatformMetadata>(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 }

0 comments on commit ca9d183

Please sign in to comment.