Skip to content

Commit 7dbbcdd

Browse files
authored
Merge pull request #3325 from qixing-jk/fix/invisible-tag
fix(修复隐藏文章中无法显示只存在于隐藏文章中的标签的问题):
2 parents c5c88a0 + 4ee9656 commit 7dbbcdd

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

lib/db/getSiteData.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,6 @@ function handleDataBeforeReturn(db) {
332332
db.notice = cleanBlock(db?.notice)
333333
delete db.notice?.id
334334
}
335-
336-
db.tagOptions = cleanIds(db?.tagOptions)
337335
db.categoryOptions = cleanIds(db?.categoryOptions)
338336
db.customMenu = cleanIds(db?.customMenu)
339337

@@ -344,6 +342,8 @@ function handleDataBeforeReturn(db) {
344342
db.allNavPages = cleanPages(db?.allNavPages, db.tagOptions)
345343
db.allPages = cleanPages(db.allPages, db.tagOptions)
346344
db.latestPosts = cleanPages(db.latestPosts, db.tagOptions)
345+
// 必须在使用完毕后才能进行清理
346+
db.tagOptions = cleanTagOptions(db?.tagOptions)
347347

348348
const POST_SCHEDULE_PUBLISH = siteConfig(
349349
'POST_SCHEDULE_PUBLISH',
@@ -464,6 +464,22 @@ function cleanIds(items) {
464464
return items
465465
}
466466

467+
/**
468+
* 清理和过滤tagOptions
469+
* @param {*} tagOptions
470+
* @returns
471+
*/
472+
function cleanTagOptions(tagOptions) {
473+
if (tagOptions && Array.isArray(tagOptions)) {
474+
return deepClone(
475+
tagOptions
476+
.filter(tagOption => tagOption.source === 'Published')
477+
.map(({ id, source, ...newTagOption }) => newTagOption)
478+
)
479+
}
480+
return tagOptions
481+
}
482+
467483
/**
468484
* 清理block数据
469485
*/

lib/notion/getAllTags.js

+32-17
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,38 @@ export function getAllTags({
1414
tagOptions,
1515
NOTION_CONFIG
1616
}) {
17+
// 保留Invisible的Page中的Tags,最后再过滤掉
1718
const allPosts = allPages?.filter(
18-
page => page.type === 'Post' && page.status === 'Published'
19+
page =>
20+
page.type === 'Post' &&
21+
(page.status === 'Published' || page.status === 'Invisible')
1922
)
2023

2124
if (!allPosts || !tagOptions) {
2225
return []
2326
}
24-
// 计数
25-
let tags = allPosts?.map(p => p.tags)
26-
tags = [...tags.flat()]
27-
const tagObj = {}
28-
tags.forEach(tag => {
29-
if (tag in tagObj) {
30-
tagObj[tag]++
31-
} else {
32-
tagObj[tag] = 1
33-
}
27+
// Tag数据统计
28+
const AllTagInfos = {}
29+
// 遍历所有文章
30+
allPosts.forEach(post => {
31+
post.tags.forEach(tag => {
32+
// 如果标签已经存在
33+
if (AllTagInfos[tag]) {
34+
if (
35+
AllTagInfos[tag].source === 'Invisible' &&
36+
post.status === 'Published'
37+
) {
38+
AllTagInfos[tag].source = post.status
39+
}
40+
AllTagInfos[tag].count++
41+
} else {
42+
// 如果标签不存在,创建一个新的标签对象
43+
AllTagInfos[tag] = {
44+
count: 1,
45+
source: post.status
46+
}
47+
}
48+
})
3449
})
3550

3651
const list = []
@@ -46,18 +61,18 @@ export function getAllTags({
4661
const savedTagNames = new Set()
4762
tagOptions.forEach(c => {
4863
if (!savedTagNames.has(c.value)) {
49-
const count = tagObj[c.value]
50-
if (count) {
51-
list.push({ id: c.id, name: c.value, color: c.color, count })
64+
const tagInfo = AllTagInfos[c.value]
65+
if (tagInfo) {
66+
list.push({ id: c.id, name: c.value, color: c.color, ...tagInfo })
5267
}
5368
savedTagNames.add(c.value)
5469
}
5570
})
5671
} else {
5772
tagOptions.forEach(c => {
58-
const count = tagObj[c.value]
59-
if (count) {
60-
list.push({ id: c.id, name: c.value, color: c.color, count })
73+
const tagInfo = AllTagInfos[c.value]
74+
if (tagInfo) {
75+
list.push({ id: c.id, name: c.value, color: c.color, ...tagInfo })
6176
}
6277
})
6378
}

0 commit comments

Comments
 (0)