From d0d29f43cc5ac01faa35aafab4e9e4ee45a3491a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Tue, 20 Apr 2021 18:16:51 +0200 Subject: [PATCH] fix(v2): sidebar autogen from subfolder should read category metadata correctly (#4651) * fix sidebar autogen bug * fix sidebar autogen bug --- .../partialAutogeneratedSidebars2.js | 16 ++++ .../src/__tests__/index.test.ts | 82 +++++++++++++++++++ .../__tests__/sidebarItemsGenerator.test.ts | 63 ++++++++++++++ .../src/sidebarItemsGenerator.ts | 1 + 4 files changed, 162 insertions(+) create mode 100644 packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js new file mode 100644 index 000000000000..eff9f9fb9eee --- /dev/null +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/site-with-autogenerated-sidebar/partialAutogeneratedSidebars2.js @@ -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', + }, + ], +}; diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts index 7c5bc8f94ea3..a06aed5bd15e 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/index.test.ts @@ -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', () => { async function loadSite(sidebarItemsGenerator: SidebarItemsGenerator) { const siteDir = path.join( diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/sidebarItemsGenerator.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/sidebarItemsGenerator.test.ts index 38b8818cd5b6..81f07f4e81ce 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/sidebarItemsGenerator.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/sidebarItemsGenerator.test.ts @@ -219,6 +219,20 @@ describe('DefaultSidebarItemsGenerator', () => { }); 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({ item: { type: 'autogenerated', @@ -257,12 +271,61 @@ describe('DefaultSidebarItemsGenerator', () => { sidebarPosition: undefined, 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([ + { + 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: 'doc4'}, + { + type: 'category', + label: 'subsubsubfolder', + collapsed: true, + items: [{type: 'doc', id: 'doc5'}], + }, ] as Sidebar); }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/sidebarItemsGenerator.ts b/packages/docusaurus-plugin-content-docs/src/sidebarItemsGenerator.ts index f5a5bd5d5133..f35c52493b0b 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebarItemsGenerator.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebarItemsGenerator.ts @@ -186,6 +186,7 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async functio }): Promise { const categoryDirPath = path.join( version.contentPath, + item.dirName, // fix https://github.com/facebook/docusaurus/issues/4638 breadcrumb.join(BreadcrumbSeparator), );