From 4ee9656c8a8e214be7974485e31ff943b2c2d828 Mon Sep 17 00:00:00 2001 From: anime Date: Fri, 11 Apr 2025 21:03:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=BF=AE=E5=A4=8D=E9=9A=90=E8=97=8F?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E4=B8=AD=E6=97=A0=E6=B3=95=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E5=8F=AA=E5=AD=98=E5=9C=A8=E4=BA=8E=E9=9A=90=E8=97=8F=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E4=B8=AD=E7=9A=84=E6=A0=87=E7=AD=BE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 具体问题为:对于新打的标签,无法在隐藏文章里显示,只能显示之前添加好的旧标签。而将文章设置为公开,就能全部显示。 Closes #3322 #3038 #3137 --- lib/db/getSiteData.js | 20 ++++++++++++++-- lib/notion/getAllTags.js | 49 ++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/lib/db/getSiteData.js b/lib/db/getSiteData.js index 1877dedb..abf595ed 100755 --- a/lib/db/getSiteData.js +++ b/lib/db/getSiteData.js @@ -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数据 */ diff --git a/lib/notion/getAllTags.js b/lib/notion/getAllTags.js index cac8dac7..d8430ff7 100644 --- a/lib/notion/getAllTags.js +++ b/lib/notion/getAllTags.js @@ -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 }) } }) }