Skip to content

Commit 2ad40d7

Browse files
committed
Respond to feedback
- Remove unnecessary double negation. - Add an escape character to `categoryDirPattern` - Handle invalid entry slugs and add tests for invalid `getIndexPageID` cases
1 parent cb3634a commit 2ad40d7

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

server/config-docs.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
DocusaurusCategory,
77
} from "./config-docs";
88

9-
describe("getIndexPageID", () => {
9+
describe("getIndexPageID with valid entries", () => {
1010
interface testCase {
1111
description: string;
1212
category: NavigationCategory;
@@ -85,6 +85,59 @@ describe("getIndexPageID", () => {
8585
});
8686
});
8787

88+
describe("getIndexPageID with invalid entries", () => {
89+
interface testCase {
90+
description: string;
91+
category: NavigationCategory;
92+
errorSubstring: string;
93+
}
94+
95+
const testCases: Array<testCase> = [
96+
{
97+
description: "non-generated with malformed slugs",
98+
category: {
99+
icon: "connect",
100+
title: "User Guides",
101+
entries: [
102+
{
103+
title: "Introduction",
104+
slug: "",
105+
},
106+
{
107+
title: "Using tsh",
108+
slug: "/docs/connect-your-client/tsh/",
109+
},
110+
],
111+
},
112+
errorSubstring: `malformed slug in docs sidebar configuration: ""`,
113+
},
114+
{
115+
description: "slugs with different top-level segments",
116+
category: {
117+
icon: "connect",
118+
title: "User Guides",
119+
entries: [
120+
{
121+
title: "Introduction",
122+
slug: "/ver/12.x/enroll-resources/introduction/",
123+
},
124+
{
125+
title: "Using tsh",
126+
slug: "/ver/12.x/connect-your-client/tsh/",
127+
},
128+
],
129+
},
130+
errorSubstring: `cannot determine a category index page ID for top-level category User Guides because not all of its entries are in the same first-level directory`,
131+
},
132+
];
133+
134+
test.each(testCases)("$description", (c) => {
135+
expect(() => {
136+
getIndexPageID(c.category);
137+
}).toThrow(c.errorSubstring);
138+
});
139+
});
140+
88141
describe("makeDocusaurusNavigationCategory", () => {
89142
interface testCase {
90143
description: string;

server/config-docs.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export interface DocusaurusCategory {
362362
// version, or the default version. Examples:
363363
// - /ver/10.x/installation/
364364
// - /database-access/introduction/
365-
const categoryDirPattern = `(/ver/[0-9]+\.x)?/([^/]*)`;
365+
const categoryDirPattern = `(/ver/[0-9]+\\.x)?/([^/]*)`;
366366

367367
// getIndexPageID infers the Docusaurus page ID of a category index page based
368368
// on the slugs of pages within the category. The legacy config.json format does
@@ -378,25 +378,34 @@ export const getIndexPageID = (category: NavigationCategory): string => {
378378

379379
// Base the ID on the directory we generated the sidebar from in the legacy
380380
// docs site.
381-
if (!!category.generateFrom) {
381+
if (category.generateFrom) {
382382
return category.generateFrom + "/" + category.generateFrom;
383383
}
384384

385+
const slugSegments = category.entries.map((e) => {
386+
const parts = e.slug.match(categoryDirPattern);
387+
if (!parts) {
388+
throw new Error(
389+
`malformed slug in docs sidebar configuration: "${e.slug}"`
390+
);
391+
}
392+
return parts[2];
393+
});
394+
385395
// Check if the entries contain the root index page. If they do, use that ID
386-
if (category.entries.some((e) => e.slug.match(categoryDirPattern)[2] == "")) {
396+
if (slugSegments.some((e) => e == "")) {
387397
return "index";
388398
}
389399

390400
// The sidebar is manually defined, so base the category index page ID on
391401
// the first-level directory that contains all entries in the category.
392402
let categoryIndexDir: string;
393-
for (let i = 0; i < category.entries.length; i++) {
394-
const rootDirName = category.entries[i].slug.match(categoryDirPattern)[2];
403+
for (let i = 0; i < slugSegments.length; i++) {
395404
if (!categoryIndexDir) {
396-
categoryIndexDir = rootDirName;
405+
categoryIndexDir = slugSegments[i];
397406
continue;
398407
}
399-
if (rootDirName != categoryIndexDir) {
408+
if (slugSegments[i] != categoryIndexDir) {
400409
throw new Error(
401410
`cannot determine a category index page ID for top-level category ${category.title} because not all of its entries are in the same first-level directory`
402411
);

0 commit comments

Comments
 (0)