gtx-cli 2.1.5-alpha.3 → 2.1.5-alpha.6

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/dist/cli/base.js CHANGED
@@ -17,6 +17,7 @@ import { upload } from '../formats/files/upload.js';
17
17
  import { attachTranslateFlags } from './flags.js';
18
18
  import { handleStage } from './commands/stage.js';
19
19
  import { handleDownload, handleTranslate, postProcessTranslations, } from './commands/translate.js';
20
+ import localizeStaticUrls from '../utils/localizeStaticUrls.js';
20
21
  import updateConfig from '../fs/config/updateConfig.js';
21
22
  export class BaseCLI {
22
23
  library;
@@ -78,11 +79,19 @@ export class BaseCLI {
78
79
  const settings = await generateSettings(initOptions);
79
80
  if (!settings.stageTranslations) {
80
81
  const results = await handleStage(initOptions, settings, this.library, false);
82
+ // Process default locale static URLs first, before translations
83
+ if (settings.options?.experimentalLocalizeStaticUrls) {
84
+ await localizeStaticUrls(settings, [settings.defaultLocale]);
85
+ }
81
86
  if (results) {
82
87
  await handleTranslate(initOptions, settings, results);
83
88
  }
84
89
  }
85
90
  else {
91
+ // Process default locale static URLs first, before downloading translations
92
+ if (settings.options?.experimentalLocalizeStaticUrls) {
93
+ await localizeStaticUrls(settings, [settings.defaultLocale]);
94
+ }
86
95
  await handleDownload(initOptions, settings);
87
96
  }
88
97
  await postProcessTranslations(settings);
@@ -35,9 +35,13 @@ export async function handleDownload(options, settings) {
35
35
  await checkFileTranslations(stagedVersionData, settings.locales, options.timeout, (sourcePath, locale) => fileMapping[locale][sourcePath] ?? null, settings);
36
36
  }
37
37
  export async function postProcessTranslations(settings) {
38
- // Localize static urls (/docs -> /[locale]/docs)
38
+ // Localize static urls (/docs -> /[locale]/docs) for non-default locales only
39
+ // Default locale is processed earlier in the flow in base.ts
39
40
  if (settings.options?.experimentalLocalizeStaticUrls) {
40
- await localizeStaticUrls(settings);
41
+ const nonDefaultLocales = settings.locales.filter(locale => locale !== settings.defaultLocale);
42
+ if (nonDefaultLocales.length > 0) {
43
+ await localizeStaticUrls(settings, nonDefaultLocales);
44
+ }
41
45
  }
42
46
  // Localize static imports (/docs -> /[locale]/docs)
43
47
  if (settings.options?.experimentalLocalizeStaticImports) {
@@ -141,6 +141,7 @@ export type AdditionalOptions = {
141
141
  experimentalLocalizeStaticUrls?: boolean;
142
142
  experimentalHideDefaultLocale?: boolean;
143
143
  experimentalFlattenJsonFiles?: boolean;
144
+ baseDomain?: string;
144
145
  };
145
146
  export type JsonSchema = {
146
147
  preset?: 'mintlify';
@@ -12,4 +12,8 @@ import { Settings } from '../types/index.js';
12
12
  * - Support more file types
13
13
  * - Support more complex paths
14
14
  */
15
- export default function localizeStaticUrls(settings: Settings): Promise<void>;
15
+ export default function localizeStaticUrls(settings: Settings, targetLocales?: string[]): Promise<void>;
16
+ /**
17
+ * Main URL transformation function that delegates to specific scenarios
18
+ */
19
+ export declare function transformUrlPath(originalUrl: string, patternHead: string, targetLocale: string, defaultLocale: string, hideDefaultLocale: boolean): string | null;
@@ -21,19 +21,24 @@ const { isMatch } = micromatch;
21
21
  * - Support more file types
22
22
  * - Support more complex paths
23
23
  */
24
- export default async function localizeStaticUrls(settings) {
24
+ export default async function localizeStaticUrls(settings, targetLocales) {
25
25
  if (!settings.files ||
26
26
  (Object.keys(settings.files.placeholderPaths).length === 1 &&
27
27
  settings.files.placeholderPaths.gt)) {
28
28
  return;
29
29
  }
30
30
  const { resolvedPaths: sourceFiles } = settings.files;
31
- const fileMapping = createFileMapping(sourceFiles, settings.files.placeholderPaths, settings.files.transformPaths, settings.locales, settings.defaultLocale);
31
+ // Use filtered locales if provided, otherwise use all locales
32
+ const locales = targetLocales || settings.locales;
33
+ const fileMapping = createFileMapping(sourceFiles, settings.files.placeholderPaths, settings.files.transformPaths, settings.locales, // Always use all locales for mapping, filter later
34
+ settings.defaultLocale);
32
35
  // Process all file types at once with a single call
33
36
  const processPromises = [];
34
37
  // First, process default locale files (from source files)
35
38
  // This is needed because they might not be in the fileMapping if they're not being translated
36
- if (!fileMapping[settings.defaultLocale]) {
39
+ // Only process default locale if it's in the target locales filter
40
+ if (!fileMapping[settings.defaultLocale] &&
41
+ locales.includes(settings.defaultLocale)) {
37
42
  const defaultLocaleFiles = [];
38
43
  // Collect all .md and .mdx files from sourceFiles
39
44
  if (sourceFiles.md) {
@@ -47,16 +52,20 @@ export default async function localizeStaticUrls(settings) {
47
52
  // Get file content
48
53
  const fileContent = await fs.promises.readFile(filePath, 'utf8');
49
54
  // Localize the file using default locale
50
- const localizedFile = localizeStaticUrlsForFile(fileContent, settings.defaultLocale, settings.defaultLocale, // Process as default locale
51
- settings.options?.experimentalHideDefaultLocale || false, settings.options?.docsUrlPattern, settings.options?.excludeStaticUrls);
52
- // Write the localized file back to the same path
53
- await fs.promises.writeFile(filePath, localizedFile);
55
+ const result = localizeStaticUrlsForFile(fileContent, settings.defaultLocale, settings.defaultLocale, // Process as default locale
56
+ settings.options?.experimentalHideDefaultLocale || false, settings.options?.docsUrlPattern, settings.options?.excludeStaticUrls, settings.options?.baseDomain);
57
+ // Only write the file if there were changes
58
+ if (result.hasChanges) {
59
+ await fs.promises.writeFile(filePath, result.content);
60
+ }
54
61
  }));
55
62
  processPromises.push(defaultPromise);
56
63
  }
57
64
  }
58
65
  // Then process all other locales from fileMapping
59
- const mappingPromises = Object.entries(fileMapping).map(async ([locale, filesMap]) => {
66
+ const mappingPromises = Object.entries(fileMapping)
67
+ .filter(([locale, filesMap]) => locales.includes(locale)) // Filter by target locales
68
+ .map(async ([locale, filesMap]) => {
60
69
  // Get all files that are md or mdx
61
70
  const targetFiles = Object.values(filesMap).filter((path) => path.endsWith('.md') || path.endsWith('.mdx'));
62
71
  // Replace the placeholder path with the target path
@@ -64,9 +73,11 @@ export default async function localizeStaticUrls(settings) {
64
73
  // Get file content
65
74
  const fileContent = await fs.promises.readFile(filePath, 'utf8');
66
75
  // Localize the file (handles both URLs and hrefs in single AST pass)
67
- const localizedFile = localizeStaticUrlsForFile(fileContent, settings.defaultLocale, locale, settings.options?.experimentalHideDefaultLocale || false, settings.options?.docsUrlPattern, settings.options?.excludeStaticUrls);
68
- // Write the localized file to the target path
69
- await fs.promises.writeFile(filePath, localizedFile);
76
+ const result = localizeStaticUrlsForFile(fileContent, settings.defaultLocale, locale, settings.options?.experimentalHideDefaultLocale || false, settings.options?.docsUrlPattern, settings.options?.excludeStaticUrls, settings.options?.baseDomain);
77
+ // Only write the file if there were changes
78
+ if (result.hasChanges) {
79
+ await fs.promises.writeFile(filePath, result.content);
80
+ }
70
81
  }));
71
82
  });
72
83
  processPromises.push(...mappingPromises);
@@ -75,21 +86,28 @@ export default async function localizeStaticUrls(settings) {
75
86
  /**
76
87
  * Determines if a URL should be processed based on pattern matching
77
88
  */
78
- function shouldProcessUrl(originalUrl, patternHead, targetLocale, defaultLocale) {
79
- // Skip absolute URLs (http://, https://, //, etc.)
80
- if (originalUrl.includes(':')) {
81
- return false;
82
- }
89
+ function shouldProcessUrl(originalUrl, patternHead, targetLocale, defaultLocale, baseDomain) {
83
90
  const patternWithoutSlash = patternHead.replace(/\/$/, '');
91
+ // Handle absolute URLs with baseDomain
92
+ let urlToCheck = originalUrl;
93
+ if (baseDomain && originalUrl.startsWith(baseDomain)) {
94
+ urlToCheck = originalUrl.substring(baseDomain.length);
95
+ }
84
96
  if (targetLocale === defaultLocale) {
85
97
  // For default locale processing, check if URL contains the pattern
86
- return originalUrl.includes(patternWithoutSlash);
98
+ return urlToCheck.includes(patternWithoutSlash);
87
99
  }
88
100
  else {
89
101
  // For non-default locales, check if URL starts with pattern
90
- return originalUrl.startsWith(patternWithoutSlash);
102
+ return urlToCheck.startsWith(patternWithoutSlash);
91
103
  }
92
104
  }
105
+ /**
106
+ * Determines if a URL should be processed based on the base domain
107
+ */
108
+ function shouldProcessAbsoluteUrl(originalUrl, baseDomain) {
109
+ return originalUrl.startsWith(baseDomain);
110
+ }
93
111
  /**
94
112
  * Checks if a URL should be excluded based on exclusion patterns
95
113
  */
@@ -98,102 +116,79 @@ function isUrlExcluded(originalUrl, exclude, defaultLocale) {
98
116
  return excludePatterns.some((pattern) => isMatch(originalUrl, pattern));
99
117
  }
100
118
  /**
101
- * Transforms URL for default locale processing
102
- */
103
- function transformDefaultLocaleUrl(originalUrl, patternHead, defaultLocale, hideDefaultLocale) {
104
- if (hideDefaultLocale) {
105
- // Remove locale from URLs that have it: '/docs/en/file' -> '/docs/file'
106
- if (originalUrl.includes(`/${defaultLocale}/`)) {
107
- return originalUrl.replace(`/${defaultLocale}/`, '/');
108
- }
109
- else if (originalUrl.endsWith(`/${defaultLocale}`)) {
110
- return originalUrl.replace(`/${defaultLocale}`, '');
111
- }
112
- return null; // URL doesn't have default locale
113
- }
114
- else {
115
- // Add locale to URLs that don't have it: '/docs/file' -> '/docs/en/file'
116
- if (originalUrl.includes(`/${defaultLocale}/`) ||
117
- originalUrl.endsWith(`/${defaultLocale}`)) {
118
- return null; // Already has default locale
119
- }
120
- if (originalUrl.startsWith(patternHead)) {
121
- const pathAfterHead = originalUrl.slice(patternHead.length);
122
- if (pathAfterHead) {
123
- return `${patternHead}${defaultLocale}/${pathAfterHead}`;
124
- }
125
- else {
126
- return `${patternHead.replace(/\/$/, '')}/${defaultLocale}`;
127
- }
128
- }
129
- return null; // URL doesn't match pattern
130
- }
131
- }
132
- /**
133
- * Transforms URL for non-default locale processing with hideDefaultLocale=true
119
+ * Main URL transformation function that delegates to specific scenarios
134
120
  */
135
- function transformNonDefaultLocaleUrlWithHidden(originalUrl, patternHead, targetLocale, defaultLocale) {
136
- // Check if already localized
137
- if (originalUrl.startsWith(`${patternHead}${targetLocale}/`) ||
138
- originalUrl === `${patternHead}${targetLocale}`) {
121
+ export function transformUrlPath(originalUrl, patternHead, targetLocale, defaultLocale, hideDefaultLocale) {
122
+ const originalPathArray = originalUrl
123
+ .split('/')
124
+ .filter((path) => path !== '');
125
+ const patternHeadArray = patternHead.split('/').filter((path) => path !== '');
126
+ // check if the pattern head matches the original path
127
+ if (!checkIfPathMatchesPattern(originalPathArray, patternHeadArray)) {
139
128
  return null;
140
129
  }
141
- // Replace default locale with target locale
142
- const expectedPathWithDefaultLocale = `${patternHead}${defaultLocale}`;
143
- if (originalUrl.startsWith(`${expectedPathWithDefaultLocale}/`) ||
144
- originalUrl === expectedPathWithDefaultLocale) {
145
- return originalUrl.replace(`${patternHead}${defaultLocale}`, `${patternHead}${targetLocale}`);
146
- }
147
- // Handle exact pattern match
148
- if (originalUrl === patternHead.replace(/\/$/, '')) {
149
- return `${patternHead.replace(/\/$/, '')}/${targetLocale}`;
130
+ if (patternHeadArray.length > originalPathArray.length) {
131
+ return null; // Pattern is longer than the URL path
150
132
  }
151
- // Add target locale to URL without any locale
152
- const pathAfterHead = originalUrl.slice(originalUrl.startsWith(patternHead) ? patternHead.length : 0);
153
- return pathAfterHead
154
- ? `${patternHead}${targetLocale}/${pathAfterHead}`
155
- : `${patternHead}${targetLocale}`;
156
- }
157
- /**
158
- * Transforms URL for non-default locale processing with hideDefaultLocale=false
159
- */
160
- function transformNonDefaultLocaleUrl(originalUrl, patternHead, targetLocale, defaultLocale) {
161
- const expectedPathWithLocale = `${patternHead}${defaultLocale}`;
162
- if (originalUrl.startsWith(`${expectedPathWithLocale}/`) ||
163
- originalUrl === expectedPathWithLocale) {
164
- // Replace existing default locale with target locale
165
- return originalUrl.replace(`${patternHead}${defaultLocale}`, `${patternHead}${targetLocale}`);
166
- }
167
- else if (originalUrl.startsWith(patternHead)) {
168
- // Add target locale to URL that doesn't have any locale
169
- const pathAfterHead = originalUrl.slice(patternHead.length);
170
- if (pathAfterHead) {
171
- return `${patternHead}${targetLocale}/${pathAfterHead}`;
133
+ let result = null;
134
+ if (targetLocale === defaultLocale) {
135
+ if (hideDefaultLocale) {
136
+ // check if default locale is already present
137
+ if (originalPathArray?.[patternHeadArray.length] !== defaultLocale) {
138
+ return null;
139
+ }
140
+ // remove default locale
141
+ const newPathArray = [
142
+ ...originalPathArray.slice(0, patternHeadArray.length),
143
+ ...originalPathArray.slice(patternHeadArray.length + 1),
144
+ ];
145
+ result = newPathArray.join('/');
172
146
  }
173
147
  else {
174
- return `${patternHead.replace(/\/$/, '')}/${targetLocale}`;
148
+ // check if default locale is already present
149
+ if (originalPathArray?.[patternHeadArray.length] === defaultLocale) {
150
+ return null;
151
+ }
152
+ // insert default locale
153
+ const newPathArray = [
154
+ ...originalPathArray.slice(0, patternHeadArray.length),
155
+ defaultLocale,
156
+ ...originalPathArray.slice(patternHeadArray.length),
157
+ ];
158
+ result = newPathArray.join('/');
175
159
  }
176
160
  }
177
- return null; // URL doesn't match pattern
178
- }
179
- /**
180
- * Main URL transformation function that delegates to specific scenarios
181
- */
182
- function transformUrlPath(originalUrl, patternHead, targetLocale, defaultLocale, hideDefaultLocale) {
183
- if (targetLocale === defaultLocale) {
184
- return transformDefaultLocaleUrl(originalUrl, patternHead, defaultLocale, hideDefaultLocale);
185
- }
186
161
  else if (hideDefaultLocale) {
187
- return transformNonDefaultLocaleUrlWithHidden(originalUrl, patternHead, targetLocale, defaultLocale);
162
+ const newPathArray = [
163
+ ...originalPathArray.slice(0, patternHeadArray.length),
164
+ targetLocale,
165
+ ...originalPathArray.slice(patternHeadArray.length),
166
+ ];
167
+ result = newPathArray.join('/');
188
168
  }
189
169
  else {
190
- return transformNonDefaultLocaleUrl(originalUrl, patternHead, targetLocale, defaultLocale);
170
+ // check default locale
171
+ if (originalPathArray?.[patternHeadArray.length] !== defaultLocale) {
172
+ return null;
173
+ }
174
+ // replace default locale with target locale
175
+ const newPathArray = [...originalPathArray];
176
+ newPathArray[patternHeadArray.length] = targetLocale;
177
+ result = newPathArray.join('/');
178
+ }
179
+ // check for leading and trailing slashes
180
+ if (originalUrl.startsWith('/')) {
181
+ result = '/' + result;
191
182
  }
183
+ if (originalUrl.endsWith('/')) {
184
+ result = result + '/';
185
+ }
186
+ return result;
192
187
  }
193
188
  /**
194
189
  * AST-based transformation for MDX files using remark-mdx
195
190
  */
196
- function transformMdxUrls(mdxContent, defaultLocale, targetLocale, hideDefaultLocale, pattern = '/[locale]', exclude = []) {
191
+ function transformMdxUrls(mdxContent, defaultLocale, targetLocale, hideDefaultLocale, pattern = '/[locale]', exclude = [], baseDomain) {
197
192
  const transformedUrls = [];
198
193
  if (!pattern.startsWith('/')) {
199
194
  pattern = '/' + pattern;
@@ -246,7 +241,18 @@ function transformMdxUrls(mdxContent, defaultLocale, targetLocale, hideDefaultLo
246
241
  // Helper function to transform URL based on pattern
247
242
  const transformUrl = (originalUrl, linkType) => {
248
243
  // Check if URL should be processed
249
- if (!shouldProcessUrl(originalUrl, patternHead, targetLocale, defaultLocale)) {
244
+ if (!shouldProcessUrl(originalUrl, patternHead, targetLocale, defaultLocale, baseDomain)) {
245
+ return null;
246
+ }
247
+ // Skip absolute URLs (http://, https://, //, etc.)
248
+ if (baseDomain && shouldProcessAbsoluteUrl(originalUrl, baseDomain)) {
249
+ // Get everything after the base domain
250
+ const afterDomain = originalUrl.substring(baseDomain.length);
251
+ const transformedPath = transformUrlPath(afterDomain, patternHead, targetLocale, defaultLocale, hideDefaultLocale);
252
+ return transformedPath ? baseDomain + transformedPath : null;
253
+ }
254
+ // Exclude colon-prefixed URLs (http://, https://, //, etc.)
255
+ if (originalUrl.split('?')[0].includes(':')) {
250
256
  return null;
251
257
  }
252
258
  // Transform the URL based on locale and configuration
@@ -381,8 +387,23 @@ function transformMdxUrls(mdxContent, defaultLocale, targetLocale, hideDefaultLo
381
387
  }
382
388
  // AST-based transformation for MDX files using remark
383
389
  function localizeStaticUrlsForFile(file, defaultLocale, targetLocale, hideDefaultLocale, pattern = '/[locale]', // eg /docs/[locale] or /[locale]
384
- exclude = []) {
390
+ exclude = [], baseDomain) {
385
391
  // Use AST-based transformation for MDX files
386
- const result = transformMdxUrls(file, defaultLocale, targetLocale, hideDefaultLocale, pattern, exclude);
387
- return result.content;
392
+ return transformMdxUrls(file, defaultLocale, targetLocale, hideDefaultLocale, pattern, exclude, baseDomain || '');
393
+ }
394
+ function cleanPath(path) {
395
+ let cleanedPath = path.startsWith('/') ? path.slice(1) : path;
396
+ cleanedPath = cleanedPath.endsWith('/')
397
+ ? cleanedPath.slice(0, -1)
398
+ : cleanedPath;
399
+ return cleanedPath;
400
+ }
401
+ function checkIfPathMatchesPattern(originalUrlArray, patternHeadArray) {
402
+ // check if the pattern head matches the original path
403
+ for (let i = 0; i < patternHeadArray.length; i++) {
404
+ if (patternHeadArray[i] !== originalUrlArray?.[i]) {
405
+ return false;
406
+ }
407
+ }
408
+ return true;
388
409
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gtx-cli",
3
- "version": "2.1.5-alpha.3",
3
+ "version": "2.1.5-alpha.6",
4
4
  "main": "dist/index.js",
5
5
  "bin": "dist/main.js",
6
6
  "files": [