File tree Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Expand file tree Collapse file tree 2 files changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,16 @@ describe("categoryHrefToDocID", () => {
19
19
href : "/reference/agent-services/" ,
20
20
expectedID : "reference/agent-services/agent-services" ,
21
21
} ,
22
+ {
23
+ description : "docs URL segment in versioned path" ,
24
+ href : "/docs/ver/15.x/admin-guides/access-controls/guides/joining-sessions" ,
25
+ expectedID : "admin-guides/access-controls/guides/joining-sessions/joining-sessions" ,
26
+ } ,
27
+ {
28
+ description : "docs URL segment in unversioned path" ,
29
+ href : "/docs/admin-guides/access-controls/guides/joining-sessions" ,
30
+ expectedID : "admin-guides/access-controls/guides/joining-sessions/joining-sessions" ,
31
+ } ,
22
32
] ;
23
33
24
34
test . each ( testCases ) ( "$description" , ( c ) => {
Original file line number Diff line number Diff line change 3
3
// IDs in the items prop, so we generate a page ID based on the assumption that
4
4
// category page slugs are the same as their containing directory names.
5
5
export function categoryHrefToDocID ( href : string ) : string {
6
- const idPrefix = href . replace ( new RegExp ( `^(/ver/[0-9]+\\.x)?/` ) , "" ) ;
7
- const slugRE = new RegExp ( `/([^/]+)/$` ) ;
6
+ // Remove initial path segments that are not involved in generating the page
7
+ // ID, such as "/docs/" and "/ver/15.x/".
8
+ let idPrefix = href . replace ( new RegExp ( `^/(docs/)?(ver/[0-9]+\\.x/)?` ) , "" ) ;
9
+ // Ensure that trailing slashes are trimmed so we can uniformly add the slug
10
+ // segment.
11
+ idPrefix = idPrefix . replace ( new RegExp ( `/$` ) , "" ) ;
12
+ const slugRE = new RegExp ( `/([^/]+)/?$` ) ;
8
13
const slug = slugRE . exec ( href ) ;
9
- if ( ! slug || slug . length != 2 ) {
10
- return "" ;
14
+ if ( ! slug || slug . length < 2 ) {
15
+ throw new Error ( `could not identify a category page ID for href ${ href } ` ) ;
11
16
}
12
- return idPrefix + slug [ 1 ] ;
17
+ return idPrefix + "/" + slug [ 1 ] ;
13
18
}
14
-
You can’t perform that action at this time.
0 commit comments