Last active 1702550933

Sorts the posts by date and time!

Revision 74bc6e7bd07475a51935b84313c828f240b0a54c

posts.ts Raw
1export const getSortedPosts = () => {
2 const fileNames = fs.readdirSync(POST_DIRECTORY)
3
4 const allPostsData = fileNames.map(filename => {
5 const slug = filename.replace(FILE_EXTENSION, '')
6
7 const fullPath = path.join(POST_DIRECTORY, filename)
8 const fileContents = fs.readFileSync(fullPath, 'utf8')
9 const { data } = matter(fileContents)
10
11 const formattedDate = new Date(data.date).toLocaleDateString('en-US')
12
13 const frontmatter = {
14 ...(data as {
15 title: string
16 excerpt: string
17 avatar: string
18 author: string
19 coavatar: string
20 authorLink: string
21 coauthorLink: string
22 isCoAuthor: boolean
23 }),
24 date: formattedDate
25 }
26
27 return {
28 slug,
29 ...frontmatter
30 }
31 })
32
33 return allPostsData.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime())
34}