Skip to content

Commit 1067043

Browse files
committed
[fix] several GitHub copilot bugs
[optimize] update Upstream packages & simplify Source Code
1 parent eb834de commit 1067043

6 files changed

Lines changed: 391 additions & 420 deletions

File tree

models/Wiki.ts

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
1-
import { ContentModel } from 'mobx-github';
2-
import { treeFrom } from 'web-utility';
1+
import { Content, ContentModel } from 'mobx-github';
2+
import { DataObject } from 'mobx-restful';
33

44
import './Base';
55

6-
export const policyContentStore = new ContentModel('fpsig', 'open-source-policy');
7-
8-
// Minimal interface and exports for compatibility
9-
export interface WikiNode {
10-
name?: string;
11-
title: string;
12-
path: string;
13-
parent_path?: string;
14-
children?: WikiNode[];
15-
type?: string;
16-
size?: number;
17-
sha?: string;
18-
url?: string;
19-
html_url?: string;
20-
git_url?: string;
21-
download_url?: string;
22-
content?: string;
23-
metadata?: Record<string, string>;
6+
export interface XContent extends Content {
7+
meta?: DataObject;
8+
children?: XContent[];
249
}
2510

26-
export const wikiStore = {
27-
async getAllContent(): Promise<WikiNode[]> {
28-
return [];
29-
},
30-
async getWikiContent(pathParam: string): Promise<WikiNode> {
31-
throw new Error('Not implemented');
32-
}
33-
};
11+
export const policyContentStore = new ContentModel('fpsig', 'open-source-policy');

package.json

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,32 @@
1515
"@koa/router": "^14.0.0",
1616
"@mdx-js/loader": "^3.1.0",
1717
"@mdx-js/react": "^3.1.0",
18-
"@next/mdx": "^15.5.0",
18+
"@next/mdx": "^15.5.2",
1919
"core-js": "^3.45.1",
2020
"file-type": "^21.0.0",
21-
"github-markdown-css": "^5.8.1",
2221
"idea-react": "^2.0.0-rc.13",
2322
"koa": "^3.0.1",
2423
"koajax": "^3.1.2",
2524
"license-filter": "^0.2.5",
26-
"marked": "^16.2.0",
25+
"marked": "^16.2.1",
2726
"mime": "^4.0.7",
2827
"mobx": "^6.13.7",
29-
"mobx-github": "^0.4.0-rc.1",
28+
"mobx-github": "^0.4.0",
3029
"mobx-i18n": "^0.7.1",
3130
"mobx-lark": "^2.4.0",
3231
"mobx-react": "^9.2.0",
3332
"mobx-react-helper": "^0.5.1",
3433
"mobx-restful": "^2.1.0",
3534
"mobx-restful-table": "^2.5.3",
36-
"next": "^15.5.0",
35+
"next": "^15.5.2",
3736
"next-pwa": "^5.6.0",
3837
"next-ssr-middleware": "^1.0.2",
3938
"react": "^19.1.1",
4039
"react-bootstrap": "^2.10.10",
4140
"react-dom": "^19.1.1",
4241
"react-typed-component": "^1.0.6",
4342
"undici": "^7.15.0",
44-
"web-utility": "^4.5.1",
43+
"web-utility": "^4.5.3",
4544
"yaml": "^2.8.1"
4645
},
4746
"devDependencies": {
@@ -50,18 +49,18 @@
5049
"@babel/preset-react": "^7.27.1",
5150
"@cspell/eslint-plugin": "^9.2.0",
5251
"@eslint/js": "^9.34.0",
53-
"@next/eslint-plugin-next": "^15.5.0",
52+
"@next/eslint-plugin-next": "^15.5.2",
5453
"@softonus/prettier-plugin-duplicate-remover": "^1.1.2",
5554
"@stylistic/eslint-plugin": "^5.2.3",
5655
"@types/eslint-config-prettier": "^6.11.3",
5756
"@types/koa": "^3.0.0",
5857
"@types/koa__router": "^12.0.4",
5958
"@types/next-pwa": "^5.6.9",
60-
"@types/node": "^22.17.2",
61-
"@types/react": "^19.1.11",
62-
"@types/react-dom": "^19.1.7",
59+
"@types/node": "^22.18.0",
60+
"@types/react": "^19.1.12",
61+
"@types/react-dom": "^19.1.9",
6362
"eslint": "^9.34.0",
64-
"eslint-config-next": "^15.5.0",
63+
"eslint-config-next": "^15.5.2",
6564
"eslint-config-prettier": "^10.1.8",
6665
"eslint-plugin-react": "^7.37.5",
6766
"eslint-plugin-simple-import-sort": "^12.1.1",
@@ -74,9 +73,9 @@
7473
"next-with-less": "^3.0.1",
7574
"prettier": "^3.6.2",
7675
"prettier-plugin-css-order": "^2.1.2",
77-
"sass": "^1.90.0",
76+
"sass": "^1.91.0",
7877
"typescript": "~5.9.2",
79-
"typescript-eslint": "^8.40.0"
78+
"typescript-eslint": "^8.41.0"
8079
},
8180
"resolutions": {
8281
"next": "$next"

pages/api/core.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,28 @@ export interface ArticleMeta {
5858
subs: ArticleMeta[];
5959
}
6060

61-
const MDX_pattern = /\.mdx?$/;
62-
63-
export async function splitFrontMatter(path: string) {
64-
const { readFile } = await import('fs/promises');
65-
66-
const file = await readFile(path, 'utf-8');
61+
export const MD_pattern = /\.(md|markdown)$/i,
62+
MDX_pattern = /\.mdx?$/i;
6763

64+
export function splitFrontMatter(raw: string) {
6865
const [, frontMatter, markdown] =
69-
file.trim().match(/^---[\r\n]([\s\S]+?[\r\n])---[\r\n]([\s\S]*)/) || [];
66+
raw.trim().match(/^---[\r\n]([\s\S]+?[\r\n])---[\r\n]([\s\S]*)/) || [];
7067

71-
if (!frontMatter) return { markdown: file };
68+
if (!frontMatter) return { markdown: raw };
7269

7370
try {
7471
const meta = parse(frontMatter) as DataObject;
7572

7673
return { markdown, meta };
7774
} catch (error) {
78-
console.error(`Error reading front matter for ${path}:`, error);
75+
console.error(`Error parsing Front Matter:`, error);
7976

8077
return { markdown };
8178
}
8279
}
8380

8481
export async function* pageListOf(path: string, prefix = 'pages'): AsyncGenerator<ArticleMeta> {
85-
const { readdir } = await import('fs/promises');
82+
const { readdir, readFile } = await import('fs/promises');
8683

8784
const list = await readdir(prefix + path, { withFileTypes: true });
8885

@@ -99,7 +96,9 @@ export async function* pageListOf(path: string, prefix = 'pages'): AsyncGenerato
9996
if (node.isFile() && isMDX) {
10097
const article: ArticleMeta = { name, path, subs: [] };
10198

102-
const { meta } = await splitFrontMatter(`${node.path}/${node.name}`);
99+
const file = await readFile(`${node.path}/${node.name}`, 'utf-8');
100+
101+
const { meta } = splitFrontMatter(file);
103102

104103
if (meta) article.meta = meta;
105104

pages/wiki/[...slug].tsx

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,83 @@
11
import { marked } from 'marked';
22
import { GetStaticPaths, GetStaticProps } from 'next';
3-
import Link from 'next/link';
43
import { ParsedUrlQuery } from 'querystring';
54
import { FC } from 'react';
65
import { Badge, Breadcrumb, Button, Container } from 'react-bootstrap';
6+
import { decodeBase64 } from 'web-utility';
77

88
import { 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

1112
interface WikiPageParams extends ParsedUrlQuery {
1213
slug: string[];
1314
}
1415

1516
export 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

Comments
 (0)