-
Notifications
You must be signed in to change notification settings - Fork 9
[refactor] replace Git submodule with GitHub content API for Policy Wiki pages #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0673c86
Initial plan
Copilot d8b3ead
Changes before error encountered
Copilot 556e842
Changes before error encountered
Copilot 22f018b
Changes before error encountered
Copilot 69868e1
[fix] GitHub copilot compatibility of Personal Access Token
TechQuery 592a14f
Changes before error encountered
Copilot eb834de
Changes before error encountered
Copilot 1067043
[fix] several GitHub copilot bugs
TechQuery File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
| package-lock.json | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { ContentModel } from 'mobx-github'; | ||
| import { treeFrom } from 'web-utility'; | ||
| import { githubClient } from './Base'; | ||
|
|
||
| export interface WikiNode { | ||
| name?: string; | ||
| title: string; | ||
| path: string; | ||
| parent_path?: string; | ||
| children?: WikiNode[]; | ||
| type?: string; | ||
| size?: number; | ||
| sha?: string; | ||
| url?: string; | ||
| html_url?: string; | ||
| git_url?: string; | ||
| download_url?: string; | ||
| content?: string; | ||
| metadata?: Record<string, string>; | ||
| labels?: string[]; | ||
| } | ||
|
|
||
| export const contentStore = new ContentModel('fpsig', 'open-source-policy'); | ||
|
|
||
| class WikiModel { | ||
| private contentModel = contentStore; | ||
|
|
||
| async getAllContent(): Promise<WikiNode[]> { | ||
| try { | ||
| const items: WikiNode[] = []; | ||
|
|
||
| // Use traverseTree to get all markdown files recursively from China/政策 | ||
| for await (const item of this.contentModel.traverseTree()) { | ||
| if (item.type === 'file' && item.name.endsWith('.md') && item.path.startsWith('China/政策/')) { | ||
| // Remove the 'China/政策/' prefix to get relative path within wiki | ||
| const relativePath = item.path.replace('China/政策/', ''); | ||
| const pathParts = relativePath.split('/'); | ||
| const fileName = pathParts.pop(); | ||
| const parent_path = pathParts.length > 0 ? pathParts.join('/') : undefined; | ||
|
|
||
| const wikiNode: WikiNode = { | ||
| name: fileName || '', | ||
| path: relativePath.replace('.md', ''), | ||
| parent_path, | ||
| title: fileName?.replace('.md', '') || '', | ||
| type: item.type, | ||
| size: item.size, | ||
| sha: item.sha, | ||
| url: item.url, | ||
| html_url: item.html_url || undefined, | ||
| git_url: item.git_url || undefined, | ||
| download_url: item.download_url || undefined, | ||
| content: '', | ||
| labels: [], | ||
| }; | ||
|
|
||
| items.push(wikiNode); | ||
| } | ||
| } | ||
|
|
||
| return items; | ||
| } catch (error) { | ||
| console.error('Error fetching content from GitHub:', error); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| async getContentTree(): Promise<WikiNode[]> { | ||
| const allContent = await this.getAllContent(); | ||
| return treeFrom(allContent, 'path', 'parent_path', 'children'); | ||
| } | ||
|
|
||
| async getWikiContent(pathParam: string): Promise<WikiNode> { | ||
| const fullPath = pathParam.endsWith('.md') ? pathParam : `${pathParam}.md`; | ||
| const filePath = `China/政策/${fullPath}`; | ||
|
|
||
| const item = await this.contentModel.getOne(filePath); | ||
|
|
||
| if (!item || item.type !== 'file') { | ||
| throw new Error(`Content not found at path: ${pathParam}`); | ||
| } | ||
|
|
||
| // Decode Base64 content | ||
| const content = item.content ? atob(item.content) : ''; | ||
|
|
||
| // Parse frontmatter | ||
| let metadata: Record<string, string> = {}; | ||
| let markdownContent = ''; | ||
|
|
||
| if (content.startsWith('---\n')) { | ||
| const parts = content.split('\n---\n'); | ||
| if (parts.length >= 2) { | ||
| const frontmatter = parts[0].substring(4); // Remove first '---\n' | ||
| markdownContent = parts.slice(1).join('\n---\n'); | ||
|
|
||
| // Simple YAML parsing for metadata | ||
| const lines = frontmatter.split('\n'); | ||
| for (const line of lines) { | ||
| const [key, ...valueParts] = line.split(': '); | ||
| if (key && valueParts.length > 0) { | ||
| metadata[key.trim()] = valueParts.join(': ').trim(); | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| markdownContent = content; | ||
| } | ||
|
|
||
| const pathParts = pathParam.split('/'); | ||
| const fileName = pathParts.pop(); | ||
| const parent_path = pathParts.length > 0 ? pathParts.join('/') : undefined; | ||
|
|
||
| return { | ||
| name: fileName || '', | ||
| path: pathParam.replace('.md', ''), | ||
| parent_path, | ||
| title: metadata['name'] || fileName?.replace('.md', '') || '', | ||
| type: item.type, | ||
| size: item.size, | ||
| sha: item.sha, | ||
| url: item.url, | ||
| html_url: item.html_url || undefined, | ||
| git_url: item.git_url || undefined, | ||
| download_url: item.download_url || undefined, | ||
| content: markdownContent, | ||
| metadata, | ||
| labels: [], | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export const wikiStore = new WikiModel(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,155 @@ | ||
| import { marked } from 'marked'; | ||
| import { DataObject } from 'mobx-restful'; | ||
| import { GetStaticPaths, GetStaticProps, NextPage } from 'next'; | ||
| import { GetStaticPaths, GetStaticProps } from 'next'; | ||
| import Link from 'next/link'; | ||
| import { ParsedUrlQuery } from 'querystring'; | ||
| import { FC } from 'react'; | ||
| import { Badge, Breadcrumb, Button, Container } from 'react-bootstrap'; | ||
|
|
||
| import { pageListOf, splitFrontMatter, traverseTree } from '../api/core'; | ||
| import { PageHead } from '../../components/Layout/PageHead'; | ||
| import { WikiNode, wikiStore } from '../../models/Wiki'; | ||
|
|
||
| interface WikiPageParams extends ParsedUrlQuery { | ||
| slug: string[]; | ||
| } | ||
|
|
||
| export const getStaticPaths: GetStaticPaths<WikiPageParams> = async () => { | ||
| const tree = await Array.fromAsync(pageListOf('wiki', 'public/')); | ||
| const list = tree.map(root => [...traverseTree(root, 'subs')]).flat(); | ||
| const paths = list | ||
| .map(({ path }) => path && { params: { slug: path.split('/') } }) | ||
| .filter(Boolean) as { params: WikiPageParams }[]; | ||
| const nodes = await wikiStore.getAllContent(); | ||
| const paths = nodes.map(({ path }) => ({ | ||
| params: { slug: path.split('/') } | ||
| })); | ||
|
|
||
| return { paths, fallback: 'blocking' }; | ||
| }; | ||
|
|
||
| interface WikiPageProps { | ||
| meta?: DataObject; | ||
| node: WikiNode; | ||
| markup: string; | ||
| } | ||
|
|
||
| export const getStaticProps: GetStaticProps<WikiPageProps, WikiPageParams> = async ({ params }) => { | ||
| const { slug } = params!; | ||
| // https://github.com/vercel/next.js/issues/12851 | ||
| if (slug[0] !== 'wiki') slug.unshift('wiki'); | ||
| const nodePath = slug.join('/'); | ||
|
|
||
| const { meta, markdown } = await splitFrontMatter(`public/${slug.join('/')}.md`); | ||
| const node = await wikiStore.getWikiContent(nodePath); | ||
| const markup = marked(node.content || '') as string; | ||
|
TechQuery marked this conversation as resolved.
Outdated
|
||
|
|
||
| const markup = marked(markdown) as string; | ||
|
|
||
| return { props: JSON.parse(JSON.stringify({ meta, markup })) }; | ||
| return { | ||
| props: { node, markup }, | ||
| revalidate: 300 // Revalidate every 5 minutes | ||
| }; | ||
| }; | ||
|
|
||
| const WikiPage: NextPage<WikiPageProps> = ({ meta, markup }) => ( | ||
| <> | ||
| {meta && ( | ||
| <blockquote> | ||
| <a target="_blank" href={meta.url} rel="noreferrer"> | ||
| {meta.url} | ||
| </a> | ||
| </blockquote> | ||
| )} | ||
| <article dangerouslySetInnerHTML={{ __html: markup }} /> | ||
| </> | ||
| const WikiPage: FC<WikiPageProps> = ({ node, markup }) => ( | ||
| <Container className="py-4"> | ||
| <PageHead title={node.title} /> | ||
|
|
||
| <Breadcrumb className="mb-4"> | ||
| <Breadcrumb.Item linkAs={Link} linkProps={{ href: '/wiki' }}> | ||
| Wiki | ||
| </Breadcrumb.Item> | ||
| {node.parent_path?.split('/').map((segment, index, array) => { | ||
| const breadcrumbPath = array.slice(0, index + 1).join('/'); | ||
|
|
||
| return ( | ||
| <Breadcrumb.Item | ||
| key={breadcrumbPath} | ||
| linkAs={Link} | ||
| linkProps={{ href: `/wiki/${breadcrumbPath}` }} | ||
| > | ||
| {segment} | ||
| </Breadcrumb.Item> | ||
| ); | ||
| })} | ||
| <Breadcrumb.Item active> | ||
| {node.title} | ||
| </Breadcrumb.Item> | ||
| </Breadcrumb> | ||
|
|
||
| <article> | ||
| <header className="mb-4"> | ||
| <h1>{node.title}</h1> | ||
|
|
||
| {node.metadata && ( | ||
| <div className="d-flex flex-wrap align-items-center gap-3 mb-3"> | ||
| <ul className="list-inline mb-0"> | ||
| {node.metadata['主题分类'] && ( | ||
| <li className="list-inline-item"> | ||
| <Badge bg="primary">{node.metadata['主题分类']}</Badge> | ||
| </li> | ||
| )} | ||
| {node.metadata['发文机构'] && ( | ||
| <li className="list-inline-item"> | ||
| <Badge bg="secondary">{node.metadata['发文机构']}</Badge> | ||
| </li> | ||
| )} | ||
| {node.metadata['有效性'] && ( | ||
| <li className="list-inline-item"> | ||
| <Badge bg={node.metadata['有效性'] === '现行有效' ? 'success' : 'warning'}> | ||
| {node.metadata['有效性']} | ||
| </Badge> | ||
| </li> | ||
| )} | ||
| </ul> | ||
| </div> | ||
| )} | ||
|
|
||
| <div className="d-flex justify-content-between align-items-center text-muted small mb-3"> | ||
| <div> | ||
| {node.metadata?.['成文日期'] && ( | ||
| <span>成文日期: {node.metadata['成文日期']}</span> | ||
| )} | ||
| {node.metadata?.['发布日期'] && node.metadata['发布日期'] !== node.metadata['成文日期'] && ( | ||
| <span className="ms-3">发布日期: {node.metadata['发布日期']}</span> | ||
| )} | ||
| </div> | ||
|
|
||
| <div className="d-flex gap-2"> | ||
| <Button | ||
| variant="outline-primary" | ||
| size="sm" | ||
| href={`https://github.com/fpsig/open-source-policy/blob/main/China/政策/${node.path}`} | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| 在 GitHub 编辑 | ||
| </Button> | ||
| {node.metadata?.url && ( | ||
| <Button | ||
| variant="outline-secondary" | ||
| size="sm" | ||
| href={node.metadata.url} | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| 查看原文 | ||
| </Button> | ||
| )} | ||
| </div> | ||
| </div> | ||
| </header> | ||
|
|
||
| <div | ||
| dangerouslySetInnerHTML={{ __html: markup }} | ||
| className="markdown-body" | ||
| /> | ||
| </article> | ||
|
|
||
| <footer className="mt-5 pt-4 border-top"> | ||
| <div className="text-center"> | ||
| <p className="text-muted"> | ||
| 这是一个基于 GitHub 仓库的政策文档页面。 | ||
| <a | ||
| href={`https://github.com/fpsig/open-source-policy/blob/main/China/政策/${node.path}`} | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| className="ms-2" | ||
| > | ||
| 在 GitHub 上查看或编辑此内容 | ||
| </a> | ||
| </p> | ||
| </div> | ||
| </footer> | ||
| </Container> | ||
| ); | ||
|
|
||
| export default WikiPage; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.