@alauda/doom 0.14.8 → 0.14.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cli/translate.d.ts +4 -2
- package/lib/cli/translate.js +28 -25
- package/lib/runtime/components/Overview.js +1 -1
- package/lib/types.d.ts +4 -0
- package/package.json +2 -2
package/lib/cli/translate.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Command } from 'commander';
|
|
2
|
+
import type { TranslateOptions } from '../types.js';
|
|
2
3
|
export interface I18nFrontmatter {
|
|
3
4
|
i18n?: {
|
|
4
5
|
title?: Record<string, string>;
|
|
@@ -8,13 +9,14 @@ export interface I18nFrontmatter {
|
|
|
8
9
|
sourceSHA?: string;
|
|
9
10
|
title?: string;
|
|
10
11
|
}
|
|
11
|
-
export
|
|
12
|
+
export interface InternalTranslateOptions extends TranslateOptions {
|
|
12
13
|
source: string;
|
|
13
14
|
sourceContent: string;
|
|
14
15
|
target: string;
|
|
15
16
|
targetContent?: string;
|
|
16
17
|
additionalPrompts?: string;
|
|
17
|
-
}
|
|
18
|
+
}
|
|
19
|
+
export declare const translate: ({ source, sourceContent, target, targetContent, systemPrompt, additionalPrompts, }: InternalTranslateOptions) => Promise<string>;
|
|
18
20
|
export interface TranslateCommandOptions {
|
|
19
21
|
source: string;
|
|
20
22
|
target: string;
|
package/lib/cli/translate.js
CHANGED
|
@@ -3,6 +3,7 @@ import fs from 'node:fs/promises';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { logger } from '@rspress/shared/logger';
|
|
5
5
|
import { Command } from 'commander';
|
|
6
|
+
import { render } from 'ejs';
|
|
6
7
|
import matter from 'gray-matter';
|
|
7
8
|
import { AzureOpenAI } from 'openai';
|
|
8
9
|
import { pRateLimit } from 'p-ratelimit';
|
|
@@ -16,28 +17,12 @@ const LANGUAGE_CODES = {
|
|
|
16
17
|
zh: '中文',
|
|
17
18
|
en: '英文',
|
|
18
19
|
};
|
|
19
|
-
|
|
20
|
-
export const translate = async ({ source, sourceContent, target, targetContent = '', additionalPrompts, }) => {
|
|
21
|
-
if (!openai) {
|
|
22
|
-
openai = new AzureOpenAI({
|
|
23
|
-
endpoint: process.env.AZURE_OPENAI_ENDPOINT ||
|
|
24
|
-
'https://apt-docs-openai.openai.azure.com',
|
|
25
|
-
apiKey: process.env.AZURE_OPENAI_API_KEY || 'doom',
|
|
26
|
-
apiVersion: process.env.OPENAI_API_VERSION || '2025-01-01-preview',
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
const sourceLang = LANGUAGE_CODES[source];
|
|
30
|
-
const targetLang = LANGUAGE_CODES[target];
|
|
31
|
-
const { choices } = await openai.beta.chat.completions.parse({
|
|
32
|
-
messages: [
|
|
33
|
-
{
|
|
34
|
-
role: 'system',
|
|
35
|
-
content: `
|
|
20
|
+
const DEFAULT_SYSTEM_PROMPT = `
|
|
36
21
|
## 角色
|
|
37
|
-
|
|
22
|
+
你是一位专业的技术文档工程师,擅长写作高质量的<%= targetLang %>技术分档。请你帮我准确地将以下<%= sourceLang %>翻译成<%= targetLang %>,风格与<%= targetLang %>技术文档保持一致。
|
|
38
23
|
|
|
39
24
|
## 规则
|
|
40
|
-
-
|
|
25
|
+
- 第一条消息为需要翻译的最新<%= sourceLang %>内容,第二条消息为之前翻译过的但内容可能过期的<%= targetLang %>内容,如果没有翻译过则为空
|
|
41
26
|
- 输入格式为 MDX 格式,输出格式也必须保留原始 MDX 格式,且不要额外包装在不必要的代码块中
|
|
42
27
|
- 文档中的资源链接不要翻译和替换
|
|
43
28
|
- MDX 组件中包含的内容需要翻译,MDX 组件参数的值不需要翻译,但以下这些特殊的 MDX 组件参数值需要翻译
|
|
@@ -50,18 +35,35 @@ export const translate = async ({ source, sourceContent, target, targetContent =
|
|
|
50
35
|
|
|
51
36
|
## 策略
|
|
52
37
|
分四步进行翻译工作,并打印每步的结果:
|
|
53
|
-
1.
|
|
38
|
+
1. 根据<%= sourceLang %>内容直译成<%= targetLang %>,保持原有格式,不要遗漏任何信息
|
|
54
39
|
2. 根据第一步直译的结果,指出其中存在的具体问题,要准确描述,不宜笼统的表示,也不需要增加原文不存在的内容或格式,包括不仅限于
|
|
55
|
-
-
|
|
40
|
+
- 不符合<%= targetLang %>表达习惯,明确指出不符合的地方
|
|
56
41
|
- 语句不通顺,指出位置,不需要给出修改意见,意译时修复
|
|
57
42
|
- 晦涩难懂,模棱两可,不易理解,可以尝试给出解释
|
|
58
|
-
3.
|
|
59
|
-
4.
|
|
43
|
+
3. 根据第一步直译的结果和第二步指出的问题,重新进行意译,保证内容的原意的基础上,使其更易于理解,更符合<%= targetLang %>技术文档的表达习惯,同时保持原有的格式不变
|
|
44
|
+
4. 当存在之前翻译的<%= targetLang %>内容时,将第三步的结果按句子与之前的<%= targetLang %>内容细致地比较,如果翻译结果意思相近,仅仅表达方式不同的,只需要保留之前的<%= targetLang %>内容即可,不需要重复翻译
|
|
60
45
|
|
|
61
46
|
最终只需要输出最后一步的结果,不需要输出之前步骤的结果。
|
|
62
47
|
|
|
63
|
-
|
|
64
|
-
`.trim()
|
|
48
|
+
<%= additionalPrompts %>
|
|
49
|
+
`.trim();
|
|
50
|
+
let openai;
|
|
51
|
+
export const translate = async ({ source, sourceContent, target, targetContent = '', systemPrompt, additionalPrompts = '', }) => {
|
|
52
|
+
if (!openai) {
|
|
53
|
+
openai = new AzureOpenAI({
|
|
54
|
+
endpoint: process.env.AZURE_OPENAI_ENDPOINT ||
|
|
55
|
+
'https://apt-docs-openai.openai.azure.com',
|
|
56
|
+
apiKey: process.env.AZURE_OPENAI_API_KEY || 'doom',
|
|
57
|
+
apiVersion: process.env.OPENAI_API_VERSION || '2025-01-01-preview',
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const sourceLang = LANGUAGE_CODES[source];
|
|
61
|
+
const targetLang = LANGUAGE_CODES[target];
|
|
62
|
+
const { choices } = await openai.beta.chat.completions.parse({
|
|
63
|
+
messages: [
|
|
64
|
+
{
|
|
65
|
+
role: 'system',
|
|
66
|
+
content: await render(systemPrompt?.trim() || DEFAULT_SYSTEM_PROMPT, { sourceLang, targetLang, additionalPrompts }, { async: true }),
|
|
65
67
|
},
|
|
66
68
|
{
|
|
67
69
|
role: 'user',
|
|
@@ -162,6 +164,7 @@ export const translateCommand = new Command('translate')
|
|
|
162
164
|
translating: { source, target, copy },
|
|
163
165
|
};
|
|
164
166
|
targetContent = await translate({
|
|
167
|
+
...config.translate,
|
|
165
168
|
source,
|
|
166
169
|
sourceContent: processor.stringify({
|
|
167
170
|
...ast,
|
|
@@ -26,7 +26,7 @@ export function Overview(props) {
|
|
|
26
26
|
};
|
|
27
27
|
const { pages } = siteData;
|
|
28
28
|
const overviewModules = pages.filter((page) => subFilter(page.routePath));
|
|
29
|
-
let
|
|
29
|
+
let overviewSidebarGroups = useSidebarData();
|
|
30
30
|
if (overviewSidebarGroups[0] &&
|
|
31
31
|
'link' in overviewSidebarGroups[0] &&
|
|
32
32
|
overviewSidebarGroups[0]?.link !== routePath) {
|
package/lib/types.d.ts
CHANGED
|
@@ -12,6 +12,9 @@ export interface GlobalCliOptions {
|
|
|
12
12
|
ignore?: boolean;
|
|
13
13
|
force?: boolean;
|
|
14
14
|
}
|
|
15
|
+
export interface TranslateOptions {
|
|
16
|
+
systemPrompt?: string;
|
|
17
|
+
}
|
|
15
18
|
declare module '@rspress/shared' {
|
|
16
19
|
interface UserConfig {
|
|
17
20
|
prefix?: string;
|
|
@@ -23,6 +26,7 @@ declare module '@rspress/shared' {
|
|
|
23
26
|
sidebar?: Omit<AutoSidebarPluginOptions, 'download' | 'excludeRoutes'>;
|
|
24
27
|
releaseNotes?: ReleaseNotesOptions;
|
|
25
28
|
internalRoutes?: string[];
|
|
29
|
+
translate?: TranslateOptions;
|
|
26
30
|
}
|
|
27
31
|
}
|
|
28
32
|
export type ContentProcessor = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alauda/doom",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Doctor Doom making docs.",
|
|
6
6
|
"repository": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@inquirer/prompts": "^7.3.2",
|
|
47
47
|
"@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0",
|
|
48
48
|
"@playwright/browser-chromium": "^1.50.1",
|
|
49
|
-
"@rspress/core": "1.
|
|
49
|
+
"@rspress/core": "1.43.1",
|
|
50
50
|
"@shikijs/transformers": "^3.1.0",
|
|
51
51
|
"chokidar": "^4.0.3",
|
|
52
52
|
"cli-progress": "^3.12.0",
|