@@ -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