@alauda/doom 2.2.0 → 2.3.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,10 +16,8 @@ export interface InternalTranslateOptions extends TranslateOptions {
16
16
  sourceContent: string;
17
17
  target: Language;
18
18
  additionalPrompts?: string;
19
- isChunk?: boolean;
20
19
  }
21
- export declare const translateWithChunks: (options: InternalTranslateOptions) => Promise<string>;
22
- export declare const translate: ({ source, sourceContent, target, systemPrompt, userPrompt, additionalPrompts, isChunk, }: InternalTranslateOptions) => Promise<string>;
20
+ export declare const translate: ({ source, sourceContent, target, systemPrompt, userPrompt, additionalPrompts, }: InternalTranslateOptions) => Promise<string>;
23
21
  export interface TranslateCommandOptions {
24
22
  source: Language;
25
23
  target: Language;
@@ -61,11 +61,6 @@ 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
- <% } %>
68
-
69
64
  <% if (userPrompt || additionalPrompts) { %>
70
65
  ## Additional Requirements
71
66
  These are additional requirements for the translation. They should be met along with the baseline requirements, and in case of any conflict, the baseline requirements should take precedence.
@@ -152,55 +147,7 @@ function getTitleTranslation(title, sourceLang, targetLang) {
152
147
  }
153
148
  return null;
154
149
  }
