Skip to content

Commit

Permalink
fix(v2): navbar dropdown crash when item.to is undefined (#3662)
Browse files Browse the repository at this point in the history
* Fix for undefined path with dropdown menu in Navbar

* Filter out 'href' links and check for undefined in utils

* Minot syntax fixes

* allow isSamePath to accept undefined values

Co-authored-by: slorber <lorber.sebastien@gmail.com>
  • Loading branch information
artemkovalyov and slorber authored Nov 2, 2020
1 parent 3ebe888 commit 5aca1d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/docusaurus-theme-classic/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
9 changes: 7 additions & 2 deletions packages/docusaurus-theme-classic/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
*/

// Compare the 2 paths, ignoring trailing /
export const isSamePath = (path1, path2) => {
const normalize = (str) => (str.endsWith('/') ? str : `${str}/`);
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);
};

0 comments on commit 5aca1d7

Please sign in to comment.