Skip to content

Commit 4bc6f4b

Browse files
committed
feat: 优化 CLI 工具的输出文件处理逻辑
- 添加默认输出文件名功能,如果没有指定输出文件名,则使用输入文件名 - 校验输出文件名的有效性 - 优化代码块的处理,移除多余的代码块标记 - 更新 README 文档,明确输出文件的默认行为
1 parent 6d51a3b commit 4bc6f4b

File tree

5 files changed

+99
-75
lines changed

5 files changed

+99
-75
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
steps:
1818
- name: Checkout code
1919
uses: actions/checkout@v3
20+
with:
21+
ref: main
2022

2123
- name: Set up Node.js
2224
uses: actions/setup-node@v3

.github/workflows/release.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
uses: actions/checkout@v3
1414
with:
1515
fetch-depth: 0
16+
ref: main
1617

1718
- name: Setup pnpm
1819
uses: pnpm/action-setup@v2
@@ -76,14 +77,15 @@ jobs:
7677
git config advice.ignoredHook false
7778
VERSION=${GITHUB_REF#refs/tags/v}
7879
git commit -m "chore(release): update CHANGELOG for v$VERSION [skip ci]" -a || echo "No changes to commit"
79-
BRANCH=$(echo "${GITHUB_REF}" | sed 's@refs/tags/@@')
80-
git push origin HEAD:$BRANCH || echo "Push to repository failed"
8180
81+
git checkout main || git checkout -b main
82+
83+
git push origin main || echo "Push to repository failed"
8284
8385
- name: Upload Release Assets
8486
uses: softprops/action-gh-release@v1
8587
with:
8688
body_path: CHANGELOG.md
8789
env:
8890
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
89-
continue-on-error: true
91+
continue-on-error: true

README-zh.md

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
以下是“ai-markdown-translator”文档的修订和合并版本,其中结合了`npx``./ai-markdown-translator`示例
1+
以下是经过修订和整合的 `ai-markdown-translator` 文档,其中结合了 `npx``./ai-markdown-translator` 的示例
22

33
---
44

55
# ai-markdown-translator
66

77
<div>
8-
<a href="https://www.npmjs.org/package/ai-markdown-translator"><img src="https://img.shields.io/npm/v/ai-markdown-translator.svg?style=flat" alt="NPM 版本"></a>
8+
<a href="https://www.npmjs.org/package/ai-markdown-translator"><img src="https://img.shields.io/npm/v/ai-markdown-translator.svg?style=flat" alt="NPM版本"></a>
99
<a href="https://github.com/h7ml/ai-markdown-translator/actions/workflows/ci.yml"><img src="https://github.com/h7ml/ai-markdown-translator/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
10-
<a href="https://github.com/h7ml/ai-markdown-translator/actions/workflows/release.yml"><img src="https://github.com/h7ml/ai-markdown-translator/actions/workflows/release.yml/badge.svg" alt="发布"></a>
11-
<a href="https://www.npmjs.org/package/ai-markdown-translator"><img src="https://img.shields.io/npm/dw/ai-markdown-translator" alt="NPM 下载量"></a>
12-
<a href="https://www.npmjs.org/package/ai-markdown-translator"><img src="https://img.shields.io/npm/l/ai-markdown-translator" alt="NPM 许可证"></a>
13-
<a href="https://github.com/h7ml/ai-markdown-translator/stargazers"><img src="https://img.shields.io/github/stars/h7ml/ai-markdown-translator.svg" alt="GitHub 星标"></a>
14-
<a href="https://github.com/h7ml/ai-markdown-translator/issues"><img src="https://img.shields.io/github/issues/h7ml/ai-markdown-translator.svg" alt="GitHub 问题"></a>
15-
<a href="https://github.com/h7ml/ai-markdown-translator/network/members"><img src="https://img.shields.io/github/forks/h7ml/ai-markdown-translator.svg" alt="GitHub 分叉"></a>
16-
<a href="https://github.com/h7ml/ai-markdown-translator/graphs/contributors"><img src="https://img.shields.io/github/contributors/h7ml/ai-markdown-translator.svg" alt="GitHub 贡献者"></a>
10+
<a href="https://github.com/h7ml/ai-markdown-translator/actions/workflows/release.yml"><img src="https://github.com/h7ml/ai-markdown-translator/actions/workflows/release.yml/badge.svg" alt="Release"></a>
11+
<a href="https://www.npmjs.org/package/ai-markdown-translator"><img src="https://img.shields.io/npm/dw/ai-markdown-translator" alt="NPM下载"></a>
12+
<a href="https://www.npmjs.org/package/ai-markdown-translator"><img src="https://img.shields.io/npm/l/ai-markdown-translator" alt="NPM许可证"></a>
13+
<a href="https://github.com/h7ml/ai-markdown-translator/stargazers"><img src="https://img.shields.io/github/stars/h7ml/ai-markdown-translator.svg" alt="GitHub Stars"></a>
14+
<a href="https://github.com/h7ml/ai-markdown-translator/issues"><img src="https://img.shields.io/github/issues/h7ml/ai-markdown-translator.svg" alt="GitHub问题"></a>
15+
<a href="https://github.com/h7ml/ai-markdown-translator/network/members"><img src="https://img.shields.io/github/forks/h7ml/ai-markdown-translator.svg" alt="GitHub分支"></a>
16+
<a href="https://github.com/h7ml/ai-markdown-translator/graphs/contributors"><img src="https://img.shields.io/github/contributors/h7ml/ai-markdown-translator.svg" alt="GitHub贡献者"></a>
1717
</div>
1818

1919
[English](README.md) | [中文](README-zh.md)
2020

21-
`ai-markdown-translator` 是一个命令行工具,使用 OpenAI 的语言模型将 Markdown 文件从一种语言翻译成另一种语言。它在翻译内容的同时保持 Markdown 语法不变
21+
`ai-markdown-translator` 是一个命令行工具,能利用OpenAI的语言模型将Markdown文件从一种语言翻译成另一种语言。它在翻译内容的同时保持了Markdown语法的完整性
2222

23-
## 功能
23+
## 特性
2424

25-
- 将 Markdown 文件翻译为 OpenAI 模型支持的任何语言
26-
- 在翻译过程中保持 Markdown 语法不变
25+
- 使用OpenAI的模型将Markdown文件翻译成任何受支持的语言
26+
- 在翻译过程中保持Markdown语法
2727
- 通过命令行参数或环境变量灵活配置。
2828

2929
## 先决条件
3030

31-
- Node.js(版本14或更高
32-
- npm(通常随 Node.js 一起提供
33-
- 一个 OpenAI API 密钥
31+
- Node.js(v14或更高版本
32+
- npm(通常与Node.js一起提供
33+
- OpenAI API密钥
3434

3535
## 安装
3636

37-
1. 克隆此存储库或下载源代码
38-
2. 在终端中进入项目目录
39-
3. 安装依赖项
37+
1. 克隆此仓库或下载源代码
38+
2. 在终端中导航到项目目录
39+
3. 安装依赖
4040

4141
```bash
4242
npm install
@@ -50,31 +50,31 @@ npm run build
5050

5151
## 脚本
5252

53-
- `build`: 将 TypeScript 文件编译为 JavaScript
54-
- `start`: 使用 Node.js 运行编译后的 JavaScript
55-
- `lint`: 运行 ESLint 检查 TypeScript 文件中的代码质量问题。
56-
- `lint:fix`: 自动修复 TypeScript 文件中的 lint 问题
57-
- `format`: 使用 Prettier 格式化 `src` 目录中各种文件类型的代码
58-
- `format:check`: 检查代码格式而不进行更改
59-
- `postbuild`: 使编译后的 `index.js` 文件可执行。
60-
- `changelog`: 基于惯例提交生成变更日志
61-
- `version`: 更新变更日志并在版本控制时准备提交
62-
- `test`: 构建项目并运行测试。
53+
- `build`:将TypeScript文件编译成JavaScript
54+
- `start`:使用Node.js运行编译的JavaScript
55+
- `lint`:运行ESLint以检查 TypeScript 文件中的代码质量问题。
56+
- `lint:fix`自动修复 TypeScript 文件中的linting问题
57+
- `format`:使用Prettier格式化`src`目录中多种文件类型的代码
58+
- `format:check`:检查代码格式,但不进行修改`src`目录中多种文件类型的代码
59+
- `postbuild`:使编译的`index.js`文件可执行。
60+
- `changelog`:基于常规提交生成更新日志
61+
- `version`:在版本控制时更新更新日志并将其暂存为提交
62+
- `test`构建项目并运行测试。
6363

64-
## 用法
64+
## 使用方法
6565

66-
可以使用 Node.js、`npx` 或作为独立可执行文件(如果已打包)运行 CLI 工具
66+
你可以使用Node.js、`npx`或者作为独立的可执行文件运行CLI工具(如果你打包过它)
6767

68-
### 使用 Node.js
68+
### 使用Node.js
6969

7070
```bash
71-
node dist/index.js --input <input-file> --output <output-file> --language <target-language> [options]
71+
node dist/index.js --input <输入文件> --output <输出文件> --language <目标语言> [选项]
7272
```
7373

74-
### 使用 npx
74+
### 使用npx
7575

7676
```bash
77-
npx ai-markdown-translator -i <input-file> -o <output-file> -l <target-language> [options]
77+
npx ai-markdown-translator -i <输入文件> -o <输出文件> -l <目标语言> [选项]
7878
```
7979

8080
例如:
@@ -83,93 +83,93 @@ npx ai-markdown-translator -i <input-file> -o <output-file> -l <target-language>
8383
npx ai-markdown-translator -u https://gitee.com/h7ml/ai-markdown-translator/raw/main/README.md -o output.md -l "Italian"
8484
```
8585

86-
### 使用独立可执行文件
86+
### 使用独立的可执行文件
8787

8888
```bash
89-
./ai-markdown-translator --input <input-file> --output <output-file> --language <target-language> [options]
89+
./ai-markdown-translator --input <输入文件> --output <输出文件> --language <目标语言> [选项]
9090
```
9191

9292
## 选项
9393

94-
- `--input`, `-i`: 输入 Markdown 文件`--url` 的替代选项)。
95-
- `--url`, `-u`: 要翻译的 Markdown 文件的 URL`--input` 的替代选项)。
96-
- `--output`, `-o`: 输出 Markdown 文件(必需)。
97-
- `--language`, `-l`: 翻译目标语言(必需)。
98-
- `--openai-url`: OpenAI API URL默认:使用 `OPENAI_URL` 环境变量)。
99-
- `--api-key`: OpenAI API 密钥(默认:使用 `API_KEY` 环境变量)。
100-
- `--model`: 要使用的 OpenAI 模型(默认:使用 `MODEL` 环境变量或 `gpt-3.5-turbo`)。
101-
- `--help`, `-h`: 显示帮助。
102-
- `--show-version`, `-v`: 显示版本。
94+
- `--input`, `-i`:输入Markdown文件`--url`的替代选项)。
95+
- `--url`, `-u`:要翻译的Markdown文件的URL`--input`的替代选项)。
96+
- `--output`, `-o`:输出Markdown文件(如果未提供,则默认为输入文件名)。
97+
- `--language`, `-l`:翻译的目标语言(必需)。
98+
- `--openai-url`OpenAI API URL (默认:使用 `OPENAI_URL` 环境变量)。
99+
- `--api-key`OpenAI API密钥 (默认:使用 `API_KEY` 环境变量)。
100+
- `--model`:要使用的OpenAI模型 (默认:使用 `MODEL` 环境变量或 `gpt-3.5-turbo`)。
101+
- `--help`, `-h`显示帮助。
102+
- `--show-version`, `-v`显示版本。
103103