155
- function splitContentIntoChunks(content, maxChunkSize) {
156
- const lines = content.split('\n');
157
- const chunks = [];
158
- let currentChunk = [];
159
- let currentSize = 0;
160
- for (const line of lines) {
161
- const lineSize = Buffer.byteLength(line + '\n', 'utf8');
162
- // If adding this line would exceed the chunk size, and we have content in current chunk
163
- if (currentSize + lineSize > maxChunkSize && currentChunk.length > 0) {
164
- chunks.push(currentChunk.join('\n'));
165
- currentChunk = [line];
166
- currentSize = lineSize;
167
- }
168
- else {
169
- currentChunk.push(line);
170
- currentSize += lineSize;
171
- }
172
- }
173
- // Add the last chunk if it has content
174
- if (currentChunk.length > 0) {
175
- chunks.push(currentChunk.join('\n'));
176
- }
177
- return chunks;
178
- }
179
- export const translateWithChunks = async (options) => {
180
- const { sourceContent } = options;
181
- const maxChunkSize = 60 * 1024;
182
- const contentSize = Buffer.byteLength(sourceContent, 'utf8');
183
- if (contentSize <= maxChunkSize) {
184
- return translate(options);
185
- }
186
- logger.info(`Content size (${Math.round(contentSize / 1024)}KB) exceeds limit, splitting into chunks...`);
187
- const chunks = splitContentIntoChunks(sourceContent, maxChunkSize);
188
- logger.info(`Split content into ${chunks.length} chunks`);
189
- const translatedChunks = [];
190
- for (let i = 0; i < chunks.length; i++) {
191
- logger.info(`Translating chunk ${i + 1}/${chunks.length}...`);
192
- const translatedChunk = await translate({
193
- ...options,
194
- sourceContent: chunks[i],
195
- isChunk: true,
196
- });
197
- translatedChunks.push(translatedChunk);
198
- }
199
- const result = translatedChunks.join('\n');
200
- logger.info(`Successfully translated ${chunks.length} chunks`);
201
- return result;
202
- };
203
- export const translate = async ({ source, sourceContent, target, systemPrompt, userPrompt = '', additionalPrompts = '', isChunk = false, }) => {
150
+ export const translate = async ({ source, sourceContent, target, systemPrompt, userPrompt = '', additionalPrompts = '', }) => {
204
151
  if (!openai) {
205
152
  openai = new OpenAI({
206
153
  baseURL: process.env.ALAUDA_OPENAI_BASE_URL,
@@ -229,7 +176,6 @@ export const translate = async ({ source, sourceContent, target, systemPrompt, u
229
176
  additionalPrompts: additionalPrompts,
230
177
  terms,
231
178
  titleTranslationPrompt,
232
- isChunk,
233
179
  }, { async: true });
234
180
  logger.debug('Final system prompt:\n', finalSystemPrompt);
235
181
  const stream = await openai.chat.completions.create({
@@ -390,7 +336,7 @@ export const translateCommand = new Command('translate')
390
336
  ...ast,
391
337
  children: ast.children.map((it) => normalizeImgSrc(it, normalizeImgSrcOptions)),
392
338
  });
393
- targetContent = await translateWithChunks({
339
+ targetContent = await translate({
394
340
  ...config.translate,
395
341
  source,
396
342
  sourceContent: normalizedSourceContent,
@@ -0,0 +1,2 @@
1
+ import type { Root } from 'mdast';
2
+ export declare const fileNaming: import("unified-lint-rule").Plugin<Root, unknown>;
@@ -0,0 +1,25 @@
1
+ import path from 'node:path';
2
+ import { lintRule } from 'unified-lint-rule';
3
+ import { getConfig } from "./utils.js";
4
+ const VALID_NAME_PATTERN = /^_?[a-z0-9]+(?:_[a-z0-9]+)*$/;
5
+ export const fileNaming = lintRule('doom-lint:file-naming', async (_root, vfile) => {
6
+ if (!vfile.path) {
7
+ return;
8
+ }
9
+ const { config } = await getConfig();
10
+ const relativePath = path.relative(config.root, vfile.path);
11
+ const prefix = config.lang ? config.lang + '/' : '';
12
+ // Ignore files outside the root directory
13
+ if (relativePath.startsWith('..') ||
14
+ relativePath === `${prefix}index.md` ||
15
+ relativePath === `${prefix}index.mdx` ||
16
+ relativePath.startsWith(`${prefix}apis/`)) {
17
+ return;
18
+ }
19
+ const stem = vfile.stem;
20
+ const isIndex = stem === 'index';
21
+ const basename = isIndex ? path.basename(vfile.dirname) : stem;
22
+ if (!VALID_NAME_PATTERN.test(basename)) {
23
+ vfile.message(`${isIndex ? 'Directory name' : 'Filename'} "${basename}" does not match the required pattern: lowercase letters, numbers, and underscores only, must not end with underscore`);
24
+ }
25
+ });
@@ -0,0 +1,2 @@
1
+ import type { Root } from 'mdast';
2
+ export declare const headingAnchorFormat: import("unified-lint-rule").Plugin<Root, unknown>;
@@ -0,0 +1,108 @@
1
+ import { lintRule } from 'unified-lint-rule';
2
+ import { visitParents } from 'unist-util-visit-parents';
3
+ const getHtmlId = (value) => {
4
+ const match = /\sid\s*=\s*(["'])(.*?)\1/iu.exec(value);
5
+ if (!match) {
6
+ return;
7
+ }
8
+ return match[2];
9
+ };
10
+ const getHtmlIdBearingElement = (node) => {
11
+ const context = node.type === 'html'
12
+ ? node.value
13
+ : node.children.every((child) => child.type === 'html')
14
+ ? node.children.map((child) => child.value).join('')
15
+ : undefined;
16
+ if (!context) {
17
+ return;
18
+ }
19
+ const id = getHtmlId(context);
20
+ if (!id) {
21
+ return;
22
+ }
23
+ return {
24
+ context,
25
+ id,
26
+ node,
27
+ };
28
+ };
29
+ const getMdxIdAttribute = (node) => {
30
+ const idAttribute = node.attributes.find((attribute) => attribute.type === 'mdxJsxAttribute' &&
31
+ attribute.name === 'id' &&
32
+ typeof attribute.value === 'string');
33
+ return idAttribute;
34
+ };
35
+ const getMdxId = (node) => {
36
+ const idAttribute = getMdxIdAttribute(node);
37
+ return typeof idAttribute?.value === 'string' ? idAttribute.value : undefined;
38
+ };
39
+ const stringifyMdxElement = (node) => {
40
+ const attributes = node.attributes
41
+ .map((attribute) => {
42
+ if (attribute.type === 'mdxJsxExpressionAttribute') {
43
+ return `{...${attribute.value}}`;
44
+ }
45
+ if (attribute.value === null || attribute.value === undefined) {
46
+ return attribute.name;
47
+ }
48
+ if (typeof attribute.value === 'string') {
49
+ return `${attribute.name}="${attribute.value}"`;
50
+ }
51
+ return `${attribute.name}={${attribute.value.value}}`;
52
+ })
53
+ .join(' ');
54
+ const name = node.name ?? '';
55
+ return `<${name}${attributes ? ` ${attributes}` : ''} />`;
56
+ };
57
+ const getIdBearingElement = (node) => {
58
+ if (node.type === 'html' || node.type === 'paragraph') {
59
+ return getHtmlIdBearingElement(node);
60
+ }
61
+ if (node.type === 'mdxJsxFlowElement' || node.type === 'mdxJsxTextElement') {
62
+ const id = getMdxId(node);
63
+ if (!id) {
64
+ return;
65
+ }
66
+ return {
67
+ context: stringifyMdxElement(node),
68
+ id,
69
+ node,
70
+ };
71
+ }
72
+ };
73
+ const getHeadingAnchorExample = (filepath, id) => filepath.endsWith('.mdx') ? `# Title \\{#${id}}` : `# Title {#${id}}`;
74
+ const createMessage = (filepath, element) => `Unexpected id-bearing element \`${element.context}\` associated with a heading, rewrite it to \`${getHeadingAnchorExample(filepath, element.id)}\` instead.`;
75
+ export const headingAnchorFormat = lintRule('doom-lint:heading-anchor-format', (root, vfile) => {
76
+ visitParents(root, ['paragraph', 'html', 'mdxJsxFlowElement', 'mdxJsxTextElement'], (node, parents) => {
77
+ const element = getIdBearingElement(node);
78
+ if (!element) {
79
+ return;
80
+ }
81
+ const report = () => {
82
+ vfile.message(createMessage(vfile.path, element), {
83
+ ancestors: [...parents, node],
84
+ place: element.node.position,
85
+ });
86
+ };
87
+ const parent = parents.at(-1);
88
+ if (parent.type === 'heading') {
89
+ report();
90
+ return;
91
+ }
92
+ const index = parent.children.indexOf(node);
93
+ if (index < 0) {
94
+ return;
95
+ }
96
+ for (const siblingIndex of [index - 1, index + 1]) {
97
+ if (siblingIndex < 0 || siblingIndex >= parent.children.length) {
98
+ continue;
99
+ }
100
+ const sibling = parent.children[siblingIndex];
101
+ if (sibling.type !== 'heading') {
102
+ continue;
103
+ }
104
+ report();
105
+ return;
106
+ }
107
+ });
108
+ });
@@ -1,6 +1,8 @@
1
1
  import type { Root } from 'mdast';
2
2
  import type { Plugin } from 'unified';
3
3
  export * from './check-dead-links.ts';
4
+ export * from './file-naming.ts';
5
+ export * from './heading-anchor-format.ts';
4
6
  export * from './list-item-punctuation.ts';
5
7
  export * from './list-item-size.ts';
6
8
  export * from './list-table-introduction.ts';
@@ -1,5 +1,7 @@
1
1
  import remarkMessageControl from 'remark-message-control';
2
2
  export * from "./check-dead-links.js";
3
+ export * from "./file-naming.js";
4
+ export * from "./heading-anchor-format.js";
3
5
  export * from "./list-item-punctuation.js";
4
6
  export * from "./list-item-size.js";
5
7
  export * from "./list-table-introduction.js";
package/lib/remarkrc.js CHANGED
@@ -10,7 +10,7 @@ import remarkLintLintMatchPunctuation from 'remark-lint-match-punctuation';
10
10
  import remarkLintNoChinesePunctuationInNumber from 'remark-lint-no-chinese-punctuation-in-number';
11
11
  import remarkLintNoDuplicateHeadingsInSection from 'remark-lint-no-duplicate-headings-in-section';
12
12
  import remarkLintNoHiddenTableCell from 'remark-lint-no-hidden-table-cell';
13
- import doomLint, { checkDeadLinks, noDeepHeading, noDeepList, noMultiOpenAPIPaths, noParagraphIndent, noUnmatchedAnchor, site, tableSize, titleRequired, unitCase, } from "./remark-lint/index.js";
13
+ import doomLint, { checkDeadLinks, headingAnchorFormat, noDeepHeading, noDeepList, noMultiOpenAPIPaths, noParagraphIndent, noUnmatchedAnchor, site, tableSize, titleRequired, unitCase, } from "./remark-lint/index.js";
14
14
  export default {
15
15
  plugins: [
16
16
  remarkDirective,
@@ -24,6 +24,7 @@ export default {
24
24
  remarkLintNoHiddenTableCell,
25
25
  doomLint,
26
26
  checkDeadLinks,
27
+ headingAnchorFormat,
27
28
  noDeepHeading,
28
29
  noDeepList,
29
30
  noMultiOpenAPIPaths,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": {