Skip to content

Commit d3ef522

Browse files
committed
feat: 动态路由展示博客
1 parent f1a69c7 commit d3ef522

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/app/(app)/notes/[nid]/page.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,43 @@ import {
99
PageTransition,
1010
} from './pageExtra';
1111

12+
import { type PostItem as PostItemType, buildPostData } from '@/core';
1213
import { LayoutRightSidePortal } from '@/providers/shared/LayoutRightSideProvider';
1314
import { ArticleRightAside } from '@/components/modules/shared/ArticleRightAside';
1415
import { Signature } from '@/components/modules/shared/signature';
1516

17+
const { postDataMap } = buildPostData();
18+
19+
export type PageInnerProps = { postData: PostItemType };
20+
1621
export default async function Page({ params }: { params: Record<string, any> }) {
1722
const { nid } = params;
18-
console.log('id', nid);
23+
const postData = postDataMap[nid];
24+
console.log(postData);
1925

2026
return (
2127
<PageTransition>
2228
<PaperLayout>
23-
<PageInner />
29+
<PageInner postData={postData} />
2430
</PaperLayout>
2531
</PageTransition>
2632
);
2733
}
2834

29-
const PageInner = () => (
35+
const PageInner = ({ postData }: PageInnerProps) => (
3036
<>
3137
<div>
32-
<NoteTitle />
38+
<NoteTitle title={postData.title} />
3339
<span className="flex flex-wrap items-center text-sm text-neutral-content/60">
34-
<NoteDateMeta />
40+
<NoteDateMeta
41+
createdAt={postData.createdAt!}
42+
updatedAt={postData.updatedAt!}
43+
modified={postData.modified}
44+
/>
3545
</span>
3646
</div>
3747
<IndentArticleContainer>
38-
<NoteMarkdown />
48+
<NoteMarkdown text={postData.text} />
3949
<div className="signature-animated my-2 flex w-full justify-end" data-hide-print="true">
4050
<Signature />
4151
</div>

src/app/(app)/notes/[nid]/pageExtra.tsx

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import React, { PropsWithChildren, useEffect, useRef } from 'react';
44
import { m } from 'framer-motion';
55
import { useShallow } from 'zustand/react/shallow';
66

7-
import text from './test.md';
8-
7+
import dayjs from '@/lib/dayjs';
98
import { MdiClockOutline } from '@/components/icons/clock';
109
import { cn } from '@/lib/helper';
1110
import { MainMarkdown } from '@/components/ui/markdown';
1211
import { useMainArticleStore } from '@/store/mainArticleStore';
1312

14-
export const NoteTitle = () => {
15-
const title = '我的一篇文章';
16-
13+
export const NoteTitle = ({ title }: { title: string }) => {
1714
if (!title) return null;
1815

1916
return (
@@ -25,11 +22,24 @@ export const NoteTitle = () => {
2522
);
2623
};
2724

28-
export const NoteDateMeta = () => {
25+
export const NoteDateMeta = ({
26+
createdAt,
27+
updatedAt,
28+
modified,
29+
}: {
30+
createdAt: Date;
31+
updatedAt: Date;
32+
modified: boolean;
33+
}) => {
2934
return (
3035
<span className="inline-flex items-center space-x-1">
3136
<MdiClockOutline />
32-
<time className="font-medium font-mono">2024年10月27日 星期日</time>
37+
<time className=" font-semibold text-base font-mono">
38+
{createdAt && !modified
39+
? dayjs(createdAt).format('YYYY 年 M 月 D 日')
40+
: '1999 年 9 月 9 日'}
41+
{modified && <>编辑于&nbsp;&nbsp; {dayjs(updatedAt).format('YYYY 年 M 月 D 日')}</>}
42+
</time>
3343
</span>
3444
);
3545
};
@@ -50,7 +60,7 @@ const MarkdownRenderers: Record<string, any> = {
5060
},
5161
};
5262

53-
export const NoteMarkdown = () => {
63+
export const NoteMarkdown = ({ text }: { text: string }) => {
5464
return <MainMarkdown className="mt-10" allowsScript renderers={MarkdownRenderers} value={text} />;
5565
};
5666

src/components/modules/toc/TocTree.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { useStateToRef } from '@/hooks/common/use-state-ref';
2121
import { cn } from '@/lib/helper';
2222
import { springScrollToElement } from '@/lib/scroller';
2323

24-
// 使用 `useState` 来管理本地的 activeId 状态
2524
function useActiveId($headings: HTMLHeadingElement[]) {
2625
const [activeId, setActiveId] = useState<string | null>(null);
2726

@@ -75,9 +74,6 @@ export const TocTree = ({
7574
return Array.from($headings).map((el, idx) => {
7675
const depth = +el.tagName.slice(1);
7776
const elClone = el.cloneNode(true) as HTMLElement;
78-
elClone.querySelectorAll('del, .katex-container').forEach((del) => {
79-
del.remove();
80-
});
8177

8278
const title = elClone.textContent || '';
8379
const index = idx;

0 commit comments

Comments
 (0)