mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-21 10:37:51 +02:00
fix(docs): Fix empty sidebar item category className
lost when post-processed to a doc (#11281)
This commit is contained in:
parent
068d4c63a9
commit
1cbc0118b0
10 changed files with 45 additions and 20 deletions
|
@ -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!
|
||||||
|
|
|
@ -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"}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -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",
|
||||||
|
|
|
@ -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'});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
|
@ -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",
|
||||||
|
|
|
@ -31,6 +31,8 @@ describe('postProcess', () => {
|
||||||
type: 'doc',
|
type: 'doc',
|
||||||
id: 'doc ID',
|
id: 'doc ID',
|
||||||
},
|
},
|
||||||
|
className: 'category-className',
|
||||||
|
customProps: {custom: true},
|
||||||
items: [],
|
items: [],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
|
@ -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!
|
||||||
|
|
|
@ -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
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue