-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWiki.ts
More file actions
132 lines (113 loc) · 3.98 KB
/
Wiki.ts
File metadata and controls
132 lines (113 loc) · 3.98 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
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();