Skip to content

Commit

Permalink
fix(src): 优化代码以处理结尾的三个反引号
Browse files Browse the repository at this point in the history
- 修改了 translateDirectory 和 main 函数中处理 modifiedContent 结尾为 '```' 的逻辑
- 新增 endIndex 变量来查找最后一个 '```' 的位置
- 添加了对 endIndex 是否为 -1 的判断,避免潜在的错误
- 移除了不必要的 trim() 调用,简化了代码逻辑
  • Loading branch information
h7ml committed Dec 20, 2024
1 parent a679fa7 commit 5575181
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,11 @@ async function translateDirectory(
}

if (modifiedContent.endsWith('```')) {
const startOfLastLine = modifiedContent.lastIndexOf('\n');
modifiedContent = modifiedContent.slice(0, startOfLastLine).trim();
const endIndex = modifiedContent.lastIndexOf('```');
if (endIndex !== -1) {
modifiedContent = modifiedContent.slice(0, endIndex);
}
}

// 根据相对路径生成输出文件路径
const outputFileName = rename
? path.join(
Expand Down Expand Up @@ -430,8 +431,10 @@ async function main() {
}

if (modifiedContent.endsWith('```')) {
const startOfLastLine = modifiedContent.lastIndexOf('\n');
modifiedContent = modifiedContent.slice(0, startOfLastLine).trim();
const endIndex = modifiedContent.lastIndexOf('```');
if (endIndex !== -1) {
modifiedContent = modifiedContent.slice(0, endIndex);
}
}
writeMarkdownFile(argv.output, modifiedContent);
console.log(`翻译 ${argv.input} 完成。输出已保存到 ${argv.output}`);
Expand Down

0 comments on commit 5575181

Please sign in to comment.