11import { marked } from 'marked' ;
22import { GetStaticPaths , GetStaticProps } from 'next' ;
3- import Link from 'next/link' ;
43import { ParsedUrlQuery } from 'querystring' ;
54import { FC } from 'react' ;
65import { Badge , Breadcrumb , Button , Container } from 'react-bootstrap' ;
6+ import { decodeBase64 } from 'web-utility' ;
77
88import { PageHead } from '../../components/Layout/PageHead' ;
9- import { WikiNode , wikiStore } from '../../models/Wiki' ;
9+ import { policyContentStore , XContent } from '../../models/Wiki' ;
10+ import { splitFrontMatter } from '../api/core' ;
1011
1112interface WikiPageParams extends ParsedUrlQuery {
1213 slug : string [ ] ;
1314}
1415
1516export const getStaticPaths : GetStaticPaths < WikiPageParams > = async ( ) => {
16- const nodes = await wikiStore . getAllContent ( ) ;
17- const paths = nodes . map ( ( { path } ) => ( {
18- params : { slug : path . split ( '/' ) }
19- } ) ) ;
17+ const nodes = await policyContentStore . getAll ( ) ;
18+
19+ const paths = nodes
20+ . filter ( ( { type } ) => type === 'file' )
21+ . map ( ( { path } ) => ( { params : { slug : path . split ( '/' ) } } ) ) ;
2022
2123 return { paths, fallback : 'blocking' } ;
2224} ;
2325
24- interface WikiPageProps {
25- node : WikiNode ;
26- markup : string ;
27- }
28-
29- export const getStaticProps : GetStaticProps < WikiPageProps , WikiPageParams > = async ( { params } ) => {
26+ export const getStaticProps : GetStaticProps < XContent , WikiPageParams > = async ( { params } ) => {
3027 const { slug } = params ! ;
31- const nodePath = slug . join ( '/' ) ;
3228
33- const node = await wikiStore . getWikiContent ( nodePath ) ;
34- const markup = marked ( node . content || '' ) as string ;
29+ const node = await policyContentStore . getOne ( slug . join ( '/' ) ) ;
3530
36- return {
37- props : { node, markup } ,
38- revalidate : 300 // Revalidate every 5 minutes
31+ const { meta, markdown } = splitFrontMatter ( decodeBase64 ( node . content ! ) ) ;
32+
33+ const markup = marked ( markdown ) as string ;
34+
35+ return {
36+ props : JSON . parse ( JSON . stringify ( { ...node , content : markup , meta } ) ) ,
37+ revalidate : 300 , // Revalidate every 5 minutes
3938 } ;
4039} ;
4140
42- const WikiPage : FC < WikiPageProps > = ( { node , markup } ) => (
41+ const WikiPage : FC < XContent > = ( { name , path , parent_path , content , meta } ) => (
4342 < Container className = "py-4" >
44- < PageHead title = { node . title } />
45-
43+ < PageHead title = { name } />
44+
4645 < Breadcrumb className = "mb-4" >
47- < Breadcrumb . Item linkAs = { Link } linkProps = { { href : '/wiki' } } >
48- Wiki
49- </ Breadcrumb . Item >
50- { node . parent_path ?. split ( '/' ) . map ( ( segment , index , array ) => {
46+ < Breadcrumb . Item href = "/wiki" > Wiki</ Breadcrumb . Item >
47+
48+ { parent_path ?. split ( '/' ) . map ( ( segment , index , array ) => {
5149 const breadcrumbPath = array . slice ( 0 , index + 1 ) . join ( '/' ) ;
5250
5351 return (
54- < Breadcrumb . Item
55- key = { breadcrumbPath }
56- linkAs = { Link }
57- linkProps = { { href : `/wiki/${ breadcrumbPath } ` } }
58- >
52+ < Breadcrumb . Item key = { breadcrumbPath } href = { `/wiki/${ breadcrumbPath } ` } >
5953 { segment }
6054 </ Breadcrumb . Item >
6155 ) ;
6256 } ) }
63- < Breadcrumb . Item active >
64- { node . title }
65- </ Breadcrumb . Item >
57+ < Breadcrumb . Item active > { name } </ Breadcrumb . Item >
6658 </ Breadcrumb >
6759
6860 < article >
6961 < header className = "mb-4" >
70- < h1 > { node . title } </ h1 >
71-
72- { node . metadata && (
62+ < h1 > { name } </ h1 >
63+
64+ { meta && (
7365 < div className = "d-flex flex-wrap align-items-center gap-3 mb-3" >
74- < ul className = "list-inline mb-0" >
75- { node . metadata [ '主题分类' ] && (
76- < li className = "list-inline-item" >
77- < Badge bg = "primary" > { node . metadata [ '主题分类' ] } </ Badge >
66+ < ul className = "mb-0" >
67+ { meta [ '主题分类' ] && (
68+ < li >
69+ < Badge bg = "primary" > { meta [ '主题分类' ] } </ Badge >
7870 </ li >
7971 ) }
80- { node . metadata [ '发文机构' ] && (
81- < li className = "list-inline-item" >
82- < Badge bg = "secondary" > { node . metadata [ '发文机构' ] } </ Badge >
72+ { meta [ '发文机构' ] && (
73+ < li >
74+ < Badge bg = "secondary" > { meta [ '发文机构' ] } </ Badge >
8375 </ li >
8476 ) }
85- { node . metadata [ '有效性' ] && (
86- < li className = "list-inline-item" >
87- < Badge bg = { node . metadata [ '有效性' ] === '现行有效' ? 'success' : 'warning' } >
88- { node . metadata [ '有效性' ] }
77+ { meta [ '有效性' ] && (
78+ < li >
79+ < Badge bg = { meta [ '有效性' ] === '现行有效' ? 'success' : 'warning' } >
80+ { meta [ '有效性' ] }
8981 </ Badge >
9082 </ li >
9183 ) }
@@ -95,29 +87,27 @@ const WikiPage: FC<WikiPageProps> = ({ node, markup }) => (
9587
9688 < div className = "d-flex justify-content-between align-items-center text-muted small mb-3" >
9789 < div >
98- { node . metadata ?. [ '成文日期' ] && (
99- < span > 成文日期: { node . metadata [ '成文日期' ] } </ span >
100- ) }
101- { node . metadata ?. [ '发布日期' ] && node . metadata [ '发布日期' ] !== node . metadata [ '成文日期' ] && (
102- < span className = "ms-3" > 发布日期: { node . metadata [ '发布日期' ] } </ span >
90+ { meta ?. [ '成文日期' ] && < span > 成文日期: { meta [ '成文日期' ] } </ span > }
91+ { meta ?. [ '发布日期' ] && meta [ '发布日期' ] !== meta [ '成文日期' ] && (
92+ < span className = "ms-3" > 发布日期: { meta [ '发布日期' ] } </ span >
10393 ) }
10494 </ div >
105-
95+
10696 < div className = "d-flex gap-2" >
107- < Button
97+ < Button
10898 variant = "outline-primary"
10999 size = "sm"
110- href = { `https://github.com/fpsig/open-source-policy/blob/main/China/政策/${ node . path } ` }
100+ href = { `https://github.com/fpsig/open-source-policy/blob/main/China/政策/${ path } ` }
111101 target = "_blank"
112102 rel = "noopener noreferrer"
113103 >
114104 在 GitHub 编辑
115105 </ Button >
116- { node . metadata ?. url && (
117- < Button
106+ { meta ?. url && (
107+ < Button
118108 variant = "outline-secondary"
119109 size = "sm"
120- href = { node . metadata . url }
110+ href = { meta . url }
121111 target = "_blank"
122112 rel = "noopener noreferrer"
123113 >
@@ -128,20 +118,17 @@ const WikiPage: FC<WikiPageProps> = ({ node, markup }) => (
128118 </ div >
129119 </ header >
130120
131- < div
132- dangerouslySetInnerHTML = { { __html : markup } }
133- className = "markdown-body"
134- />
121+ < div dangerouslySetInnerHTML = { { __html : content || '' } } className = "markdown-body" />
135122 </ article >
136123
137124 < footer className = "mt-5 pt-4 border-top" >
138125 < div className = "text-center" >
139126 < p className = "text-muted" >
140127 这是一个基于 GitHub 仓库的政策文档页面。
141- < a
142- href = { `https://github.com/fpsig/open-source-policy/blob/main/China/政策/${ node . path } ` }
143- target = "_blank"
144- rel = "noopener noreferrer"
128+ < a
129+ href = { `https://github.com/fpsig/open-source-policy/blob/main/China/政策/${ path } ` }
130+ target = "_blank"
131+ rel = "noopener noreferrer"
145132 className = "ms-2"
146133 >
147134 在 GitHub 上查看或编辑此内容
0 commit comments