Skip to content

Commit 7356d15

Browse files
committed
修复博客文章查找逻辑,更新 slug 访问方式;调整文档配置结构
1 parent cee0616 commit 7356d15

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

apps/site/app/blog/[slug]/page.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export default async function BlogPost({
1010
params: Promise<{ slug: string }>;
1111
}) {
1212
const { slug } = await params;
13-
const post = blog.find((post: any) => post.slug === slug) as any;
13+
const post = blog.find((post: any) => post.info.path.replace(/\.mdx?$/, '') === slug) as any;
1414

1515
if (!post) notFound();
1616

17-
const MDX = post.data.body;
17+
const MDX = post.body;
1818

1919
return (
2020
<HomeLayout
@@ -35,15 +35,15 @@ export default async function BlogPost({
3535
<article className="container mx-auto max-w-3xl px-6 py-12">
3636
<header className="mb-12 text-center">
3737
<div className="mb-4 text-sm text-fd-muted-foreground">
38-
<time>{new Date(post.data.date).toLocaleDateString()}</time>
39-
{post.data.author && <span>{post.data.author}</span>}
38+
<time>{new Date(post.date).toLocaleDateString()}</time>
39+
{post.author && <span>{post.author}</span>}
4040
</div>
4141
<h1 className="text-4xl font-bold leading-tight md:text-5xl">
42-
{post.data.title}
42+
{post.title}
4343
</h1>
44-
{post.data.description && (
44+
{post.description && (
4545
<p className="mt-6 text-xl text-fd-muted-foreground">
46-
{post.data.description}
46+
{post.description}
4747
</p>
4848
)}
4949
</header>
@@ -57,8 +57,8 @@ export default async function BlogPost({
5757
}
5858

5959
export function generateStaticParams() {
60-
return blog.map((post) => ({
61-
slug: post.slug,
60+
return blog.map((post: any) => ({
61+
slug: post.info.path.replace(/\.mdx?$/, ''),
6262
}));
6363
}
6464

@@ -68,10 +68,10 @@ export async function generateMetadata({
6868
params: Promise<{ slug: string }>
6969
}) {
7070
const { slug } = await params;
71-
const post = blog.find((post) => post.slug === slug);
71+
const post = blog.find((post: any) => post.info.path.replace(/\.mdx?$/, '') === slug) as any;
7272
if (!post) return;
7373
return {
74-
title: post.data.title,
75-
description: post.data.description,
74+
title: post.title,
75+
description: post.description,
7676
};
7777
}

apps/site/app/blog/page.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Logo } from '../components/Logo';
55

66
export default function BlogIndex() {
77
const posts = [...blog].sort(
8-
(a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime()
8+
(a: any, b: any) => new Date(b.date).getTime() - new Date(a.date).getTime()
99
);
1010

1111
return (
@@ -27,22 +27,25 @@ export default function BlogIndex() {
2727
<main className="container mx-auto max-w-4xl px-6 py-12">
2828
<h1 className="mb-8 text-4xl font-bold">Latest Updates</h1>
2929
<div className="grid gap-6">
30-
{posts.map((post) => (
31-
<Link
32-
key={post.url}
33-
href={`/blog/${post.slug}`}
34-
className="flex flex-col gap-2 rounded-lg border p-6 transition-colors hover:bg-fd-accent/50"
35-
>
36-
<h2 className="text-2xl font-bold">{post.data.title}</h2>
37-
<div className="flex gap-2 text-sm text-fd-muted-foreground">
38-
<time>{new Date(post.data.date).toLocaleDateString()}</time>
39-
{post.data.author && <span>{post.data.author}</span>}
40-
</div>
41-
{post.data.description && (
42-
<p className="text-fd-muted-foreground">{post.data.description}</p>
43-
)}
44-
</Link>
45-
))}
30+
{posts.map((post: any) => {
31+
const slug = post.info.path.replace(/\.mdx?$/, '');
32+
return (
33+
<Link
34+
key={slug}
35+
href={`/blog/${slug}`}
36+
className="flex flex-col gap-2 border p-6 rounded-lg hover:bg-fd-accent/50 transition-colors"
37+
>
38+
<h2 className="text-2xl font-bold">{post.title}</h2>
39+
<div className="flex gap-2 text-sm text-fd-muted-foreground">
40+
<time>{new Date(post.date).toLocaleDateString()}</time>
41+
{post.author && <span>{post.author}</span>}
42+
</div>
43+
{post.description && (
44+
<p className="text-fd-muted-foreground">{post.description}</p>
45+
)}
46+
</Link>
47+
);
48+
})}
4649
</div>
4750
</main>
4851
</HomeLayout>

apps/site/source.config.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { z } from 'zod';
33

44
export const { docs, meta } = defineDocs({
55
dir: '../../docs',
6-
root: '../../docs',
7-
files: [
8-
'**/*.{md,mdx}',
9-
'!**/node_modules/**',
10-
'!**/.vitepress/**',
11-
'!**/dist/**',
12-
'!**/.*/**', // ignore dotfiles/folders generally
13-
],
6+
docs: {
7+
files: [
8+
'**/*.{md,mdx}',
9+
'!**/node_modules/**',
10+
'!**/.vitepress/**',
11+
'!**/dist/**',
12+
'!**/.*/**',
13+
],
14+
},
1415
});
1516

1617
export const blog = defineCollections({

0 commit comments

Comments
 (0)