-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathgenerate-jetson-config.js
More file actions
271 lines (224 loc) · 8.2 KB
/
generate-jetson-config.js
File metadata and controls
271 lines (224 loc) · 8.2 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// 定义要搜索的多语言目录
const APP_DIRS = {
en: 'sites/en/docs/Edge/NVIDIA_Jetson/Application',
zh: 'sites/zh-CN/docs/Edge/NVIDIA_Jetson/Application',
ja: 'sites/ja/docs/Edge/NVIDIA_Jetson/Application',
es: 'sites/es/docs/Edge/NVIDIA_Jetson/Application',
pt: 'sites/pt-BR/docs/Edge/NVIDIA_Jetson/Application'
};
// 站点语言前缀
const LANG_URL_PREFIX = {
en: '',
zh: '/cn',
ja: '/ja',
es: '/es',
pt: '/pt-br'
};
// 定义分类映射(目录名 -> 配置变量名)
const CATEGORY_MAPPING = {
'Computer_Vision': 'communityList_cv',
'Generative_AI': 'communityList_gen',
'Robotics': 'communityList_robot',
'Developer_Tools': 'developerToolsList',
'Multimodal_AI': 'multimodalList',
'Physical_AI': 'physicalAIList',
'Managed_Services': 'managedServicesList'
};
// 存储提取的数据(按分类)
const data = {
communityList_cv: [],
communityList_gen: [],
communityList_robot: [],
developerToolsList: [],
multimodalList: [],
physicalAIList: [],
managedServicesList: []
};
// 用于跨语言聚合同一项目
const projectMap = {
communityList_cv: new Map(),
communityList_gen: new Map(),
communityList_robot: new Map(),
developerToolsList: new Map(),
multimodalList: new Map(),
physicalAIList: new Map(),
managedServicesList: new Map()
};
// 遍历目录提取数据
function extractData() {
Object.entries(APP_DIRS).forEach(([lang, dir]) => {
if (!fs.existsSync(dir)) {
console.warn(`[WARN] Directory not found for ${lang}: ${dir}`);
return;
}
fs.readdirSync(dir, { withFileTypes: true }).forEach(categoryDir => {
if (!categoryDir.isDirectory()) return;
const category = categoryDir.name;
const categoryKey = CATEGORY_MAPPING[category] || 'communityList_cv';
const categoryPath = path.join(dir, category);
fs.readdirSync(categoryPath, { withFileTypes: true }).forEach(file => {
if (!file.isFile() || !(file.name.endsWith('.md') || file.name.endsWith('.mdx'))) {
return;
}
const filePath = path.join(categoryPath, file.name);
const content = fs.readFileSync(filePath, 'utf8');
const project = extractProjectInfo(content, filePath, lang);
if (!project) return;
const map = projectMap[categoryKey];
const mergeKey = project.mergeKey;
if (!map.has(mergeKey)) {
map.set(mergeKey, {
name: {},
img: project.img,
URL: {},
category: {},
lastUpdated: project.lastUpdated,
author: project.author
});
}
const existing = map.get(mergeKey);
existing.name[lang] = project.name;
existing.URL[lang] = project.URL;
existing.category[lang] = project.category;
if (!existing.img && project.img) {
existing.img = project.img;
}
if (lang === 'en' || !existing.lastUpdated) {
existing.lastUpdated = project.lastUpdated;
}
if (lang === 'en' || !existing.author) {
existing.author = project.author;
}
});
});
});
Object.keys(projectMap).forEach(categoryKey => {
data[categoryKey] = Array.from(projectMap[categoryKey].values());
});
}
// 从文件内容中提取项目信息
function extractProjectInfo(content, filePath, lang) {
// 提取 frontmatter
const frontmatterMatch = content.match(/^---\s*\n([\s\S]*?)\n---/);
const frontmatter = frontmatterMatch ? frontmatterMatch[1] : '';
// 提取标题(优先从 frontmatter 的 title 字段,否则从正文)
const titleMatch = frontmatter.match(/^title:\s*(.*)$/m) || content.match(/^#\s+(.*)$/m);
if (!titleMatch) return null;
const name = cleanValue(titleMatch[1]);
// 提取图片(优先从 frontmatter 的 image 字段)
let img = 'https://files.seeedstudio.com/wiki/reComputer-Jetson/default-project.png';
const frontmatterImgMatch = frontmatter.match(/^image:\s*(.*)$/m);
if (frontmatterImgMatch) {
img = cleanValue(frontmatterImgMatch[1]);
// 如果是临时图片,寻找文档中的首个真实图片
if (img === 'https://files.seeedstudio.com/wiki/wiki-platform/S-tempor.png') {
const markdownImgMatch = content.match(/!\[.*?\]\((.*?\.(jpg|jpeg|png|webp|gif))\)/i);
const htmlImgMatch = content.match(/<img[^>]+src=["']([^"']*?\.(jpg|jpeg|png|webp|gif))["']?[^>]*>/is);
const htmlImgMatchMultiline = content.match(/<img[^>]*src=["']([^"']*?\.(jpg|jpeg|png|webp|gif))["']?[^>]*>/is);
if (markdownImgMatch && markdownImgMatch[1] !== img) {
img = markdownImgMatch[1];
} else if (htmlImgMatch && htmlImgMatch[1] !== img) {
img = htmlImgMatch[1].replace(/\s+/g, '');
} else if (htmlImgMatchMultiline && htmlImgMatchMultiline[1] !== img) {
img = htmlImgMatchMultiline[1].replace(/\s+/g, '');
}
}
} else {
const markdownImgMatch = content.match(/!\[.*?\]\((.*?)\)/);
const htmlImgMatch = content.match(/<img[^>]+src=["']([^"']+)["'][^>]*>/i);
if (markdownImgMatch) {
img = markdownImgMatch[1];
} else if (htmlImgMatch) {
img = htmlImgMatch[1].replace(/\s+/g, '');
}
}
// 生成 URL(所有语言 slug 相同,靠站点前缀区分)
let slug = '';
const slugMatch = frontmatter.match(/^slug:\s*(.*)$/m);
if (slugMatch) {
slug = cleanSlug(slugMatch[1]);
} else {
slug = buildSlugFromFilePath(filePath, lang);
}
const URL = `https://wiki.seeedstudio.com${LANG_URL_PREFIX[lang]}${slug}`;
const mergeKey = slug;
// 提取分类标签
const categoryMatch = content.match(/categories:\s*\[(.*?)\]/s);
const categoryArray = categoryMatch
? categoryMatch[1]
.split(',')
.map(c => c.trim().replace(/['"]/g, ''))
.filter(Boolean)
: [];
// 提取最后更新时间
const dateMatch =
frontmatter.match(/last_update:\s*\n\s*date:\s*(.*)/) ||
content.match(/lastUpdated:\s*(.*)/);
const lastUpdated = dateMatch ? cleanValue(dateMatch[1]) : new Date().toLocaleDateString();
// 提取作者
const authorMatch =
frontmatter.match(/last_update:\s*\n[\s\S]*?author:\s*(.*)/) ||
content.match(/author:\s*(.*)/);
const author = authorMatch ? cleanValue(authorMatch[1]) : 'Seeed Studio';
return {
mergeKey,
name,
img,
URL,
category: categoryArray,
lastUpdated,
author
};
}
function cleanValue(value) {
return String(value || '')
.trim()
.replace(/^['"]|['"]$/g, '');
}
function cleanSlug(value) {
const slug = cleanValue(value);
if (!slug) return '';
return slug.startsWith('/') ? slug : `/${slug}`;
}
// 当没有 slug 时,从文件名推导 slug
function buildSlugFromFilePath(filePath, lang) {
const normalized = filePath.replace(/\\/g, '/');
const baseDir = APP_DIRS[lang].replace(/\\/g, '/');
let relativePath = normalized.replace(`${baseDir}/`, '').replace(/\.mdx?$/, '');
// 只取文件名作为 slug,避免把 docs 目录路径带进 URL
const fileName = path.basename(relativePath);
return `/${fileName}`;
}
// 生成配置文件
function generateConfig() {
let output = `// Auto-generated by generate-jetson-config.js
// DO NOT EDIT MANUALLY - Run "node scripts/generate-jetson-config.js" to regenerate
// Generated at: ${new Date().toISOString()}\n\n`;
Object.entries(data).forEach(([key, projects]) => {
output += `export const ${key} = [\n`;
projects.forEach((project, index) => {
const jsonStr = JSON.stringify(project, null, 2);
const indentedStr = jsonStr.split('\n').map(line => ' ' + line).join('\n');
output += indentedStr;
if (index < projects.length - 1) {
output += ',\n';
}
});
output += '\n]\n\n';
});
const outputPath = path.join(__dirname, '../src/components/jetson/config.auto.js');
fs.writeFileSync(outputPath, output);
console.log(`Generated config.auto.js at ${outputPath}`);
console.log(`Total projects extracted: ${Object.values(data).reduce((sum, arr) => sum + arr.length, 0)}`);
}
// 执行提取和生成
if (require.main === module) {
console.log('Extracting Jetson project data...');
extractData();
generateConfig();
console.log('Done!');
}
module.exports = { extractData, generateConfig };