@alauda/doom 1.3.0 → 1.4.1

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,
@@ -27,10 +27,10 @@ export function sitemapPlugin(options) {
27
27
  return {
28
28
  name: 'rspress-plugin-sitemap',
29
29
  extendPageData(pageData, isProd) {
30
- if (!isProd || set.has(pageData.id)) {
30
+ if (!isProd || set.has(pageData.routePath)) {
31
31
  return;
32
32
  }
33
- set.add(pageData.id);
33
+ set.add(pageData.routePath);
34
34
  sitemaps.push({
35
35
  loc: `${options.domain}${pageData.routePath}`,
36
36
  lastmod: fs.statSync(pageData._filepath).mtime.toISOString(),
@@ -12,6 +12,7 @@ import { OpenAPIProperties, OpenAPIProperty, OpenAPIRef } from './OpenAPIRef.js'
12
12
  import { HeadingTitle } from './_HeadingTitle.js';
13
13
  import { RefLink } from './_RefLink.js';
14
14
  import { X } from './_X.js';
15
+ import { UidProvider } from './_context.js';
15
16
  export const OpenAPIParameters = ({ parameters, openapi, }) => {
16
17
  return (_jsx(X.ul, { children: parameters.map((param, index) => {
17
18
  const paramObj = '$ref' in param
@@ -116,7 +117,7 @@ export const OpenAPIPath = ({ path, openapiPath: openapiPath_, pathPrefix: pathP
116
117
  console.error(`No OpenAPI path definition found for ${path}`);
117
118
  return null;
118
119
  }
119
- return (_jsxs(_Fragment, { children: [_jsxs(HeadingTitle, { slugger: slugger, uid: uid, level: 2, children: [pathPrefix, path] }), pathItem.parameters && (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { slugger: slugger, uid: uid, level: 3, children: "Common Parameters" }), _jsx(OpenAPIParameters, { parameters: pathItem.parameters, openapi: openapi })] })), Object.values(OpenAPIV3.HttpMethods).map((method) => {
120
+ return (_jsxs(UidProvider, { value: uid, children: [_jsxs(HeadingTitle, { slugger: slugger, level: 2, children: [pathPrefix, path] }), pathItem.parameters && (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { slugger: slugger, level: 3, children: "Common Parameters" }), _jsx(OpenAPIParameters, { parameters: pathItem.parameters, openapi: openapi })] })), Object.values(OpenAPIV3.HttpMethods).map((method) => {
120
121
  if (!pathItem[method]) {
121
122
  return null;
122
123
  }
@@ -128,9 +129,9 @@ export const OpenAPIPath = ({ path, openapiPath: openapiPath_, pathPrefix: pathP
128
129
  const requestBodySchema = requestBodyRef
129
130
  ? resolveRef(openapi, requestBodyRef)
130
131
  : undefined;
131
- return (_jsxs(Fragment, { children: [_jsxs(HeadingTitle, { slugger: slugger, uid: uid, level: 3, children: [_jsx("code", { children: method }), summary] }), _jsx(Markdown, { children: description }), parameters && (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { slugger: slugger, uid: uid, level: 4, children: "Parameters" }), _jsx(OpenAPIParameters, { parameters: parameters, openapi: openapi })] })), requestBodySchema && (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { slugger: slugger, uid: uid, level: 4, children: "Request Body" }), _jsxs(X.p, { children: [_jsx(RefLink, { "$ref": requestBodyRef }), requestBodySchema.required && _jsx(Badge, { children: "required" })] })] })), responses && (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { slugger: slugger, uid: uid, level: 4, children: "Response" }), _jsx(OpenAPIResponses, { responses: responses, openapi: openapi })] }))] }, method));
132
+ return (_jsxs(Fragment, { children: [_jsxs(HeadingTitle, { slugger: slugger, level: 3, children: [_jsx("code", { children: method }), summary] }), _jsx(Markdown, { children: description }), parameters && (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { slugger: slugger, level: 4, children: "Parameters" }), _jsx(OpenAPIParameters, { parameters: parameters, openapi: openapi })] })), requestBodySchema && (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { slugger: slugger, level: 4, children: "Request Body" }), _jsxs(X.p, { children: [_jsx(RefLink, { "$ref": requestBodyRef }), requestBodySchema.required && _jsx(Badge, { children: "required" })] })] })), responses && (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { slugger: slugger, level: 4, children: "Response" }), _jsx(OpenAPIResponses, { responses: responses, openapi: openapi })] }))] }, method));
132
133
  }), refs?.map((ref) => {
133
- return (_jsx(OpenAPIRef, { schema: ref, openapiPath: openapiPath, uid: uid, collectRefs: false }, ref));
134
+ return (_jsx(OpenAPIRef, { schema: ref, openapiPath: openapiPath, isCommonRef: false, collectRefs: false }, ref));
134
135
  })] }));