104-
> 注意:`--input``--url` 是互斥的你必须提供其中之一。
104+
> 注意:`--input``--url` 是互斥的你必须提供其中之一。
105105
106106
## 环境变量
107107

108-
可以设置以下环境变量,而不是将它们作为命令行参数传递:
108+
你可以设置以下环境变量,而不是将它们作为命令行参数传递:
109109

110-
- `OPENAI_URL`: OpenAI API 的 URL
111-
- `API_KEY`: 你的 OpenAI API 密钥
112-
- `MODEL`: 要使用的 OpenAI 模型(例如,`'gpt-3.5-turbo'`)。
110+
- `OPENAI_URL`OpenAI API的URL
111+
- `API_KEY`:你的OpenAI API密钥
112+
- `MODEL`:要使用的OpenAI模型(例如,`'gpt-3.5-turbo'`)。
113113

114-
可以在项目根目录中的 `.env` 文件中设置这些变量,或者在你的 shell 中导出它们
114+
你可以在项目根目录中的`.env`文件中设置这些变量,或者在你的shell中导出它们
115115

116116
## 示例
117117

118-
1. **将 Markdown 文件从英语翻译为西班牙语**
118+
1. **将Markdown文件从英语翻译成西班牙语**
119119

120120
```bash
121121
npx ai-markdown-translator -i english.md -o spanish.md -l "Spanish"
122122
```
123123

