From 5a9e7d9d25b9bcfcf8b21e1f844cbe29d9f749c9 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Fri, 2 Feb 2024 17:24:31 +0100 Subject: [PATCH] fix(navigation): ignore extensions on navigation sort (#2529) --- src/runtime/server/navigation.ts | 4 ++++ test/features/navigation.ts | 9 +++++++-- .../fixtures/basic-aq/content/test-navigation/foo-bar.md | 1 + test/fixtures/basic-aq/content/test-navigation/foo.md | 1 + test/fixtures/basic/content/test-navigation/foo-bar.md | 1 + test/fixtures/basic/content/test-navigation/foo.md | 1 + 6 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/basic-aq/content/test-navigation/foo-bar.md create mode 100644 test/fixtures/basic-aq/content/test-navigation/foo.md create mode 100644 test/fixtures/basic/content/test-navigation/foo-bar.md create mode 100644 test/fixtures/basic/content/test-navigation/foo.md diff --git a/src/runtime/server/navigation.ts b/src/runtime/server/navigation.ts index 2364c6717..94d2265ee 100644 --- a/src/runtime/server/navigation.ts +++ b/src/runtime/server/navigation.ts @@ -118,6 +118,10 @@ const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'bas * Sort items by path and clear empty children keys. */ function sortAndClear (nav: PrivateNavItem[]) { + nav.forEach((item) => { + // Remove extension from path + item._file = item._file.split('.').slice(0, -1).join('.') + }) const sorted = nav.sort((a, b) => collator.compare(a._file, b._file)) for (const item of sorted) { diff --git a/test/features/navigation.ts b/test/features/navigation.ts index 0788c9423..3e38535e7 100644 --- a/test/features/navigation.ts +++ b/test/features/navigation.ts @@ -55,14 +55,19 @@ export const testNavigation = () => { } }) - // page.md, index.md, /not-hidden-dir - expect(list[0].children).toHaveLength(3) + // page.md, index.md, /not-hidden-dir, /foo, /foo-bar + expect(list[0].children).toHaveLength(5) // /hidden-dir should not exist expect(list[0].children.find(item => item._path.includes('/hidden-dir'))).toBe(undefined) // /not-hidden-dir should exist expect(list[0].children.find(item => item._path.includes('/not-hidden-dir'))).toBeTruthy() + + expect(list[0].children.findIndex(item => item._path === '/test-navigation/foo')) + .lessThan( + list[0].children.findIndex(item => item._path === '/test-navigation/foo-bar') + ) }) test('Get numbers navigation', async () => { diff --git a/test/fixtures/basic-aq/content/test-navigation/foo-bar.md b/test/fixtures/basic-aq/content/test-navigation/foo-bar.md new file mode 100644 index 000000000..33b313221 --- /dev/null +++ b/test/fixtures/basic-aq/content/test-navigation/foo-bar.md @@ -0,0 +1 @@ +# foo - bar \ No newline at end of file diff --git a/test/fixtures/basic-aq/content/test-navigation/foo.md b/test/fixtures/basic-aq/content/test-navigation/foo.md new file mode 100644 index 000000000..ad96c729e --- /dev/null +++ b/test/fixtures/basic-aq/content/test-navigation/foo.md @@ -0,0 +1 @@ +# foo \ No newline at end of file diff --git a/test/fixtures/basic/content/test-navigation/foo-bar.md b/test/fixtures/basic/content/test-navigation/foo-bar.md new file mode 100644 index 000000000..33b313221 --- /dev/null +++ b/test/fixtures/basic/content/test-navigation/foo-bar.md @@ -0,0 +1 @@ +# foo - bar \ No newline at end of file diff --git a/test/fixtures/basic/content/test-navigation/foo.md b/test/fixtures/basic/content/test-navigation/foo.md new file mode 100644 index 000000000..ad96c729e --- /dev/null +++ b/test/fixtures/basic/content/test-navigation/foo.md @@ -0,0 +1 @@ +# foo \ No newline at end of file