@aigne/doc-smith 0.8.15-beta → 0.8.15-beta.10
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/CHANGELOG.md +83 -0
- package/agents/clear/choose-contents.mjs +4 -4
- package/agents/clear/clear-auth-tokens.mjs +8 -8
- package/agents/clear/clear-deployment-config.mjs +2 -2
- package/agents/clear/clear-document-config.mjs +3 -3
- package/agents/clear/clear-document-structure.mjs +10 -10
- package/agents/clear/clear-generated-docs.mjs +103 -14
- package/agents/clear/clear-media-description.mjs +7 -7
- package/agents/evaluate/document-structure.yaml +3 -1
- package/agents/evaluate/document.yaml +3 -1
- package/agents/evaluate/index.yaml +1 -3
- package/agents/generate/check-diagram.mjs +1 -1
- package/agents/generate/check-need-generate-structure.mjs +2 -7
- package/agents/generate/draw-diagram.yaml +4 -0
- package/agents/generate/generate-structure.yaml +117 -65
- package/agents/generate/index.yaml +3 -3
- package/agents/generate/{merge-d2-diagram.yaml → merge-diagram.yaml} +7 -6
- package/agents/generate/update-document-structure.yaml +1 -1
- package/agents/generate/user-review-document-structure.mjs +1 -0
- package/agents/generate/utils/merge-document-structures.mjs +30 -0
- package/agents/init/check.mjs +3 -1
- package/agents/init/index.mjs +37 -7
- package/agents/media/load-media-description.mjs +12 -24
- package/agents/publish/publish-docs.mjs +3 -8
- package/agents/schema/document-execution-structure.yaml +1 -1
- package/agents/schema/document-structure-item.yaml +23 -0
- package/agents/schema/document-structure-refine-item.yaml +20 -0
- package/agents/schema/document-structure.yaml +1 -1
- package/agents/translate/index.yaml +1 -4
- package/agents/translate/record-translation-history.mjs +6 -2
- package/agents/translate/translate-multilingual.yaml +1 -1
- package/agents/update/batch-generate-document.yaml +1 -1
- package/agents/update/batch-update-document.yaml +1 -1
- package/agents/update/check-document.mjs +50 -13
- package/agents/update/check-generate-diagram.mjs +26 -0
- package/agents/update/generate-diagram.yaml +29 -0
- package/agents/update/generate-document.yaml +17 -30
- package/agents/update/handle-document-update.yaml +10 -1
- package/agents/update/save-and-translate-document.mjs +18 -47
- package/agents/update/update-document-detail.yaml +2 -1
- package/agents/update/update-single-document.yaml +1 -1
- package/agents/update/user-review-document.mjs +6 -5
- package/agents/utils/choose-docs.mjs +8 -2
- package/agents/utils/load-sources.mjs +132 -57
- package/agents/utils/{save-docs.mjs → post-generate.mjs} +2 -51
- package/agents/utils/save-doc-translation.mjs +27 -0
- package/agents/utils/{save-single-doc.mjs → save-doc.mjs} +17 -12
- package/agents/utils/save-sidebar.mjs +59 -0
- package/agents/utils/transform-detail-data-sources.mjs +45 -0
- package/aigne.yaml +16 -8
- package/package.json +2 -1
- package/prompts/common/document/content-rules-core.md +6 -6
- package/prompts/common/document/media-file-list-usage-rules.md +12 -0
- package/prompts/common/document/openapi-usage-rules.md +36 -0
- package/prompts/common/document/role-and-personality.md +1 -2
- package/prompts/common/document-structure/conflict-resolution-guidance.md +2 -2
- package/prompts/common/document-structure/document-structure-rules.md +8 -8
- package/prompts/common/document-structure/output-constraints.md +3 -3
- package/prompts/detail/custom/custom-components.md +38 -3
- package/prompts/detail/d2-diagram/rules.md +11 -14
- package/prompts/detail/d2-diagram/system-prompt.md +33 -21
- package/prompts/detail/d2-diagram/user-prompt.md +39 -0
- package/prompts/detail/generate/document-rules.md +3 -3
- package/prompts/detail/generate/system-prompt.md +2 -6
- package/prompts/detail/generate/user-prompt.md +20 -24
- package/prompts/detail/update/system-prompt.md +2 -6
- package/prompts/detail/update/user-prompt.md +7 -6
- package/prompts/evaluate/document.md +0 -4
- package/prompts/structure/check-document-structure.md +4 -4
- package/prompts/structure/generate/system-prompt.md +0 -31
- package/prompts/structure/generate/user-prompt.md +99 -26
- package/prompts/structure/review/structure-review-system.md +79 -0
- package/prompts/structure/update/system-prompt.md +1 -1
- package/prompts/structure/update/user-prompt.md +4 -4
- package/prompts/translate/code-block.md +13 -3
- package/prompts/translate/translate-document.md +1 -1
- package/types/document-structure-schema.mjs +3 -3
- package/utils/docs-finder-utils.mjs +48 -0
- package/utils/extract-api.mjs +32 -0
- package/utils/file-utils.mjs +124 -93
- package/utils/history-utils.mjs +20 -8
- package/utils/load-config.mjs +1 -1
- package/utils/markdown-checker.mjs +35 -1
- package/utils/openapi/index.mjs +24 -0
- package/utils/utils.mjs +67 -65
- package/agents/generate/document-structure-tools/generate-sub-structure.mjs +0 -131
- package/agents/generate/generate-structure-without-tools.yaml +0 -65
- package/agents/utils/transform-detail-datasources.mjs +0 -23
- package/prompts/common/document/media-handling-rules.md +0 -9
package/utils/file-utils.mjs
CHANGED
|
@@ -8,9 +8,11 @@ import { isBinaryFile } from "isbinaryfile";
|
|
|
8
8
|
import { encode } from "gpt-tokenizer";
|
|
9
9
|
import { fileTypeFromBuffer } from "file-type";
|
|
10
10
|
import { gunzipSync } from "node:zlib";
|
|
11
|
+
|
|
12
|
+
import { debug } from "./debug.mjs";
|
|
11
13
|
import { isGlobPattern } from "./utils.mjs";
|
|
12
|
-
import { INTELLIGENT_SUGGESTION_TOKEN_THRESHOLD } from "./constants/index.mjs";
|
|
13
14
|
import { uploadFiles } from "./upload-files.mjs";
|
|
15
|
+
import { extractApi } from "./extract-api.mjs";
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
18
|
* Check if a directory is inside a git repository using git command
|
|
@@ -284,6 +286,11 @@ export async function loadFilesFromPaths(sourcesPath, options = {}) {
|
|
|
284
286
|
continue;
|
|
285
287
|
}
|
|
286
288
|
|
|
289
|
+
if (isRemoteFile(dir)) {
|
|
290
|
+
allFiles.push(dir);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
|
|
287
294
|
// First try to access as a file or directory
|
|
288
295
|
const stats = await stat(dir);
|
|
289
296
|
|
|
@@ -313,7 +320,13 @@ export async function loadFilesFromPaths(sourcesPath, options = {}) {
|
|
|
313
320
|
: [];
|
|
314
321
|
|
|
315
322
|
finalIncludePatterns = [...defaultIncludePatterns, ...userInclude];
|
|
316
|
-
finalExcludePatterns = [
|
|
323
|
+
finalExcludePatterns = [
|
|
324
|
+
...defaultExcludePatterns,
|
|
325
|
+
...userExclude.map((x) => {
|
|
326
|
+
const prefix = `${dir}/`;
|
|
327
|
+
return x.startsWith(prefix) ? x.slice(prefix.length) : x;
|
|
328
|
+
}),
|
|
329
|
+
];
|
|
317
330
|
} else {
|
|
318
331
|
// Use only user patterns
|
|
319
332
|
if (includePatterns) {
|
|
@@ -374,6 +387,10 @@ export async function loadFilesFromPaths(sourcesPath, options = {}) {
|
|
|
374
387
|
* @returns {Promise<boolean>} True if file appears to be a text file
|
|
375
388
|
*/
|
|
376
389
|
async function isTextFile(filePath) {
|
|
390
|
+
if (isRemoteFile(filePath)) {
|
|
391
|
+
return isRemoteTextFile(filePath);
|
|
392
|
+
}
|
|
393
|
+
|
|
377
394
|
try {
|
|
378
395
|
const isBinary = await isBinaryFile(filePath);
|
|
379
396
|
return !isBinary;
|
|
@@ -383,6 +400,81 @@ async function isTextFile(filePath) {
|
|
|
383
400
|
}
|
|
384
401
|
}
|
|
385
402
|
|
|
403
|
+
/**
|
|
404
|
+
* Check if a string is an HTTP/HTTPS URL
|
|
405
|
+
* @param {string} fileUrl - The string to check
|
|
406
|
+
* @returns {boolean} - True if the string starts with http:// or https://
|
|
407
|
+
*/
|
|
408
|
+
export function isRemoteFile(fileUrl) {
|
|
409
|
+
if (typeof fileUrl !== "string") return false;
|
|
410
|
+
|
|
411
|
+
try {
|
|
412
|
+
const url = new URL(fileUrl);
|
|
413
|
+
// Only accept http and https url
|
|
414
|
+
if (["http:", "https:"].includes(url.protocol)) {
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
// other protocol will be treated as bad url
|
|
418
|
+
return false;
|
|
419
|
+
} catch {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export async function isRemoteFileAvailable(fileUrl) {
|
|
425
|
+
if (!isRemoteFile(fileUrl)) return false;
|
|
426
|
+
|
|
427
|
+
try {
|
|
428
|
+
const res = await fetch(fileUrl, {
|
|
429
|
+
method: "HEAD",
|
|
430
|
+
});
|
|
431
|
+
return res.ok;
|
|
432
|
+
} catch (error) {
|
|
433
|
+
debug(`Failed to check HTTP file availability: ${fileUrl} - ${error.message}`);
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export async function isRemoteTextFile(fileUrl) {
|
|
439
|
+
try {
|
|
440
|
+
const res = await fetch(fileUrl, {
|
|
441
|
+
method: "HEAD",
|
|
442
|
+
});
|
|
443
|
+
const contentType = res.headers.get("content-type") || "";
|
|
444
|
+
const textMimeTypes = [
|
|
445
|
+
"application/json",
|
|
446
|
+
"application/ld+json",
|
|
447
|
+
"application/graphql+json",
|
|
448
|
+
"application/xml",
|
|
449
|
+
"application/xhtml+xml",
|
|
450
|
+
"application/javascript",
|
|
451
|
+
"application/ecmascript",
|
|
452
|
+
"application/x-www-form-urlencoded",
|
|
453
|
+
"application/rss+xml",
|
|
454
|
+
"application/atom+xml",
|
|
455
|
+
];
|
|
456
|
+
if (contentType.startsWith("text/") || textMimeTypes.includes(contentType)) {
|
|
457
|
+
return true;
|
|
458
|
+
}
|
|
459
|
+
return false;
|
|
460
|
+
} catch (error) {
|
|
461
|
+
debug(`Failed to check HTTP file content type: ${fileUrl} - ${error.message}`);
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
export async function getRemoteFileContent(fileUrl) {
|
|
467
|
+
if (!fileUrl) return null;
|
|
468
|
+
try {
|
|
469
|
+
const res = await fetch(fileUrl);
|
|
470
|
+
const text = await res.text();
|
|
471
|
+
return text;
|
|
472
|
+
} catch (error) {
|
|
473
|
+
debug(`Failed to fetch HTTP file content: ${fileUrl} - ${error.message}`);
|
|
474
|
+
return null;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
386
478
|
/**
|
|
387
479
|
* Read and parse file contents from an array of file paths
|
|
388
480
|
* @param {string[]} files - Array of file paths to read
|
|
@@ -405,12 +497,26 @@ export async function readFileContents(files, baseDir = process.cwd(), options =
|
|
|
405
497
|
}
|
|
406
498
|
|
|
407
499
|
try {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
500
|
+
if (isRemoteFile(file)) {
|
|
501
|
+
const content = await getRemoteFileContent(file);
|
|
502
|
+
if (content) {
|
|
503
|
+
return {
|
|
504
|
+
sourceId: file,
|
|
505
|
+
content,
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return null;
|
|
510
|
+
} else {
|
|
511
|
+
const content = await extractApi(file);
|
|
512
|
+
if (!content) return null;
|
|
513
|
+
|
|
514
|
+
const relativePath = path.relative(baseDir, file);
|
|
515
|
+
return {
|
|
516
|
+
sourceId: relativePath,
|
|
517
|
+
content,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
414
520
|
} catch (error) {
|
|
415
521
|
// If reading as text fails (e.g., binary file), skip it
|
|
416
522
|
console.warn(`Failed to read file as text: ${file} - ${error.message}`);
|
|
@@ -423,6 +529,11 @@ export async function readFileContents(files, baseDir = process.cwd(), options =
|
|
|
423
529
|
return results.filter((result) => result !== null);
|
|
424
530
|
}
|
|
425
531
|
|
|
532
|
+
export function calculateTokens(text) {
|
|
533
|
+
const tokens = encode(text);
|
|
534
|
+
return tokens.length;
|
|
535
|
+
}
|
|
536
|
+
|
|
426
537
|
/**
|
|
427
538
|
* Calculate total lines and tokens from file contents
|
|
428
539
|
* @param {Array<{content: string}>} sourceFiles - Array of objects containing content property
|
|
@@ -448,97 +559,17 @@ export function calculateFileStats(sourceFiles) {
|
|
|
448
559
|
}
|
|
449
560
|
|
|
450
561
|
/**
|
|
451
|
-
* Build sources content string
|
|
452
|
-
* For large contexts, only include core project files to avoid token limit issues
|
|
562
|
+
* Build sources content string
|
|
453
563
|
* @param {Array<{sourceId: string, content: string}>} sourceFiles - Array of source file objects
|
|
454
|
-
* @param {boolean} isLargeContext - Whether the context is large
|
|
455
564
|
* @returns {string} Concatenated sources content with sourceId comments
|
|
456
565
|
*/
|
|
457
|
-
export function buildSourcesContent(sourceFiles
|
|
458
|
-
// Define core file patterns that represent project structure and key information
|
|
459
|
-
const coreFilePatterns = [
|
|
460
|
-
// Configuration files
|
|
461
|
-
/package\.json$/,
|
|
462
|
-
/tsconfig\.json$/,
|
|
463
|
-
/jsconfig\.json$/,
|
|
464
|
-
/\.env\.example$/,
|
|
465
|
-
/Cargo\.toml$/,
|
|
466
|
-
/go\.mod$/,
|
|
467
|
-
/pom\.xml$/,
|
|
468
|
-
/build\.gradle$/,
|
|
469
|
-
/Gemfile$/,
|
|
470
|
-
/requirements\.txt$/,
|
|
471
|
-
/Pipfile$/,
|
|
472
|
-
/composer\.json$/,
|
|
473
|
-
/pyproject\.toml$/,
|
|
474
|
-
|
|
475
|
-
// Documentation
|
|
476
|
-
/README\.md$/i,
|
|
477
|
-
/CHANGELOG\.md$/i,
|
|
478
|
-
/CONTRIBUTING\.md$/i,
|
|
479
|
-
/\.github\/.*\.md$/i,
|
|
480
|
-
|
|
481
|
-
// Entry points and main files
|
|
482
|
-
/index\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
483
|
-
/main\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
484
|
-
/app\.(js|ts|jsx|tsx|py)$/,
|
|
485
|
-
/server\.(js|ts|jsx|tsx|py)$/,
|
|
486
|
-
|
|
487
|
-
// API definitions
|
|
488
|
-
/api\/.*\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
489
|
-
/routes\/.*\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
490
|
-
/controllers\/.*\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
491
|
-
|
|
492
|
-
// Type definitions and schemas
|
|
493
|
-
/types\.(ts|d\.ts)$/,
|
|
494
|
-
/schema\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
495
|
-
/.*\.d\.ts$/,
|
|
496
|
-
|
|
497
|
-
// Core utilities
|
|
498
|
-
/utils\/.*\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
499
|
-
/lib\/.*\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
500
|
-
/helpers\/.*\.(js|ts|jsx|tsx|py|go|rs|java|rb|php)$/,
|
|
501
|
-
];
|
|
502
|
-
|
|
503
|
-
// Function to check if a file is a core file
|
|
504
|
-
const isCoreFile = (filePath) => {
|
|
505
|
-
return coreFilePatterns.some((pattern) => pattern.test(filePath));
|
|
506
|
-
};
|
|
507
|
-
|
|
566
|
+
export function buildSourcesContent(sourceFiles) {
|
|
508
567
|
// Build sources string
|
|
509
568
|
let allSources = "";
|
|
510
569
|
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
// Determine which files to use and set appropriate message
|
|
516
|
-
const filesToInclude = coreFiles.length > 0 ? coreFiles : sourceFiles;
|
|
517
|
-
const noteMessage =
|
|
518
|
-
coreFiles.length > 0
|
|
519
|
-
? "// Note: Context is large, showing only core project files.\n"
|
|
520
|
-
: "// Note: Context is large, showing a sample of files.\n";
|
|
521
|
-
|
|
522
|
-
allSources += noteMessage;
|
|
523
|
-
let accumulatedTokens = 0;
|
|
524
|
-
|
|
525
|
-
for (const source of filesToInclude) {
|
|
526
|
-
const fileContent = `// sourceId: ${source.sourceId}\n${source.content}\n`;
|
|
527
|
-
const fileTokens = encode(fileContent);
|
|
528
|
-
|
|
529
|
-
// Check if adding this file would exceed the token limit
|
|
530
|
-
if (accumulatedTokens + fileTokens.length > INTELLIGENT_SUGGESTION_TOKEN_THRESHOLD) {
|
|
531
|
-
break;
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
allSources += fileContent;
|
|
535
|
-
accumulatedTokens += fileTokens.length;
|
|
536
|
-
}
|
|
537
|
-
} else {
|
|
538
|
-
// Include all files for normal contexts
|
|
539
|
-
for (const source of sourceFiles) {
|
|
540
|
-
allSources += `// sourceId: ${source.sourceId}\n${source.content}\n`;
|
|
541
|
-
}
|
|
570
|
+
// Include all files for normal contexts
|
|
571
|
+
for (const source of sourceFiles) {
|
|
572
|
+
allSources += `\n// sourceId: ${source.sourceId}\n${source.content}\n`;
|
|
542
573
|
}
|
|
543
574
|
|
|
544
575
|
return allSources;
|
package/utils/history-utils.mjs
CHANGED
|
@@ -99,7 +99,7 @@ function recordUpdateGit({ feedback }) {
|
|
|
99
99
|
/**
|
|
100
100
|
* Records an update in the YAML file.
|
|
101
101
|
*/
|
|
102
|
-
function recordUpdateYaml({ operation, feedback,
|
|
102
|
+
function recordUpdateYaml({ operation, feedback, docPaths = null }) {
|
|
103
103
|
try {
|
|
104
104
|
const docSmithDir = join(process.cwd(), DOC_SMITH_DIR);
|
|
105
105
|
if (!existsSync(docSmithDir)) {
|
|
@@ -125,9 +125,9 @@ function recordUpdateYaml({ operation, feedback, documentPath = null }) {
|
|
|
125
125
|
feedback,
|
|
126
126
|
};
|
|
127
127
|
|
|
128
|
-
// Add document
|
|
129
|
-
if (
|
|
130
|
-
entry.
|
|
128
|
+
// Add document paths if provided
|
|
129
|
+
if (Array.isArray(docPaths) && docPaths.length > 0) {
|
|
130
|
+
entry.docPaths = docPaths;
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
// Add to beginning (newest first)
|
|
@@ -153,14 +153,14 @@ function recordUpdateYaml({ operation, feedback, documentPath = null }) {
|
|
|
153
153
|
* @param {Object} params
|
|
154
154
|
* @param {string} params.operation - The type of operation (e.g., 'document_update', 'structure_update', 'translation_update').
|
|
155
155
|
* @param {string} params.feedback - The user's feedback text.
|
|
156
|
-
* @param {string} params.
|
|
156
|
+
* @param {string[]} [params.docPaths] - Document path list for updates.
|
|
157
157
|
*/
|
|
158
|
-
export function recordUpdate({ operation, feedback,
|
|
158
|
+
export function recordUpdate({ operation, feedback, docPaths = null }) {
|
|
159
159
|
// Skip if no feedback
|
|
160
160
|
if (!feedback?.trim()) return;
|
|
161
161
|
|
|
162
162
|
// Always record in YAML
|
|
163
|
-
recordUpdateYaml({ operation, feedback,
|
|
163
|
+
recordUpdateYaml({ operation, feedback, docPaths });
|
|
164
164
|
|
|
165
165
|
// Also record in git if git is available and not in a git repository
|
|
166
166
|
if (isGitAvailable() && !isInGitRepository(process.cwd())) {
|
|
@@ -183,7 +183,19 @@ export function getHistory() {
|
|
|
183
183
|
|
|
184
184
|
try {
|
|
185
185
|
const content = readFileSync(historyPath, "utf8");
|
|
186
|
-
|
|
186
|
+
const parsed = parse(content) || { entries: [] };
|
|
187
|
+
const entries = Array.isArray(parsed.entries) ? parsed.entries : [];
|
|
188
|
+
|
|
189
|
+
const normalized = entries.map((entry) => {
|
|
190
|
+
if (!entry) return entry;
|
|
191
|
+
// Normalize legacy entries: documentPath -> docPaths: [documentPath]
|
|
192
|
+
if (!entry.docPaths && entry.documentPath) {
|
|
193
|
+
return { ...entry, docPaths: [entry.documentPath] };
|
|
194
|
+
}
|
|
195
|
+
return entry;
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
return { ...parsed, entries: normalized };
|
|
187
199
|
} catch (error) {
|
|
188
200
|
console.warn("Could not read the history:", error.message);
|
|
189
201
|
return { entries: [] };
|
package/utils/load-config.mjs
CHANGED
|
@@ -28,7 +28,7 @@ export default async function loadConfig({ config, appUrl }) {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
// Parse new configuration fields and convert keys to actual content
|
|
31
|
-
const processedConfig = processConfigFields(parsedConfig);
|
|
31
|
+
const processedConfig = await processConfigFields(parsedConfig);
|
|
32
32
|
|
|
33
33
|
return {
|
|
34
34
|
lastGitHead: parsedConfig.lastGitHead || "",
|
|
@@ -3,9 +3,12 @@ import path from "node:path";
|
|
|
3
3
|
import remarkGfm from "remark-gfm";
|
|
4
4
|
import remarkLint from "remark-lint";
|
|
5
5
|
import remarkParse from "remark-parse";
|
|
6
|
+
import { isRelative } from "ufo";
|
|
6
7
|
import { unified } from "unified";
|
|
7
8
|
import { visit } from "unist-util-visit";
|
|
8
9
|
import { VFile } from "vfile";
|
|
10
|
+
|
|
11
|
+
import { isRemoteFile, isRemoteFileAvailable } from "./file-utils.mjs";
|
|
9
12
|
import { validateMermaidSyntax } from "./mermaid-validator.mjs";
|
|
10
13
|
|
|
11
14
|
/**
|
|
@@ -232,6 +235,34 @@ function checkLocalImages(markdown, source, errorMessages, markdownFilePath, bas
|
|
|
232
235
|
}
|
|
233
236
|
}
|
|
234
237
|
|
|
238
|
+
async function checkRemoteImages(markdown, source, errorMessages) {
|
|
239
|
+
const imageRegex = /!\[([^\]]*)\]\(([^)]+)\)/g;
|
|
240
|
+
let match;
|
|
241
|
+
|
|
242
|
+
while (true) {
|
|
243
|
+
match = imageRegex.exec(markdown);
|
|
244
|
+
if (match === null) break;
|
|
245
|
+
const imagePath = match[2].trim();
|
|
246
|
+
const altText = match[1];
|
|
247
|
+
|
|
248
|
+
if (isRelative(imagePath)) continue;
|
|
249
|
+
if (imagePath.startsWith("/")) continue;
|
|
250
|
+
|
|
251
|
+
// Skip data URLs
|
|
252
|
+
if (/^data:/.test(imagePath)) continue;
|
|
253
|
+
|
|
254
|
+
if (isRemoteFile(imagePath)) {
|
|
255
|
+
const isAvailable = await isRemoteFileAvailable(imagePath);
|
|
256
|
+
if (isAvailable) continue;
|
|
257
|
+
else {
|
|
258
|
+
errorMessages.push(
|
|
259
|
+
`Found invalid remote image in ${source}:  - only valid media resources can be used`,
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
235
266
|
/**
|
|
236
267
|
* Check content structure and formatting issues
|
|
237
268
|
* @param {string} markdown - The markdown content
|
|
@@ -370,7 +401,10 @@ export async function checkMarkdown(markdown, source = "content", options = {})
|
|
|
370
401
|
// 2. Check local images existence
|
|
371
402
|
checkLocalImages(markdown, source, errorMessages, filePath, baseDir);
|
|
372
403
|
|
|
373
|
-
// 3. Check
|
|
404
|
+
// 3. Check remote images existence
|
|
405
|
+
await checkRemoteImages(markdown, source, errorMessages);
|
|
406
|
+
|
|
407
|
+
// 4. Check content structure and formatting issues
|
|
374
408
|
checkContentStructure(markdown, source, errorMessages);
|
|
375
409
|
|
|
376
410
|
// Check mermaid code blocks and other custom validations
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { parse } from "yaml";
|
|
2
|
+
|
|
3
|
+
export function isOpenAPISpecFile(content) {
|
|
4
|
+
const trimmedContent = content.trim();
|
|
5
|
+
try {
|
|
6
|
+
const parsed = parse(trimmedContent, {
|
|
7
|
+
logLevel: "silent",
|
|
8
|
+
});
|
|
9
|
+
if (parsed.openapi || parsed.swagger) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
} catch {
|
|
13
|
+
//
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const parsed = JSON.parse(trimmedContent);
|
|
17
|
+
if (parsed.openapi || parsed.swagger) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
} catch {
|
|
21
|
+
//
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
package/utils/utils.mjs
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
SUPPORTED_LANGUAGES,
|
|
19
19
|
TARGET_AUDIENCES,
|
|
20
20
|
} from "./constants/index.mjs";
|
|
21
|
+
import { isRemoteFile, getRemoteFileContent } from "./file-utils.mjs";
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* Normalize path to absolute path for consistent comparison
|
|
@@ -47,16 +48,6 @@ export function isGlobPattern(pattern) {
|
|
|
47
48
|
return /[*?[\]]|(\*\*)/.test(pattern);
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
/**
|
|
51
|
-
* Check if a string is an HTTP/HTTPS URL
|
|
52
|
-
* @param {string} url - The string to check
|
|
53
|
-
* @returns {boolean} - True if the string starts with http:// or https://
|
|
54
|
-
*/
|
|
55
|
-
export function isHttp(url) {
|
|
56
|
-
if (typeof url !== "string") return false;
|
|
57
|
-
return url.startsWith("http://") || url.startsWith("https://");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
51
|
export function processContent({ content }) {
|
|
61
52
|
// Match markdown regular links [text](link), exclude images 
|
|
62
53
|
return content.replace(/(?<!!)\[([^\]]+)\]\(([^)]+)\)/g, (match, text, link) => {
|
|
@@ -81,77 +72,72 @@ export function processContent({ content }) {
|
|
|
81
72
|
});
|
|
82
73
|
}
|
|
83
74
|
|
|
75
|
+
// Helper function to generate filename based on language
|
|
76
|
+
export function getFileName(docPath, language) {
|
|
77
|
+
// Flatten path: remove leading /, replace all / with -
|
|
78
|
+
const flatName = docPath.replace(/^\//, "").replace(/\//g, "-");
|
|
79
|
+
const isEnglish = language === "en";
|
|
80
|
+
return isEnglish ? `${flatName}.md` : `${flatName}.${language}.md`;
|
|
81
|
+
}
|
|
82
|
+
|
|
84
83
|
/**
|
|
85
|
-
* Save a single document
|
|
84
|
+
* Save a single document to files
|
|
86
85
|
* @param {Object} params
|
|
87
86
|
* @param {string} params.path - Relative path (without extension)
|
|
88
87
|
* @param {string} params.content - Main document content
|
|
89
88
|
* @param {string} params.docsDir - Root directory
|
|
90
89
|
* @param {string} params.locale - Main content language (e.g., 'en', 'zh', 'fr')
|
|
91
|
-
* @param {Array<{language: string, translation: string}>} [params.translates] - Translation content
|
|
92
90
|
* @param {Array<string>} [params.labels] - Document labels for front matter
|
|
93
|
-
* @returns {Promise<
|
|
91
|
+
* @returns {Promise<{ path: string, success: boolean, error?: string }>}
|
|
94
92
|
*/
|
|
95
|
-
export async function
|
|
96
|
-
path: docPath,
|
|
97
|
-
content,
|
|
98
|
-
docsDir,
|
|
99
|
-
locale,
|
|
100
|
-
translates = [],
|
|
101
|
-
labels,
|
|
102
|
-
isTranslate = false,
|
|
103
|
-
}) {
|
|
104
|
-
const results = [];
|
|
93
|
+
export async function saveDoc({ path: docPath, content, docsDir, locale, labels }) {
|
|
105
94
|
try {
|
|
106
|
-
// Flatten path: remove leading /, replace all / with -
|
|
107
|
-
const flatName = docPath.replace(/^\//, "").replace(/\//g, "-");
|
|
108
95
|
await fs.mkdir(docsDir, { recursive: true });
|
|
96
|
+
const mainFileName = getFileName(docPath, locale);
|
|
97
|
+
const mainFilePath = path.join(docsDir, mainFileName);
|
|
109
98
|
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
const isEnglish = language === "en";
|
|
113
|
-
return isEnglish ? `${flatName}.md` : `${flatName}.${language}.md`;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
// Save main content with appropriate filename based on locale (skip if isTranslate is true)
|
|
117
|
-
if (!isTranslate) {
|
|
118
|
-
const mainFileName = getFileName(locale);
|
|
119
|
-
const mainFilePath = path.join(docsDir, mainFileName);
|
|
120
|
-
|
|
121
|
-
// Add labels front matter if labels are provided
|
|
122
|
-
let finalContent = processContent({ content });
|
|
99
|
+
// Add labels front matter if labels are provided
|
|
100
|
+
let finalContent = processContent({ content });
|
|
123
101
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
await fs.writeFile(mainFilePath, finalContent, "utf8");
|
|
130
|
-
results.push({ path: mainFilePath, success: true });
|
|
102
|
+
if (labels && labels.length > 0) {
|
|
103
|
+
const frontMatter = `---\nlabels: ${JSON.stringify(labels)}\n---\n\n`;
|
|
104
|
+
finalContent = frontMatter + finalContent;
|
|
131
105
|
}
|
|
132
106
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
107
|
+
await fs.writeFile(mainFilePath, finalContent, "utf8");
|
|
108
|
+
return { path: mainFilePath, success: true };
|
|
109
|
+
} catch (err) {
|
|
110
|
+
return { path: docPath, success: false, error: err.message };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
137
113
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
114
|
+
export async function saveDocTranslation({
|
|
115
|
+
path: docPath,
|
|
116
|
+
docsDir,
|
|
117
|
+
translation,
|
|
118
|
+
language,
|
|
119
|
+
labels,
|
|
120
|
+
}) {
|
|
121
|
+
try {
|
|
122
|
+
await fs.mkdir(docsDir, { recursive: true });
|
|
123
|
+
const translateFileName = getFileName(docPath, language);
|
|
124
|
+
const translatePath = path.join(docsDir, translateFileName);
|
|
142
125
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
126
|
+
// Add labels front matter to translation content if labels are provided
|
|
127
|
+
let finalTranslationContent = processContent({
|
|
128
|
+
content: translation,
|
|
129
|
+
});
|
|
147
130
|
|
|
148
|
-
|
|
149
|
-
|
|
131
|
+
if (labels && labels.length > 0) {
|
|
132
|
+
const frontMatter = `---\nlabels: ${JSON.stringify(labels)}\n---\n\n`;
|
|
133
|
+
finalTranslationContent = frontMatter + finalTranslationContent;
|
|
150
134
|
}
|
|
135
|
+
|
|
136
|
+
await fs.writeFile(translatePath, finalTranslationContent, "utf8");
|
|
137
|
+
return { path: translatePath, success: true };
|
|
151
138
|
} catch (err) {
|
|
152
|
-
|
|
139
|
+
return { path: docPath, success: false, error: err.message };
|
|
153
140
|
}
|
|
154
|
-
return results;
|
|
155
141
|
}
|
|
156
142
|
|
|
157
143
|
/**
|
|
@@ -963,7 +949,7 @@ export function processTargetAudience(targetAudienceTypes, existingTargetAudienc
|
|
|
963
949
|
* @param {Object} config - Parsed configuration
|
|
964
950
|
* @returns {Object} Processed configuration with content fields
|
|
965
951
|
*/
|
|
966
|
-
export function processConfigFields(config) {
|
|
952
|
+
export async function processConfigFields(config) {
|
|
967
953
|
const processed = {};
|
|
968
954
|
const allRulesContent = [];
|
|
969
955
|
|
|
@@ -995,7 +981,15 @@ export function processConfigFields(config) {
|
|
|
995
981
|
if (typeof config.rules === "string") {
|
|
996
982
|
const existingRules = config.rules.trim();
|
|
997
983
|
if (existingRules) {
|
|
998
|
-
|
|
984
|
+
// load rules from remote url
|
|
985
|
+
if (isRemoteFile(existingRules)) {
|
|
986
|
+
const remoteFileContent = await getRemoteFileContent(existingRules);
|
|
987
|
+
if (remoteFileContent) {
|
|
988
|
+
allRulesContent.push(remoteFileContent);
|
|
989
|
+
}
|
|
990
|
+
} else {
|
|
991
|
+
allRulesContent.push(existingRules);
|
|
992
|
+
}
|
|
999
993
|
}
|
|
1000
994
|
} else if (Array.isArray(config.rules)) {
|
|
1001
995
|
// Handle array of rules - join them with newlines
|
|
@@ -1054,6 +1048,12 @@ export function processConfigFields(config) {
|
|
|
1054
1048
|
}
|
|
1055
1049
|
}
|
|
1056
1050
|
|
|
1051
|
+
if (config.glossary) {
|
|
1052
|
+
if (isRemoteFile(config.glossary)) {
|
|
1053
|
+
processed.glossary = await getRemoteFileContent(config.glossary);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
1057
|
// Detect and handle conflicts in user selections
|
|
1058
1058
|
const conflicts = detectResolvableConflicts(config);
|
|
1059
1059
|
if (conflicts.length > 0) {
|
|
@@ -1097,8 +1097,10 @@ export function processConfigFields(config) {
|
|
|
1097
1097
|
* @returns {Promise<any>} - The processed configuration with file content loaded in place of references.
|
|
1098
1098
|
*/
|
|
1099
1099
|
export async function resolveFileReferences(obj, basePath = process.cwd()) {
|
|
1100
|
-
if (typeof obj === "string"
|
|
1101
|
-
|
|
1100
|
+
if (typeof obj === "string") {
|
|
1101
|
+
if (obj.startsWith("@")) {
|
|
1102
|
+
return await loadFileContent(obj.slice(1), basePath);
|
|
1103
|
+
}
|
|
1102
1104
|
}
|
|
1103
1105
|
|
|
1104
1106
|
if (Array.isArray(obj)) {
|