gtx-cli 2.0.10 → 2.0.11
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 +6 -0
- package/dist/utils/localizeStaticUrls.js +56 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# gtx-cli
|
|
2
2
|
|
|
3
|
+
## 2.0.11
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#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
|
|
8
|
+
|
|
3
9
|
## 2.0.10
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -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,
|
|
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(`\\]\\(${
|
|
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(`\\]\\(${
|
|
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
|
+
}
|