Skip to content

Commit 5e3fca3

Browse files
committed
feat: add robots.txt
1 parent a45d933 commit 5e3fca3

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import * as fs from 'node:fs/promises';
2+
import fg from 'fast-glob';
3+
import matter from 'gray-matter';
4+
import { i18n } from '@/lib/i18n';
5+
6+
export const revalidate = false;
7+
8+
// 获取域名配置
9+
function getDomain(): string {
10+
return process.env.FASTGPT_HOME_DOMAIN || 'https://fastgpt.io';
11+
}
12+
13+
// 将文件路径转换为URL路径
14+
function filePathToUrl(filePath: string, defaultLanguage: string): string {
15+
// 移除 ./content/docs/ 前缀
16+
let urlPath = filePath.replace('./content/docs/', '');
17+
18+
// 确定基础路径
19+
const basePath = defaultLanguage === 'zh-CN' ? '/docs' : '/en/docs';
20+
21+
// 如果是英文文件,移除 .en 后缀
22+
if (defaultLanguage !== 'zh-CN' && urlPath.endsWith('.en.mdx')) {
23+
urlPath = urlPath.replace('.en.mdx', '');
24+
} else if (urlPath.endsWith('.mdx')) {
25+
urlPath = urlPath.replace('.mdx', '');
26+
}
27+
28+
// 处理 index 文件
29+
if (urlPath.endsWith('/index')) {
30+
urlPath = urlPath.replace('/index', '');
31+
}
32+
33+
// 拼接完整路径
34+
return `${basePath}/${urlPath}`.replace(/\/\/+/g, '/');
35+
}
36+
37+
export async function GET(request: Request) {
38+
const defaultLanguage = i18n.defaultLanguage;
39+
const feconfigDomain = getDomain();
40+
const domain = feconfigDomain === 'https://fastgpt.io' ? 'https://doc.fastgpt.io' : 'https://doc.fastgpt.cn';
41+
42+
// 检查请求路径是否为 /en/robots
43+
const requestUrl = new URL(request.url);
44+
const isEnRobotsRoute = requestUrl.pathname === '/en/robots';
45+
46+
let globPattern;
47+
48+
if (isEnRobotsRoute) {
49+
// 如果是 /en/robots 路由,只选择 .en.mdx 文件
50+
globPattern = ['./content/docs/**/*.en.mdx'];
51+
} else if (defaultLanguage === 'zh-CN') {
52+
// 中文环境下的普通路由
53+
globPattern = ['./content/docs/**/*.mdx'];
54+
} else {
55+
// 英文环境下的普通路由
56+
globPattern = ['./content/docs/**/*.en.mdx'];
57+
}
58+
59+
const files = await fg(globPattern);
60+
61+
const urls = await Promise.all(
62+
files.map(async (file: string) => {
63+
const urlPath = filePathToUrl(file, defaultLanguage);
64+
return `${domain}${urlPath}`;
65+
})
66+
);
67+
68+
// 按URL排序
69+
urls.sort((a, b) => a.localeCompare(b));
70+
71+
// 生成HTML链接列表
72+
const html = `
73+
<html>
74+
<head>
75+
<title>FastGPT Documentation Links</title>
76+
<style>
77+
body { font-family: Arial, sans-serif; margin: 20px; }
78+
h1 { color: #333; }
79+
ul { list-style-type: none; padding: 0; }
80+
li { margin: 10px 0; }
81+
a { color: #0066cc; text-decoration: none; }
82+
a:hover { text-decoration: underline; }
83+
</style>
84+
</head>
85+
<body>
86+
<h1>Documentation Links</h1>
87+
<ul>
88+
${urls.map(url => `<li><a href="${url}">${url}</a></li>`).join('')}
89+
</ul>
90+
</body>
91+
</html>
92+
`;
93+
94+
return new Response(html, {
95+
headers: {
96+
'Content-Type': 'text/html',
97+
},
98+
});
99+
}

0 commit comments

Comments
 (0)