Repository navigation
Expand file tree
/
Copy pathdata.ts
More file actions
172 lines (147 loc) · 4.21 KB
/
Copy pathdata.ts
File metadata and controls
172 lines (147 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { getCollection, render } from 'astro:content'
import { getYear } from '~/utils/datetime'
import type { CollectionEntry } from 'astro:content'
/**
* Ensures that a value is a positive integer.
*/
function ensurePositiveInteger(value: number, name: string) {
if (Number.isInteger(value) && value > 0) return value
throw new Error(
`'${name}' must be a positive integer. Please check 'src/config.ts' for the correct configuration.`
)
}
/**
* Parses a tuple of boolean and number.
*/
export function parseTuple(
tuple: boolean | [boolean, number] | undefined,
name: string
) {
if (!tuple || !Array.isArray(tuple) || !tuple[0]) return undefined
return ensurePositiveInteger(tuple[1], name)
}
/**
* Retrieves the minutes read for a post.
*/
export function getMinutesRead(
minutesRead: number | boolean,
computedMinutesRead: number
) {
return minutesRead === false || minutesRead === 0
? 0
: typeof minutesRead === 'number' && minutesRead > 0
? minutesRead
: computedMinutesRead
}
/**
* Retrieves filtered posts from the specified content collection.
* In production, it filters out draft posts.
*/
export async function getFilteredPosts(collection: 'blogs') {
return await getCollection(collection, ({ data }) => {
return import.meta.env.PROD ? !data.draft : true
})
}
/**
* Sorts an array of posts by their publication date in descending order.
*/
export function getSortedPosts(posts: CollectionEntry<'blogs'>[]) {
return [...posts].sort(
(a, b) =>
b.data.pubDate.valueOf() - a.data.pubDate.valueOf() ||
a.id.localeCompare(b.id)
)
}
export async function getPublishedBlogPosts() {
return await getCollection('blogs', ({ data }) => !data.draft)
}
/**
* Retrieves the five newest published posts for the home page.
*/
export async function getRecentBlogPosts() {
const posts = await getPublishedBlogPosts()
return getSortedPosts(posts).slice(0, 5)
}
export interface GroupedBlogItem {
year: string
id: CollectionEntry<'blogs'>['id']
data: CollectionEntry<'blogs'>['data']
minutesRead: number
wordCount: number
}
export interface GroupedBlogYear {
year: string
items: GroupedBlogItem[]
}
export type FriendData = CollectionEntry<'friends'>['data']
export interface GroupedFriendItem {
id: CollectionEntry<'friends'>['id']
data: FriendData
}
/**
* Retrieves date-sorted blog items with reading metadata for list views.
*/
export async function getBlogListItems(
collection: 'blogs' = 'blogs'
): Promise<GroupedBlogItem[]> {
const items = await getFilteredPosts(collection)
const sortedPosts = getSortedPosts(items)
const enrichedPosts = await Promise.all(
sortedPosts.map(async (item) => {
const { data, id } = item
const { remarkPluginFrontmatter } = await render(item)
const wordCount =
typeof remarkPluginFrontmatter.wordCount === 'number'
? remarkPluginFrontmatter.wordCount
: 0
return {
year: getYear(data.pubDate).toString(),
id,
data,
minutesRead: getMinutesRead(
data.minutesRead,
remarkPluginFrontmatter.minutesRead
),
wordCount,
}
})
)
return enrichedPosts
}
/** Keeps the existing year-based list on the same article data source. */
export async function getGroupedPostsByYear(
collection: 'blogs'
): Promise<GroupedBlogYear[]> {
const enrichedPosts = await getBlogListItems(collection)
return enrichedPosts.reduce<GroupedBlogYear[]>((groups, item) => {
const existingGroup = groups.find((group) => group.year === item.year)
if (existingGroup) {
existingGroup.items.push(item)
return groups
}
groups.push({
year: item.year,
items: [item],
})
return groups
}, [])
}
/**
* Retrieves sorted friends from the specified content collection.
*/
export async function getSortedFriends(
collection: 'friends'
): Promise<GroupedFriendItem[]> {
const items = await getCollection(collection)
return items
.map((item) => ({
id: item.id,
data: item.data,
}))
.sort((a, b) => {
if (a.data.order !== b.data.order) {
return a.data.order - b.data.order
}
return a.data.name.localeCompare(b.data.name, 'zh-Hans-CN')
})
}