Skip to content

Commit

Permalink
fix: #563 (#566)
Browse files Browse the repository at this point in the history
* fix: #563

* chore: add changeset
  • Loading branch information
promer94 committed Jul 23, 2022
1 parent 6644bd5 commit c15f570
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-pigs-dance.md
@@ -0,0 +1,5 @@
---
'nextra-theme-docs': patch
---

fix: query should not affect nav highlight
25 changes: 25 additions & 0 deletions packages/nextra-theme-docs/__test__/getFSRoute.spec.ts
@@ -0,0 +1,25 @@
import { describe, it, expect } from 'vitest'
import { getFSRoute } from '../src/utils/get-fs-route'

describe('getFSRoute', () => {
it('replace locale', () => {
const withLocale = getFSRoute('/getting-started.en-US', 'en-US')
expect(withLocale).toEqual('/getting-started')
})
it('replace index', () => {
const withIndex = getFSRoute('/getting-started/index')
expect(withIndex).toEqual('/getting-started')
const withIndexAndLocale = getFSRoute('/getting-started/index')
expect(withIndexAndLocale).toEqual('/getting-started')
})
it('ignore query', () => {
const withQuery = getFSRoute('/getting-started?query=1')
expect(withQuery).toEqual('/getting-started')

const withQueryLocale = getFSRoute('/getting-started.en-US?query=1', 'en-US')
expect(withQueryLocale).toEqual('/getting-started')

const withIndexLocaleQuery = getFSRoute('/getting-started/index.en-US?query=1', 'en-US')
expect(withIndexLocaleQuery).toEqual('/getting-started')
})
})
1 change: 0 additions & 1 deletion packages/nextra-theme-docs/src/sidebar.tsx
Expand Up @@ -148,7 +148,6 @@ function File({ item, anchors, topLevel }: FileProps) {
const slugger = new Slugger()
const activeAnchor = useActiveAnchor()
const { setMenu } = useMenuContext()

const title = item.title

if (item.type === 'separator') {
Expand Down
11 changes: 5 additions & 6 deletions packages/nextra-theme-docs/src/utils/get-fs-route.ts
@@ -1,9 +1,8 @@
const template = 'https://nextra.vercel.app'
export const getFSRoute = (asPath: string, locale?: string | undefined) => {
const pathname = new URL(asPath, template).pathname
const cleanedPath = locale
? asPath.replace(new RegExp(`\.${locale}(\/|$)`), '$1')
: asPath

return (
cleanedPath.replace(new RegExp('/index(/|$)'), '$1').split('#')[0] || '/'
)
? pathname.replace(new RegExp(`\.${locale}(\/|$)`), '$1')
: pathname
return (cleanedPath.replace(new RegExp('/index(/|$)'), '$1').split('#')[0] || '/')
}

0 comments on commit c15f570

Please sign in to comment.