mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-02 11:47:23 +02:00
fix(v2): sidebar autogen from subfolder should read category metadata correctly (#4651)
* fix sidebar autogen bug * fix sidebar autogen bug
This commit is contained in:
parent
03536a0682
commit
d0d29f43cc
4 changed files with 162 additions and 0 deletions
|
@ -0,0 +1,16 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
someSidebar: [
|
||||||
|
{type: 'doc', id: 'API/api-end'},
|
||||||
|
{
|
||||||
|
type: 'autogenerated',
|
||||||
|
dirName: '3-API',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
|
@ -1493,6 +1493,88 @@ describe('site with partial autogenerated sidebars', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('site with partial autogenerated sidebars 2 (fix #4638)', () => {
|
||||||
|
// Test added for edge case https://github.com/facebook/docusaurus/issues/4638
|
||||||
|
|
||||||
|
async function loadSite() {
|
||||||
|
const siteDir = path.join(
|
||||||
|
__dirname,
|
||||||
|
'__fixtures__',
|
||||||
|
'site-with-autogenerated-sidebar',
|
||||||
|
);
|
||||||
|
const context = await loadContext(siteDir, {});
|
||||||
|
const plugin = pluginContentDocs(
|
||||||
|
context,
|
||||||
|
normalizePluginOptions(OptionsSchema, {
|
||||||
|
path: 'docs',
|
||||||
|
sidebarPath: path.join(
|
||||||
|
__dirname,
|
||||||
|
'__fixtures__',
|
||||||
|
'site-with-autogenerated-sidebar',
|
||||||
|
'partialAutogeneratedSidebars2.js',
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const content = (await plugin.loadContent?.())!;
|
||||||
|
|
||||||
|
return {content, siteDir};
|
||||||
|
}
|
||||||
|
|
||||||
|
test('sidebar is partially autogenerated', async () => {
|
||||||
|
const {content} = await loadSite();
|
||||||
|
const version = content.loadedVersions[0];
|
||||||
|
|
||||||
|
expect(version.sidebars).toEqual({
|
||||||
|
someSidebar: [
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
id: 'API/api-end',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
id: 'API/api-overview',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'category',
|
||||||
|
label: 'Core APIs',
|
||||||
|
collapsed: true,
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
|
||||||
|
id: 'API/Core APIs/Client API',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
id: 'API/Core APIs/Server API',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'category',
|
||||||
|
label: 'Extension APIs (label from _category_.yml)', // Fix #4638
|
||||||
|
collapsed: true,
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
id: 'API/Extension APIs/Plugin API',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
id: 'API/Extension APIs/Theme API',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'doc',
|
||||||
|
id: 'API/api-end',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('site with custom sidebar items generator', () => {
|
describe('site with custom sidebar items generator', () => {
|
||||||
async function loadSite(sidebarItemsGenerator: SidebarItemsGenerator) {
|
async function loadSite(sidebarItemsGenerator: SidebarItemsGenerator) {
|
||||||
const siteDir = path.join(
|
const siteDir = path.join(
|
||||||
|
|
|
@ -219,6 +219,20 @@ describe('DefaultSidebarItemsGenerator', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('generates subfolder sidebar', async () => {
|
test('generates subfolder sidebar', async () => {
|
||||||
|
// Ensure that category metadata file is correctly read
|
||||||
|
// fix edge case found in https://github.com/facebook/docusaurus/issues/4638
|
||||||
|
mockCategoryMetadataFiles({
|
||||||
|
'subfolder/subsubfolder/subsubsubfolder2/_category_.yml': {
|
||||||
|
position: 2,
|
||||||
|
label: 'subsubsubfolder2 (_category_.yml label)',
|
||||||
|
},
|
||||||
|
'subfolder/subsubfolder/subsubsubfolder3/_category_.json': {
|
||||||
|
position: 1,
|
||||||
|
label: 'subsubsubfolder3 (_category_.json label)',
|
||||||
|
collapsed: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const sidebarSlice = await DefaultSidebarItemsGenerator({
|
const sidebarSlice = await DefaultSidebarItemsGenerator({
|
||||||
item: {
|
item: {
|
||||||
type: 'autogenerated',
|
type: 'autogenerated',
|
||||||
|
@ -257,12 +271,61 @@ describe('DefaultSidebarItemsGenerator', () => {
|
||||||
sidebarPosition: undefined,
|
sidebarPosition: undefined,
|
||||||
frontMatter: {},
|
frontMatter: {},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'doc5',
|
||||||
|
source: 'doc5.md',
|
||||||
|
sourceDirName: 'subfolder/subsubfolder/subsubsubfolder',
|
||||||
|
sidebarPosition: undefined,
|
||||||
|
frontMatter: {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'doc6',
|
||||||
|
source: 'doc6.md',
|
||||||
|
sourceDirName: 'subfolder/subsubfolder/subsubsubfolder2',
|
||||||
|
sidebarPosition: undefined,
|
||||||
|
frontMatter: {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'doc7',
|
||||||
|
source: 'doc7.md',
|
||||||
|
sourceDirName: 'subfolder/subsubfolder/subsubsubfolder3',
|
||||||
|
sidebarPosition: 2,
|
||||||
|
frontMatter: {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'doc8',
|
||||||
|
source: 'doc8.md',
|
||||||
|
sourceDirName: 'subfolder/subsubfolder/subsubsubfolder3',
|
||||||
|
sidebarPosition: 1,
|
||||||
|
frontMatter: {},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(sidebarSlice).toEqual([
|
expect(sidebarSlice).toEqual([
|
||||||
|
{
|
||||||
|
type: 'category',
|
||||||
|
label: 'subsubsubfolder3 (_category_.json label)',
|
||||||
|
collapsed: false,
|
||||||
|
items: [
|
||||||
|
{type: 'doc', id: 'doc8'},
|
||||||
|
{type: 'doc', id: 'doc7'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'category',
|
||||||
|
label: 'subsubsubfolder2 (_category_.yml label)',
|
||||||
|
collapsed: true,
|
||||||
|
items: [{type: 'doc', id: 'doc6'}],
|
||||||
|
},
|
||||||
{type: 'doc', id: 'doc1'},
|
{type: 'doc', id: 'doc1'},
|
||||||
{type: 'doc', id: 'doc4'},
|
{type: 'doc', id: 'doc4'},
|
||||||
|
{
|
||||||
|
type: 'category',
|
||||||
|
label: 'subsubsubfolder',
|
||||||
|
collapsed: true,
|
||||||
|
items: [{type: 'doc', id: 'doc5'}],
|
||||||
|
},
|
||||||
] as Sidebar);
|
] as Sidebar);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -186,6 +186,7 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async functio
|
||||||
}): Promise<SidebarItemCategory & WithPosition> {
|
}): Promise<SidebarItemCategory & WithPosition> {
|
||||||
const categoryDirPath = path.join(
|
const categoryDirPath = path.join(
|
||||||
version.contentPath,
|
version.contentPath,
|
||||||
|
item.dirName, // fix https://github.com/facebook/docusaurus/issues/4638
|
||||||
breadcrumb.join(BreadcrumbSeparator),
|
breadcrumb.join(BreadcrumbSeparator),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue