fix(docs): Fix empty sidebar item category className lost when post-processed to a doc (#11281)

This commit is contained in:
Sébastien Lorber 2025-06-19 19:04:28 +02:00 committed by GitHub
parent 068d4c63a9
commit 1cbc0118b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 45 additions and 20 deletions

View file

@ -2,6 +2,8 @@
id: hello-2 id: hello-2
title: Hello 2 title: Hello 2
sidebar_label: Hello 2 From Doc sidebar_label: Hello 2 From Doc
sidebar_class_name: front-matter-class-name
sidebar_custom_props: {custom: "from front matter"}
--- ---
Hello World 2! Hello World 2!

View file

@ -8,7 +8,9 @@
{ {
"id": "hello-2", "id": "hello-2",
"type": "doc", "type": "doc",
"label": "Hello Two" "label": "Hello Two",
"className": "class-name-from-sidebars.json",
"customProps": {"test": "from sidebars.json"}
} }
] ]
} }

View file

@ -8,6 +8,10 @@ exports[`sidebar site with undefined sidebar 1`] = `
"type": "doc", "type": "doc",
}, },
{ {
"className": "front-matter-class-name",
"customProps": {
"custom": "from front matter",
},
"id": "hello-2", "id": "hello-2",
"label": "Hello 2 From Doc", "label": "Hello 2 From Doc",
"type": "doc", "type": "doc",

View file

@ -582,14 +582,16 @@ describe('site with doc label', () => {
); );
}); });
it('sidebar_label in doc has higher precedence over label in sidebar.json', async () => { it('frontMatter.sidebar_* data in doc has higher precedence over sidebar.json data', async () => {
const {content} = await loadSite(); const {content} = await loadSite();
const loadedVersion = content.loadedVersions[0]!; const loadedVersion = content.loadedVersions[0]!;
const sidebarProps = toSidebarsProp(loadedVersion); const sidebarProps = toSidebarsProp(loadedVersion);
expect((sidebarProps.docs![1] as PropSidebarItemLink).label).toBe( const item = sidebarProps.docs![1] as PropSidebarItemLink;
'Hello 2 From Doc',
); expect(item.label).toBe('Hello 2 From Doc');
expect(item.className).toBe('front-matter-class-name');
expect(item.customProps).toStrictEqual({custom: 'from front matter'});
}); });
}); });

View file

@ -38,22 +38,14 @@ export function toSidebarDocItemLinkProp({
'id' | 'title' | 'permalink' | 'unlisted' | 'frontMatter' 'id' | 'title' | 'permalink' | 'unlisted' | 'frontMatter'
>; >;
}): PropSidebarItemLink { }): PropSidebarItemLink {
const { const {id, title, permalink, frontMatter, unlisted} = doc;
id,
title,
permalink,
frontMatter: {
sidebar_label: sidebarLabel,
sidebar_custom_props: customProps,
},
unlisted,
} = doc;
return { return {
type: 'link', type: 'link',
label: sidebarLabel ?? item.label ?? title,
href: permalink, href: permalink,
className: item.className, // Front Matter data takes precedence over sidebars.json
customProps: item.customProps ?? customProps, label: frontMatter.sidebar_label ?? item.label ?? title,
className: frontMatter.sidebar_class_name ?? item.className,
customProps: frontMatter.sidebar_custom_props ?? item.customProps,
docId: id, docId: id,
unlisted, unlisted,
}; };

View file

@ -76,6 +76,10 @@ exports[`postProcess transforms category without subitems 1`] = `
{ {
"sidebar": [ "sidebar": [
{ {
"className": "category-className",
"customProps": {
"custom": true,
},
"id": "doc ID", "id": "doc ID",
"label": "Category 2", "label": "Category 2",
"type": "doc", "type": "doc",

View file

@ -31,6 +31,8 @@ describe('postProcess', () => {
type: 'doc', type: 'doc',
id: 'doc ID', id: 'doc ID',
}, },
className: 'category-className',
customProps: {custom: true},
items: [], items: [],
}, },
], ],

View file

@ -77,10 +77,13 @@ function postProcessSidebarItem(
) { ) {
return null; return null;
} }
const {label, className, customProps} = category;
return { return {
type: 'doc', type: 'doc',
label: category.label,
id: category.link.id, id: category.link.id,
label,
...(className && {className}),
...(customProps && {customProps}),
}; };
} }
// A non-collapsible category can't be collapsed! // A non-collapsible category can't be collapsed!

View file

@ -0,0 +1,14 @@
---
sidebar_label: 'Dir with unique index label'
sidebar_class_name: 'dogfood_sidebar_class_name_test'
sidebar_custom_props:
prop: custom
number: 1
boolean: true
---
# Single index.md in dir
This doc has `sidebar_class_*` front matter
Dogfood test for bug https://github.com/facebook/docusaurus/issues/11258

View file

@ -4,4 +4,4 @@ sidebar_class_name: 'dogfood_sidebar_class_name_test'
# Doc With Sidebar Class Name # Doc With Sidebar Class Name
This doc has `sidebar_label` front matter This doc has `sidebar_class_name` front matter