135
136
  };
136
137
  export default OpenAPIPath;
@@ -8,7 +8,17 @@ export interface OpenAPIRefProps {
8
8
  * The specific path to the OpenAPI schema, otherwise the first matched will be used.
9
9
  */
10
10
  openapiPath?: string;
11
- uid?: string;
11
+ /**
12
+ * Whether is a common reference, no `uid` will be generated.
13
+ *
14
+ * @default true
15
+ */
16
+ isCommonRef?: boolean;
17
+ /**
18
+ * Whether to collect references from the schema.
19
+ *
20
+ * @default true
21
+ */
12
22
  collectRefs?: boolean;
13
23
  }
14
24
  export declare const OpenAPIProperty: ({ name, property, openapi, }: {
@@ -20,5 +30,5 @@ export declare const OpenAPIProperties: ({ properties, openapi, }: {
20
30
  properties: Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject>;
21
31
  openapi: OpenAPIV3_1.Document;
22
32
  }) => import("react/jsx-runtime").JSX.Element;
23
- export declare const OpenAPIRef: ({ schema, openapiPath: openapiPath_, uid, collectRefs, }: OpenAPIRefProps) => import("react/jsx-runtime").JSX.Element | null;
33
+ export declare const OpenAPIRef: ({ schema, openapiPath: openapiPath_, isCommonRef, collectRefs, }: OpenAPIRefProps) => import("react/jsx-runtime").JSX.Element | null;
24
34
  export default OpenAPIRef;
@@ -7,6 +7,7 @@ import { Markdown } from './Markdown.js';
7
7
  import { HeadingTitle } from './_HeadingTitle.js';
8
8
  import { RefLink } from './_RefLink.js';
9
9
  import { X } from './_X.js';
10
+ import { UidProvider, useUid } from './_context.js';
10
11
  export const OpenAPIProperty = ({ name, property, openapi, }) => {
11
12
  const propObj = '$ref' in property ? resolveRef(openapi, property.$ref) : property;
12
13
  const type = propObj.type;
@@ -68,10 +69,14 @@ const getRefsForSchema = (openapi, schema, knownRefs) => {
68
69
  collectRefs(refSchema);
69
70
  return Array.from(refs);
70
71
  };
71
- export const OpenAPIRef = ({ schema, openapiPath: openapiPath_, uid, collectRefs = true, }) => {
72
+ export const OpenAPIRef = ({ schema, openapiPath: openapiPath_, isCommonRef = true, collectRefs = true, }) => {
72
73
  const { page } = usePageData();
74
+ let uid = useUid();
73
75
  const innerUid = useId();
74
- if (uid == null) {
76
+ if (isCommonRef) {
77
+ uid = ''; // common references do not need a unique ID
78
+ }
79
+ else if (!uid) {
75
80
  uid = innerUid;
76
81
  }
77
82
  const [schemaItem, openapi, openapiPath] = useMemo(() => {
@@ -95,6 +100,6 @@ export const OpenAPIRef = ({ schema, openapiPath: openapiPath_, uid, collectRefs
95
100
  console.error(`No OpenAPI schema definition found for ${schema}\n`);
96
101
  return null;
97
102
  }
98
- return (_jsxs(_Fragment, { children: [_jsx(HeadingTitle, { uid: uid, slug: schema, level: 2, children: modelName(schema) }), _jsx(Markdown, { children: schemaItem.description }), schemaItem.properties && (_jsx(OpenAPIProperties, { properties: schemaItem.properties, openapi: openapi })), refs?.map((schema) => (_jsx(OpenAPIRef, { schema: schema, openapiPath: openapiPath, uid: uid, collectRefs: false }, schema)))] }));
103
+ return (_jsxs(UidProvider, { value: uid, children: [_jsx(HeadingTitle, { slug: schema, level: 2, children: modelName(schema) }), _jsx(Markdown, { children: schemaItem.description }), schemaItem.properties && (_jsx(OpenAPIProperties, { properties: schemaItem.properties, openapi: openapi })), refs?.map((schema) => (_jsx(OpenAPIRef, { schema: schema, openapiPath: openapiPath, isCommonRef: isCommonRef, collectRefs: false }, schema)))] }));
99
104
  };
100
105
  export default OpenAPIRef;
@@ -3,8 +3,7 @@ import { type ReactNode } from 'react';
3
3
  export interface HeadingTitleProps {
4
4
  slug?: string;
5
5
  slugger?: BananaSlug;
6
- uid?: string;
7
6
  level: 1 | 2 | 3 | 4 | 5 | 6;
8
7
  children: ReactNode;
9
8
  }
10
- export declare const HeadingTitle: ({ slug, slugger, uid, level, children, }: HeadingTitleProps) => import("react/jsx-runtime").JSX.Element;
9
+ export declare const HeadingTitle: ({ slug, slugger, level, children, }: HeadingTitleProps) => import("react/jsx-runtime").JSX.Element;
@@ -2,8 +2,10 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import BananaSlug from 'github-slugger';
3
3
  import { Children, useMemo } from 'react';
4
4
  import { X } from './_X.js';
5
- // TODO: use context to simplify the usage of `slugger` and `uid`
6
- export const HeadingTitle = ({ slug, slugger, uid, level, children, }) => {
5
+ import { useUid } from './_context.js';
6
+ // TODO: use context to simplify the usage of `slugger`
7
+ export const HeadingTitle = ({ slug, slugger, level, children, }) => {
8
+ const uid = useUid();
7
9
  const HeadingComponents = useMemo(() => {
8
10
  return [null, X.h1, X.h2, X.h3, X.h4, X.h5, X.h6];
9
11
  }, []);
@@ -1,11 +1,14 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { COMMON_REFS, modelName } from '../utils.js';
3
3
  import { X } from './_X.js';
4
+ import { useUid } from './_context.js';
4
5
  export const RefLink = ({ $ref }) => {
6
+ const uid = useUid();
5
7
  if (!$ref) {
6
8
  return null;
7
9
  }
8
10
  const ref = $ref.replace('/components/schemas/', '');
9
- const refName = modelName(ref);
10
- return _jsx(X.a, { href: COMMON_REFS[ref.slice(1)] || ref, children: refName });
11
+ const plainRef = ref.slice(1);
12
+ const refName = modelName(plainRef);
13
+ return (_jsx(X.a, { href: COMMON_REFS[plainRef] || (uid ? `#${uid}-${plainRef}` : ref), children: refName }));
11
14
  };
@@ -0,0 +1,3 @@
1
+ export declare const UidContext: import("react").Context<string>;
2
+ export declare const useUid: () => string;
3
+ export declare const UidProvider: import("react").Provider<string>;
@@ -0,0 +1,4 @@
1
+ import { createContext, use } from 'react';
2
+ export const UidContext = createContext('');
3
+ export const useUid = () => use(UidContext);
4
+ export const UidProvider = UidContext.Provider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": "git+https://github.com/alauda/doom.git",
@@ -34,11 +34,11 @@
34
34
  "rspress-plugins"
35
35
  ],
36
36
  "dependencies": {
37
- "@cspell/eslint-plugin": "^8.19.4 || ^9.0.2",
38
- "@eslint-react/eslint-plugin": "^1.52.1",
37
+ "@cspell/eslint-plugin": "^8.19.4 || ^9.1.1",
38
+ "@eslint-react/eslint-plugin": "^1.52.2",
39
39
  "@inquirer/prompts": "^7.5.3",
40
40
  "@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0",
41
- "@playwright/browser-chromium": "^1.53.0",
41
+ "@playwright/browser-chromium": "^1.53.1",
42
42
  "@rsbuild/plugin-react": "^1.3.2",
43
43
  "@rsbuild/plugin-sass": "^1.3.2",
44
44
  "@rsbuild/plugin-svgr": "^1.2.0",
@@ -54,19 +54,19 @@
54
54
  "commander": "^13.1.0 || ^14.0.0",
55
55
  "ejs": "^3.1.10",
56
56
  "es-toolkit": "^1.39.3",
57
- "eslint": "^9.28.0",
58
- "eslint-plugin-mdx": "^3.4.2",
57
+ "eslint": "^9.29.0",
58
+ "eslint-plugin-mdx": "^3.5.0",
59
59
  "html-tag-names": "^2.1.0",
60
60
  "mdast-util-mdx": "^3.0.0",
61
61
  "mdast-util-mdx-jsx": "^3.2.0",
62
62
  "mermaid": "^11.6.0",
63
- "openai": "^5.3.0",
63
+ "openai": "^5.5.1",
64
64
  "openapi-types": "^12.1.3",
65
65
  "p-ratelimit": "^1.0.1",
66
66
  "pdf-lib": "^1.17.1",
67
67
  "pdf-merger-js": "^5.1.2",
68
68
  "picomatch": "^4.0.2",
69
- "playwright": "^1.53.0",
69
+ "playwright": "^1.53.1",
70
70
  "react-markdown": "^10.1.0",
71
71
  "rehype-raw": "^7.0.0",
72
72
  "rehype-sanitize": "^6.0.0",
@@ -81,7 +81,7 @@
81
81
  "tinyglobby": "^0.2.14",
82
82
  "type-fest": "^4.41.0",
83
83
  "typescript": "^5.8.3",
84
- "typescript-eslint": "^8.34.0",
84
+ "typescript-eslint": "^8.34.1",
85
85
  "x-fetch": "^0.2.6",
86
86
  "yaml": "^2.8.0",
87
87
  "yoctocolors": "^2.1.1"