From f47c0515c9c878fc812240a55e46c824f6436aa7 Mon Sep 17 00:00:00 2001 From: dnjin Date: Sun, 2 Aug 2026 21:20:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=20issuse=203?= =?UTF-8?q?2=2033=20=E6=8F=90=E5=88=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/PULL_REQUEST_TEMPLATE.md | 23 +++++++++++++++++ apps/vscode-extension/src/atcoder.ts | 24 ++++++++++++++--- apps/vscode-extension/src/tools/deepl.ts | 33 +++++++++++++++++------- packages/webview/src/WebviewApp.tsx | 11 ++++++++ packages/webview/src/types.ts | 1 + 5 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..170f36e --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,23 @@ +## 关联 Issue + +## 变更概述 + +## 变更类型 + +- [ ] fix Bug +- [ ] Feature +- [ ] UI / Style +- [ ] Refactor +- [ ] Documentation + +## 涉及文件 + +- [ ] 前端 + +``` +``` + +- [ ] 后端 + +``` +``` \ No newline at end of file diff --git a/apps/vscode-extension/src/atcoder.ts b/apps/vscode-extension/src/atcoder.ts index f99358b..81318a5 100644 --- a/apps/vscode-extension/src/atcoder.ts +++ b/apps/vscode-extension/src/atcoder.ts @@ -17,6 +17,7 @@ export interface AtCoderProblem { inputFormat: string; outputFormat: string; samples: SampleCase[]; + sampleUrl?: string; } function decodeEntities(text: string): string { @@ -129,9 +130,22 @@ function extractSection(html: string, headings: string[]): string { } function getLangContent(html: string, lang: "en" | "ja"): string { - const pattern = `]*\\sclass="[^"]*\\blang-${lang}\\b[^"]*"[^>]*>([\\s\\S]*?)<\\/span>`; - const match = html.match(new RegExp(pattern, "i")); - return match ? match[1] : html; + const startMatch = html.match( + new RegExp(`]*\\sclass="[^"]*\\blang-${lang}\\b[^"]*"[^>]*>`, "i") + ); + if (!startMatch || startMatch.index === undefined) return html; + const start = startMatch.index + startMatch[0].length; + const now = /<\/?span\b[^>]*>/gi; + let dep = 1; + now.lastIndex = start; + let m: RegExpExecArray | null; + for (; (m = now.exec(html)) !== null;) { + if (m[0].startsWith("]*>\s*Sample\s*(Input|Output)\s*(\d+)\s*<\/h3>/gi)); @@ -190,6 +207,7 @@ export function parseProblemPage(html: string, url: string): AtCoderProblem { inputFormat, outputFormat, samples, + sampleUrl, }; } diff --git a/apps/vscode-extension/src/tools/deepl.ts b/apps/vscode-extension/src/tools/deepl.ts index 7ce2e3c..5f80550 100644 --- a/apps/vscode-extension/src/tools/deepl.ts +++ b/apps/vscode-extension/src/tools/deepl.ts @@ -33,6 +33,9 @@ export async function translateTextFree(text: string, lang: string): Promise { + let settled = false; + const settle = (fn: () => void) => { if (!settled) { settled = true; fn(); } }; + const req = https.request( { hostname: "www2.deepl.com", @@ -40,29 +43,41 @@ export async function translateTextFree(text: string, lang: string): Promise { let data = ""; res.on("data", (chunk) => (data += chunk)); res.on("end", () => { - try { - const json = JSON.parse(data); - if (json?.result?.texts?.[0]?.text) { - resolve(json.result.texts[0].text); - } else { + settle(() => { + if (res.statusCode && res.statusCode >= 400) { + reject(new Error(res.statusCode === 429 ? "翻译请求过于频繁,请稍后再试" : `翻译接口错误 (${res.statusCode})`)); + return; + } + try { + const json = JSON.parse(data); + if (json?.result?.texts?.[0]?.text) { + resolve(json.result.texts[0].text); + } else { + reject(new Error("翻译接口返回异常")); + } + } catch { reject(new Error("翻译接口返回异常")); } - } catch { - reject(new Error("翻译接口返回异常")); - } + }); }); } ); - req.on("error", () => reject(new Error("翻译请求失败"))); + + req.setTimeout(20000, () => req.destroy(new Error("翻译请求超时"))); + req.on("error", (err: Error) => + settle(() => reject(new Error(err.message === "翻译请求超时" ? "翻译请求超时" : `翻译请求失败: ${err.message}`))) + ); req.write(postData); req.end(); }); diff --git a/packages/webview/src/WebviewApp.tsx b/packages/webview/src/WebviewApp.tsx index 91959a2..da02959 100644 --- a/packages/webview/src/WebviewApp.tsx +++ b/packages/webview/src/WebviewApp.tsx @@ -698,6 +698,17 @@ const WebviewApp: React.FC = ({ )) + ) : problem.sampleUrl ? ( +
+
该题目没有内嵌样例,样例在外部链接中。
+ +
) : (
当前题目没有找到样例。
)} diff --git a/packages/webview/src/types.ts b/packages/webview/src/types.ts index 9ef0a18..5b9875b 100644 --- a/packages/webview/src/types.ts +++ b/packages/webview/src/types.ts @@ -13,6 +13,7 @@ export interface ContestProblem { inputFormat: string; outputFormat: string; samples: SampleCase[]; + sampleUrl?: string; } export interface SubmitResult {