File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import readingTime from 'reading-time';
33import { readdir , readFile } from 'node:fs/promises' ;
44import { join } from 'path' ;
55
6- import { Post , PostCategory } from '@/lib/types' ;
6+ import { Post , PostCategory , PostMetadata } from '@/lib/types' ;
77import { extractMarkdownSlug } from '@/lib/utils' ;
88
99const POSTS_DIRECTORY = join ( process . cwd ( ) , '_posts' ) ;
@@ -70,6 +70,12 @@ export async function getPosts(): Promise<Post[]> {
7070 return Promise . all ( postPromises ) ;
7171}
7272
73+ export async function getPostsMetadata ( ) : Promise < PostMetadata [ ] > {
74+ const posts = await getPosts ( ) ;
75+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
76+ return posts . map ( ( { content, ...post } ) => post ) ;
77+ }
78+
7379export async function getPostSlugs ( ) {
7480 const fileNames = await readdir ( POSTS_DIRECTORY ) ;
7581 const markdownFiles = fileNames . filter ( ( fileName ) => fileName . endsWith ( '.md' ) ) ;
Original file line number Diff line number Diff line change 1- import { Post , PostCategory , Snippet } from '@/lib/types' ;
1+ import { Post , PostCategory , PostMetadata , Snippet } from '@/lib/types' ;
22
33export const sortByBirthtime = (
4- first : Post | Snippet ,
5- second : Post | Snippet ,
4+ first : Post | PostMetadata | Snippet ,
5+ second : Post | PostMetadata | Snippet ,
66) => second . data . createDate - first . data . createDate ;
77
8- export const filterByFeatured = ( post : Post ) => post . data . featured ;
9- export const filterByNotFeatured = ( post : Post ) => ! post . data . featured
8+ export const filterByFeatured = ( post : Post | PostMetadata ) => post . data . featured ;
9+ export const filterByNotFeatured = ( post : Post | PostMetadata ) => ! post . data . featured
1010 && ! post . data . categories
1111 . map ( ( category ) => category . toLowerCase ( ) )
1212 . includes ( PostCategory . AdvancedReact . toLowerCase ( ) as PostCategory ) ;
1313
14- export const filterByAdvanceReact = ( post : Post ) => post . data . categories
14+ export const filterByAdvanceReact = ( post : Post | PostMetadata ) => post . data . categories
1515 . map ( ( category ) => category . toLowerCase ( ) )
1616 . includes ( PostCategory . AdvancedReact . toLowerCase ( ) as PostCategory ) ;
1717
1818
19- export const filterByHeading = ( post : Post , heading : string ) => post . data . heading . toLowerCase ( ) . includes ( heading . toLowerCase ( ) ) ;
19+ export const filterByHeading = ( post : Post | PostMetadata , heading : string ) => post . data . heading . toLowerCase ( ) . includes ( heading . toLowerCase ( ) ) ;
Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ export type Post = {
2626 content : string ;
2727} ;
2828
29+ export type PostMetadata = Omit < Post , 'content' > ;
30+
2931export enum PostCategory {
3032 JS = 'js' ,
3133 Vercel = 'vercel' ,
Original file line number Diff line number Diff line change 11import Head from 'next/head' ;
22import { useState } from 'react' ;
33
4- import { getPosts , getPostsCategories } from '@/lib/posts/api' ;
4+ import { getPostsMetadata , getPostsCategories } from '@/lib/posts/api' ;
55import { BlogPostPreview } from '@/components/blog-post-preview' ;
66import { filterByHeading , sortByBirthtime } from '@/lib/posts/utils' ;
7- import { Post , PostCategory } from '@/lib/types' ;
7+ import { PostMetadata , PostCategory } from '@/lib/types' ;
88import { Categories } from '@/components/categories' ;
99
1010interface BlogPageProps {
11- posts : Post [ ] ;
11+ posts : PostMetadata [ ] ;
1212 categories : PostCategory [ ] ;
1313}
1414
@@ -116,7 +116,7 @@ function BlogPage(props: BlogPageProps) {
116116}
117117
118118export async function getStaticProps ( ) {
119- const posts = await getPosts ( ) ;
119+ const posts = await getPostsMetadata ( ) ;
120120 const categories = await getPostsCategories ( ) ;
121121 const sortedPosts = posts . sort ( sortByBirthtime ) ;
122122
You can’t perform that action at this time.
0 commit comments