2024-12-12 16:47:01 +00:00
|
|
|
|
// vitepress-plugin-github-issues.mts
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import type { Plugin } from 'vitepress';
|
|
|
|
|
|
|
|
|
|
interface ReplaceRule {
|
|
|
|
|
baseUrl: string; // 要匹配的基地址
|
|
|
|
|
targetUrl: string; // 替换后的目标地址
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface GitHubIssuesPluginOptions {
|
2024-12-13 08:23:27 +00:00
|
|
|
|
repo: string; // GitHub repository info in the format 'owner/repo'
|
|
|
|
|
token: string;
|
|
|
|
|
replaceRules: ReplaceRule[];
|
|
|
|
|
githubProxy: string;
|
2024-12-12 16:47:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchAllIssues(repo: string, token: string): Promise<any[]> {
|
2024-12-13 08:23:27 +00:00
|
|
|
|
const maxRetries = 3; // 最大重试次数
|
|
|
|
|
let attempt = 0;
|
|
|
|
|
const allIssues: any[] = [];
|
|
|
|
|
let page = 1;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
while (attempt < maxRetries) {
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.get(`https://api.github.com/repos/${repo}/issues`, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `token ${token}`
|
|
|
|
|
},
|
|
|
|
|
params: {
|
|
|
|
|
page: page,
|
|
|
|
|
per_page: 100 // 每页最多返回100条记录
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 如果没有更多问题了,退出循环
|
|
|
|
|
if (response.data.length === 0) {
|
|
|
|
|
return allIssues;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
allIssues.push(...response.data);
|
|
|
|
|
page++; // 下一页
|
|
|
|
|
attempt = 0; // 重置尝试次数
|
|
|
|
|
break; // 退出尝试循环
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error.response && error.response.status === 503) {
|
|
|
|
|
console.error(`服务不可用, 正在重试...`);
|
|
|
|
|
attempt++;
|
|
|
|
|
const waitTime = Math.pow(2, attempt) * 1000; // 指数等待时间
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
|
|
|
} else {
|
|
|
|
|
throw error; // 如果不是503错误,抛出错误并停止重试
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (attempt >= maxRetries) {
|
|
|
|
|
throw new Error('最大重试次数已达,请检查 API 状态(可能是请求过于频繁)');
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-12 16:47:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function fetchIssueComments(repo: string, issueNumber: number, token: string): Promise<any[]> {
|
|
|
|
|
const maxRetries = 3;
|
|
|
|
|
let attempt = 0;
|
|
|
|
|
const allComments: any[] = [];
|
|
|
|
|
let page = 1;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
while (attempt < maxRetries) {
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.get(
|
|
|
|
|
`https://api.github.com/repos/${repo}/issues/${issueNumber}/comments`,
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `token ${token}`,
|
|
|
|
|
},
|
|
|
|
|
params: {
|
|
|
|
|
page: page,
|
|
|
|
|
per_page: 100,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (response.data.length === 0) {
|
|
|
|
|
return allComments; // 如果没有更多评论,退出循环
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
allComments.push(...response.data);
|
|
|
|
|
page++;
|
|
|
|
|
attempt = 0;
|
|
|
|
|
break; // 成功获取评论数据,退出重试
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
if (error.response && error.response.status === 503) {
|
|
|
|
|
console.error('服务不可用,正在重试...');
|
|
|
|
|
attempt++;
|
|
|
|
|
const waitTime = Math.pow(2, attempt) * 1000;
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
|
|
|
} else {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (attempt >= maxRetries) {
|
|
|
|
|
throw new Error('最大重试次数已达,请检查 API 状态(可能是请求过于频繁)');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clearDirectory(dir: string) {
|
2024-12-13 08:23:27 +00:00
|
|
|
|
if (fs.existsSync(dir)) {
|
|
|
|
|
fs.readdirSync(dir).forEach((file) => {
|
|
|
|
|
const filePath = path.join(dir, file);
|
|
|
|
|
if (fs.lstatSync(filePath).isDirectory()) {
|
|
|
|
|
clearDirectory(filePath); // 递归清理子目录
|
|
|
|
|
fs.rmdirSync(filePath);
|
|
|
|
|
} else {
|
|
|
|
|
fs.unlinkSync(filePath); // 删除文件
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
console.log(`Cleared directory: ${dir}`);
|
|
|
|
|
}
|
2024-12-12 16:47:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function copyReadmeFile(source: string, destination: string) {
|
2024-12-13 08:23:27 +00:00
|
|
|
|
if (fs.existsSync(source)) {
|
|
|
|
|
fs.copyFileSync(source, destination);
|
|
|
|
|
console.log(`Copied README.md from ${source} to ${destination}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.error(`README.md not found at ${source}`);
|
|
|
|
|
}
|
2024-12-12 16:47:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function GitHubIssuesPlugin(options: GitHubIssuesPluginOptions): Plugin {
|
2024-12-13 08:23:27 +00:00
|
|
|
|
const { repo, token, replaceRules, githubProxy } = options;
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
return {
|
|
|
|
|
name: 'vitepress-plugin-github-issues',
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
async buildStart() {
|
|
|
|
|
try {
|
|
|
|
|
const issues = await fetchAllIssues(repo, token);
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
console.log(`Fetched ${issues.length} issues from GitHub`); // Log the number of issues fetched
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
const docsDir = path.join(process.cwd(), 'issues');
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
// 清空 issues 目录
|
|
|
|
|
clearDirectory(docsDir);
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
// Create a directory to store markdown files if it doesn't exist
|
|
|
|
|
if (!fs.existsSync(docsDir)) {
|
|
|
|
|
fs.mkdirSync(docsDir);
|
|
|
|
|
console.log(`Created docs directory: ${docsDir}`);
|
|
|
|
|
}
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
// 拷贝 ../README.md 文件到当前目录
|
|
|
|
|
const readmeSource = path.join(process.cwd(), '../README.md');
|
|
|
|
|
const readmeDestination = path.join(docsDir, 'index.md');
|
|
|
|
|
copyReadmeFile(readmeSource, readmeDestination);
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
for (const issue of issues) {
|
|
|
|
|
// 仅处理包含 "文档" 标签的 issue
|
|
|
|
|
const hasDocumentLabel = issue.labels.some(label => label.name === '文档');
|
|
|
|
|
if (hasDocumentLabel) {
|
|
|
|
|
const title = issue.title.replace(/[\/\\?%*:|"<>]/g, '-');
|
|
|
|
|
const fileName = `${issue.number}.md`;
|
2024-12-12 16:47:01 +00:00
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
// 获取评论数据
|
2024-12-12 16:47:01 +00:00
|
|
|
|
const comments = await fetchIssueComments(repo, issue.number, token);
|
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
let content =
|
|
|
|
|
`---
|
2024-12-12 16:47:01 +00:00
|
|
|
|
title: ${issue.title}
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# ${title}
|
|
|
|
|
|
|
|
|
|
${issue.body}
|
|
|
|
|
|
|
|
|
|
## 评论
|
|
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
2024-12-13 08:23:27 +00:00
|
|
|
|
// 插入评论
|
|
|
|
|
if (comments.length > 0) {
|
|
|
|
|
comments.forEach((comment, index) => {
|
|
|
|
|
content += `
|
2024-12-12 16:47:01 +00:00
|
|
|
|
### 评论 ${index + 1} - ${comment.user.login}
|
|
|
|
|
|
|
|
|
|
${comment.body}
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
`;
|
2024-12-13 08:23:27 +00:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
content += "没有评论。\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
replaceRules.forEach(({ baseUrl, targetUrl }) => {
|
|
|
|
|
// 将 baseUrl 转换为正则表达式,匹配后接的路径部分
|
|
|
|
|
const pattern = new RegExp(`${baseUrl.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')}(/\\d+)`, 'g');
|
|
|
|
|
// 替换为目标 URL
|
|
|
|
|
content = content.replace(pattern, `${targetUrl}$1.html`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
content = content.replace(
|
|
|
|
|
/(https:\/\/github\.com\/([^\/]+\/[^\/]+\/assets\/[^\s]+)/g,
|
|
|
|
|
(match, p1) => {
|
|
|
|
|
const proxyUrl = `${githubProxy}/${p1}`; // 使用传入的 base URL
|
|
|
|
|
return proxyUrl;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
content += `[链接到 GitHub Issue](${issue.html_url})\n`
|
|
|
|
|
|
|
|
|
|
const filePath = path.join(docsDir, fileName);
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(filePath, content, { encoding: 'utf8' });
|
|
|
|
|
console.log(`Created file: ${filePath}`); // Log each created file
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`Skipped issue: ${issue.title}`); // Log skipped issues
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`Successfully created markdown files from GitHub issues.`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching GitHub issues:', error);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
2024-12-12 16:47:01 +00:00
|
|
|
|
}
|
|
|
|
|
|