@alauda/doom 1.2.3 → 1.4.0

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.
@@ -16,8 +16,10 @@ export interface InternalTranslateOptions extends TranslateOptions {
16
16
  sourceContent: string;
17
17
  target: Language;
18
18
  additionalPrompts?: string;
19
+ isChunk?: boolean;
19
20
  }
20
- export declare const translate: ({ source, sourceContent, target, systemPrompt, userPrompt, additionalPrompts, }: InternalTranslateOptions) => Promise<string>;
21
+ export declare const translateWithChunks: (options: InternalTranslateOptions) => Promise<string>;
22
+ export declare const translate: ({ source, sourceContent, target, systemPrompt, userPrompt, additionalPrompts, isChunk, }: InternalTranslateOptions) => Promise<string>;
21
23
  export interface TranslateCommandOptions {
22
24
  source: Language;
23
25
  target: Language;
@@ -61,6 +61,10 @@ You are a professional technical documentation engineer, skilled in writing high
61
61
  <% if (terms) { %>
62
62
  <%- terms %>
63
63
  <% } %>
64
+ <% if (isChunk) { %>
65
+ ## Chunk Translation Notice
66
+ This is part of a larger document that has been split into smaller chunks for translation. Please translate this chunk as if it's part of a continuous document, maintaining consistency with the overall document style and context.
67
+ <% } %>
64
68
 
65
69
  <% if (userPrompt || additionalPrompts) { %>
66
70
  ## Additional Requirements
@@ -147,7 +151,55 @@ function getTitleTranslation(title, sourceLang, targetLang) {
147
151
  }
148
152
  return null;
149
153
  }
150
- export const translate = async ({ source, sourceContent, target, systemPrompt, userPrompt = '', additionalPrompts = '', }) => {
154
+ function splitContentIntoChunks(content, maxChunkSize) {
155
+ const lines = content.split('\n');
156
+ const chunks = [];
157
+ let currentChunk = [];
158
+ let currentSize = 0;
159
+ for (const line of lines) {
160
+ const lineSize = Buffer.byteLength(line + '\n', 'utf8');
161
+ // If adding this line would exceed the chunk size, and we have content in current chunk
162
+ if (currentSize + lineSize > maxChunkSize && currentChunk.length > 0) {
163
+ chunks.push(currentChunk.join('\n'));
164
+ currentChunk = [line];
165
+ currentSize = lineSize;
166
+ }
167
+ else {
168
+ currentChunk.push(line);
169
+ currentSize += lineSize;
170
+ }
171
+ }
172
+ // Add the last chunk if it has content
173
+ if (currentChunk.length > 0) {
174
+ chunks.push(currentChunk.join('\n'));
175
+ }
176
+ return chunks;
177
+ }
178
+ export const translateWithChunks = async (options) => {
179
+ const { sourceContent } = options;
180
+ const maxChunkSize = 60 * 1024;
181
+ const contentSize = Buffer.byteLength(sourceContent, 'utf8');
182
+ if (contentSize <= maxChunkSize) {
183
+ return translate(options);
184
+ }
185
+ logger.info(`Content size (${Math.round(contentSize / 1024)}KB) exceeds limit, splitting into chunks...`);
186
+ const chunks = splitContentIntoChunks(sourceContent, maxChunkSize);
187
+ logger.info(`Split content into ${chunks.length} chunks`);
188
+ const translatedChunks = [];
189
+ for (let i = 0; i < chunks.length; i++) {
190
+ logger.info(`Translating chunk ${i + 1}/${chunks.length}...`);
191
+ const translatedChunk = await translate({
192
+ ...options,
193
+ sourceContent: chunks[i],
194
+ isChunk: true,
195
+ });
196
+ translatedChunks.push(translatedChunk);
197
+ }
198
+ const result = translatedChunks.join('\n');
199
+ logger.info(`Successfully translated ${chunks.length} chunks`);
200
+ return result;
201
+ };
202
+ export const translate = async ({ source, sourceContent, target, systemPrompt, userPrompt = '', additionalPrompts = '', isChunk = false, }) => {
151
203
  if (!openai) {
152
204
  openai = new AzureOpenAI({
153
205
  endpoint: process.env.AZURE_OPENAI_ENDPOINT ||
@@ -178,6 +230,7 @@ export const translate = async ({ source, sourceContent, target, systemPrompt, u
178
230
  additionalPrompts: additionalPrompts,
179
231
  terms,
180
232
  titleTranslationPrompt,
233
+ isChunk,
181
234
  }, { async: true });
182
235
  logger.debug('Final system prompt:\n', finalSystemPrompt);
183
236
  const { choices } = await openai.chat.completions.parse({
@@ -337,7 +390,7 @@ export const translateCommand = new Command('translate')
337
390
  ...ast,
338
391
  children: ast.children.map((it) => normalizeImgSrc(it, normalizeImgSrcOptions)),
339
392
  });
340
- targetContent = await translate({
393
+ targetContent = await translateWithChunks({
341
394
  ...config.translate,
342
395
  source,
343
396
  sourceContent: normalizedSourceContent,
@@ -7,7 +7,7 @@ import crdsMap from 'doom-@api-crdsMap';
7
7
  import { useCallback, useEffect, useMemo, useState } from 'react';
8
8
  import { useIsPrint, useTranslation } from '../hooks/index.js';
9
9
  import { Directive } from './Directive.js';
10
- import { Markdown } from './_Markdown.js';
10
+ import { Markdown } from './Markdown.js';
11
11
  import { X } from './_X.js';
12
12
  export const K8sCrdSchemaPart = ({ name, parent, schema: property, expandAll, }) => {
13
13
  const [open, setOpen] = useState(expandAll);
@@ -0,0 +1,10 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import {} from 'react';
3
+ import ReactMarkdown from 'react-markdown';
4
+ import rehypeRaw from 'rehype-raw';
5
+ import rehypeSanitize from 'rehype-sanitize';
6
+ import remarkGfm from 'remark-gfm';
7
+ import { X } from './_X.js';
8
+ const remarkPlugins = [remarkGfm];
9
+ const rehypePlugins = [rehypeRaw, rehypeSanitize];
10
+ export const Markdown = ({ children }) => children && (_jsx(ReactMarkdown, { remarkPlugins: remarkPlugins, rehypePlugins: rehypePlugins, components: X, children: children }));
@@ -7,9 +7,9 @@ import BananaSlug from 'github-slugger';
7
7
  import { OpenAPIV3 } from 'openapi-types';
8
8
  import { Fragment, useId, useMemo } from 'react';
9
9
  import { omitRoutePathRefs, resolveRef } from '../utils.js';
10
+ import { Markdown } from './Markdown.js';
10
11
  import { OpenAPIProperties, OpenAPIProperty, OpenAPIRef } from './OpenAPIRef.js';
11
12
  import { HeadingTitle } from './_HeadingTitle.js';
12
- import { Markdown } from './_Markdown.js';
13
13
  import { RefLink } from './_RefLink.js';
14
14
  import { X } from './_X.js';
15
15
  export const OpenAPIParameters = ({ parameters, openapi, }) => {
@@ -3,8 +3,8 @@ import { usePageData } from '@rspress/core/runtime';
3
3
  import openapisMap from 'doom-@api-openapisMap';
4
4
  import { useId, useMemo } from 'react';
5
5
  import { modelName, omitRoutePathRefs, resolveRef } from '../utils.js';
6
+ import { Markdown } from './Markdown.js';
6
7
  import { HeadingTitle } from './_HeadingTitle.js';
7
- import { Markdown } from './_Markdown.js';
8
8
  import { RefLink } from './_RefLink.js';
9
9
  import { X } from './_X.js';
10
10
  export const OpenAPIProperty = ({ name, property, openapi, }) => {
@@ -10,6 +10,7 @@ export * from './JsonViewer.js';
10
10
  export * from './K8sCrd.js';
11
11
  export * from './K8sPermissionTable.js';
12
12
  export * from './LinkCard.js';
13
+ export * from './Markdown.js';
13
14
  export * from './Mermaid.js';
14
15
  export * from './OpenAPIPath.js';
15
16
  export * from './OpenAPIRef.js';
@@ -10,6 +10,7 @@ export * from './JsonViewer.js';
10
10
  export * from './K8sCrd.js';
11
11
  export * from './K8sPermissionTable.js';
12
12
  export * from './LinkCard.js';
13
+ export * from './Markdown.js';
13
14
  export * from './Mermaid.js';
14
15
  export * from './OpenAPIPath.js';
15
16
  export * from './OpenAPIRef.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "1.2.3",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": "git+https://github.com/alauda/doom.git",
@@ -35,7 +35,7 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "@cspell/eslint-plugin": "^8.19.4 || ^9.0.2",
38
- "@eslint-react/eslint-plugin": "^1.51.3",
38
+ "@eslint-react/eslint-plugin": "^1.52.1",
39
39
  "@inquirer/prompts": "^7.5.3",
40
40
  "@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0",
41
41
  "@playwright/browser-chromium": "^1.53.0",
@@ -43,9 +43,9 @@
43
43
  "@rsbuild/plugin-sass": "^1.3.2",
44
44
  "@rsbuild/plugin-svgr": "^1.2.0",
45
45
  "@rsbuild/plugin-yaml": "^1.0.2",
46
- "@rspress/core": "2.0.0-beta.13",
47
- "@rspress/plugin-algolia": "2.0.0-beta.13",
48
- "@rspress/plugin-llms": "2.0.0-beta.13",
46
+ "@rspress/core": "2.0.0-beta.14",
47
+ "@rspress/plugin-algolia": "2.0.0-beta.14",
48
+ "@rspress/plugin-llms": "2.0.0-beta.14",
49
49
  "@shikijs/transformers": "^3.6.0",
50
50
  "@total-typescript/ts-reset": "^0.6.1",
51
51
  "chokidar": "^4.0.3",
@@ -68,6 +68,8 @@
68
68
  "picomatch": "^4.0.2",
69
69
  "playwright": "^1.53.0",
70
70
  "react-markdown": "^10.1.0",
71
+ "rehype-raw": "^7.0.0",
72
+ "rehype-sanitize": "^6.0.0",
71
73
  "remark-directive": "^4.0.0",
72
74
  "remark-frontmatter": "^5.0.0",
73
75
  "remark-gfm": "^4.0.1",
@@ -1,6 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import {} from 'react';
3
- import ReactMarkdown from 'react-markdown';
4
- import remarkGfm from 'remark-gfm';
5
- import { X } from './_X.js';
6
- export const Markdown = ({ children }) => children && (_jsx(ReactMarkdown, { remarkPlugins: [[remarkGfm]], components: X, skipHtml: true, children: children }));