Files
duyl328.github.io/docs/.vitepress/theme/posts.data.mts
T
2025-01-18 00:48:37 +08:00

58 lines
1.2 KiB
TypeScript

import { createContentLoader } from 'vitepress'
interface Post {
title: string
url: string
date: {
time: number
string: string
year: string
monthDay: string
}
tags: string[]
excerpt: string | undefined
}
export declare const data: Post[]
export default createContentLoader('posts/**/*.md', {
excerpt: excerptFn,
transform (raw): Post[] {
return raw.map(({ url, frontmatter, excerpt }) => ({
title: frontmatter.title,
url,
excerpt,
date: formatDate(frontmatter.date),
tags: frontmatter.tags,
})).sort((a, b) => b.date.time - a.date.time)
},
})
function excerptFn (file: {
data: { [key: string]: any };
content: string;
excerpt?: string
}, options?: any) {
file.excerpt = file.content.split('<!-- DESC SEP -->')[1]
}
function formatDate (raw: string): Post['date'] {
const date = new Date(raw)
date.setUTCHours(12)
return {
time: +date,
string: date.toLocaleDateString('zh-Hans', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
}),
year: date.toLocaleDateString('zh-Hans', {
year: 'numeric',
}),
monthDay: date.toLocaleDateString('zh-Hans', {
month: '2-digit',
day: '2-digit',
}),
}
}