Merge pull request #3325 from qixing-jk/fix/invisible-tag

fix(修复隐藏文章中无法显示只存在于隐藏文章中的标签的问题):
This commit is contained in:
tangly1024
2025-04-11 22:38:52 +08:00
committed by GitHub
2 changed files with 50 additions and 19 deletions

View File

@@ -332,8 +332,6 @@ function handleDataBeforeReturn(db) {
db.notice = cleanBlock(db?.notice)
delete db.notice?.id
}
db.tagOptions = cleanIds(db?.tagOptions)
db.categoryOptions = cleanIds(db?.categoryOptions)
db.customMenu = cleanIds(db?.customMenu)
@@ -344,6 +342,8 @@ function handleDataBeforeReturn(db) {
db.allNavPages = cleanPages(db?.allNavPages, db.tagOptions)
db.allPages = cleanPages(db.allPages, db.tagOptions)
db.latestPosts = cleanPages(db.latestPosts, db.tagOptions)
// 必须在使用完毕后才能进行清理
db.tagOptions = cleanTagOptions(db?.tagOptions)
const POST_SCHEDULE_PUBLISH = siteConfig(
'POST_SCHEDULE_PUBLISH',
@@ -464,6 +464,22 @@ function cleanIds(items) {
return items
}
/**
* 清理和过滤tagOptions
* @param {*} tagOptions
* @returns
*/
function cleanTagOptions(tagOptions) {
if (tagOptions && Array.isArray(tagOptions)) {
return deepClone(
tagOptions
.filter(tagOption => tagOption.source === 'Published')
.map(({ id, source, ...newTagOption }) => newTagOption)
)
}
return tagOptions
}
/**
* 清理block数据
*/

View File

@@ -14,23 +14,38 @@ export function getAllTags({
tagOptions,
NOTION_CONFIG
}) {
// 保留Invisible的Page中的Tags最后再过滤掉
const allPosts = allPages?.filter(
page => page.type === 'Post' && page.status === 'Published'
page =>
page.type === 'Post' &&
(page.status === 'Published' || page.status === 'Invisible')
)
if (!allPosts || !tagOptions) {
return []
}
// 计
let tags = allPosts?.map(p => p.tags)
tags = [...tags.flat()]
const tagObj = {}
tags.forEach(tag => {
if (tag in tagObj) {
tagObj[tag]++
} else {
tagObj[tag] = 1
}
// Tag数据统
const AllTagInfos = {}
// 遍历所有文章
allPosts.forEach(post => {
post.tags.forEach(tag => {
// 如果标签已经存在
if (AllTagInfos[tag]) {
if (
AllTagInfos[tag].source === 'Invisible' &&
post.status === 'Published'
) {
AllTagInfos[tag].source = post.status
}
AllTagInfos[tag].count++
} else {
// 如果标签不存在,创建一个新的标签对象
AllTagInfos[tag] = {
count: 1,
source: post.status
}
}
})
})
const list = []
@@ -46,18 +61,18 @@ export function getAllTags({
const savedTagNames = new Set()
tagOptions.forEach(c => {
if (!savedTagNames.has(c.value)) {
const count = tagObj[c.value]
if (count) {
list.push({ id: c.id, name: c.value, color: c.color, count })
const tagInfo = AllTagInfos[c.value]
if (tagInfo) {
list.push({ id: c.id, name: c.value, color: c.color, ...tagInfo })
}
savedTagNames.add(c.value)
}
})
} else {
tagOptions.forEach(c => {
const count = tagObj[c.value]
if (count) {
list.push({ id: c.id, name: c.value, color: c.color, count })
const tagInfo = AllTagInfos[c.value]
if (tagInfo) {
list.push({ id: c.id, name: c.value, color: c.color, ...tagInfo })
}
})
}