@viteplus/versions 2.0.4 → 2.0.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/components/version-switcher.component.vue +83 -71
- package/dist/index.d.ts +815 -186
- package/dist/index.js +2 -2
- package/dist/index.js.map +5 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export type * from 'vitepress';
|
|
|
8
8
|
import type { DefaultTheme, UserConfig } from 'vitepress';
|
|
9
9
|
import type { DefaultTheme } from 'vitepress';
|
|
10
10
|
import type { LocaleSpecificConfig } from 'vitepress';
|
|
11
|
+
import type { SidebarItemType } from '@viteplus/versions';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Import will remove at compile time
|
|
@@ -679,7 +680,7 @@ export type SidebarType = Array<SidebarItemType> | PathSidebarInterface;
|
|
|
679
680
|
* @see SidebarItemType
|
|
680
681
|
* @since 2.0.0
|
|
681
682
|
*/
|
|
682
|
-
export type SidebarObjectType = Record<string, Array<SidebarItemType>>;
|
|
683
|
+
export type SidebarObjectType = Record<string, Array<SidebarItemType | SidebarGroupInterface>>;
|
|
683
684
|
|
|
684
685
|
/**
|
|
685
686
|
* Import will remove at compile time
|
|
@@ -916,7 +917,6 @@ type RequireKeysType<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
|
|
|
916
917
|
*
|
|
917
918
|
* const state = new StateModel(config).init();
|
|
918
919
|
* console.log(state.versionsList);
|
|
919
|
-
* console.log(state.fileMap);
|
|
920
920
|
* ```
|
|
921
921
|
*
|
|
922
922
|
* @since 2.0.0
|
|
@@ -1015,19 +1015,6 @@ declare class StateModel {
|
|
|
1015
1015
|
* @since 2.0.0
|
|
1016
1016
|
*/
|
|
1017
1017
|
readonly localesMap: Record<string, string>;
|
|
1018
|
-
/**
|
|
1019
|
-
* A map of version names to arrays of Markdown file paths relative to the sources directory.
|
|
1020
|
-
*
|
|
1021
|
-
* @remarks
|
|
1022
|
-
* - The key is the version name (or `''` for the root/source version).
|
|
1023
|
-
* - The value is an array of file paths (strings) for that version.
|
|
1024
|
-
*
|
|
1025
|
-
* This structure is built during initialization to facilitate generating versioned
|
|
1026
|
-
* documentation and navigation.
|
|
1027
|
-
*
|
|
1028
|
-
* @since 2.0.0
|
|
1029
|
-
*/
|
|
1030
|
-
readonly fileMap: Record<string, Array<string>>;
|
|
1031
1018
|
/**
|
|
1032
1019
|
* Creates an instance of `StateModel`.
|
|
1033
1020
|
*
|
|
@@ -1047,18 +1034,101 @@ declare class StateModel {
|
|
|
1047
1034
|
*/
|
|
1048
1035
|
constructor(configuration?: ConfigurationInterface);
|
|
1049
1036
|
/**
|
|
1050
|
-
* Initializes the state by
|
|
1037
|
+
* Initializes the state model by scanning archive directory and setting up locale configuration.
|
|
1051
1038
|
*
|
|
1052
|
-
* @returns The instance
|
|
1039
|
+
* @returns The current instance for method chaining.
|
|
1053
1040
|
*
|
|
1054
1041
|
* @remarks
|
|
1055
|
-
* This method performs the
|
|
1056
|
-
*
|
|
1057
|
-
*
|
|
1058
|
-
*
|
|
1059
|
-
*
|
|
1060
|
-
*
|
|
1061
|
-
*
|
|
1042
|
+
* This method performs the initial setup sequence for a multi-version documentation system
|
|
1043
|
+
* by discovering available versions and preparing the locale infrastructure. It executes
|
|
1044
|
+
* three primary operations:
|
|
1045
|
+
*
|
|
1046
|
+
* 1. **Root Locale Initialization**: Sets up the root locale entry in {@link vitepressConfig.locales}
|
|
1047
|
+
* with an empty theme configuration, establishing the foundation for all locale-specific settings
|
|
1048
|
+
* 2. **Version Discovery**: Scans {@link archivePath} using {@link readdirSync} to identify all
|
|
1049
|
+
* subdirectories, which represent available documentation versions. Each subdirectory name
|
|
1050
|
+
* is added to {@link versionsList}
|
|
1051
|
+
* 3. **Locale Map Setup**: Calls {@link initializeLocalesMap} to establish the locale mapping
|
|
1052
|
+
* structure and populate versioned locale entries
|
|
1053
|
+
*
|
|
1054
|
+
* The archive directory structure is expected to contain version folders:
|
|
1055
|
+
* ```
|
|
1056
|
+
* archive/
|
|
1057
|
+
* ├── v1.0/
|
|
1058
|
+
* ├── v2.0/
|
|
1059
|
+
* └── latest/
|
|
1060
|
+
* ```
|
|
1061
|
+
*
|
|
1062
|
+
* Each subdirectory discovered becomes a version entry, enabling the system to generate
|
|
1063
|
+
* versioned locale configurations and routing structures for multi-version documentation.
|
|
1064
|
+
*
|
|
1065
|
+
* The root locale is initialized first to ensure a consistent base exists before user-defined
|
|
1066
|
+
* locale configurations are applied through {@link initializeLocalesMap}.
|
|
1067
|
+
*
|
|
1068
|
+
* Method chaining support allows for fluent initialization patterns, making it easy to
|
|
1069
|
+
* combine multiple setup operations in a readable sequence.
|
|
1070
|
+
*
|
|
1071
|
+
* @example
|
|
1072
|
+
* ```ts
|
|
1073
|
+
* // Basic initialization
|
|
1074
|
+
* const state = new StateModel(config);
|
|
1075
|
+
* state.init();
|
|
1076
|
+
*
|
|
1077
|
+
* console.log(state.versionsList);
|
|
1078
|
+
* // ['v1.0', 'v2.0', 'v3.0', 'latest']
|
|
1079
|
+
*
|
|
1080
|
+
* console.log(state.vitepressConfig.locales['root']);
|
|
1081
|
+
* // { themeConfig: {} }
|
|
1082
|
+
*
|
|
1083
|
+
* // Method chaining pattern
|
|
1084
|
+
* const state = new StateModel(config)
|
|
1085
|
+
* .init();
|
|
1086
|
+
*
|
|
1087
|
+
* console.log(`Initialized with ${state.versionsList.length} versions`);
|
|
1088
|
+
*
|
|
1089
|
+
* // Use case: Dynamic version detection
|
|
1090
|
+
* const config = {
|
|
1091
|
+
* versionsConfig: {
|
|
1092
|
+
* sources: 'src',
|
|
1093
|
+
* archive: 'versions'
|
|
1094
|
+
* },
|
|
1095
|
+
* locales: {
|
|
1096
|
+
* root: { lang: 'en-US', label: 'English' },
|
|
1097
|
+
* '/de/': { lang: 'de-DE', label: 'Deutsch' }
|
|
1098
|
+
* }
|
|
1099
|
+
* };
|
|
1100
|
+
*
|
|
1101
|
+
* const state = new StateModel(config).init();
|
|
1102
|
+
*
|
|
1103
|
+
* // Archive structure:
|
|
1104
|
+
* // versions/
|
|
1105
|
+
* // ├── v1.0.0/
|
|
1106
|
+
* // ├── v2.0.0/
|
|
1107
|
+
* // └── latest/
|
|
1108
|
+
*
|
|
1109
|
+
* console.log(state.versionsList);
|
|
1110
|
+
* // ['v1.0.0', 'v2.0.0']
|
|
1111
|
+
*
|
|
1112
|
+
* // Locale entries created for each version
|
|
1113
|
+
* console.log(Object.keys(state.vitepressConfig.locales));
|
|
1114
|
+
* // ['root', 'v1.0.0', 'v2.0.0', 'latest', '/de/', 'de/v1.0.0', 'de/v2.0.0']
|
|
1115
|
+
*
|
|
1116
|
+
* // Use case: Error handling
|
|
1117
|
+
* try {
|
|
1118
|
+
* const state = new StateModel(config).init();
|
|
1119
|
+
* if (state.versionsList.length === 0) {
|
|
1120
|
+
* console.warn('No versions found in archive');
|
|
1121
|
+
* }
|
|
1122
|
+
* } catch (error) {
|
|
1123
|
+
* console.error('Failed to initialize state:', error);
|
|
1124
|
+
* }
|
|
1125
|
+
* ```
|
|
1126
|
+
*
|
|
1127
|
+
* @see archivePath
|
|
1128
|
+
* @see readdirSync
|
|
1129
|
+
* @see versionsList
|
|
1130
|
+
* @see vitepressConfig
|
|
1131
|
+
* @see initializeLocalesMap
|
|
1062
1132
|
*
|
|
1063
1133
|
* @since 2.0.0
|
|
1064
1134
|
*/
|
|
@@ -1105,6 +1175,156 @@ declare class StateModel {
|
|
|
1105
1175
|
private createVitepressConfig;
|
|
1106
1176
|
}
|
|
1107
1177
|
|
|
1178
|
+
/**
|
|
1179
|
+
* Import will remove at compile time
|
|
1180
|
+
*/
|
|
1181
|
+
/**
|
|
1182
|
+
* Extracts the language code from a locale string.
|
|
1183
|
+
*
|
|
1184
|
+
* @param locale - The locale string to parse (e.g., 'en-US', 'fr_CA', 'zh-Hans-CN').
|
|
1185
|
+
* @returns The language code portion of the locale (e.g., 'en', 'fr', 'zh').
|
|
1186
|
+
*
|
|
1187
|
+
* @remarks
|
|
1188
|
+
* This function parses a locale string and returns only the primary language code,
|
|
1189
|
+
* discarding region, script, and variant information. It handles both standard
|
|
1190
|
+
* separators used in locale strings:
|
|
1191
|
+
* - Hyphen (`-`): BCP 47 standard (e.g., 'en-US', 'zh-Hans-CN')
|
|
1192
|
+
* - Underscore (`_`): POSIX/Java standard (e.g., 'en_US', 'pt_BR')
|
|
1193
|
+
*
|
|
1194
|
+
* The language code is always the first component of a locale identifier and
|
|
1195
|
+
* represents the base language without regional or script variations.
|
|
1196
|
+
*
|
|
1197
|
+
* Common use cases:
|
|
1198
|
+
* - Fallback language selection when a specific locale is unavailable
|
|
1199
|
+
* - Grouping locales by language family
|
|
1200
|
+
* - Simplifying locale matching for translations
|
|
1201
|
+
* - Language detection without regional specificity
|
|
1202
|
+
* - API requests that only accept language codes
|
|
1203
|
+
*
|
|
1204
|
+
* Locale format examples:
|
|
1205
|
+
* - Language only: 'en', 'fr', 'ja'
|
|
1206
|
+
* - Language + Region: 'en-US', 'fr-CA', 'pt-BR'
|
|
1207
|
+
* - Language + Script + Region: 'zh-Hans-CN', 'zh-Hant-TW'
|
|
1208
|
+
* - POSIX format: 'en_US', 'de_DE', 'es_MX'
|
|
1209
|
+
*
|
|
1210
|
+
* @example
|
|
1211
|
+
* ```ts
|
|
1212
|
+
* // BCP 47 format with region
|
|
1213
|
+
* console.log(getLanguageOnly('en-US')); // 'en'
|
|
1214
|
+
* console.log(getLanguageOnly('fr-CA')); // 'fr'
|
|
1215
|
+
* console.log(getLanguageOnly('pt-BR')); // 'pt'
|
|
1216
|
+
*
|
|
1217
|
+
* // POSIX format with underscore
|
|
1218
|
+
* console.log(getLanguageOnly('en_GB')); // 'en'
|
|
1219
|
+
* console.log(getLanguageOnly('de_DE')); // 'de'
|
|
1220
|
+
*
|
|
1221
|
+
* // Complex locale with script
|
|
1222
|
+
* console.log(getLanguageOnly('zh-Hans-CN')); // 'zh'
|
|
1223
|
+
* console.log(getLanguageOnly('zh-Hant-TW')); // 'zh'
|
|
1224
|
+
*
|
|
1225
|
+
* // Language code only (no separator)
|
|
1226
|
+
* console.log(getLanguageOnly('ja')); // 'ja'
|
|
1227
|
+
* console.log(getLanguageOnly('ko')); // 'ko'
|
|
1228
|
+
*
|
|
1229
|
+
* // Use case: Fallback language selection
|
|
1230
|
+
* const userLocale = 'es-MX';
|
|
1231
|
+
* const language = getLanguageOnly(userLocale); // 'es'
|
|
1232
|
+
* const translations = loadTranslations(language); // Load Spanish translations
|
|
1233
|
+
* ```
|
|
1234
|
+
*
|
|
1235
|
+
* @since 2.0.5
|
|
1236
|
+
*/
|
|
1237
|
+
declare function getLanguageOnly(locale: string): string;
|
|
1238
|
+
/**
|
|
1239
|
+
* Parses and configures locale settings for a multi-version, multi-language documentation site.
|
|
1240
|
+
*
|
|
1241
|
+
* @remarks
|
|
1242
|
+
* This function orchestrates the complete locale configuration process by combining
|
|
1243
|
+
* user-defined locale settings with version information to generate a comprehensive
|
|
1244
|
+
* locale map. It handles both root and non-root locales across multiple documentation
|
|
1245
|
+
* versions.
|
|
1246
|
+
*
|
|
1247
|
+
* The function operates on injected state from {@link StateModel} and processes:
|
|
1248
|
+
* - {@link versionsList}: Available documentation versions (e.g., ['v1', 'v2', 'v3'])
|
|
1249
|
+
* - {@link vitepressConfig}: VitePress configuration containing locale definitions
|
|
1250
|
+
* - {@link configuration}: User-provided locale configurations
|
|
1251
|
+
* - {@link localesMap}: Mapping between locale keys and indices
|
|
1252
|
+
*
|
|
1253
|
+
* Processing workflow:
|
|
1254
|
+
* 1. Injects application state to access configuration data
|
|
1255
|
+
* 2. Determines if 'root' key exists in user-provided locales
|
|
1256
|
+
* 3. Iterates through each locale in the locale map
|
|
1257
|
+
* 4. Creates sanitized base locale using {@link createSanitizedLocale} (removes nav/sidebar)
|
|
1258
|
+
* 5. Sets up the root locale entry with a generated link via {@link generateLocaleLink}
|
|
1259
|
+
* 6. Creates versioned locale entries for each version using {@link createVersionedLocale}
|
|
1260
|
+
*
|
|
1261
|
+
* Locale key structure:
|
|
1262
|
+
* - Root locale: Direct version keys (e.g., 'v1', 'v2')
|
|
1263
|
+
* - Non-root locales: Prefixed version keys (e.g., 'fr/v1', 'es/v2')
|
|
1264
|
+
*
|
|
1265
|
+
* The sanitization process removes navigation structures to reduce memory overhead
|
|
1266
|
+
* and prevent duplication of large theme configuration objects across versions.
|
|
1267
|
+
*
|
|
1268
|
+
* @example
|
|
1269
|
+
* ```ts
|
|
1270
|
+
* // Configuration state before parsing
|
|
1271
|
+
* const state = {
|
|
1272
|
+
* versionsList: ['v1', 'v2'],
|
|
1273
|
+
* localesMap: {
|
|
1274
|
+
* '/': 'root',
|
|
1275
|
+
* '/fr/': 'fr',
|
|
1276
|
+
* '/es/': 'es'
|
|
1277
|
+
* },
|
|
1278
|
+
* configuration: {
|
|
1279
|
+
* locales: {
|
|
1280
|
+
* root: {
|
|
1281
|
+
* lang: 'en-US',
|
|
1282
|
+
* label: 'English',
|
|
1283
|
+
* themeConfig: { nav: [...], sidebar: {...} }
|
|
1284
|
+
* },
|
|
1285
|
+
* fr: {
|
|
1286
|
+
* lang: 'fr-CA',
|
|
1287
|
+
* label: 'Français',
|
|
1288
|
+
* themeConfig: { nav: [...], sidebar: {...} }
|
|
1289
|
+
* }
|
|
1290
|
+
* }
|
|
1291
|
+
* }
|
|
1292
|
+
* };
|
|
1293
|
+
*
|
|
1294
|
+
* parseLocale();
|
|
1295
|
+
*
|
|
1296
|
+
* // Result in vitepressConfig.locales:
|
|
1297
|
+
* // {
|
|
1298
|
+
* // 'root': { lang: 'en-US', label: 'English', link: '/', ... },
|
|
1299
|
+
* // 'v1': { lang: 'en-US', label: ' ', link: '/', ... },
|
|
1300
|
+
* // 'v2': { lang: 'en-US', label: ' ', link: '/', ... },
|
|
1301
|
+
* // 'fr': { lang: 'fr-CA', label: 'Français', link: '/fr/', ... },
|
|
1302
|
+
* // 'fr/v1': { lang: 'fr-CA', label: ' ', link: '/fr/', ... },
|
|
1303
|
+
* // 'fr/v2': { lang: 'fr-CA', label: ' ', link: '/fr/', ... }
|
|
1304
|
+
* // }
|
|
1305
|
+
*
|
|
1306
|
+
* // Use case: Multi-version documentation with multiple languages
|
|
1307
|
+
* const state = inject(StateModel);
|
|
1308
|
+
* state.versionsList = ['latest', 'v3', 'v2'];
|
|
1309
|
+
* state.configuration.locales = {
|
|
1310
|
+
* '/': { lang: 'en-US', label: 'English' },
|
|
1311
|
+
* '/de/': { lang: 'de-DE', label: 'Deutsch' },
|
|
1312
|
+
* '/ja/': { lang: 'ja-JP', label: '日本語' }
|
|
1313
|
+
* };
|
|
1314
|
+
*
|
|
1315
|
+
* parseLocale();
|
|
1316
|
+
* // Generates 12 locale entries (3 versions × 3 languages + 3 base locales)
|
|
1317
|
+
* ```
|
|
1318
|
+
*
|
|
1319
|
+
* @see StateModel
|
|
1320
|
+
* @see generateLocaleLink
|
|
1321
|
+
* @see createSanitizedLocale
|
|
1322
|
+
* @see createVersionedLocale
|
|
1323
|
+
*
|
|
1324
|
+
* @since 2.0.5
|
|
1325
|
+
*/
|
|
1326
|
+
declare function parseLocale(): void;
|
|
1327
|
+
|
|
1108
1328
|
/**
|
|
1109
1329
|
* Import will remove at compile time
|
|
1110
1330
|
*/
|
|
@@ -1412,7 +1632,7 @@ interface InjectableOptionsInterface<T extends ConstructorType = ConstructorType
|
|
|
1412
1632
|
declare const defaultConfiguration: ConfigurationInterface;
|
|
1413
1633
|
|
|
1414
1634
|
/**
|
|
1415
|
-
*
|
|
1635
|
+
* Import will remove at compile time
|
|
1416
1636
|
*/
|
|
1417
1637
|
/**
|
|
1418
1638
|
* Rewrites a documentation source path using version and locale.
|
|
@@ -1437,6 +1657,75 @@ declare const defaultConfiguration: ConfigurationInterface;
|
|
|
1437
1657
|
* @since 2.0.0
|
|
1438
1658
|
*/
|
|
1439
1659
|
declare function rewritesHook(source: string, version: string, locale: string): string;
|
|
1660
|
+
/**
|
|
1661
|
+
* Extracts locale and version information from URL path segments.
|
|
1662
|
+
*
|
|
1663
|
+
* @param segments - Array of URL path segments to parse (will be modified by shifting elements).
|
|
1664
|
+
* @param localesList - List of valid locale identifiers for matching.
|
|
1665
|
+
* @param versionsList - List of valid version identifiers for matching.
|
|
1666
|
+
* @returns An object containing extracted language and version {@link PathSegmentsInterface}.
|
|
1667
|
+
*
|
|
1668
|
+
* @remarks
|
|
1669
|
+
* This function parses URL path segments to identify and extract locale and version
|
|
1670
|
+
* information, supporting both version-first and locale-first URL structures. The
|
|
1671
|
+
* function operates on the segment array destructively, removing matched elements
|
|
1672
|
+
* via {@link Array.shift}.
|
|
1673
|
+
*
|
|
1674
|
+
* **Parsing logic**:
|
|
1675
|
+
* 1. If the first segment matches a version → extract it as `version`.
|
|
1676
|
+
* 2. If the first segment matches a locale → extract it as `lang`.
|
|
1677
|
+
* 3. Return both extracted values (empty strings if not found).
|
|
1678
|
+
*
|
|
1679
|
+
* **URL patterns supported**:
|
|
1680
|
+
* - `/v2.0/guide/` → `{ lang: '', version: 'v2.0' }`
|
|
1681
|
+
* - `/fr/v2.0/guide/` → `{ lang: 'fr', version: 'v2.0' }`
|
|
1682
|
+
* - `/fr/guide/` → `{ lang: 'fr', version: '' }`
|
|
1683
|
+
* - `/guide/` → `{ lang: '', version: '' }`
|
|
1684
|
+
*
|
|
1685
|
+
* The segment array is mutated during extraction, with matched segments removed
|
|
1686
|
+
* from the beginning. This allows the following processing to work with the remaining
|
|
1687
|
+
* path components.
|
|
1688
|
+
*
|
|
1689
|
+
* @example
|
|
1690
|
+
* ```ts
|
|
1691
|
+
* const localesList = ['en', 'fr', 'de'];
|
|
1692
|
+
* const versionsList = ['v1.0', 'v2.0', 'latest'];
|
|
1693
|
+
*
|
|
1694
|
+
* // Version-first pattern
|
|
1695
|
+
* const segments1 = ['v2.0', 'guide', 'intro'];
|
|
1696
|
+
* const result1 = extractLocale(segments1, localesList, versionsList);
|
|
1697
|
+
* console.log(result1); // { lang: '', version: 'v2.0' }
|
|
1698
|
+
* console.log(segments1); // ['guide', 'intro']
|
|
1699
|
+
*
|
|
1700
|
+
* // Locale-first pattern
|
|
1701
|
+
* const segments2 = ['fr', 'v2.0', 'api', 'reference'];
|
|
1702
|
+
* const result2 = extractLocale(segments2, localesList, versionsList);
|
|
1703
|
+
* console.log(result2); // { lang: 'fr', version: 'v2.0' }
|
|
1704
|
+
* console.log(segments2); // ['api', 'reference']
|
|
1705
|
+
*
|
|
1706
|
+
* // No locale or version
|
|
1707
|
+
* const segments3 = ['docs', 'getting-started'];
|
|
1708
|
+
* const result3 = extractLocale(segments3, localesList, versionsList);
|
|
1709
|
+
* console.log(result3); // { lang: '', version: '' }
|
|
1710
|
+
* console.log(segments3); // ['docs', 'getting-started']
|
|
1711
|
+
*
|
|
1712
|
+
* // Use case: URL parsing for routing
|
|
1713
|
+
* const url = '/de/latest/guide/installation';
|
|
1714
|
+
* const segments = url.split('/').filter(Boolean);
|
|
1715
|
+
* const { lang, version } = extractLocale(
|
|
1716
|
+
* segments,
|
|
1717
|
+
* ['en', 'de', 'fr'],
|
|
1718
|
+
* ['latest', 'v1', 'v2']
|
|
1719
|
+
* );
|
|
1720
|
+
* const route = segments.join('/');
|
|
1721
|
+
* console.log({ lang, version, route });
|
|
1722
|
+
* // { lang: 'de', version: 'latest', route: 'guide/installation' }
|
|
1723
|
+
* ```
|
|
1724
|
+
*
|
|
1725
|
+
* @see PathSegmentsInterface
|
|
1726
|
+
* @since 2.0.5
|
|
1727
|
+
*/
|
|
1728
|
+
declare function extractLocale(segments: Array<string>, localesList: Array<string>, versionsList: Array<string>): PathSegmentsInterface;
|
|
1440
1729
|
/**
|
|
1441
1730
|
* Parses all documentation routes and generates rewritten paths for versioned and localized files.
|
|
1442
1731
|
*
|
|
@@ -1463,68 +1752,90 @@ declare function rewritesHook(source: string, version: string, locale: string):
|
|
|
1463
1752
|
declare function parseRoutesComponent(): void;
|
|
1464
1753
|
|
|
1465
1754
|
/**
|
|
1466
|
-
*
|
|
1467
|
-
*/
|
|
1468
|
-
/**
|
|
1469
|
-
* Deeply merges two object types into a single object.
|
|
1470
|
-
*
|
|
1471
|
-
* @typeParam T - The type of the first object.
|
|
1472
|
-
* @typeParam U - The type of the second object.
|
|
1473
|
-
*
|
|
1474
|
-
* @param obj1 - The base object whose properties will be merged.
|
|
1475
|
-
* @param obj2 - The object whose properties will overwrite or merge into `obj1`.
|
|
1476
|
-
* @returns A new object containing the deeply merged properties of both `obj1` and `obj2`.
|
|
1755
|
+
* Represents extracted locale and version information from URL path segments.
|
|
1477
1756
|
*
|
|
1478
1757
|
* @remarks
|
|
1479
|
-
*
|
|
1480
|
-
*
|
|
1481
|
-
*
|
|
1482
|
-
*
|
|
1483
|
-
*
|
|
1484
|
-
*
|
|
1758
|
+
* This interface defines the structure returned by {@link extractLocale} when parsing
|
|
1759
|
+
* URL paths in a multi-version, multi-locale documentation system. It separates the
|
|
1760
|
+
* locale identifier from the version identifier, enabling proper routing and content
|
|
1761
|
+
* resolution.
|
|
1762
|
+
*
|
|
1763
|
+
* Both properties use empty strings as sentinel values to indicate absence, rather
|
|
1764
|
+
* than undefined or null, simplifying string concatenation and path construction
|
|
1765
|
+
* operations.
|
|
1766
|
+
*
|
|
1767
|
+
* Common usage patterns:
|
|
1768
|
+
* - Route parsing and URL analysis
|
|
1769
|
+
* - Content path resolution
|
|
1770
|
+
* - Locale and version detection from request URLs
|
|
1771
|
+
* - Sidebar and navigation path construction
|
|
1772
|
+
* - Determining which content variant to serve
|
|
1773
|
+
*
|
|
1774
|
+
* The interface supports various URL structures:
|
|
1775
|
+
* - Root locale, no version: `{ lang: '', version: '' }` → `/guide/`
|
|
1776
|
+
* - Root locale with version: `{ lang: '', version: 'v2.0' }` → `/v2.0/guide/`
|
|
1777
|
+
* - Specific locale, no version: `{ lang: 'fr', version: '' }` → `/fr/guide/`
|
|
1778
|
+
* - Specific locale with version: `{ lang: 'fr', version: 'v2.0' }` → `/fr/v2.0/guide/`
|
|
1485
1779
|
*
|
|
1486
1780
|
* @example
|
|
1487
1781
|
* ```ts
|
|
1488
|
-
*
|
|
1489
|
-
* const
|
|
1490
|
-
*
|
|
1491
|
-
*
|
|
1492
|
-
*
|
|
1493
|
-
*
|
|
1494
|
-
*
|
|
1495
|
-
*
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
*
|
|
1500
|
-
*
|
|
1782
|
+
* // Parse URL segments
|
|
1783
|
+
* const segments = ['fr', 'v2.0', 'guide', 'intro'];
|
|
1784
|
+
* const result: PathSegmentsInterface = extractLocale(
|
|
1785
|
+
* segments,
|
|
1786
|
+
* ['en', 'fr', 'de'],
|
|
1787
|
+
* ['v1.0', 'v2.0', 'latest']
|
|
1788
|
+
* );
|
|
1789
|
+
* console.log(result);
|
|
1790
|
+
* // { lang: 'fr', version: 'v2.0' }
|
|
1791
|
+
*
|
|
1792
|
+
* // Handle missing locale/version
|
|
1793
|
+
* const segments2 = ['docs', 'api'];
|
|
1794
|
+
* const result2: PathSegmentsInterface = extractLocale(segments2, locales, versions);
|
|
1795
|
+
* console.log(result2);
|
|
1796
|
+
* // { lang: '', version: '' }
|
|
1797
|
+
*
|
|
1798
|
+
* // Use in routing logic
|
|
1799
|
+
* function resolveContent(path: string): string {
|
|
1800
|
+
* const segments = path.split('/').filter(Boolean);
|
|
1801
|
+
* const { lang, version }: PathSegmentsInterface = extractLocale(
|
|
1802
|
+
* segments,
|
|
1803
|
+
* availableLocales,
|
|
1804
|
+
* availableVersions
|
|
1805
|
+
* );
|
|
1806
|
+
*
|
|
1807
|
+
* const contentPath = [
|
|
1808
|
+
* lang || 'en',
|
|
1809
|
+
* version || 'latest',
|
|
1810
|
+
* ...segments
|
|
1811
|
+
* ].join('/');
|
|
1812
|
+
*
|
|
1813
|
+
* return loadContent(contentPath);
|
|
1814
|
+
* }
|
|
1501
1815
|
*
|
|
1502
|
-
*
|
|
1503
|
-
*
|
|
1504
|
-
*
|
|
1816
|
+
* // Use in path construction
|
|
1817
|
+
* function buildPath(info: PathSegmentsInterface, ...segments: string[]): string {
|
|
1818
|
+
* const parts = [
|
|
1819
|
+
* info.lang,
|
|
1820
|
+
* info.version,
|
|
1821
|
+
* ...segments
|
|
1822
|
+
* ].filter(Boolean);
|
|
1505
1823
|
*
|
|
1506
|
-
*
|
|
1507
|
-
*
|
|
1508
|
-
* - Only files with a `.md` extension are included in the results.
|
|
1509
|
-
* - Returned paths are **relative to `rootDir`**, regardless of how deep the files are nested.
|
|
1510
|
-
* - Uses Node.js `fs.readdirSync` with `withFileTypes: true` for efficient directory traversal.
|
|
1511
|
-
*
|
|
1512
|
-
* @example
|
|
1513
|
-
* ```ts
|
|
1514
|
-
* const markdownFiles = getAllMarkdownFilesRelative("/project/docs");
|
|
1824
|
+
* return '/' + parts.join('/');
|
|
1825
|
+
* }
|
|
1515
1826
|
*
|
|
1516
|
-
*
|
|
1517
|
-
*
|
|
1518
|
-
* //
|
|
1519
|
-
* // "README.md",
|
|
1520
|
-
* // "guide/intro.md",
|
|
1521
|
-
* // "tutorial/setup.md"
|
|
1522
|
-
* // ]
|
|
1827
|
+
* const pathInfo: PathSegmentsInterface = { lang: 'de', version: 'v3.0' };
|
|
1828
|
+
* console.log(buildPath(pathInfo, 'guide', 'intro'));
|
|
1829
|
+
* // '/de/v3.0/guide/intro'
|
|
1523
1830
|
* ```
|
|
1524
1831
|
*
|
|
1525
|
-
* @
|
|
1832
|
+
* @see extractLocale
|
|
1833
|
+
* @since 2.0.5
|
|
1526
1834
|
*/
|
|
1527
|
-
|
|
1835
|
+
interface PathSegmentsInterface {
|
|
1836
|
+
lang: string;
|
|
1837
|
+
version: string;
|
|
1838
|
+
}
|
|
1528
1839
|
|
|
1529
1840
|
/**
|
|
1530
1841
|
* Import will remove at compile time
|
|
@@ -1700,6 +2011,56 @@ declare function normalizeNav(results: NavObjectType, nav: NavType, prefix?: str
|
|
|
1700
2011
|
* @since 2.0.0
|
|
1701
2012
|
*/
|
|
1702
2013
|
declare function normalizeNavs(state: StateModel): Record<string, Array<NavItemType>>;
|
|
2014
|
+
/**
|
|
2015
|
+
* Retrieves the appropriate navigation configuration for a given locale key.
|
|
2016
|
+
*
|
|
2017
|
+
* @param navs - The navigation configurations mapped by locale keys.
|
|
2018
|
+
* @param key - The locale key to retrieve navigation for (e.g., 'root', 'fr', 'fr/v1.0.0').
|
|
2019
|
+
* @returns The navigation configuration for the locale, falling back to parent locale or root.
|
|
2020
|
+
*
|
|
2021
|
+
* @remarks
|
|
2022
|
+
* This function implements a fallback strategy for finding navigation configurations:
|
|
2023
|
+
* 1. If an exact match exists for the key, return it
|
|
2024
|
+
* 2. If the key contains '/' (versioned locale), extract the base locale and try that
|
|
2025
|
+
* 3. If no base locale exists, fall back to 'root'
|
|
2026
|
+
*
|
|
2027
|
+
* This allows versioned locales (e.g., 'fr/v1.0.0', 'de/v2.0') to inherit navigation
|
|
2028
|
+
* from their base locale ('fr', 'de') without duplicating configuration.
|
|
2029
|
+
*
|
|
2030
|
+
* Fallback hierarchy examples:
|
|
2031
|
+
* - `'fr'` → Direct match, returns `navs.fr`
|
|
2032
|
+
* - `'fr/v1.0.0'` → No direct match → Checks `'fr'` → Returns `navs.fr`
|
|
2033
|
+
* - `'es/v1.0.0'` → No direct match → Checks `'es'` (doesn't exist) → Returns `navs.root`
|
|
2034
|
+
* - `'root'` → Direct match, returns `navs.root`
|
|
2035
|
+
*
|
|
2036
|
+
* @example
|
|
2037
|
+
* ```ts
|
|
2038
|
+
* const navs = {
|
|
2039
|
+
* root: [{ text: 'Home', link: '/' }],
|
|
2040
|
+
* fr: [{ text: 'Accueil', link: '/fr/' }],
|
|
2041
|
+
* de: [{ text: 'Startseite', link: '/de/' }]
|
|
2042
|
+
* };
|
|
2043
|
+
*
|
|
2044
|
+
* // Exact match
|
|
2045
|
+
* getNavForLocale(navs, 'fr'); // Returns fr nav
|
|
2046
|
+
*
|
|
2047
|
+
* // Versioned locale - inherits from base
|
|
2048
|
+
* getNavForLocale(navs, 'fr/v1.0.0'); // Returns fr nav
|
|
2049
|
+
* getNavForLocale(navs, 'de/v2.0'); // Returns de nav
|
|
2050
|
+
*
|
|
2051
|
+
* // No base locale exists - falls back to root
|
|
2052
|
+
* getNavForLocale(navs, 'es/v1.0.0'); // Returns root nav
|
|
2053
|
+
*
|
|
2054
|
+
* // Root locale
|
|
2055
|
+
* getNavForLocale(navs, 'root'); // Returns root nav
|
|
2056
|
+
* ```
|
|
2057
|
+
*
|
|
2058
|
+
* @see {@link parseNavs} - Uses this function to populate navigation for all locales
|
|
2059
|
+
* @see {@link NavItemType} - The navigation item type structure
|
|
2060
|
+
*
|
|
2061
|
+
* @since 2.0.5
|
|
2062
|
+
*/
|
|
2063
|
+
declare function getNavForLocale(navs: Record<string, Array<NavItemType>>, key: string): Array<NavItemType>;
|
|
1703
2064
|
/**
|
|
1704
2065
|
* Parses and populates the navigation structure for all locales.
|
|
1705
2066
|
*
|
|
@@ -1774,189 +2135,457 @@ declare function versionSwitcher(themeConfig: VersionsConfigInterface, versions:
|
|
|
1774
2135
|
* Imports
|
|
1775
2136
|
*/
|
|
1776
2137
|
/**
|
|
1777
|
-
*
|
|
2138
|
+
* Deeply merges two object types into a single object.
|
|
2139
|
+
*
|
|
2140
|
+
* @typeParam T - The type of the first object.
|
|
2141
|
+
* @typeParam U - The type of the second object.
|
|
2142
|
+
*
|
|
2143
|
+
* @param obj1 - The base object whose properties will be merged.
|
|
2144
|
+
* @param obj2 - The object whose properties will overwrite or merge into `obj1`.
|
|
2145
|
+
* @returns A new object containing the deeply merged properties of both `obj1` and `obj2`.
|
|
1778
2146
|
*
|
|
1779
2147
|
* @remarks
|
|
1780
|
-
* -
|
|
1781
|
-
* -
|
|
1782
|
-
*
|
|
1783
|
-
*
|
|
2148
|
+
* - This function performs a deep merge, recursively merging nested objects.
|
|
2149
|
+
* - If a property exists in both `obj1` and `obj2`:
|
|
2150
|
+
* - If both values are plain objects, they are merged recursively.
|
|
2151
|
+
* - Otherwise, the value from `obj2` overwrites the value from `obj1`.
|
|
2152
|
+
* - Arrays are **not** merged; if a property is an array in `obj2`, it will replace
|
|
2153
|
+
* the corresponding property in `obj1`.
|
|
1784
2154
|
*
|
|
1785
2155
|
* @example
|
|
1786
2156
|
* ```ts
|
|
1787
|
-
*
|
|
1788
|
-
*
|
|
1789
|
-
*
|
|
2157
|
+
* const obj1 = { a: 1, b: { c: 2, d: 3 } };
|
|
2158
|
+
* const obj2 = { b: { c: 42 }, e: 99 };
|
|
2159
|
+
*
|
|
2160
|
+
* const merged = deepMerge(obj1, obj2);
|
|
2161
|
+
* // Result: { a: 1, b: { c: 42, d: 3 }, e: 99 }
|
|
1790
2162
|
* ```
|
|
1791
2163
|
*
|
|
1792
2164
|
* @since 2.0.0
|
|
1793
2165
|
*/
|
|
1794
|
-
declare function
|
|
2166
|
+
declare function deepMerge<T extends object, U extends object>(obj1: T, obj2: U): T & U;
|
|
2167
|
+
/**
|
|
2168
|
+
* Recursively collects all Markdown (`.md`) files within a given directory,
|
|
2169
|
+
* returning their paths relative to a specified root directory.
|
|
2170
|
+
*
|
|
2171
|
+
* @param rootDir - The root directory used as the base for relative paths.
|
|
2172
|
+
* @param currentDir - The current directory to scan. Defaults to `rootDir`.
|
|
2173
|
+
* @returns An array of relative file paths to Markdown files.
|
|
2174
|
+
*
|
|
2175
|
+
* @remarks
|
|
2176
|
+
* - This function traverses directories recursively.
|
|
2177
|
+
* - Only files with a `.md` extension are included in the results.
|
|
2178
|
+
* - Returned paths are **relative to `rootDir`**, regardless of how deep the files are nested.
|
|
2179
|
+
* - Uses Node.js `fs.readdirSync` with `withFileTypes: true` for efficient directory traversal.
|
|
2180
|
+
*
|
|
2181
|
+
* @example
|
|
2182
|
+
* ```ts
|
|
2183
|
+
* const markdownFiles = getAllMarkdownFilesRelative("/project/docs");
|
|
2184
|
+
*
|
|
2185
|
+
* console.log(markdownFiles);
|
|
2186
|
+
* // Example output:
|
|
2187
|
+
* // [
|
|
2188
|
+
* // "README.md",
|
|
2189
|
+
* // "guide/intro.md",
|
|
2190
|
+
* // "tutorial/setup.md"
|
|
2191
|
+
* // ]
|
|
2192
|
+
* ```
|
|
2193
|
+
*
|
|
2194
|
+
* @since 2.0.0
|
|
2195
|
+
*/
|
|
2196
|
+
declare function getAllMarkdownFilesRelative(rootDir: string, currentDir?: string): Array<string>;
|
|
1795
2197
|
|
|
1796
2198
|
/**
|
|
1797
2199
|
* Import will remove at compile time
|
|
1798
2200
|
*/
|
|
1799
2201
|
/**
|
|
1800
|
-
* Populates sidebar items with
|
|
2202
|
+
* Populates sidebar items with version-specific base paths.
|
|
1801
2203
|
*
|
|
1802
|
-
* @param sidebar -
|
|
1803
|
-
* @param version - The version
|
|
1804
|
-
*
|
|
1805
|
-
* @returns A new sidebar array with versioned `base` properties applied.
|
|
2204
|
+
* @param sidebar - Array of sidebar items to process {@link SidebarItemType}.
|
|
2205
|
+
* @param version - The version string to apply to base paths, or 'root' for the default version.
|
|
2206
|
+
* @returns A new array of sidebar items with updated base paths.
|
|
1806
2207
|
*
|
|
1807
2208
|
* @remarks
|
|
1808
|
-
*
|
|
1809
|
-
* -
|
|
1810
|
-
*
|
|
1811
|
-
*
|
|
2209
|
+
* This function transforms sidebar items by adding version-specific base paths to enable
|
|
2210
|
+
* multi-version documentation navigation. It processes each sidebar item according to
|
|
2211
|
+
* specific rules:
|
|
2212
|
+
*
|
|
2213
|
+
* **Skipped items** (returned unchanged):
|
|
2214
|
+
* - Items with `skipVersioning: true` flag
|
|
2215
|
+
* - Items with external links (starting with 'http')
|
|
2216
|
+
* - Items without links or nested items
|
|
2217
|
+
* - Items when a version is empty or 'root'
|
|
2218
|
+
*
|
|
2219
|
+
* **Processed items**:
|
|
2220
|
+
* - The base path is prepended with a version: `/{version}/{base}/`
|
|
2221
|
+
* - Original item properties are preserved via spread operator
|
|
2222
|
+
* - The normalized version replaces 'root' with empty string
|
|
2223
|
+
*
|
|
2224
|
+
* The base path structure follows the pattern: `/{version}/{originalBase}/`
|
|
2225
|
+
* using {@link join} from path/posix for consistent URL formatting across platforms.
|
|
1812
2226
|
*
|
|
1813
2227
|
* @example
|
|
1814
2228
|
* ```ts
|
|
1815
|
-
* const sidebar: SidebarItemType
|
|
1816
|
-
* { text:
|
|
1817
|
-
* {
|
|
1818
|
-
*
|
|
1819
|
-
*
|
|
1820
|
-
* { text: "Setup", link: "/setup" }
|
|
1821
|
-
* ]
|
|
1822
|
-
* }
|
|
2229
|
+
* const sidebar: Array<SidebarItemType> = [
|
|
2230
|
+
* { text: 'Guide', link: '/guide/', base: '/docs' },
|
|
2231
|
+
* { text: 'API', link: '/api/', base: '/reference' },
|
|
2232
|
+
* { text: 'External', link: 'https://example.com' },
|
|
2233
|
+
* { text: 'Skip', link: '/skip/', skipVersioning: true }
|
|
1823
2234
|
* ];
|
|
1824
2235
|
*
|
|
1825
|
-
*
|
|
1826
|
-
*
|
|
1827
|
-
*
|
|
1828
|
-
* //
|
|
1829
|
-
*
|
|
1830
|
-
* { text:
|
|
1831
|
-
*
|
|
1832
|
-
*
|
|
1833
|
-
*
|
|
1834
|
-
*
|
|
1835
|
-
*
|
|
1836
|
-
*
|
|
1837
|
-
*
|
|
1838
|
-
* ]
|
|
2236
|
+
* const versioned = populateSidebar(sidebar, 'v2.0');
|
|
2237
|
+
* // [
|
|
2238
|
+
* // { text: 'Guide', link: '/guide/', base: '/v2.0/docs/' },
|
|
2239
|
+
* // { text: 'API', link: '/api/', base: '/v2.0/reference/' },
|
|
2240
|
+
* // { text: 'External', link: 'https://example.com' },
|
|
2241
|
+
* // { text: 'Skip', link: '/skip/', skipVersioning: true }
|
|
2242
|
+
* // ]
|
|
2243
|
+
*
|
|
2244
|
+
* // Root version (no prefix)
|
|
2245
|
+
* const rootVersioned = populateSidebar(sidebar, 'root');
|
|
2246
|
+
* // Base paths remain unchanged
|
|
2247
|
+
*
|
|
2248
|
+
* // Use case: Version-specific sidebar configuration
|
|
2249
|
+
* const versions = ['v1.0', 'v2.0', 'latest'];
|
|
2250
|
+
* const sidebarConfigs = versions.map(version => ({
|
|
2251
|
+
* version,
|
|
2252
|
+
* items: populateSidebar(baseSidebar, version)
|
|
2253
|
+
* }));
|
|
1839
2254
|
* ```
|
|
1840
2255
|
*
|
|
1841
2256
|
* @see SidebarItemType
|
|
1842
|
-
* @since 2.0.
|
|
2257
|
+
* @since 2.0.5
|
|
1843
2258
|
*/
|
|
1844
2259
|
declare function populateSidebar(sidebar: Array<SidebarItemType>, version: string): Array<SidebarItemType>;
|
|
1845
2260
|
/**
|
|
1846
|
-
*
|
|
2261
|
+
* Extracts locale and version information from URL path segments.
|
|
1847
2262
|
*
|
|
1848
|
-
* @param
|
|
1849
|
-
* @param
|
|
1850
|
-
* @param
|
|
1851
|
-
* @
|
|
1852
|
-
*
|
|
1853
|
-
* @returns void — Mutates the `results` object directly by appending normalized sidebar items.
|
|
2263
|
+
* @param segments - Array of URL path segments to parse (will be modified by shifting elements).
|
|
2264
|
+
* @param localesList - List of valid locale identifiers for matching.
|
|
2265
|
+
* @param versionsList - List of valid version identifiers for matching.
|
|
2266
|
+
* @returns An object containing extracted language and version {@link PathSegmentsInterface}.
|
|
1854
2267
|
*
|
|
1855
2268
|
* @remarks
|
|
1856
|
-
*
|
|
1857
|
-
*
|
|
1858
|
-
*
|
|
1859
|
-
*
|
|
2269
|
+
* This function parses URL path segments to identify and extract locale and version
|
|
2270
|
+
* information, supporting both version-first and locale-first URL structures. The
|
|
2271
|
+
* function operates on the segment array destructively, removing matched elements
|
|
2272
|
+
* via {@link Array.shift}.
|
|
2273
|
+
*
|
|
2274
|
+
* **Parsing logic**:
|
|
2275
|
+
* 1. If the first segment matches a version → return `{ lang: '', version }` (version-first pattern)
|
|
2276
|
+
* 2. If the first segment matches a locale → extract it as `lang` (locale-first pattern)
|
|
2277
|
+
* 3. If the second segment matches a version → extract it as `version`
|
|
2278
|
+
* 4. Return both extracted values (empty strings if not found)
|
|
2279
|
+
*
|
|
2280
|
+
* **URL patterns supported**:
|
|
2281
|
+
* - `/v2.0/guide/` → `{ lang: '', version: 'v2.0' }`
|
|
2282
|
+
* - `/fr/v2.0/guide/` → `{ lang: 'fr', version: 'v2.0' }`
|
|
2283
|
+
* - `/fr/guide/` → `{ lang: 'fr', version: '' }`
|
|
2284
|
+
* - `/guide/` → `{ lang: '', version: '' }`
|
|
2285
|
+
*
|
|
2286
|
+
* The segment array is mutated during extraction, with matched segments removed
|
|
2287
|
+
* from the beginning. This allows the following processing to work with the remaining
|
|
2288
|
+
* path components.
|
|
1860
2289
|
*
|
|
1861
2290
|
* @example
|
|
1862
2291
|
* ```ts
|
|
1863
|
-
* const
|
|
1864
|
-
*
|
|
1865
|
-
*
|
|
1866
|
-
*
|
|
2292
|
+
* const localesList = ['en', 'fr', 'de'];
|
|
2293
|
+
* const versionsList = ['v1.0', 'v2.0', 'latest'];
|
|
2294
|
+
*
|
|
2295
|
+
* // Version-first pattern
|
|
2296
|
+
* const segments1 = ['v2.0', 'guide', 'intro'];
|
|
2297
|
+
* const result1 = extractLocale(segments1, localesList, versionsList);
|
|
2298
|
+
* console.log(result1); // { lang: '', version: 'v2.0' }
|
|
2299
|
+
* console.log(segments1); // ['guide', 'intro']
|
|
2300
|
+
*
|
|
2301
|
+
* // Locale-first pattern
|
|
2302
|
+
* const segments2 = ['fr', 'v2.0', 'api', 'reference'];
|
|
2303
|
+
* const result2 = extractLocale(segments2, localesList, versionsList);
|
|
2304
|
+
* console.log(result2); // { lang: 'fr', version: 'v2.0' }
|
|
2305
|
+
* console.log(segments2); // ['api', 'reference']
|
|
2306
|
+
*
|
|
2307
|
+
* // No locale or version
|
|
2308
|
+
* const segments3 = ['docs', 'getting-started'];
|
|
2309
|
+
* const result3 = extractLocale(segments3, localesList, versionsList);
|
|
2310
|
+
* console.log(result3); // { lang: '', version: '' }
|
|
2311
|
+
* console.log(segments3); // ['docs', 'getting-started']
|
|
2312
|
+
*
|
|
2313
|
+
* // Use case: URL parsing for routing
|
|
2314
|
+
* const url = '/de/latest/guide/installation';
|
|
2315
|
+
* const segments = url.split('/').filter(Boolean);
|
|
2316
|
+
* const { lang, version } = extractLocale(
|
|
2317
|
+
* segments,
|
|
2318
|
+
* ['en', 'de', 'fr'],
|
|
2319
|
+
* ['latest', 'v1', 'v2']
|
|
2320
|
+
* );
|
|
2321
|
+
* const route = segments.join('/');
|
|
2322
|
+
* console.log({ lang, version, route });
|
|
2323
|
+
* // { lang: 'de', version: 'latest', route: 'guide/installation' }
|
|
2324
|
+
* ```
|
|
1867
2325
|
*
|
|
1868
|
-
*
|
|
2326
|
+
* @see PathSegmentsInterface
|
|
2327
|
+
* @since 2.0.5
|
|
2328
|
+
*/
|
|
2329
|
+
declare function extractLocale(segments: Array<string>, localesList: Array<string>, versionsList: Array<string>): PathSegmentsInterface;
|
|
2330
|
+
/**
|
|
2331
|
+
* Normalizes sidebar configuration into a structured format for multi-version documentation.
|
|
2332
|
+
*
|
|
2333
|
+
* @param state - The state model containing configuration data {@link StateModel}.
|
|
2334
|
+
* @param results - Accumulator object to store normalized sidebar entries {@link SidebarObjectType}.
|
|
2335
|
+
* @param sidebar - The sidebar configuration to normalize {@link SidebarType}.
|
|
2336
|
+
* @param prefix - Locale prefix for the sidebar (e.g., 'root', 'fr', 'de').
|
|
2337
|
+
*
|
|
2338
|
+
* @remarks
|
|
2339
|
+
* This function transforms sidebar configurations from various input formats into a
|
|
2340
|
+
* normalized structure that supports multi-version, multi-locale documentation. It
|
|
2341
|
+
* handles both array-based and object-based sidebar definitions.
|
|
2342
|
+
*
|
|
2343
|
+
* **Input formats supported**:
|
|
2344
|
+
* - **Array format**: Simple list of sidebar items for a single path
|
|
2345
|
+
* - **Object format**: Key-value pairs where keys are paths and values are sidebar items
|
|
2346
|
+
* - **Undefined**: Gracefully handles missing sidebar configuration
|
|
2347
|
+
*
|
|
2348
|
+
* **Normalization process**:
|
|
2349
|
+
* 1. Early return if the sidebar is undefined
|
|
2350
|
+
* 2. Convert the 'root' prefix to an empty string for URL normalization
|
|
2351
|
+
* 3. For array format: Store items under the normalized path
|
|
2352
|
+
* 4. For object format: Parse each key to extract locale/version using {@link extractLocale}
|
|
2353
|
+
* 5. Filter entries that don't match the current locale prefix
|
|
2354
|
+
* 6. Construct versioned paths and store items in the result accumulator
|
|
2355
|
+
*
|
|
2356
|
+
* **Results structure**:
|
|
2357
|
+
* ```ts
|
|
1869
2358
|
* {
|
|
1870
|
-
* root: [
|
|
1871
|
-
*
|
|
1872
|
-
* ]
|
|
2359
|
+
* 'root': { '/': [...items], '/guide/': [...items] },
|
|
2360
|
+
* 'fr': { '/fr/': [...items], '/fr/guide/': [...items] },
|
|
2361
|
+
* 'fr/v2.0': { '/fr/v2.0/': [...items] }
|
|
1873
2362
|
* }
|
|
1874
2363
|
* ```
|
|
1875
2364
|
*
|
|
2365
|
+
* The function uses state data to access:
|
|
2366
|
+
* - {@link StateModel.localesMap} for valid locale identifiers
|
|
2367
|
+
* - {@link StateModel.versionsList} for valid version identifiers
|
|
2368
|
+
*
|
|
1876
2369
|
* @example
|
|
1877
2370
|
* ```ts
|
|
1878
|
-
* const
|
|
1879
|
-
*
|
|
1880
|
-
* guide: [
|
|
1881
|
-
* { text: "Getting Started", link: "/guide/start" }
|
|
1882
|
-
* ]
|
|
1883
|
-
* }, "en");
|
|
2371
|
+
* const state = new StateModel(config).init();
|
|
2372
|
+
* const results: Record<string, SidebarObjectType> = {};
|
|
1884
2373
|
*
|
|
1885
|
-
* //
|
|
1886
|
-
*
|
|
1887
|
-
*
|
|
1888
|
-
*
|
|
1889
|
-
*
|
|
2374
|
+
* // Array format sidebar
|
|
2375
|
+
* const arraySidebar = [
|
|
2376
|
+
* { text: 'Guide', link: '/guide/' },
|
|
2377
|
+
* { text: 'API', link: '/api/' }
|
|
2378
|
+
* ];
|
|
2379
|
+
* normalizeSidebar(state, results, arraySidebar, 'root');
|
|
2380
|
+
* // results = { 'root': { '/': [{ text: 'Guide', ... }, { text: 'API', ... }] } }
|
|
2381
|
+
*
|
|
2382
|
+
* // Object format sidebar
|
|
2383
|
+
* const objectSidebar = {
|
|
2384
|
+
* '/': [{ text: 'Home', link: '/' }],
|
|
2385
|
+
* '/guide/': [{ text: 'Getting Started', link: '/guide/start' }],
|
|
2386
|
+
* '/fr/guide/': [{ text: 'Commencer', link: '/fr/guide/start' }]
|
|
2387
|
+
* };
|
|
2388
|
+
* normalizeSidebar(state, results, objectSidebar, 'root');
|
|
2389
|
+
*
|
|
2390
|
+
* // With locale prefix
|
|
2391
|
+
* normalizeSidebar(state, results, objectSidebar, 'fr');
|
|
2392
|
+
* // Only '/fr/guide/' entries are processed
|
|
2393
|
+
*
|
|
2394
|
+
* // Use case: Multi-locale sidebar processing
|
|
2395
|
+
* const results = {};
|
|
2396
|
+
* const locales = ['root', 'fr', 'de'];
|
|
2397
|
+
* for (const locale of locales) {
|
|
2398
|
+
* const sidebar = getLocaleSidebar(locale);
|
|
2399
|
+
* normalizeSidebar(state, results, sidebar, locale);
|
|
1890
2400
|
* }
|
|
1891
2401
|
* ```
|
|
1892
2402
|
*
|
|
2403
|
+
* @see StateModel
|
|
1893
2404
|
* @see SidebarType
|
|
2405
|
+
* @see extractLocale
|
|
1894
2406
|
* @see SidebarObjectType
|
|
1895
2407
|
*
|
|
1896
|
-
* @since 2.0.
|
|
2408
|
+
* @since 2.0.5
|
|
1897
2409
|
*/
|
|
1898
|
-
declare function normalizeSidebar(results:
|
|
2410
|
+
declare function normalizeSidebar(state: StateModel, results: Record<string, SidebarObjectType>, sidebar?: SidebarType, prefix?: string): void;
|
|
1899
2411
|
/**
|
|
1900
|
-
* Normalizes all sidebar
|
|
1901
|
-
*
|
|
1902
|
-
* @param state - The current {@link StateModel} providing configuration data:
|
|
1903
|
-
* - `configuration.themeConfig.sidebar` — global sidebar configuration.
|
|
1904
|
-
* - `configuration.locales` — locale-specific configurations with optional `sidebar`.
|
|
1905
|
-
* - `localesMap` — maps locale keys to normalized paths for root fallback.
|
|
2412
|
+
* Normalizes all sidebar configurations across locales and versions.
|
|
1906
2413
|
*
|
|
1907
|
-
* @
|
|
1908
|
-
*
|
|
2414
|
+
* @param state - The state model containing configuration data {@link StateModel}.
|
|
2415
|
+
* @returns A normalized sidebar structure organized by locale and path {@link SidebarObjectType}.
|
|
1909
2416
|
*
|
|
1910
2417
|
* @remarks
|
|
1911
|
-
*
|
|
1912
|
-
* -
|
|
1913
|
-
* -
|
|
1914
|
-
*
|
|
1915
|
-
*
|
|
1916
|
-
*
|
|
2418
|
+
* This function orchestrates the complete normalization of all sidebar configurations
|
|
2419
|
+
* in a multi-version, multi-locale documentation site. It processes sidebars from both
|
|
2420
|
+
* global theme configuration and locale-specific settings, then propagates them to all
|
|
2421
|
+
* versioned locale entries.
|
|
2422
|
+
*
|
|
2423
|
+
* **Processing stages**:
|
|
2424
|
+
* 1. **Initial Normalization**: Process sidebars from global theme config and each locale's
|
|
2425
|
+
* theme config using {@link normalizeSidebar}
|
|
2426
|
+
* 2. **Version Propagation**: For each locale entry in {@link StateModel.vitepressConfig},
|
|
2427
|
+
* if it lacks a normalized sidebar, inherit from its parent locale
|
|
2428
|
+
* 3. **Path Adjustment**: Adjust inherited paths to include the version/locale prefix
|
|
2429
|
+
*
|
|
2430
|
+
* **Data sources processed**:
|
|
2431
|
+
* - {@link StateModel.configuration.themeConfig.sidebar}: Global sidebar configuration
|
|
2432
|
+
* - Each locale's `themeConfig.sidebar`: Locale-specific sidebar configuration
|
|
2433
|
+
*
|
|
2434
|
+
* **Output structure**:
|
|
2435
|
+
* ```ts
|
|
2436
|
+
* {
|
|
2437
|
+
* 'root': { '/': [...], '/guide/': [...] },
|
|
2438
|
+
* 'v2.0': { '/v2.0/': [...], '/v2.0/guide/': [...] },
|
|
2439
|
+
* 'fr': { '/fr/': [...], '/fr/guide/': [...] },
|
|
2440
|
+
* 'fr/v2.0': { '/fr/v2.0/': [...], '/fr/v2.0/guide/': [...] }
|
|
2441
|
+
* }
|
|
2442
|
+
* ```
|
|
2443
|
+
*
|
|
2444
|
+
* The function ensures that every locale entry in the VitePress configuration has
|
|
2445
|
+
* appropriate sidebar items, either directly defined or inherited from parent locales.
|
|
2446
|
+
* This enables consistent navigation across all versions and locales.
|
|
1917
2447
|
*
|
|
1918
2448
|
* @example
|
|
1919
2449
|
* ```ts
|
|
2450
|
+
* const state = new StateModel({
|
|
2451
|
+
* themeConfig: {
|
|
2452
|
+
* sidebar: {
|
|
2453
|
+
* '/': [{ text: 'Home', link: '/' }],
|
|
2454
|
+
* '/guide/': [{ text: 'Guide', link: '/guide/' }]
|
|
2455
|
+
* }
|
|
2456
|
+
* },
|
|
2457
|
+
* locales: {
|
|
2458
|
+
* root: { lang: 'en-US' },
|
|
2459
|
+
* '/fr/': {
|
|
2460
|
+
* lang: 'fr-FR',
|
|
2461
|
+
* themeConfig: {
|
|
2462
|
+
* sidebar: {
|
|
2463
|
+
* '/fr/guide/': [{ text: 'Guide', link: '/fr/guide/' }]
|
|
2464
|
+
* }
|
|
2465
|
+
* }
|
|
2466
|
+
* }
|
|
2467
|
+
* },
|
|
2468
|
+
* versionsConfig: {
|
|
2469
|
+
* sources: 'src',
|
|
2470
|
+
* archive: 'versions'
|
|
2471
|
+
* }
|
|
2472
|
+
* }).init();
|
|
2473
|
+
*
|
|
2474
|
+
* const normalized = normalizeSidebars(state);
|
|
2475
|
+
*
|
|
2476
|
+
* console.log(Object.keys(normalized));
|
|
2477
|
+
* // ['root', 'v1.0', 'v2.0', 'fr', 'fr/v1.0', 'fr/v2.0']
|
|
2478
|
+
*
|
|
2479
|
+
* console.log(normalized['v2.0']);
|
|
2480
|
+
* // { '/v2.0/': [...], '/v2.0/guide/': [...] }
|
|
2481
|
+
*
|
|
2482
|
+
* console.log(normalized['fr/v2.0']);
|
|
2483
|
+
* // { '/fr/v2.0/': [...], '/fr/v2.0/guide/': [...] }
|
|
2484
|
+
*
|
|
2485
|
+
* // Use case: Building a complete sidebar structure
|
|
1920
2486
|
* const state = inject(StateModel);
|
|
1921
2487
|
* const sidebars = normalizeSidebars(state);
|
|
1922
|
-
*
|
|
1923
|
-
* //
|
|
1924
|
-
*
|
|
1925
|
-
*
|
|
1926
|
-
*
|
|
2488
|
+
*
|
|
2489
|
+
* // Apply to VitePress config
|
|
2490
|
+
* for (const [locale, paths] of Object.entries(sidebars)) {
|
|
2491
|
+
* vitepressConfig.locales[locale].themeConfig.sidebar = paths;
|
|
2492
|
+
* }
|
|
1927
2493
|
* ```
|
|
1928
2494
|
*
|
|
1929
|
-
* @see
|
|
2495
|
+
* @see StateModel
|
|
2496
|
+
* @see extractLocale
|
|
1930
2497
|
* @see normalizeSidebar
|
|
1931
2498
|
* @see SidebarObjectType
|
|
1932
2499
|
*
|
|
1933
|
-
* @since 2.0.
|
|
2500
|
+
* @since 2.0.5
|
|
1934
2501
|
*/
|
|
1935
|
-
declare function normalizeSidebars(state: StateModel): SidebarObjectType
|
|
2502
|
+
declare function normalizeSidebars(state: StateModel): Record<string, SidebarObjectType>;
|
|
1936
2503
|
/**
|
|
1937
|
-
* Parses and
|
|
2504
|
+
* Parses and applies sidebar configurations to all locales in the VitePress configuration.
|
|
1938
2505
|
*
|
|
1939
2506
|
* @remarks
|
|
1940
|
-
*
|
|
1941
|
-
* -
|
|
1942
|
-
*
|
|
1943
|
-
*
|
|
1944
|
-
*
|
|
1945
|
-
*
|
|
1946
|
-
*
|
|
1947
|
-
*
|
|
2507
|
+
* This function is the main entry point for sidebar processing in a multi-version,
|
|
2508
|
+
* multi-locale documentation system. It coordinates the complete sidebar normalization
|
|
2509
|
+
* and population workflow by:
|
|
2510
|
+
*
|
|
2511
|
+
* 1. **Injecting State**: Retrieves the {@link StateModel} singleton instance
|
|
2512
|
+
* 2. **Normalizing**: Calls {@link normalizeSidebars} to process all sidebar configurations
|
|
2513
|
+
* 3. **Applying**: Iterates through each locale in {@link StateModel.vitepressConfig.locales}
|
|
2514
|
+
* 4. **Populating**: Uses {@link populateSidebar} to add version-specific base paths
|
|
2515
|
+
* 5. **Merging**: Assigns processed sidebars to each locale's theme configuration
|
|
2516
|
+
*
|
|
2517
|
+
* **Sidebar structure handling**:
|
|
2518
|
+
* - Initializes `themeConfig` if it doesn't exist
|
|
2519
|
+
* - Converts array-based sidebars to object format for consistent processing
|
|
2520
|
+
* - Preserves existing sidebar structure when no normalized version exists
|
|
2521
|
+
* - Ensures sidebar is always in object format after processing
|
|
2522
|
+
*
|
|
2523
|
+
* The function modifies the VitePress configuration in place, updating each locale's
|
|
2524
|
+
* `themeConfig.sidebar` with the normalized and versioned sidebar items. This ensures
|
|
2525
|
+
* that all routes have appropriate navigation structures.
|
|
2526
|
+
*
|
|
2527
|
+
* **Processing flow**:
|
|
2528
|
+
* ```
|
|
2529
|
+
* User Config → normalizeSidebars() → Normalized Structure
|
|
2530
|
+
* ↓
|
|
2531
|
+
* VitePress Config ← populateSidebar() ← Per-Locale Items
|
|
2532
|
+
* ```
|
|
1948
2533
|
*
|
|
1949
2534
|
* @example
|
|
1950
2535
|
* ```ts
|
|
1951
|
-
*
|
|
1952
|
-
*
|
|
2536
|
+
* // Setup configuration
|
|
2537
|
+
* const config = {
|
|
2538
|
+
* themeConfig: {
|
|
2539
|
+
* sidebar: {
|
|
2540
|
+
* '/': [{ text: 'Home', link: '/' }],
|
|
2541
|
+
* '/guide/': [{ text: 'Getting Started', link: '/guide/start' }]
|
|
2542
|
+
* }
|
|
2543
|
+
* },
|
|
2544
|
+
* locales: {
|
|
2545
|
+
* root: { lang: 'en-US', label: 'English' },
|
|
2546
|
+
* '/fr/': {
|
|
2547
|
+
* lang: 'fr-FR',
|
|
2548
|
+
* label: 'Français',
|
|
2549
|
+
* themeConfig: {
|
|
2550
|
+
* sidebar: {
|
|
2551
|
+
* '/fr/guide/': [{ text: 'Commencer', link: '/fr/guide/start' }]
|
|
2552
|
+
* }
|
|
2553
|
+
* }
|
|
2554
|
+
* }
|
|
2555
|
+
* }
|
|
2556
|
+
* };
|
|
2557
|
+
*
|
|
2558
|
+
* const state = new StateModel(config).init();
|
|
2559
|
+
* provide(StateModel, state);
|
|
2560
|
+
*
|
|
2561
|
+
* // Parse and apply sidebars
|
|
2562
|
+
* parseSidebars();
|
|
2563
|
+
*
|
|
2564
|
+
* // Result: Each locale now has versioned sidebars
|
|
2565
|
+
* console.log(state.vitepressConfig.locales['v2.0'].themeConfig.sidebar);
|
|
2566
|
+
* // {
|
|
2567
|
+
* // '/v2.0/': [{ text: 'Home', link: '/', base: '/v2.0/' }],
|
|
2568
|
+
* // '/v2.0/guide/': [{ text: 'Getting Started', link: '/guide/start', base: '/v2.0/guide/' }]
|
|
2569
|
+
* // }
|
|
2570
|
+
*
|
|
2571
|
+
* console.log(state.vitepressConfig.locales['fr/v2.0'].themeConfig.sidebar);
|
|
2572
|
+
* // {
|
|
2573
|
+
* // '/fr/v2.0/guide/': [{ text: 'Commencer', link: '/fr/guide/start', base: '/fr/v2.0/guide/' }]
|
|
2574
|
+
* // }
|
|
2575
|
+
*
|
|
2576
|
+
* // Use case: Integration in VitePress config hook
|
|
2577
|
+
* export default defineConfig({
|
|
2578
|
+
* async buildStart() {
|
|
2579
|
+
* parseSidebars();
|
|
2580
|
+
* },
|
|
2581
|
+
* // ... rest of config
|
|
2582
|
+
* });
|
|
1953
2583
|
* ```
|
|
1954
2584
|
*
|
|
1955
|
-
* @see
|
|
2585
|
+
* @see StateModel
|
|
1956
2586
|
* @see populateSidebar
|
|
1957
|
-
* @see SidebarObjectType
|
|
1958
2587
|
* @see normalizeSidebars
|
|
1959
2588
|
*
|
|
1960
|
-
* @since 2.0.
|
|
2589
|
+
* @since 2.0.5
|
|
1961
2590
|
*/
|
|
1962
|
-
declare function
|
|
2591
|
+
declare function parseSidebars(): void;
|