forked from vitejs/vite
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblog.data.ts
40 lines (36 loc) · 828 Bytes
/
blog.data.ts
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
import { createContentLoader } from 'vitepress'
interface Post {
title: string
url: string
date: {
time: number
string: string
}
}
declare const data: Post[]
export { type data }
export default createContentLoader('blog/*.md', {
// excerpt: true,
transform(raw): Post[] {
return raw
.map(({ url, frontmatter }) => ({
title: frontmatter.head.find((e) => e[1].property === 'og:title')[1]
.content,
url,
date: formatDate(frontmatter.date),
}))
.sort((a, b) => b.date.time - a.date.time)
},
})
function formatDate(raw: string): Post['date'] {
const date = new Date(raw)
date.setUTCHours(12)
return {
time: +date,
string: date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
}),
}
}