@@ -109,32 +109,37 @@ export function getVersionObjectFromPath(href: string | undefined) {
109109 return allVersions [ versionFromPath ]
110110}
111111
112- // TODO needs refactoring + tests
113112// Return the product segment from the path
113+ // Extracts the product identifier from various URL patterns including versioned paths
114114export function getProductStringFromPath ( href : string | undefined ) : string {
115+ // Handle empty or undefined paths
115116 if ( ! href ) return 'homepage'
116- href = getPathWithoutLanguage ( href )
117117
118- if ( href === '/' ) return 'homepage'
118+ const normalizedHref = getPathWithoutLanguage ( href )
119+ if ( normalizedHref === '/' ) return 'homepage'
119120
120- // The first segment will always be empty on this split
121- const pathParts = href . split ( '/' )
121+ // Split path into segments ( first segment is always empty string)
122+ const pathParts = normalizedHref . split ( '/' )
122123
124+ // Handle special product paths that appear anywhere in the URL
123125 if ( pathParts . includes ( 'early-access' ) ) return 'early-access'
124126
125- // For rest pages the currentProduct should be rest
126- // We use this to show SidebarRest, which is a different sidebar than the rest of the site
127- if ( pathParts [ 1 ] === 'rest' ) return 'rest'
128- if ( pathParts [ 1 ] === 'copilot' ) return 'copilot'
129- if ( pathParts [ 1 ] === 'get-started' ) return 'get-started'
130-
131- // Possible scenarios for href (assume part[0] is an empty string):
132- //
133- // * part[1] is a version and part[2] is undefined, so return part[1] as an enterprise landing page
134- // * part[1] is a version and part[2] is defined, so return part[2] as the product
135- // * part[1] is NOT a version, so return part[1] as the product
136- const isEnterprise = supportedVersions . has ( pathParts [ 1 ] )
137- const productString = isEnterprise && pathParts [ 2 ] ? pathParts [ 2 ] : pathParts [ 1 ]
127+ // Handle special products that always appear as the first segment
128+ // These products use custom sidebars and need explicit handling
129+ const specialProducts = [ 'rest' , 'copilot' , 'get-started' ]
130+ if ( specialProducts . includes ( pathParts [ 1 ] ) ) {
131+ return pathParts [ 1 ]
132+ }
133+
134+ // Determine if first segment is a version (e.g., '[email protected] ') 135+ // If yes, product is in pathParts[2], otherwise it's in pathParts[1]
136+ // Examples:
137+ // /[email protected] /admin -> product is 'admin' 138+ // /github/getting-started -> product is 'github'
139+ // /[email protected] -> product is '[email protected] ' (enterprise landing) 140+ const hasVersionPrefix = supportedVersions . has ( pathParts [ 1 ] )
141+ const productString = hasVersionPrefix && pathParts [ 2 ] ? pathParts [ 2 ] : pathParts [ 1 ]
142+
138143 return productString
139144}
140145
0 commit comments