Skip to content

Commit

Permalink
fix(theme-common): isSamePath should be case-insensitive (#6758)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber authored Feb 25, 2022
1 parent 39b66d8 commit 44a65f3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ describe('isSamePath', () => {
expect(isSamePath('/docs', '/docs/')).toBeTruthy();
});

test('should be true for compared path with different case', () => {
expect(isSamePath('/doCS', '/DOcs')).toBeTruthy();
});

test('should be true for compared path with different case + trailing slash', () => {
expect(isSamePath('/doCS', '/DOcs/')).toBeTruthy();
});

test('should be false for compared path with double trailing slash', () => {
expect(isSamePath('/docs', '/docs//')).toBeFalsy();
});
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus-theme-common/src/utils/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/

// Compare the 2 paths, ignoring trailing /
// Compare the 2 paths, case insensitive and ignoring trailing slash
export const isSamePath = (
path1: string | undefined,
path2: string | undefined,
): boolean => {
const normalize = (pathname: string | undefined) =>
!pathname || pathname?.endsWith('/') ? pathname : `${pathname}/`;
(!pathname || pathname?.endsWith('/')
? pathname
: `${pathname}/`
)?.toLowerCase();
return normalize(path1) === normalize(path2);
};
5 changes: 5 additions & 0 deletions website/_dogfooding/_docs tests/tests/Case-Sentitive-Doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Case-Sensitive doc

This doc has uppercase and lowercase chars in its filename, and thus in its path / slug.

It should still work fine if the doc is server from a lowercase/uppercase path.

0 comments on commit 44a65f3

Please sign in to comment.