Skip to content

Commit

Permalink
Make content field optional in post
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallku committed Feb 16, 2025
1 parent 8a0e41d commit 5631c67
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/blog/src/app/[category]/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const cx = classNames(styles, "page");
export default async function Post({ params }: PostProps) {
const { category, slug } = await params;
const postSlug = `${category}/${slug.map((x) => decodeURIComponent(x)).join("/")}`;
const post = getPostBySlug(postSlug);
const post = getPostBySlug(postSlug, true);

if (!post) {
return notFound();
Expand Down
2 changes: 1 addition & 1 deletion apps/blog/src/components/PostCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Post } from "#types";
import styles from "./index.module.scss";

export interface PostCardProps {
post: Post;
post: Omit<Post, "content">;
}

const cx = classNames(styles, "post-card");
Expand Down
2 changes: 1 addition & 1 deletion apps/blog/src/components/PostList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Post } from "#types";
import styles from "./index.module.scss";

export interface PostListProps {
posts: Post[];
posts: Omit<Post, "content">[];
paginationProps?: Omit<PaginationProps, "rowSize">;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/blog/src/components/PostListGallery/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Post } from "#types";
import styles from "./index.module.scss";

export interface PostListGalleryProps {
posts: Post[];
posts: Omit<Post, "content">[];
}

const cx = classNames(styles, "post-list-gallery");
Expand Down
6 changes: 3 additions & 3 deletions apps/blog/src/components/PrevNextPost/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import styles from "./index.module.scss";
import { Icon } from "@marshallku/icon";

export interface PrevNextPostProps {
previousPost?: Post;
nextPost?: Post;
previousPost?: Omit<Post, "content">;
nextPost?: Omit<Post, "content">;
}

const cx = classNames(styles, "prev-next-post");

const Article = ({ post, isPreviousPost }: { post: Post; isPreviousPost?: boolean }) => (
const Article = ({ post, isPreviousPost }: { post: Omit<Post, "content">; isPreviousPost?: boolean }) => (
<article className={cx("-post", isPreviousPost && "-post--previous")}>
<Link href={post.slug} className={cx("-post__meta")}>
<Typography variant="c1" className={cx("-post__label")}>
Expand Down
24 changes: 17 additions & 7 deletions apps/blog/src/utils/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export function getPostSlugs(subDirectory?: string): string[] {
return files.map((file) => file.replace(POSTS_DIRECTORY, "").replace(/\.mdx$/, ""));
}

export function getPostBySlug(slug: string): Post | undefined {
export function getPostBySlug<T extends boolean>(
slug: string,
getContent?: T,
): (T extends true ? Post : Omit<Post, "content">) | undefined {
const fullPath = join(POSTS_DIRECTORY, `${slug}.mdx`);

if (!existsSync(fullPath)) {
Expand All @@ -37,7 +40,7 @@ export function getPostBySlug(slug: string): Post | undefined {
const fileContents = readFileSync(fullPath, "utf8");
const { data, content } = matter(fileContents);

return {
const post: Omit<Post, "content"> & { content?: Post["content"] } = {
data: {
title: data.title.replace(/\\/g, ""),
description: data.description,
Expand All @@ -50,21 +53,28 @@ export function getPostBySlug(slug: string): Post | undefined {
ogImage: data.ogImage,
displayAd: data.displayAd ?? false,
},
content,
slug,
category: parse(slug).dir,
};

if (getContent) {
post.content = content;

return post as Post;
}

return post as Post;
}

export function getPostsByTag(tag: string): Post[] {
export function getPostsByTag(tag: string): Omit<Post, "content">[] {
const posts = getPosts();

return posts.filter(({ data: { tags } }) => tags?.find((x) => x.toLocaleLowerCase() === tag.toLocaleLowerCase()));
}

export function getPosts(category?: string): Post[] {
export function getPosts(category?: string): Omit<Post, "content">[] {
return getPostSlugs(category)
.map((slug) => getPostBySlug(slug)!)
.map((slug) => getPostBySlug(slug, false)!)
.sort((a, b) => {
if (a.data.date.posted > b.data.date.posted) {
return -1;
Expand Down Expand Up @@ -103,7 +113,7 @@ export function getTags(): Tag[] {
).result;
}

export function getGroupedPostByCategory(): Record<string, Post[]> {
export function getGroupedPostByCategory(): Record<string, Omit<Post, "content">[]> {
return groupBy(getPosts(), ({ category }) => category.split("/")[1]);
}

Expand Down

1 comment on commit 5631c67

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Visual regression test result - success

Component Story Success Viewport MisMatch Percentage
components-button string-children phone 0.00%
components-button string-children tablet 0.10%
input-input default phone 0.00%
input-input default tablet 0.10%
input-input line phone 0.00%
input-input line tablet 0.04%
input-input box phone 0.00%
input-input box tablet 0.04%
components-profile default phone 0.00%
components-profile default tablet 0.00%
input-textarea line phone 0.00%
input-textarea line tablet 0.04%
input-textarea box phone 0.00%
input-textarea box tablet 0.04%
typography-typography default phone 0.00%
typography-typography default tablet 0.07%

Please sign in to comment.