gtx-cli 2.0.8 → 2.0.9

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # gtx-cli
2
2
 
3
+ ## 2.0.9
4
+
5
+ ### Patch Changes
6
+
7
+ - [#497](https://github.com/generaltranslation/gt/pull/497) [`0f44ba0`](https://github.com/generaltranslation/gt/commit/0f44ba0eb1b31f339a43854efe4c64ca2df7e4ca) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - feat: add customizability for localize static url
8
+
3
9
  ## 2.0.8
4
10
 
5
11
  ### Patch Changes
@@ -60,6 +60,10 @@ export async function generateSettings(options, cwd = process.cwd()) {
60
60
  gtConfig.projectId !== projectIdEnv) {
61
61
  logErrorAndExit(`Project ID mismatch between ${chalk.green(gtConfig.projectId)} and ${chalk.green(projectIdEnv)}! Please use the same projectId in all configs.`);
62
62
  }
63
+ if (options.options?.docsUrlPattern &&
64
+ !options.options?.docsUrlPattern.includes('[locale]')) {
65
+ logErrorAndExit('Failed to localize static urls: URL pattern must include "[locale]" to denote the location of the locale');
66
+ }
63
67
  // merge options
64
68
  const mergedOptions = { ...gtConfig, ...options };
65
69
  // merge locales
@@ -98,6 +98,7 @@ export type AdditionalOptions = {
98
98
  jsonSchema?: {
99
99
  [fileGlob: string]: JsonSchema;
100
100
  };
101
+ docsUrlPattern?: string;
101
102
  };
102
103
  export type JsonSchema = {
103
104
  preset?: 'mintlify';
@@ -30,23 +30,28 @@ export default async function localizeStaticUrls(settings) {
30
30
  // Get file content
31
31
  const fileContent = fs.readFileSync(filePath, 'utf8');
32
32
  // Localize the file
33
- const localizedFile = localizeStaticUrlsForFile(fileContent, settings.defaultLocale, locale, settings.experimentalHideDefaultLocale || false);
33
+ const localizedFile = localizeStaticUrlsForFile(fileContent, settings.defaultLocale, locale, settings.experimentalHideDefaultLocale || false, settings.options?.docsUrlPattern);
34
34
  // Write the localized file to the target path
35
35
  await fs.promises.writeFile(filePath, localizedFile);
36
36
  }));
37
37
  }));
38
38
  }
39
- // Assumption: we will be seeing localized paths in the source files: (docs/en/ -> docs/ja/)
40
- function localizeStaticUrlsForFile(file, defaultLocale, targetLocale, hideDefaultLocale) {
39
+ // Naive find and replace, in the future, construct an AST
40
+ function localizeStaticUrlsForFile(file, defaultLocale, targetLocale, hideDefaultLocale, pattern = '/[locale]' // eg /docs/[locale] or /[locale]
41
+ ) {
42
+ if (!pattern.startsWith('/')) {
43
+ pattern = '/' + pattern;
44
+ }
41
45
  // 1. Search for all instances of:
46
+ const patternHead = pattern.split('[locale]')[0];
42
47
  let regex;
43
48
  if (hideDefaultLocale) {
44
49
  // Match complete markdown links: `](/docs/...)` or `](/docs)`
45
- regex = new RegExp(`\\]\\(/docs(?:/([^)]*))?\\)`, 'g');
50
+ regex = new RegExp(`\\]\\(${patternHead}(?:/([^)]*))?\\)`, 'g');
46
51
  }
47
52
  else {
48
53
  // Match complete markdown links with default locale: `](/docs/${defaultLocale}/...)` or `](/docs/${defaultLocale})`
49
- regex = new RegExp(`\\]\\(/docs/${defaultLocale}(?:/([^)]*))?\\)`, 'g');
54
+ regex = new RegExp(`\\]\\(${patternHead}${defaultLocale}(?:/([^)]*))?\\)`, 'g');
50
55
  }
51
56
  const matches = file.match(regex);
52
57
  if (!matches) {
@@ -64,14 +69,14 @@ function localizeStaticUrlsForFile(file, defaultLocale, targetLocale, hideDefaul
64
69
  }
65
70
  // Add target locale to the path
66
71
  if (!pathContent || pathContent === '') {
67
- return `](/docs/${targetLocale})`;
72
+ return `](${patternHead}${targetLocale})`;
68
73
  }
69
- return `](/docs/${targetLocale}/${pathContent})`;
74
+ return `](${patternHead}${targetLocale}/${pathContent})`;
70
75
  }
71
76
  else {
72
77
  // For non-hideDefaultLocale, replace defaultLocale with targetLocale
73
78
  // pathContent contains everything after the default locale (no leading slash if present)
74
- return `](/docs/${targetLocale}${pathContent ? '/' + pathContent : ''})`;
79
+ return `](${patternHead}${targetLocale}${pathContent ? '/' + pathContent : ''})`;
75
80
  }
76
81
  });
77
82
  return localizedFile;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gtx-cli",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "main": "dist/index.js",
5
5
  "bin": "dist/main.js",
6
6
  "files": [