Skip to content

Commit ae0823a

Browse files
author
tkokhing
committed
make reusable, major revamp on all page.tsx
1 parent e7af42f commit ae0823a

7 files changed

Lines changed: 85 additions & 130 deletions

File tree

src/app/_components/coverpage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ export default function Index() {
1313
<p>Fell in love in programming when I first crosspath with it back in polytechnic during the 90s. Had fair amount of chances to nurture it, during Uni and some projects in the military. Now that I retired after serving for more than two decades, so here I come...</p>
1414
<h1><strong>Changes that I observed</strong></h1>
1515
<h2>Well ...</h2>
16-
<ul className="space-y-4 ml-4">
16+
<div className="space-y-4 ml-4">
1717
<p>1. Programming or coding - Nobody called it coding back then, or at least not where I am from LOL.</p>
1818
<p>2. Good program is called "strong algorithm" then.</p>
1919
<p>3. Scripts are owned - You can even sell it and definitely there isn't GitHub, GitLab, Bitbucket or ...</p>
2020
<p>4. Learning has becomes a breeze - besides the above, there are YouTube and more! You even need to pay to get a IDE!</p>
2121
<p>5. Colourful IDE - Simply love this. It was all plaintext mono-colour. Missing a bracket, GBY!</p>
2222
<p>6. Syntax - Learned python on my own and I simply wonder why do we still need to end with a ";" these days?!?</p>
23-
</ul>
23+
</div>
2424
</div>
2525
<p>Learning never stops 🏃!</p>
2626

src/app/blog/posts/[slug]/page.tsx

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
// pull from private repo: [tkokhing/blog_post]
2-
import { Metadata } from "next";
1+
// pull from private repo: [tkokhing/blog_post/_posts] MDX_FOLDER
32
import { notFound } from "next/navigation";
4-
import { getAllPosts, getPostBySlug } from "@/lib/api";
5-
import { CMS_NAME } from "@/lib/constants";
63
import Container from "@/app/_components/container";
74
import { PostHeader } from "@/app/_components/post-header";
85
import { PostBody } from "@/app/_components/post-body";
6+
import { getPostBySlug } from "@/lib/api";
7+
import { generatePageMetadata } from "@/lib/generatePageMetadata";
8+
import { generatePageStaticParams } from "@/lib/generatePageStaticParams";
99

10-
export default async function Post(props: Params) {
11-
const params = await props.params;
12-
const post = getPostBySlug(params.slug, "_posts");
13-
if (!post) {
14-
return notFound();
15-
}
10+
const MDX_FOLDER = "_posts";
11+
12+
export default async function Post({ params }: { params: { slug: string } }) {
13+
const post = getPostBySlug(params.slug, MDX_FOLDER);
14+
if (!post) return notFound();
15+
1616
return (
1717
<main>
1818
<Container>
19-
<article className="mb-32">
19+
<article className="mb-32">
2020
<PostHeader
2121
title={post.title}
2222
coverImage={post.coverImage}
@@ -32,35 +32,10 @@ export default async function Post(props: Params) {
3232
);
3333
}
3434

35-
type Params = {
36-
params: Promise<{
37-
slug: string;
38-
}>;
39-
};
40-
41-
export async function generateMetadata(props: Params): Promise<Metadata> {
42-
const params = await props.params;
43-
const post = getPostBySlug(params.slug, "_posts");
44-
45-
if (!post) {
46-
return notFound();
47-
}
48-
49-
const title = `${post.title} | Next.js Blog Example with ${CMS_NAME}`;
50-
51-
return {
52-
title,
53-
openGraph: {
54-
title,
55-
images: [post.ogImage.url],
56-
},
57-
};
35+
export async function generateMetadata({ params }: { params: { slug: string } }) {
36+
return generatePageMetadata(params.slug, MDX_FOLDER);
5837
}
5938

6039
export async function generateStaticParams() {
61-
const posts = getAllPosts("_posts");
62-
63-
return posts.map((post) => ({
64-
slug: post.slug,
65-
}));
40+
return generatePageStaticParams(MDX_FOLDER);
6641
}
Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
// pull from private repo: [tkokhing/topic_post]
2-
import { Metadata } from "next";
1+
// pull from private repo: [tkokhing/topic_post/_topics] MDX_FOLDER
32
import { notFound } from "next/navigation";
4-
import { getAllPosts, getPostBySlug } from "@/lib/api";
5-
import { CMS_NAME } from "@/lib/constants";
63
import Container from "@/app/_components/container";
74
import { PostHeader } from "@/app/_components/post-header";
85
import { PostBody } from "@/app/_components/post-body";
6+
import { getPostBySlug } from "@/lib/api";
7+
import { generatePageMetadata } from "@/lib/generatePageMetadata";
8+
import { generatePageStaticParams } from "@/lib/generatePageStaticParams";
99

10-
export default async function Post(props: Params) {
11-
const params = await props.params;
12-
const post = getPostBySlug(params.slug, "_topics");
13-
if (!post) {
14-
return notFound();
15-
}
10+
const MDX_FOLDER = "_topics";
11+
12+
export default async function Post({ params }: { params: { slug: string } }) {
13+
const post = getPostBySlug(params.slug, MDX_FOLDER);
14+
if (!post) return notFound();
15+
1616
return (
1717
<main>
1818
<Container>
19-
<article className="mb-32">
19+
<article className="mb-32">
2020
<PostHeader
2121
title={post.title}
2222
coverImage={post.coverImage}
@@ -32,35 +32,10 @@ export default async function Post(props: Params) {
3232
);
3333
}
3434

35-
type Params = {
36-
params: Promise<{
37-
slug: string;
38-
}>;
39-
};
40-
41-
export async function generateMetadata(props: Params): Promise<Metadata> {
42-
const params = await props.params;
43-
const post = getPostBySlug(params.slug, "_topics");
44-
45-
if (!post) {
46-
return notFound();
47-
}
48-
49-
const title = `${post.title} | Next.js Blog Example with ${CMS_NAME}`;
50-
51-
return {
52-
title,
53-
openGraph: {
54-
title,
55-
images: [post.ogImage.url],
56-
},
57-
};
35+
export async function generateMetadata({ params }: { params: { slug: string } }) {
36+
return generatePageMetadata(params.slug, MDX_FOLDER);
5837
}
5938

6039
export async function generateStaticParams() {
61-
const posts = getAllPosts("_topics");
62-
63-
return posts.map((post) => ({
64-
slug: post.slug,
65-
}));
40+
return generatePageStaticParams(MDX_FOLDER);
6641
}
Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
// pull from private repo: [tkokhing/linux_post]
2-
import { Metadata } from "next";
1+
// pull from private repo: [tkokhing/linux_post/_linux] MDX_FOLDER
32
import { notFound } from "next/navigation";
4-
import { getAllPosts, getPostBySlug } from "@/lib/api";
5-
import { CMS_NAME } from "@/lib/constants";
63
import Container from "@/app/_components/container";
74
import { PostHeader } from "@/app/_components/post-header";
85
import { PostBody } from "@/app/_components/post-body";
6+
import { getPostBySlug } from "@/lib/api";
7+
import { generatePageMetadata } from "@/lib/generatePageMetadata";
8+
import { generatePageStaticParams } from "@/lib/generatePageStaticParams";
99

1010
import Alert from "@/app/_components/blog_frame/alert";
1111
import Note from "@/app/_components/blog_frame/note";
1212
import Tip from "@/app/_components/blog_frame/tip";
1313

14+
const MDX_FOLDER = "_linux";
1415

15-
export default async function Post(props: Params) {
16-
const params = await props.params;
17-
const post = getPostBySlug(params.slug, "_linux");
16+
export default async function Post({ params }: { params: { slug: string } }) {
17+
const post = getPostBySlug(params.slug, MDX_FOLDER);
1818
const LinuxBlogComponents = {
1919
Tip,
2020
Note,
2121
Alert,
2222
};
23-
24-
if (!post) {
25-
return notFound();
26-
}
23+
if (!post) return notFound();
24+
2725
return (
2826
<main>
2927
<Container>
30-
<article className="mb-32">
28+
<article className="mb-32">
3129
<PostHeader
3230
title={post.title}
3331
coverImage={post.coverImage}
@@ -43,35 +41,10 @@ export default async function Post(props: Params) {
4341
);
4442
}
4543

46-
type Params = {
47-
params: Promise<{
48-
slug: string;
49-
}>;
50-
};
51-
52-
export async function generateMetadata(props: Params): Promise<Metadata> {
53-
const params = await props.params;
54-
const post = getPostBySlug(params.slug, "_linux");
55-
56-
if (!post) {
57-
return notFound();
58-
}
59-
60-
const title = `${post.title} | Next.js Blog Example with ${CMS_NAME}`;
61-
62-
return {
63-
title,
64-
openGraph: {
65-
title,
66-
images: [post.ogImage.url],
67-
},
68-
};
44+
export async function generateMetadata({ params }: { params: { slug: string } }) {
45+
return generatePageMetadata(params.slug, MDX_FOLDER);
6946
}
7047

7148
export async function generateStaticParams() {
72-
const posts = getAllPosts("_linux");
73-
74-
return posts.map((post) => ({
75-
slug: post.slug,
76-
}));
49+
return generatePageStaticParams(MDX_FOLDER);
7750
}

src/lib/api.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ function useBasePath(): string {
88
return process.env.GITHUB_ACTIONS ? REPO_NAME : '';
99
}
1010

11-
function postsDirectory(subPath: string): string{
12-
fs.readdir((join(process.cwd(), subPath)), (err) => {
11+
function postsDirectory(mdx_folder: string): string{
12+
fs.readdir((join(process.cwd(), mdx_folder)), (err) => {
1313
if (err) {
1414
console.error('Error reading directory:', err);
1515
}
1616
})
17-
return join(process.cwd(), subPath);
17+
return join(process.cwd(), mdx_folder);
1818
}
1919

20-
function getPostSlugs(subPath: string) {
21-
return fs.readdirSync(postsDirectory(subPath));
20+
function getPostSlugs(mdx_folder: string) {
21+
return fs.readdirSync(postsDirectory(mdx_folder));
2222
}
2323

24-
export function getPostBySlug(slug: string, subPath: string){
24+
export function getPostBySlug(slug: string, mdx_folder: string){
2525
const realSlug = slug.replace(/\.mdx?$/, "");
26-
const fullPath = join(postsDirectory(subPath), `${realSlug}.mdx`);
26+
const fullPath = join(postsDirectory(mdx_folder), `${realSlug}.mdx`);
2727
const fileContents = fs.readFileSync(fullPath, "utf8");
2828
const { data, content } = matter(fileContents);
2929
const BASE_PATH = useBasePath();
@@ -35,8 +35,8 @@ export function getPostBySlug(slug: string, subPath: string){
3535
return { ...data, slug: realSlug, content } as Post;
3636
}
3737

38-
export function getAllPosts(subPath: string): Post[] {
39-
const slugs = getPostSlugs(subPath);
40-
const posts = slugs.map((slug) => getPostBySlug(slug, subPath));
38+
export function getAllPosts(mdx_folder: string): Post[] {
39+
const slugs = getPostSlugs(mdx_folder);
40+
const posts = slugs.map((slug) => getPostBySlug(slug, mdx_folder));
4141
return posts.sort((post1, post2) => (post1.date > post2.date ? -1 : 1));
4242
}

src/lib/generatePageMetadata.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Metadata } from "next";
2+
import { notFound } from "next/navigation";
3+
import { getPostBySlug } from "@/lib/api";
4+
5+
export async function generatePageMetadata(slug: string, mdx_folder: string): Promise<Metadata> {
6+
const post = getPostBySlug(slug, mdx_folder);
7+
if (!post) return notFound();
8+
9+
const title = `${post.title}`;
10+
return {
11+
title,
12+
description: post.excerpt,
13+
openGraph: {
14+
title,
15+
description: post.excerpt,
16+
images: [post.ogImage.url ?? "/img/logo/t.png"],
17+
type: "article",
18+
},
19+
twitter: {
20+
card: "summary_large_image",
21+
title,
22+
description: post.excerpt,
23+
images: [post.ogImage.url ?? "/img/logo/t.png"],
24+
},
25+
};
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getAllPosts } from "@/lib/api";
2+
3+
export function generatePageStaticParams(mdx_folder: string) {
4+
const posts = getAllPosts(mdx_folder);
5+
return posts.map((post) => ({ slug: post.slug }));
6+
}

0 commit comments

Comments
 (0)