Skip to content

Commit 910f443

Browse files
authored
Remove redundant sidebar items (#144)
Closes #87 Remove sidebar items that have the same IDs as top-level category index pages. This is because the sidebar item categories already link to these pages.
1 parent 1058d62 commit 910f443

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

docusaurus.config.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {
2323
getRootDir,
2424
updatePathsInIncludes,
2525
} from "./server/asset-path-helpers";
26-
import { orderSidebarItems } from "./server/sidebar-order";
26+
import {
27+
orderSidebarItems,
28+
removeRedundantItems,
29+
} from "./server/sidebar-order";
2730
import { extendedPostcssConfigPlugin } from "./server/postcss";
2831
import { rehypeHLJS } from "./server/rehype-hljs";
2932
import { definer as hcl } from "highlightjs-terraform";
@@ -186,14 +189,20 @@ const config: Config = {
186189
categoriesMetadata,
187190
isCategoryIndex,
188191
});
192+
189193
const idToDocPage = new Map();
190194
docs.forEach((d) => {
191195
idToDocPage.set(d.id, d);
192196
});
197+
193198
const getDocPageByID = (id: string) => {
194199
return idToDocPage.get(id);
195200
};
196-
return orderSidebarItems(items, getDocPageByID);
201+
202+
return orderSidebarItems(
203+
removeRedundantItems(items, item.dirName),
204+
getDocPageByID
205+
);
197206
},
198207
// Host docs on the root page, later it will be exposed on goteleport.com/docs
199208
// next to the website and blog

server/sidebar-order.test.ts

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from "@jest/globals";
22
import { basename, dirname } from "node:path";
3-
import { orderSidebarItems } from "./sidebar-order";
3+
import { orderSidebarItems, removeRedundantItems } from "./sidebar-order";
44
import type { docPage } from "./sidebar-order";
55
import type { NormalizedSidebarItem } from "@docusaurus/plugin-content-docs/src/sidebars/types.ts";
66

@@ -24,7 +24,7 @@ function getDocPageForId(id: string): docPage {
2424
};
2525
}
2626

27-
describe.only("orderSidebarItems", () => {
27+
describe("orderSidebarItems", () => {
2828
interface testCase {
2929
description: string;
3030
input: Array<NormalizedSidebarItem>;
@@ -503,3 +503,71 @@ describe.only("orderSidebarItems", () => {
503503
expect(actual).toEqual(c.expected);
504504
});
505505
});
506+
507+
describe("removeRedundantItems", () => {
508+
interface testCase {
509+
description: string;
510+
input: Array<NormalizedSidebarItem>;
511+
dirname: string;
512+
expected: Array<NormalizedSidebarItem>;
513+
}
514+
515+
// To write a test case, you can print the items array returned by
516+
// defaultSidebarItemsGenerator in docusaurus.config.ts and find the
517+
// subarray of items you would like to include.
518+
const testCases: Array<testCase> = [
519+
{
520+
description: "Removes top-level category index pages",
521+
dirname: "reference",
522+
input: [
523+
{
524+
type: "doc",
525+
id: "reference/reference",
526+
},
527+
{
528+
type: "category",
529+
label: "My Docs Category",
530+
items: [
531+
{
532+
type: "doc",
533+
id: "reference/my-category/page-c",
534+
},
535+
{
536+
type: "doc",
537+
id: "reference/my-category/page-a",
538+
},
539+
{
540+
type: "doc",
541+
id: "reference/my-category/page-b",
542+
},
543+
],
544+
},
545+
],
546+
expected: [
547+
{
548+
type: "category",
549+
label: "My Docs Category",
550+
items: [
551+
{
552+
type: "doc",
553+
id: "reference/my-category/page-c",
554+
},
555+
{
556+
type: "doc",
557+
id: "reference/my-category/page-a",
558+
},
559+
{
560+
type: "doc",
561+
id: "reference/my-category/page-b",
562+
},
563+
],
564+
},
565+
],
566+
},
567+
];
568+
569+
test.each(testCases)("$description", (c) => {
570+
const actual = removeRedundantItems(c.input, c.dirname);
571+
expect(actual).toEqual(c.expected);
572+
});
573+
});

server/sidebar-order.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,26 @@ export const orderSidebarItems = (
103103
}
104104
});
105105
};
106+
107+
// removeRedundantItems removes top-level category index pages from the sidebar,
108+
// since we expect these to be defined as links within each top-level category.
109+
export const removeRedundantItems = (
110+
items: Array<NormalizedSidebarItem>,
111+
dirname: string
112+
): Array<NormalizedSidebarItem> => {
113+
// Return all items except for the one with the ID of the index page to
114+
// remove from the body of the sidebar section. We expect the top-level category index
115+
// page to be in, and named after, the section's root directory, e.g.:
116+
//
117+
// - "reference/reference"
118+
// - "admin-guides/admin-guides"
119+
return items.filter((item) => {
120+
if (!item.hasOwnProperty("id")) {
121+
return true;
122+
}
123+
return (
124+
(item as { id: string; [propName: string]: unknown }).id !==
125+
dirname + "/" + dirname
126+
);
127+
});
128+
};

0 commit comments

Comments
 (0)