@@ -14,23 +14,38 @@ export function getAllTags({
14
14
tagOptions,
15
15
NOTION_CONFIG
16
16
} ) {
17
+ // 保留Invisible的Page中的Tags,最后再过滤掉
17
18
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' )
19
22
)
20
23
21
24
if ( ! allPosts || ! tagOptions ) {
22
25
return [ ]
23
26
}
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
+ } )
34
49
} )
35
50
36
51
const list = [ ]
@@ -46,18 +61,18 @@ export function getAllTags({
46
61
const savedTagNames = new Set ( )
47
62
tagOptions . forEach ( c => {
48
63
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 } )
52
67
}
53
68
savedTagNames . add ( c . value )
54
69
}
55
70
} )
56
71
} else {
57
72
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 } )
61
76
}
62
77
} )
63
78
}
0 commit comments