fix(theme-common): isSamePath should be case-insensitive (#6758)

This commit is contained in:
Sébastien Lorber 2022-02-25 14:58:54 +01:00 committed by GitHub
parent 39b66d82ef
commit 44a65f3337
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 2 deletions

View file

@ -16,6 +16,14 @@ describe('isSamePath', () => {
expect(isSamePath('/docs', '/docs/')).toBeTruthy(); 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', () => { test('should be false for compared path with double trailing slash', () => {
expect(isSamePath('/docs', '/docs//')).toBeFalsy(); expect(isSamePath('/docs', '/docs//')).toBeFalsy();
}); });

View file

@ -5,12 +5,15 @@
* LICENSE file in the root directory of this source tree. * 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 = ( export const isSamePath = (
path1: string | undefined, path1: string | undefined,
path2: string | undefined, path2: string | undefined,
): boolean => { ): boolean => {
const normalize = (pathname: string | undefined) => const normalize = (pathname: string | undefined) =>
!pathname || pathname?.endsWith('/') ? pathname : `${pathname}/`; (!pathname || pathname?.endsWith('/')
? pathname
: `${pathname}/`
)?.toLowerCase();
return normalize(path1) === normalize(path2); return normalize(path1) === normalize(path2);
}; };

View file

@ -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.