FMHYedit/.vitepress/theme/posts.data.ts

31 lines
767 B
TypeScript
Raw Normal View History

2024-01-25 16:32:45 +00:00
import { createContentLoader, type ContentData } from 'vitepress'
import { groupBy } from '../utils'
2023-12-31 06:57:10 +00:00
interface Post {
2024-01-25 16:32:45 +00:00
title: string
url: string
date: string
2023-12-31 06:57:10 +00:00
}
2024-01-25 16:32:45 +00:00
type Dictionary = ReturnType<typeof transformRawPosts>
2023-12-31 06:57:10 +00:00
2024-01-25 16:32:45 +00:00
declare const data: Dictionary
export { data }
2023-12-31 06:57:10 +00:00
function transformRawPosts(rawPosts: ContentData[]): Record<string, Post[]> {
const posts: Post[] = rawPosts
.map(({ url, frontmatter }) => ({
title: frontmatter.title,
url,
2024-01-25 16:32:45 +00:00
date: (frontmatter.date as Date).toISOString().slice(0, 10)
2023-12-31 06:57:10 +00:00
}))
2024-01-25 16:32:45 +00:00
.sort((a, b) => b.date.localeCompare(a.date))
2023-12-31 06:57:10 +00:00
2024-01-25 16:32:45 +00:00
return groupBy(posts, (post) => post.date.slice(0, 4))
2023-12-31 06:57:10 +00:00
}
2024-01-25 16:32:45 +00:00
export default createContentLoader('posts/*.md', {
2023-12-31 06:57:10 +00:00
includeSrc: true,
2024-01-25 16:32:45 +00:00
transform: (raw) => transformRawPosts(raw)
})