11const PROD_CATEGORY_PATH = '/docs/category/user-related-apis' ;
22const PRELIVE_CATEGORY_PATH = '/docs/category/user-related-apis-pre-live' ;
3- const PROD_LATEST_PREFIX = '/docs/user_related_apis_versioned/' ;
4- const PRELIVE_PREFIX = '/docs/user_related_apis_prelive/' ;
3+ const PROD_LATEST_ROOT = '/docs/user_related_apis_versioned' ;
4+ const PRELIVE_ROOT = '/docs/user_related_apis_prelive' ;
5+ const PROD_LATEST_PREFIX = `${ PROD_LATEST_ROOT } /` ;
6+ const PRELIVE_PREFIX = `${ PRELIVE_ROOT } /` ;
57const PROD_VERSION_PREFIX_REGEX = / ^ \d + \. \d + \. \d + ( \/ | $ ) / ;
68
9+ const normalizePath = ( value ) =>
10+ value && value !== '/' ? value . replace ( / \/ + $ / , '' ) : value ;
11+ const isPathInTree = ( pathname , rootPath ) =>
12+ pathname === rootPath || pathname . startsWith ( `${ rootPath } /` ) ;
13+ const isApiTreeRoot = ( pathname ) =>
14+ pathname === PROD_LATEST_ROOT || pathname === PRELIVE_ROOT ;
15+ const hasAvailablePath = ( availablePaths , targetPath ) => {
16+ const normalizedTargetPath = normalizePath ( targetPath ) ;
17+
18+ return (
19+ availablePaths . has ( normalizedTargetPath ) ||
20+ availablePaths . has ( `${ normalizedTargetPath } /` )
21+ ) ;
22+ } ;
723const stripPrefix = ( value , prefix ) => value . slice ( prefix . length ) ;
824const getFallbackPath = ( environment ) =>
925 environment === 'production' ? PROD_CATEGORY_PATH : PRELIVE_CATEGORY_PATH ;
@@ -15,7 +31,7 @@ function getUserRelatedDocsAvailablePaths(allDocsData) {
1531 pluginData ?. versions ?. forEach ( ( version ) => {
1632 version ?. docs ?. forEach ( ( doc ) => {
1733 if ( typeof doc ?. path === 'string' ) {
18- paths . add ( doc . path ) ;
34+ paths . add ( normalizePath ( doc . path ) ) ;
1935 }
2036 } ) ;
2137 } ) ;
@@ -25,16 +41,18 @@ function getUserRelatedDocsAvailablePaths(allDocsData) {
2541}
2642
2743function getUserRelatedDocsEnvironment ( pathname ) {
44+ const normalizedPathname = normalizePath ( pathname ) ;
45+
2846 if (
29- pathname === PROD_CATEGORY_PATH ||
30- pathname . startsWith ( PROD_LATEST_PREFIX )
47+ normalizedPathname === PROD_CATEGORY_PATH ||
48+ isPathInTree ( normalizedPathname , PROD_LATEST_ROOT )
3149 ) {
3250 return 'production' ;
3351 }
3452
3553 if (
36- pathname === PRELIVE_CATEGORY_PATH ||
37- pathname . startsWith ( PRELIVE_PREFIX )
54+ normalizedPathname === PRELIVE_CATEGORY_PATH ||
55+ isPathInTree ( normalizedPathname , PRELIVE_ROOT )
3856 ) {
3957 return 'pre-live' ;
4058 }
@@ -43,36 +61,45 @@ function getUserRelatedDocsEnvironment(pathname) {
4361}
4462
4563function getTargetPath ( pathname , targetEnvironment ) {
46- const currentEnvironment = getUserRelatedDocsEnvironment ( pathname ) ;
64+ const normalizedPathname = normalizePath ( pathname ) ;
65+ const currentEnvironment = getUserRelatedDocsEnvironment ( normalizedPathname ) ;
4766 const fallbackPath = getFallbackPath ( targetEnvironment ) ;
4867 let targetPath = fallbackPath ;
4968
5069 if ( currentEnvironment && currentEnvironment === targetEnvironment ) {
5170 return {
5271 fallbackPath,
53- targetPath : pathname ,
72+ targetPath : normalizedPathname ,
5473 } ;
5574 }
5675
5776 if ( targetEnvironment === 'production' ) {
58- if ( pathname === PRELIVE_CATEGORY_PATH ) {
77+ if ( normalizedPathname === PRELIVE_CATEGORY_PATH ) {
5978 targetPath = PROD_CATEGORY_PATH ;
60- } else if ( pathname . startsWith ( PRELIVE_PREFIX ) ) {
61- targetPath = `${ PROD_LATEST_PREFIX } ${ stripPrefix ( pathname , PRELIVE_PREFIX ) } ` ;
79+ } else if ( isPathInTree ( normalizedPathname , PRELIVE_ROOT ) ) {
80+ targetPath =
81+ normalizedPathname === PRELIVE_ROOT
82+ ? PROD_LATEST_ROOT
83+ : `${ PROD_LATEST_PREFIX } ${ stripPrefix ( normalizedPathname , PRELIVE_PREFIX ) } ` ;
6284 }
6385 } else if ( targetEnvironment === 'pre-live' ) {
64- if ( pathname === PROD_CATEGORY_PATH ) {
86+ if ( normalizedPathname === PROD_CATEGORY_PATH ) {
6587 targetPath = PRELIVE_CATEGORY_PATH ;
66- } else if ( pathname . startsWith ( PROD_LATEST_PREFIX ) ) {
67- const relativePath = stripPrefix ( pathname , PROD_LATEST_PREFIX ) ;
88+ } else if ( isPathInTree ( normalizedPathname , PROD_LATEST_ROOT ) ) {
89+ const relativePath =
90+ normalizedPathname === PROD_LATEST_ROOT
91+ ? ''
92+ : stripPrefix ( normalizedPathname , PROD_LATEST_PREFIX ) ;
6893 const normalizedRelativePath = relativePath . replace ( PROD_VERSION_PREFIX_REGEX , '' ) ;
6994
70- targetPath = `${ PRELIVE_PREFIX } ${ normalizedRelativePath } ` ;
95+ targetPath = normalizedRelativePath
96+ ? `${ PRELIVE_PREFIX } ${ normalizedRelativePath } `
97+ : PRELIVE_ROOT ;
7198 }
7299 } else {
73100 return {
74- fallbackPath : pathname ,
75- targetPath : pathname ,
101+ fallbackPath : normalizedPathname ,
102+ targetPath : normalizedPathname ,
76103 } ;
77104 }
78105
@@ -84,10 +111,10 @@ function getTargetPath(pathname, targetEnvironment) {
84111
85112function getUserRelatedDocsTarget ( pathname , targetEnvironment , options = { } ) {
86113 const { fallbackPath, targetPath } = getTargetPath ( pathname , targetEnvironment ) ;
87- const hasExplicitDocTarget = targetPath !== fallbackPath ;
114+ const hasExplicitDocTarget = targetPath !== fallbackPath && ! isApiTreeRoot ( targetPath ) ;
88115 const hasEquivalentDoc = ! hasExplicitDocTarget || ! options . availablePaths
89116 ? true
90- : options . availablePaths . has ( targetPath ) ;
117+ : hasAvailablePath ( options . availablePaths , targetPath ) ;
91118 const path = hasEquivalentDoc ? targetPath : fallbackPath ;
92119
93120 return {
0 commit comments