From 44a65f3337db39ecb8d93b995614572dbcbcf988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Fri, 25 Feb 2022 14:58:54 +0100 Subject: [PATCH] fix(theme-common): isSamePath should be case-insensitive (#6758) --- .../src/utils/__tests__/pathUtils.test.ts | 8 ++++++++ packages/docusaurus-theme-common/src/utils/pathUtils.ts | 7 +++++-- .../_dogfooding/_docs tests/tests/Case-Sentitive-Doc.md | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 website/_dogfooding/_docs tests/tests/Case-Sentitive-Doc.md diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/pathUtils.test.ts b/packages/docusaurus-theme-common/src/utils/__tests__/pathUtils.test.ts index 71d88b7d9a..1e7d436a76 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/pathUtils.test.ts +++ b/packages/docusaurus-theme-common/src/utils/__tests__/pathUtils.test.ts @@ -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(); }); diff --git a/packages/docusaurus-theme-common/src/utils/pathUtils.ts b/packages/docusaurus-theme-common/src/utils/pathUtils.ts index cde5ae1a06..9441add949 100644 --- a/packages/docusaurus-theme-common/src/utils/pathUtils.ts +++ b/packages/docusaurus-theme-common/src/utils/pathUtils.ts @@ -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); }; diff --git a/website/_dogfooding/_docs tests/tests/Case-Sentitive-Doc.md b/website/_dogfooding/_docs tests/tests/Case-Sentitive-Doc.md new file mode 100644 index 0000000000..58de34b32e --- /dev/null +++ b/website/_dogfooding/_docs tests/tests/Case-Sentitive-Doc.md @@ -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.