From 4972f12be64bdc4b9ffd22a1d9d0395fb55530c8 Mon Sep 17 00:00:00 2001 From: Artem Kovalov Date: Fri, 30 Oct 2020 15:40:19 +0100 Subject: [PATCH 1/4] Fix for undefined path with dropdown menu in Navbar --- packages/docusaurus-theme-classic/src/__tests__/utils.test.js | 3 +++ packages/docusaurus-theme-classic/src/utils/index.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-theme-classic/src/__tests__/utils.test.js b/packages/docusaurus-theme-classic/src/__tests__/utils.test.js index 3c78736cafd7..13511e89c498 100644 --- a/packages/docusaurus-theme-classic/src/__tests__/utils.test.js +++ b/packages/docusaurus-theme-classic/src/__tests__/utils.test.js @@ -19,4 +19,7 @@ describe('isSamePath', () => { test('should be false for compared path with double trailing slash', () => { expect(isSamePath('/docs', '/docs//')).toBeFalsy(); }); + test('should not fail if path is "undefined" when it is absent in case of dropdown in Navbar ', () => { + expect(isSamePath(undefined, undefined)).toBeTruthy(); + }); }); diff --git a/packages/docusaurus-theme-classic/src/utils/index.ts b/packages/docusaurus-theme-classic/src/utils/index.ts index 6575f1fa08c6..d9abb790e6d0 100644 --- a/packages/docusaurus-theme-classic/src/utils/index.ts +++ b/packages/docusaurus-theme-classic/src/utils/index.ts @@ -8,5 +8,5 @@ // Compare the 2 paths, ignoring trailing / export const isSamePath = (path1, path2) => { const normalize = (str) => (str.endsWith('/') ? str : `${str}/`); - return normalize(path1) === normalize(path2); + return path1 && path2 ? normalize(path1) === normalize(path2) : true; }; From 571583bba3593e38d972da284c3c11186758a49a Mon Sep 17 00:00:00 2001 From: Artem Kovalov Date: Sun, 1 Nov 2020 23:15:44 +0100 Subject: [PATCH 2/4] Filter out 'href' links and check for undefined in utils --- packages/docusaurus-theme-classic/src/__tests__/utils.test.js | 3 --- .../src/theme/NavbarItem/DefaultNavbarItem.tsx | 2 +- packages/docusaurus-theme-classic/src/utils/index.ts | 4 ++-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/__tests__/utils.test.js b/packages/docusaurus-theme-classic/src/__tests__/utils.test.js index 13511e89c498..3c78736cafd7 100644 --- a/packages/docusaurus-theme-classic/src/__tests__/utils.test.js +++ b/packages/docusaurus-theme-classic/src/__tests__/utils.test.js @@ -19,7 +19,4 @@ describe('isSamePath', () => { test('should be false for compared path with double trailing slash', () => { expect(isSamePath('/docs', '/docs//')).toBeFalsy(); }); - test('should not fail if path is "undefined" when it is absent in case of dropdown in Navbar ', () => { - expect(isSamePath(undefined, undefined)).toBeTruthy(); - }); }); diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx index 2ba2a7e495bf..738fe0579882 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx @@ -159,7 +159,7 @@ function NavItemMobile({ const menuListRef = useRef(null); const {pathname} = useLocation(); const [collapsed, setCollapsed] = useState( - () => !items?.some((item) => isSamePath(item.to, pathname)) ?? true, + () => !items?.filter((item) => item.to).some((item) => isSamePath(item.to, pathname)) ?? true, ); const navLinkClassNames = (extraClassName?: string, isSubList = false) => diff --git a/packages/docusaurus-theme-classic/src/utils/index.ts b/packages/docusaurus-theme-classic/src/utils/index.ts index d9abb790e6d0..1b0e0473f53a 100644 --- a/packages/docusaurus-theme-classic/src/utils/index.ts +++ b/packages/docusaurus-theme-classic/src/utils/index.ts @@ -7,6 +7,6 @@ // Compare the 2 paths, ignoring trailing / export const isSamePath = (path1, path2) => { - const normalize = (str) => (str.endsWith('/') ? str : `${str}/`); - return path1 && path2 ? normalize(path1) === normalize(path2) : true; + const normalize = (str) => (str?.endsWith('/') ? str : `${str}/`); // + return normalize(path1) === normalize(path2) }; From d88d4da5c606edb1c80a10951548e7e797307d98 Mon Sep 17 00:00:00 2001 From: Artem Kovalov Date: Sun, 1 Nov 2020 23:19:19 +0100 Subject: [PATCH 3/4] Minot syntax fixes --- packages/docusaurus-theme-classic/src/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/utils/index.ts b/packages/docusaurus-theme-classic/src/utils/index.ts index 1b0e0473f53a..54cda35a8d60 100644 --- a/packages/docusaurus-theme-classic/src/utils/index.ts +++ b/packages/docusaurus-theme-classic/src/utils/index.ts @@ -7,6 +7,6 @@ // Compare the 2 paths, ignoring trailing / export const isSamePath = (path1, path2) => { - const normalize = (str) => (str?.endsWith('/') ? str : `${str}/`); // - return normalize(path1) === normalize(path2) + const normalize = (str) => (str?.endsWith('/') ? str : `${str}/`); + return normalize(path1) === normalize(path2); }; From 1724fee0d3b193fd11243df247d6ec364ed18c67 Mon Sep 17 00:00:00 2001 From: slorber Date: Mon, 2 Nov 2020 18:56:14 +0100 Subject: [PATCH 4/4] allow isSamePath to accept undefined values --- .../src/__tests__/utils.test.js | 10 ++++++++++ .../src/theme/NavbarItem/DefaultNavbarItem.tsx | 2 +- packages/docusaurus-theme-classic/src/utils/index.ts | 11 ++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/__tests__/utils.test.js b/packages/docusaurus-theme-classic/src/__tests__/utils.test.js index 3c78736cafd7..bdae0c5259b4 100644 --- a/packages/docusaurus-theme-classic/src/__tests__/utils.test.js +++ b/packages/docusaurus-theme-classic/src/__tests__/utils.test.js @@ -19,4 +19,14 @@ describe('isSamePath', () => { test('should be false for compared path with double trailing slash', () => { expect(isSamePath('/docs', '/docs//')).toBeFalsy(); }); + + test('should be true for twice undefined/null', () => { + expect(isSamePath(undefined, undefined)).toBeTruthy(); + expect(isSamePath(undefined, undefined)).toBeTruthy(); + }); + + test('should be false when one undefined', () => { + expect(isSamePath('/docs', undefined)).toBeFalsy(); + expect(isSamePath(undefined, '/docs')).toBeFalsy(); + }); }); diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx index 738fe0579882..2ba2a7e495bf 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx @@ -159,7 +159,7 @@ function NavItemMobile({ const menuListRef = useRef(null); const {pathname} = useLocation(); const [collapsed, setCollapsed] = useState( - () => !items?.filter((item) => item.to).some((item) => isSamePath(item.to, pathname)) ?? true, + () => !items?.some((item) => isSamePath(item.to, pathname)) ?? true, ); const navLinkClassNames = (extraClassName?: string, isSubList = false) => diff --git a/packages/docusaurus-theme-classic/src/utils/index.ts b/packages/docusaurus-theme-classic/src/utils/index.ts index 54cda35a8d60..d2b769cab124 100644 --- a/packages/docusaurus-theme-classic/src/utils/index.ts +++ b/packages/docusaurus-theme-classic/src/utils/index.ts @@ -6,7 +6,12 @@ */ // Compare the 2 paths, ignoring trailing / -export const isSamePath = (path1, path2) => { - const normalize = (str) => (str?.endsWith('/') ? str : `${str}/`); - return normalize(path1) === normalize(path2); +export const isSamePath = ( + path1: string | undefined, + path2: string | undefined, +) => { + const normalize = (pathname: string | undefined) => { + return !pathname || pathname?.endsWith('/') ? pathname : `${pathname}/`; + }; + return normalize(path1) === normalize(path2); };