fix: 改为在文件末尾添加独立章节

- 启用时添加 "## 中文回复" 章节到文件末尾
- 禁用时移除整个章节
- 不再依赖标题匹配
This commit is contained in:
catlog22
2026-01-08 20:27:09 +08:00
parent 8f7ab3e268
commit faf32b5086

View File

@@ -918,14 +918,10 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
const isCodex = target === 'codex'; const isCodex = target === 'codex';
const targetDir = isCodex ? join(homedir(), '.codex') : join(homedir(), '.claude'); const targetDir = isCodex ? join(homedir(), '.codex') : join(homedir(), '.claude');
const targetFile = isCodex ? join(targetDir, 'AGENTS.md') : join(targetDir, 'CLAUDE.md'); const targetFile = isCodex ? join(targetDir, 'AGENTS.md') : join(targetDir, 'CLAUDE.md');
const headerText = isCodex ? '# Codex Code Guidelines\n\n' : '# Claude Instructions\n\n';
// Match common header patterns for both tools
const headerPattern = isCodex
? /^#\s*(Codex\s*(Code\s*)?(Guidelines|Instructions))\n\n?/i
: /^#\s*Claude\s*Instructions\n\n?/i;
const chineseRefLine = `- **中文回复准则**: @${guidelinesRef}`; const chineseRefLine = `- **中文回复准则**: @${guidelinesRef}`;
const chineseRefPattern = /^- \*\*中文回复准则\*\*:.*chinese-response\.md.*$/gm; const chineseRefPattern = /^- \*\*中文回复准则\*\*:.*chinese-response\.md.*$/gm;
const chineseSectionPattern = /\n*## 中文回复\n+- \*\*中文回复准则\*\*:.*chinese-response\.md.*\n*/gm;
// Ensure target directory exists // Ensure target directory exists
if (!existsSync(targetDir)) { if (!existsSync(targetDir)) {
@@ -936,7 +932,8 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
if (existsSync(targetFile)) { if (existsSync(targetFile)) {
content = readFileSync(targetFile, 'utf8'); content = readFileSync(targetFile, 'utf8');
} else { } else {
// Create new file with header // Create new file with minimal header
const headerText = isCodex ? '# Codex Code Guidelines\n\n' : '# Claude Instructions\n\n';
content = headerText; content = headerText;
} }
@@ -946,18 +943,12 @@ export async function handleClaudeRoutes(ctx: RouteContext): Promise<boolean> {
return { success: true, message: 'Already enabled' }; return { success: true, message: 'Already enabled' };
} }
// Add reference after the header line or at the beginning // Add new section at the end of file
const headerMatch = content.match(headerPattern); const newSection = `\n## 中文回复\n\n${chineseRefLine}\n`;
if (headerMatch) { content = content.trimEnd() + '\n' + newSection;
const insertPosition = headerMatch[0].length;
content = content.slice(0, insertPosition) + chineseRefLine + '\n' + content.slice(insertPosition);
} else {
// Add header and reference
content = headerText + chineseRefLine + '\n' + content;
}
} else { } else {
// Remove reference // Remove the entire section
content = content.replace(chineseRefPattern, '').replace(/\n{3,}/g, '\n\n').trim(); content = content.replace(chineseSectionPattern, '\n').replace(/\n{3,}/g, '\n\n').trim();
if (content) content += '\n'; if (content) content += '\n';
} }