mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-15 07:31:19 +00:00

* add new doc (#5175) Co-authored-by: dreamer6680 <146868355@qq.com> * Test docs (#5235) * fix: change the page of doc * chore: add new dependencies, update global styles/layout, optimize docs, add Feishu & GitHub icons, update API examples * fix: docs/index 404 not found * Update environment variable names, optimize styles, add new API routes, fix component styles, adjust documentation, and update GitHub and Feishu icons * update readme * feat: add a linkfastgpt compontent * feat: update new doc * fix:remove unuse page and redirect homepage to docs (#5288) * fix:remove some unuse doc * fix: redirect homepage to doc * git ignore * fix:navbar to index (#5295) * sidbar * fix: navtab unlight (#5298) * doc --------- Co-authored-by: dreamer6680 <1468683855@qq.com> Co-authored-by: dreamer6680 <146868355@qq.com>
93 lines
2.7 KiB
JavaScript
93 lines
2.7 KiB
JavaScript
import fs from 'fs-extra';
|
||
import path from 'path';
|
||
|
||
// 从 mdx 文件中读取 weight
|
||
async function getWeightFromFile(filePath) {
|
||
const content = await fs.readFile(filePath, 'utf-8');
|
||
const weightMatch = content.match(/weight:\s*(\d+)/);
|
||
return weightMatch ? parseInt(weightMatch[1], 10) : 0;
|
||
}
|
||
|
||
// 从 meta.json 中读取最小 weight(用于子目录)
|
||
async function getWeightFromMeta(dir) {
|
||
const metaPath = path.join(dir, 'meta.json');
|
||
if (!(await fs.pathExists(metaPath))) return Infinity;
|
||
|
||
try {
|
||
const meta = await fs.readJson(metaPath);
|
||
const pages = meta.pages || [];
|
||
let minWeight = Infinity;
|
||
|
||
for (const pageName of pages) {
|
||
const mdxPath = path.join(dir, `${pageName}.mdx`);
|
||
if (await fs.pathExists(mdxPath)) {
|
||
const w = await getWeightFromFile(mdxPath);
|
||
if (w < minWeight) minWeight = w;
|
||
}
|
||
}
|
||
return minWeight === Infinity ? 0 : minWeight;
|
||
} catch {
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
// 主函数,返回当前目录的最小 weight
|
||
async function generateMetaRecursive(dir) {
|
||
const entries = await fs.readdir(dir, { withFileTypes: true });
|
||
const items = [];
|
||
|
||
for (const entry of entries) {
|
||
const fullPath = path.join(dir, entry.name);
|
||
|
||
if (entry.isDirectory()) {
|
||
const subWeight = await generateMetaRecursive(fullPath);
|
||
items.push({ name: entry.name, weight: subWeight });
|
||
} else if (
|
||
entry.isFile() &&
|
||
entry.name.endsWith('.mdx') &&
|
||
!entry.name.endsWith('.en.mdx')
|
||
) {
|
||
const nameWithoutExt = entry.name.replace(/\.mdx$/, '');
|
||
const weight = await getWeightFromFile(fullPath);
|
||
items.push({ name: nameWithoutExt, weight });
|
||
}
|
||
}
|
||
|
||
// 排序 pages
|
||
items.sort((a, b) => a.weight - b.weight);
|
||
const pages = items.map((item) => item.name);
|
||
|
||
// 读取或创建 meta.json
|
||
const metaPath = path.join(dir, 'meta.json');
|
||
let meta = {
|
||
title: 'FastGPT',
|
||
description: 'FastGPT Docs',
|
||
};
|
||
|
||
if (await fs.pathExists(metaPath)) {
|
||
try {
|
||
meta = await fs.readJson(metaPath);
|
||
} catch {
|
||
console.warn(`⚠️ Failed to parse existing meta.json at ${metaPath}, using defaults.`);
|
||
}
|
||
}
|
||
|
||
meta.pages = pages;
|
||
|
||
// 写入 meta.json,格式化为一行的 pages
|
||
const jsonString = JSON.stringify(meta, null, 2);
|
||
const oneLinePages = `"pages": ${JSON.stringify(pages)}`;
|
||
const finalJson = jsonString.replace(/"pages": \[[\s\S]*?\]/, oneLinePages);
|
||
await fs.writeFile(metaPath, finalJson, 'utf-8');
|
||
console.log(`✅ Updated meta.json in ${dir}`);
|
||
|
||
return items.length > 0 ? items[0].weight : 0;
|
||
}
|
||
|
||
// 启动
|
||
const targetDir = './content/docs/introduction/development/upgrading';
|
||
|
||
generateMetaRecursive(targetDir)
|
||
.then(() => console.log('🎉 All meta.json files generated/updated!'))
|
||
.catch((err) => console.error(err));
|