Skip to content

Commit 545041c

Browse files
committed
Remove /docs/ segments in categoryHrefToDocID
1 parent d292ce0 commit 545041c

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/theme/DocCardList/href-to-id.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ describe("categoryHrefToDocID", () => {
1919
href: "/reference/agent-services/",
2020
expectedID: "reference/agent-services/agent-services",
2121
},
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+
},
2232
];
2333

2434
test.each(testCases)("$description", (c) => {

src/theme/DocCardList/href-to-id.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
// IDs in the items prop, so we generate a page ID based on the assumption that
44
// category page slugs are the same as their containing directory names.
55
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(`/([^/]+)/?$`);
813
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}`);
1116
}
12-
return idPrefix + slug[1];
17+
return idPrefix + "/" + slug[1];
1318
}
14-

0 commit comments

Comments
 (0)