mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 03:32:51 +02:00
refactor: update and theme configuration
This commit is contained in:
parent
7659bbd1e2
commit
63aa495d8b
19 changed files with 299 additions and 310 deletions
|
@ -9,7 +9,10 @@ export type PostsGroupByYear = Map<number, Post[]>
|
|||
export async function getPosts() {
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: Post) => import.meta.env.DEV || !data.draft,
|
||||
({ data }: Post) => {
|
||||
const shouldInclude = (import.meta.env.DEV || !data.draft) && !data.pin
|
||||
return shouldInclude
|
||||
},
|
||||
)
|
||||
|
||||
return posts.sort((a: Post, b: Post) =>
|
||||
|
@ -17,46 +20,19 @@ export async function getPosts() {
|
|||
)
|
||||
}
|
||||
|
||||
// Group posts by tags
|
||||
export async function sortPostsByTags() {
|
||||
const posts = await getPosts()
|
||||
const tagMap = new Map<string, Post[]>()
|
||||
|
||||
posts.forEach((post: Post) => {
|
||||
if (post.data.tags && post.data.tags.length > 0) {
|
||||
post.data.tags.forEach((tag: string) => {
|
||||
if (!tagMap.has(tag)) {
|
||||
tagMap.set(tag, [])
|
||||
}
|
||||
tagMap.get(tag)?.push(post)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return tagMap
|
||||
}
|
||||
|
||||
// Get posts by specific tag
|
||||
export async function getPostsByTag(tag: string) {
|
||||
const posts = await getPosts()
|
||||
|
||||
return posts.filter((post: Post) =>
|
||||
post.data.tags?.includes(tag),
|
||||
// Get pinned posts
|
||||
export async function getPinnedPosts() {
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: Post) => {
|
||||
const shouldInclude = (import.meta.env.DEV || !data.draft) && data.pin
|
||||
return shouldInclude
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// Get all tags list
|
||||
export async function getAllTags() {
|
||||
const posts = await getPosts()
|
||||
const tags = new Set<string>()
|
||||
|
||||
posts.forEach((post: Post) => {
|
||||
post.data.tags?.forEach((tag: string) =>
|
||||
tags.add(tag),
|
||||
)
|
||||
})
|
||||
|
||||
return Array.from(tags)
|
||||
return posts.sort((a: Post, b: Post) =>
|
||||
b.data.published.valueOf() - a.data.published.valueOf(),
|
||||
)
|
||||
}
|
||||
|
||||
// Group posts by year and sort
|
||||
|
@ -89,3 +65,48 @@ export async function getPostsByYear(): Promise<PostsGroupByYear> {
|
|||
|
||||
return new Map([...yearMap.entries()].sort((a, b) => b[0] - a[0]))
|
||||
}
|
||||
|
||||
// Group posts by tags
|
||||
export async function sortPostsByTags() {
|
||||
const posts = await getPosts()
|
||||
const tagMap = new Map<string, Post[]>()
|
||||
|
||||
posts.forEach((post: Post) => {
|
||||
if (post.data.tags && post.data.tags.length > 0) {
|
||||
post.data.tags.forEach((tag: string) => {
|
||||
if (!tagMap.has(tag)) {
|
||||
tagMap.set(tag, [])
|
||||
}
|
||||
tagMap.get(tag)?.push(post)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return tagMap
|
||||
}
|
||||
|
||||
// Get posts by specific tag
|
||||
export async function getPostsByTag(tag: string) {
|
||||
const posts = await getPosts()
|
||||
|
||||
return posts.filter((post: Post) =>
|
||||
post.data.tags?.includes(tag),
|
||||
)
|
||||
}
|
||||
|
||||
// Get all tags list
|
||||
export async function getAllTags() {
|
||||
const posts = await getCollection(
|
||||
'posts',
|
||||
({ data }: Post) => import.meta.env.DEV || !data.draft,
|
||||
)
|
||||
const tags = new Set<string>()
|
||||
|
||||
posts.forEach((post: Post) => {
|
||||
post.data.tags?.forEach((tag: string) =>
|
||||
tags.add(tag),
|
||||
)
|
||||
})
|
||||
|
||||
return Array.from(tags)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue