gtx-cli 2.0.10 → 2.0.12

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,17 @@
1
1
  # gtx-cli
2
2
 
3
+ ## 2.0.12
4
+
5
+ ### Patch Changes
6
+
7
+ - [#503](https://github.com/generaltranslation/gt/pull/503) [`9549d88`](https://github.com/generaltranslation/gt/commit/9549d88485af4dc57fb19847016d53aa3375b380) Thanks [@brian-lou](https://github.com/brian-lou)! - Fix path resolution mechanism for useGT/getGT usage scanning
8
+
9
+ ## 2.0.11
10
+
11
+ ### Patch Changes
12
+
13
+ - [#501](https://github.com/generaltranslation/gt/pull/501) [`d353c84`](https://github.com/generaltranslation/gt/commit/d353c84aaa159dbc77cff3ac29953adef4c64597) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - feat: add localization for href
14
+
3
15
  ## 2.0.10
4
16
 
5
17
  ### Patch Changes
@@ -11,7 +11,7 @@ import fs from 'node:fs';
11
11
  import path from 'node:path';
12
12
  import { parse } from '@babel/parser';
13
13
  import { createMatchPath, loadConfig } from 'tsconfig-paths';
14
- import * as resolve from 'resolve';
14
+ import resolve from 'resolve';
15
15
  /**
16
16
  * Processes a single translation function call (e.g., t('hello world', { id: 'greeting' })).
17
17
  * Extracts the translatable string content and metadata, then adds it to the updates array.
@@ -31,8 +31,9 @@ export default async function localizeStaticUrls(settings) {
31
31
  const fileContent = await fs.promises.readFile(filePath, 'utf8');
32
32
  // Localize the file
33
33
  const localizedFile = localizeStaticUrlsForFile(fileContent, settings.defaultLocale, locale, settings.experimentalHideDefaultLocale || false, settings.options?.docsUrlPattern);
34
+ const localizedFileHrefs = localizeStaticHrefsForFile(localizedFile, settings.defaultLocale, locale, settings.experimentalHideDefaultLocale || false, settings.options?.docsUrlPattern);
34
35
  // Write the localized file to the target path
35
- await fs.promises.writeFile(filePath, localizedFile);
36
+ await fs.promises.writeFile(filePath, localizedFileHrefs);
36
37
  }));
37
38
  }));
38
39
  }
@@ -44,14 +45,18 @@ function localizeStaticUrlsForFile(file, defaultLocale, targetLocale, hideDefaul
44
45
  }
45
46
  // 1. Search for all instances of:
46
47
  const patternHead = pattern.split('[locale]')[0];
48
+ // Escape special regex characters and remove trailing slash if present
49
+ const escapedPatternHead = patternHead
50
+ .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
51
+ .replace(/\/$/, '');
47
52
  let regex;
48
53
  if (hideDefaultLocale) {
49
54
  // Match complete markdown links: `](/docs/...)` or `](/docs)`
50
- regex = new RegExp(`\\]\\(${patternHead}(?:/([^)]*))?\\)`, 'g');
55
+ regex = new RegExp(`\\]\\(${escapedPatternHead}(?:/([^)]*))?\\)`, 'g');
51
56
  }
52
57
  else {
53
58
  // Match complete markdown links with default locale: `](/docs/${defaultLocale}/...)` or `](/docs/${defaultLocale})`
54
- regex = new RegExp(`\\]\\(${patternHead}${defaultLocale}(?:/([^)]*))?\\)`, 'g');
59
+ regex = new RegExp(`\\]\\(${escapedPatternHead}/${defaultLocale}(?:/([^)]*))?\\)`, 'g');
55
60
  }
56
61
  const matches = file.match(regex);
57
62
  if (!matches) {
@@ -81,3 +86,51 @@ function localizeStaticUrlsForFile(file, defaultLocale, targetLocale, hideDefaul
81
86
  });
82
87
  return localizedFile;
83
88
  }
89
+ function localizeStaticHrefsForFile(file, defaultLocale, targetLocale, hideDefaultLocale, pattern = '/[locale]' // eg /docs/[locale] or /[locale]
90
+ ) {
91
+ if (!pattern.startsWith('/')) {
92
+ pattern = '/' + pattern;
93
+ }
94
+ // 1. Search for all instances of:
95
+ const patternHead = pattern.split('[locale]')[0];
96
+ // Escape special regex characters and remove trailing slash if present
97
+ const escapedPatternHead = patternHead
98
+ .replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
99
+ .replace(/\/$/, '');
100
+ let regex;
101
+ if (hideDefaultLocale) {
102
+ // Match complete href attributes: `href="/docs/..."` or `href="/docs"`
103
+ regex = new RegExp(`href="${escapedPatternHead}(?:/([^"]*))?"`, 'g');
104
+ }
105
+ else {
106
+ // Match complete href attributes with default locale: `href="/docs/${defaultLocale}/..."` or `href="/docs/${defaultLocale}"`
107
+ regex = new RegExp(`href="${escapedPatternHead}/${defaultLocale}(?:/([^"]*))?"`, 'g');
108
+ }
109
+ const matches = file.match(regex);
110
+ if (!matches) {
111
+ return file;
112
+ }
113
+ // 2. Replace the default locale with the target locale in all matched instances
114
+ const localizedFile = file.replace(regex, (match, pathContent) => {
115
+ if (hideDefaultLocale) {
116
+ // For hideDefaultLocale, check if path already has target locale
117
+ if (pathContent) {
118
+ if (pathContent.startsWith(`${targetLocale}/`) ||
119
+ pathContent === targetLocale) {
120
+ return match; // Already localized
121
+ }
122
+ }
123
+ // Add target locale to the path
124
+ if (!pathContent || pathContent === '') {
125
+ return `href="${patternHead}${targetLocale}"`;
126
+ }
127
+ return `href="${patternHead}${targetLocale}/${pathContent}"`;
128
+ }
129
+ else {
130
+ // For non-hideDefaultLocale, replace defaultLocale with targetLocale
131
+ // pathContent contains everything after the default locale (no leading slash if present)
132
+ return `href="${patternHead}${targetLocale}${pathContent ? '/' + pathContent : ''}"`;
133
+ }
134
+ });
135
+ return localizedFile;
136
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gtx-cli",
3
- "version": "2.0.10",
3
+ "version": "2.0.12",
4
4
  "main": "dist/index.js",
5
5
  "bin": "dist/main.js",
6
6
  "files": [