@@ -16,45 +16,36 @@ import {
1616 sidebarConfig
1717} from './cake/config.js'
1818
19- // Re-export configuration for backward compatibility
20- export { supportedLocales , versions , localizedVersions , sidebarConfig }
21-
2219// Helper functions for version management
2320export function getCurrentVersion ( locale = 'en' ) {
2421 const versionList = getVersionsByLocale ( locale )
2522 return versionList . find ( v => v . isCurrentVersion )
2623}
2724
2825export function getVersionsByLocale ( locale = 'en' ) {
29- // Return English versions by default
3026 if ( locale === 'en' ) {
3127 return versions
3228 }
3329
34- // Check if we have localized versions for this locale
3530 if ( localizedVersions [ locale ] ) {
3631 return localizedVersions [ locale ]
3732 }
3833
39- // Fallback to English versions if locale not found
4034 return versions
4135}
4236
4337export function getVersionByPath ( path ) {
4438 // Detect locale from path
4539 const locale = detectLocaleFromPath ( path )
4640
47- // Get version list for detected locale
4841 const versionList = getVersionsByLocale ( locale )
4942
50- // Check for version-specific paths in the detected locale
5143 for ( const version of versionList ) {
5244 if ( path . startsWith ( version . publicPath ) ) {
5345 return version
5446 }
5547 }
5648
57- // Default to current version for the detected locale
5849 return getCurrentVersion ( locale )
5950}
6051
@@ -68,17 +59,56 @@ export function getAllVersionPaths(locale = 'en') {
6859 return versionList . map ( v => v . publicPath )
6960}
7061
71- // Navigation configuration for version dropdown
72- export function getVersionNavItems ( locale = 'en' ) {
73- const versionList = getVersionsByLocale ( locale )
74- return versionList . map ( version => ( {
75- text : version . displayName ,
76- link : version . publicPath ,
77- path : version . publicPath ,
78- version : version . version
79- } ) )
62+ export function convertPathToVersion ( currentPath , targetVersion , locale = 'en' ) {
63+ const currentLocale = detectLocaleFromPath ( currentPath )
64+ const currentVersionObj = getVersionByPath ( currentPath )
65+
66+ if ( currentVersionObj . version === targetVersion ) {
67+ return currentPath
68+ }
69+
70+ const versionList = getVersionsByLocale ( currentLocale )
71+ const targetVersionObj = versionList . find ( v => v . version === targetVersion )
72+
73+ if ( ! targetVersionObj ) {
74+ return locale === 'en' ? `/${ targetVersion } .x/` : `/${ locale } /${ targetVersion } .x/`
75+ }
76+
77+ let relativePath = currentPath
78+ if ( relativePath . startsWith ( currentVersionObj . publicPath ) ) {
79+ relativePath = relativePath . substring ( currentVersionObj . publicPath . length )
80+ }
81+
82+ if ( ! relativePath || relativePath === '' || relativePath === '/' ) {
83+ return targetVersionObj . publicPath
84+ }
85+
86+ let targetPath = targetVersionObj . publicPath
87+ if ( ! targetPath . endsWith ( '/' ) ) {
88+ targetPath += '/'
89+ }
90+ if ( relativePath . startsWith ( '/' ) ) {
91+ relativePath = relativePath . substring ( 1 )
92+ }
93+
94+ return targetPath + relativePath
8095}
8196
97+ export function getVersionNavItems ( locale = 'en' , currentPath = null ) {
98+ const versionList = getVersionsByLocale ( locale )
99+ return versionList . map ( version => {
100+ const link = currentPath
101+ ? convertPathToVersion ( currentPath , version . version , locale )
102+ : version . publicPath
103+
104+ return {
105+ text : version . displayName ,
106+ link : link ,
107+ path : version . publicPath ,
108+ version : version . version
109+ }
110+ } )
111+ }
82112
83113// Helper function to get supported locales
84114export function getSupportedLocales ( ) {
@@ -98,7 +128,7 @@ export function detectLocaleFromPath(path) {
98128 return locale
99129 }
100130 }
101- // Default to 'en' if no locale prefix found
131+
102132 return 'en'
103133}
104134
0 commit comments