Skip to content

Respect sidebar_label when ordering the sidebar #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 169 additions & 19 deletions server/sidebar-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function getDocPageForId(id: string): docPage {
return {
title: title,
id: id,
frontmatter: {
frontMatter: {
title: title,
description: "Provides information on Teleport functionality",
},
Expand Down Expand Up @@ -510,7 +510,7 @@ describe("orderSidebarItems", () => {
"page-a": {
title: "Page A",
id: "page-a",
frontmatter: {
frontMatter: {
title: "Page A",
description: "Page A",
},
Expand All @@ -520,7 +520,7 @@ describe("orderSidebarItems", () => {
"page-b": {
title: "Page B",
id: "page-b",
frontmatter: {
frontMatter: {
title: "Page B",
description: "Page B",
},
Expand All @@ -530,7 +530,7 @@ describe("orderSidebarItems", () => {
"page-c": {
title: "Page C",
id: "page-c",
frontmatter: {
frontMatter: {
title: "Page C",
description: "Page C",
},
Expand All @@ -540,7 +540,7 @@ describe("orderSidebarItems", () => {
"page-d": {
title: "Introduction",
id: "page-d",
frontmatter: {
frontMatter: {
title: "Introduction",
description: "Introduction",
},
Expand All @@ -550,7 +550,7 @@ describe("orderSidebarItems", () => {
"page-e": {
title: "Page E",
id: "page-e",
frontmatter: {
frontMatter: {
title: "Page E",
description: "Page E",
sidebar_position: 2,
Expand All @@ -562,7 +562,7 @@ describe("orderSidebarItems", () => {
"page-f": {
title: "Getting Started",
id: "page-f",
frontmatter: {
frontMatter: {
title: "Getting Started",
description: "Getting Started",
},
Expand All @@ -572,7 +572,7 @@ describe("orderSidebarItems", () => {
"page-g": {
title: "Introduction",
id: "page-g",
frontmatter: {
frontMatter: {
title: "Introduction",
description: "Introduction",
},
Expand Down Expand Up @@ -760,12 +760,162 @@ describe("orderSidebarItems", () => {
});
});

describe("sidebar label", () => {
const idToDocPage = {
"page-a": {
title: "Page A",
id: "page-a",
frontMatter: {
title: "Page A",
sidebar_label: "Page Z",
description: "Page A",
},
source: "@site/docs/page-a.mdx",
sourceDirName: "",
},
"page-b": {
title: "Page B",
id: "page-b",
frontMatter: {
title: "Page B",
sidebar_label: "Page W",
description: "Page B",
},
source: "@site/docs/page-b.mdx",
sourceDirName: "",
},
"page-c": {
title: "Page C",
id: "page-c",
frontMatter: {
title: "Page C",
sidebar_label: "Page X",
description: "Page C",
},
source: "@site/docs/page-c.mdx",
sourceDirName: "",
},
"page-d": {
title: "Page D",
id: "page-d",
frontMatter: {
title: "Page D",
description: "Page D",
},
source: "@site/docs/page-d.mdx",
sourceDirName: "",
},
};

interface testCase {
description: string;
input: Array<NormalizedSidebarItem>;
expected: Array<NormalizedSidebarItem>;
}

const testCases: Array<testCase> = [
{
description: "all pages use sidebar_label",
input: [
{
type: "category",
label: "My Docs Category",
items: [
{
type: "doc",
id: "page-a",
},
{
type: "doc",
id: "page-b",
},
{
type: "doc",
id: "page-c",
},
],
},
],
expected: [
{
type: "category",
label: "My Docs Category",
items: [
{
type: "doc",
id: "page-b",
},
{
type: "doc",
id: "page-c",
},
{
type: "doc",
id: "page-a",
},
],
},
],
},
{
description: "one page uses title",
input: [
{
type: "category",
label: "My Docs Category",
items: [
{
type: "doc",
id: "page-d",
},
{
type: "doc",
id: "page-b",
},
{
type: "doc",
id: "page-c",
},
],
},
],
expected: [
{
type: "category",
label: "My Docs Category",
items: [
{
type: "doc",
id: "page-d",
},
{
type: "doc",
id: "page-b",
},
{
type: "doc",
id: "page-c",
},
],
},
],
},
];

test.each(testCases)("$description", (c) => {
const actual = orderSidebarItems(c.input, (id: string): docPage => {
return idToDocPage[id];
});
expect(actual).toEqual(c.expected);
});
});

describe("ordering category index pages", () => {
const idToDocPage = {
"section-a/section-a": {
title: "Section A",
id: "section-a/section-a",
frontmatter: {
frontMatter: {
title: "Section A",
description: "Section A",
},
Expand All @@ -775,7 +925,7 @@ describe("orderSidebarItems", () => {
"section-a/page-a": {
title: "Section A Page A",
id: "section-a/page-a",
frontmatter: {
frontMatter: {
title: "Section A Page A",
description: "Page A",
},
Expand All @@ -785,7 +935,7 @@ describe("orderSidebarItems", () => {
"section-a/page-b": {
title: "Section A Page B",
id: "section-a/page-b",
frontmatter: {
frontMatter: {
title: "Section A Page B",
description: "Page B",
},
Expand All @@ -795,7 +945,7 @@ describe("orderSidebarItems", () => {
"section-b/section-b": {
title: "Section B",
id: "section-b/section-b",
frontmatter: {
frontMatter: {
title: "Section B",
description: "Section B",
},
Expand All @@ -806,7 +956,7 @@ describe("orderSidebarItems", () => {
"section-b/page-a": {
title: "Section B Page A",
id: "section-b/page-a",
frontmatter: {
frontMatter: {
title: "Section B Page A",
description: "Page A",
},
Expand All @@ -817,7 +967,7 @@ describe("orderSidebarItems", () => {
"section-b/page-b": {
title: "Section B Page B",
id: "section-b/page-b",
frontmatter: {
frontMatter: {
title: "Section B Page B",
description: "Page B",
},
Expand All @@ -827,7 +977,7 @@ describe("orderSidebarItems", () => {
intro: {
title: "Introduction",
id: "intro",
frontmatter: {
frontMatter: {
title: "Introduction",
description: "Introduction",
},
Expand Down Expand Up @@ -936,7 +1086,7 @@ describe("orderSidebarItems", () => {
"page-a": {
title: "Page A",
id: "page-a",
frontmatter: {
frontMatter: {
title: "Page A",
description: "Page A",
},
Expand All @@ -947,7 +1097,7 @@ describe("orderSidebarItems", () => {
"page-b": {
title: "Page B",
id: "page-b",
frontmatter: {
frontMatter: {
title: "Page B",
description: "Page B",
},
Expand Down Expand Up @@ -988,7 +1138,7 @@ describe("orderSidebarItems", () => {
"page-a": {
title: "Page A",
id: "page-a",
frontmatter: {
frontMatter: {
title: "Page A",
description: "Page A",
},
Expand All @@ -999,7 +1149,7 @@ describe("orderSidebarItems", () => {
"page-b": {
title: "Page B",
id: "page-b",
frontmatter: {
frontMatter: {
title: "Page B",
description: "Page B",
},
Expand Down
5 changes: 4 additions & 1 deletion server/sidebar-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
export interface docPage {
title: string;
id: string;
frontmatter: {
frontMatter: {
[index: string]: any;
};
source: string;
Expand Down Expand Up @@ -40,6 +40,9 @@ const getOrderAttributes = (
title = page.title;
sidebarPosition = page.sidebarPosition;
id = page.id;
if (page.frontMatter && page.frontMatter.sidebar_label) {
title = page.frontMatter.sidebar_label;
}
break;
case "category":
const cat = item as NormalizedSidebarItemCategory;
Expand Down