Skip to content

Commit 546a657

Browse files
authored
Merge pull request #447 from Shramkoweb/419-migrate-from-next-lint-to-eslint-cli-nextjs-16-deprecation
feat: introduce PostMetadata type and update post-related functions t…
2 parents ba4d535 + f7686a9 commit 546a657

4 files changed

Lines changed: 20 additions & 12 deletions

File tree

lib/posts/api.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import readingTime from 'reading-time';
33
import { readdir, readFile } from 'node:fs/promises';
44
import { join } from 'path';
55

6-
import { Post, PostCategory } from '@/lib/types';
6+
import { Post, PostCategory, PostMetadata } from '@/lib/types';
77
import { extractMarkdownSlug } from '@/lib/utils';
88

99
const 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+
7379
export async function getPostSlugs() {
7480
const fileNames = await readdir(POSTS_DIRECTORY);
7581
const markdownFiles = fileNames.filter((fileName) => fileName.endsWith('.md'));

lib/posts/utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { Post, PostCategory, Snippet } from '@/lib/types';
1+
import { Post, PostCategory, PostMetadata, Snippet } from '@/lib/types';
22

33
export 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());

lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export type Post = {
2626
content: string;
2727
};
2828

29+
export type PostMetadata = Omit<Post, 'content'>;
30+
2931
export enum PostCategory {
3032
JS = 'js',
3133
Vercel = 'vercel',

pages/blog/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import Head from 'next/head';
22
import { useState } from 'react';
33

4-
import { getPosts, getPostsCategories } from '@/lib/posts/api';
4+
import { getPostsMetadata, getPostsCategories } from '@/lib/posts/api';
55
import { BlogPostPreview } from '@/components/blog-post-preview';
66
import { filterByHeading, sortByBirthtime } from '@/lib/posts/utils';
7-
import { Post, PostCategory } from '@/lib/types';
7+
import { PostMetadata, PostCategory } from '@/lib/types';
88
import { Categories } from '@/components/categories';
99

1010
interface BlogPageProps {
11-
posts: Post[];
11+
posts: PostMetadata[];
1212
categories: PostCategory[];
1313
}
1414

@@ -116,7 +116,7 @@ function BlogPage(props: BlogPageProps) {
116116
}
117117

118118
export 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

0 commit comments

Comments
 (0)