-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path[...slug].tsx
More file actions
150 lines (127 loc) · 4.08 KB
/
[...slug].tsx
File metadata and controls
150 lines (127 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { marked } from 'marked';
import { GetStaticPaths, GetStaticProps } from 'next';
import Link from 'next/link';
import { ParsedUrlQuery } from 'querystring';
import { FC } from 'react';
import { Badge, Container } from 'react-bootstrap';
import { PageHead } from '../../components/Layout/PageHead';
import { WikiNode, wikiStore } from '../../models/Wiki';
interface WikiPageParams extends ParsedUrlQuery {
slug: string[];
}
export const getStaticPaths: GetStaticPaths<WikiPageParams> = async () => {
try {
const nodes = await wikiStore.getAll();
const paths = nodes.map(({ id }) => ({
params: { slug: [id.toString()] }
}));
return { paths, fallback: 'blocking' };
} catch (error) {
console.error('Failed to generate static paths:', error);
return { paths: [], fallback: 'blocking' };
}
};
interface WikiPageProps {
node?: WikiNode;
markup: string;
}
export const getStaticProps: GetStaticProps<WikiPageProps, WikiPageParams> = async ({ params }) => {
const { slug } = params!;
const [nodeId] = slug;
try {
const node = await wikiStore.getOne(nodeId);
if (!node) {
return { notFound: true };
}
const markup = marked(node.body || '') as string;
return {
props: { node, markup },
revalidate: 300 // Revalidate every 5 minutes
};
} catch (error) {
console.error('Failed to load wiki node:', error);
return { notFound: true };
}
};
const WikiPage: FC<WikiPageProps> = ({ node, markup }) => {
if (!node) {
return (
<Container className="py-4">
<div className="text-center">
<h2>Wiki 页面未找到</h2>
<p>请检查页面链接是否正确。</p>
<Link href="/wiki" className="btn btn-primary">
返回 Wiki 首页
</Link>
</div>
</Container>
);
}
return (
<Container className="py-4">
<PageHead title={node.title} />
<nav aria-label="breadcrumb" className="mb-4">
<ol className="breadcrumb">
<li className="breadcrumb-item">
<Link href="/wiki">Wiki</Link>
</li>
<li className="breadcrumb-item active" aria-current="page">
{node.title}
</li>
</ol>
</nav>
<article>
<header className="mb-4">
<h1>{node.title}</h1>
<div className="d-flex flex-wrap align-items-center gap-3 mb-3">
<div>
{node.labels.map(label => (
<Badge key={label} bg="secondary" className="me-1">
{label}
</Badge>
))}
</div>
{node.repository && (
<small className="text-muted">
来源: {node.repository.full_name}
</small>
)}
</div>
<div className="d-flex justify-content-between align-items-center text-muted small">
<div>
创建于: {new Date(node.created_at).toLocaleDateString('zh-CN')}
{node.updated_at !== node.created_at && (
<> | 更新于: {new Date(node.updated_at).toLocaleDateString('zh-CN')}</>
)}
</div>
<div>
<a
className="btn btn-sm btn-outline-primary"
href={node.html_url}
rel="noopener noreferrer"
target="_blank"
>
在 GitHub 编辑
</a>
</div>
</div>
</header>
<div
dangerouslySetInnerHTML={{ __html: markup }}
className="wiki-content"
/>
</article>
<footer className="mt-5 pt-4 border-top">
<div className="text-center">
<p className="text-muted">
这是一个基于 GitHub Issues 的 Wiki 页面。
<a href={node.html_url} target="_blank" rel="noopener noreferrer" className="ms-2">
在 GitHub 上查看或编辑此内容
</a>
</p>
</div>
</footer>
</Container>
);
};
export default WikiPage;