Skip to content

Commit

Permalink
fix(navigation): ignore extensions on navigation sort (#2529)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Feb 2, 2024
1 parent d343708 commit 5a9e7d9
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/runtime/server/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions test/features/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/basic-aq/content/test-navigation/foo-bar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# foo - bar
1 change: 1 addition & 0 deletions test/fixtures/basic-aq/content/test-navigation/foo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# foo
1 change: 1 addition & 0 deletions test/fixtures/basic/content/test-navigation/foo-bar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# foo - bar
1 change: 1 addition & 0 deletions test/fixtures/basic/content/test-navigation/foo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# foo

0 comments on commit 5a9e7d9

Please sign in to comment.