124-
2. **使用特定的 OpenAI 模型进行翻译**
124+
2. **使用特定的OpenAI模型进行翻译**
125125

126126
```bash
127127
npx ai-markdown-translator -i input.md -o output.md -l "French" --model "gpt-4"
128128
```
129129

130-
3. **使用自定义 OpenAI URL 和 API 密钥进行翻译**
130+
3. **使用自定义的OpenAI URL和API密钥进行翻译**
131131

132132
```bash
133133
npx ai-markdown-translator -i input.md -o output.md -l "German" --openai-url "https://api.302.ai/v1/chat/completions" --api-key "sk-302-api-key"
134134
```
135135

136-
4. **翻译 URL 的 Markdown 内容**
136+
4. **翻译URL中的Markdown内容**
137137

138138
```bash
139139
npx ai-markdown-translator -u https://gitee.com/h7ml/ai-markdown-translator/raw/main/README.md -o output.md -l "Italian"
140140
```
141141

142142
## 许可证
143143

144-
[MIT License](LICENSE)
144+
[MIT许可证](LICENSE)
145145

146-
## Git 信息
146+
## Git信息
147147

148-
- **仓库**: [h7ml/ai-markdown-translator](https://github.com/h7ml/ai-markdown-translator)
149-
- **问题**: [报告问题](https://github.com/h7ml/ai-markdown-translator/issues)
148+
- **仓库**[h7ml/ai-markdown-translator](https://github.com/h7ml/ai-markdown-translator)
149+
- **问题**[报告问题](https://github.com/h7ml/ai-markdown-translator/issues)
150150

151151
## 版本信息
152152

153-
- **当前版本**: 1.0.6
154-
- **NPM 包**: [ai-markdown-translator](https://www.npmjs.com/package/ai-markdown-translator)
153+
- **当前版本**1.0.6
154+
- **NPM包**[ai-markdown-translator](https://www.npmjs.com/package/ai-markdown-translator)
155155

156-
## CI 信息
156+
## CI信息
157157

158-
此项目使用 GitHub Actions 进行持续集成。CI 工作流包括
158+
此项目使用GitHub Actions进行持续集成。CI工作流包括
159159

160-
- 使用 ESLint 检查代码
160+
- 使用ESLint对代码进行linting
161161
- 运行测试(如果适用)
162162
- 构建项目
163-
- 缓存依赖项以加快构建速度
163+
- 缓存依赖以加速构建
164164

165165
## 贡献
166166

167-
欢迎贡献!请随时提交拉取请求
167+
欢迎贡献!请随时提交Pull Request
168168

169169
## 支持
170170

171-
如果遇到任何问题或有任何问题,请在此存储库中提出问题
171+
如果你遇到任何问题或有任何问题,请在这个仓库中打开一个issue
172172

173173
---
174174

175-
此版本结合了 `npx` 和独立可执行文件`./ai-markdown-translator`)的使用示例,让我知道您是否希望做进一步的调整
175+
这个版本将 `npx` 和独立可执行文件 (`./ai-markdown-translator`) 的使用示例合并成了一个连贯的部分。如果你想进行任何进一步的调整,请告诉我

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ npx ai-markdown-translator -u https://gitee.com/h7ml/ai-markdown-translator/raw/
9393

9494
- `--input`, `-i`: Input Markdown file (alternative to `--url`).
9595
- `--url`, `-u`: URL of a Markdown file to translate (alternative to `--input`).
96-
- `--output`, `-o`: Output Markdown file (required).
96+
- `--output`, `-o`: Output Markdown file (if not provided, defaults to the input file name).
9797
- `--language`, `-l`: Target language for translation (required).
9898
- `--openai-url`: OpenAI API URL (default: uses `OPENAI_URL` environment variable).
9999
- `--api-key`: OpenAI API Key (default: uses `API_KEY` environment variable).

src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ async function main() {
233233
alias: 'o',
234234
description: '输出的Markdown文件',
235235
type: 'string',
236-
demandOption: true,
237236
})
238237
.option('language', {
239238
alias: 'l',
@@ -295,6 +294,14 @@ async function main() {
295294
markdownContent = markdownContent.slice(0, -3).trim();
296295
}
297296

297+
if (!argv.output && argv.input) {
298+
argv.output = argv.input;
299+
}
300+
301+
if (typeof argv.output !== 'string') {
302+
throw new Error('输出文件名无效。');
303+
}
304+
298305
const translatedContent = await translateText(
299306
markdownContent,
300307
argv.language,
@@ -304,7 +311,20 @@ async function main() {
304311
);
305312

306313
if (translatedContent) {
307-
writeMarkdownFile(argv.output, translatedContent);
314+
let modifiedContent = translatedContent; // Create a mutable variable
315+
// 校验下第一行是否含有``` 和 最后是否含有``` 如果是的话 那么删掉第一行和最后行
316+
if (modifiedContent.startsWith('```')) {
317+
const endOfFirstLine = modifiedContent.indexOf('\n');
318+
// 删除整行代码块标记(包括```和可能的语言标识符)
319+
modifiedContent = modifiedContent.slice(endOfFirstLine + 1).trim();
320+
}
321+
322+
if (modifiedContent.endsWith('```')) {
323+
const startOfLastLine = modifiedContent.lastIndexOf('\n');
324+
// 删除整行代码块标记
325+
modifiedContent = modifiedContent.slice(0, startOfLastLine).trim();
326+
}
327+
writeMarkdownFile(argv.output, modifiedContent);
308328
console.log(`翻译完成。输出已保存到 ${argv.output}。`);
309329
} else {
310330
console.log('翻译失败。');

0 commit comments

Comments
 (0)