Skip to content

Commit e262ef6

Browse files
committedJan 1, 2021
feat(theme-default): nav.item.activeMatch
1 parent a41928e commit e262ef6

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed
 

Diff for: ‎docs/.vitepress/config.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ module.exports = {
2323
},
2424

2525
nav: [
26-
{ text: 'Guide', link: '/' },
27-
{ text: 'Config Reference', link: '/config/basics' },
26+
{ text: 'Guide', link: '/', activeMatch: '^/$|^/guide/' },
27+
{
28+
text: 'Config Reference',
29+
link: '/config/basics',
30+
activeMatch: '^/config/'
31+
},
2832
{
2933
text: 'Release Notes',
3034
link: 'https://github.com/vuejs/vitepress/releases'

Diff for: ‎src/client/theme-default/composables/navLink.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,22 @@ export function useNavLink(item: DefaultTheme.NavItemWithLink) {
1111
const isExternal = isExternalCheck(item.link)
1212

1313
const props = computed(() => {
14+
const routePath = normalizePath(route.path)
15+
16+
let active = false
17+
if (item.activeMatch) {
18+
active = new RegExp(item.activeMatch).test(routePath)
19+
} else {
20+
const itemPath = normalizePath(withBase(item.link))
21+
active =
22+
itemPath === '/'
23+
? itemPath === routePath
24+
: routePath.startsWith(itemPath)
25+
}
26+
1427
return {
1528
class: {
16-
active: normalizePath(route.path).startsWith(
17-
normalizePath(withBase(item.link))
18-
),
29+
active,
1930
isExternal
2031
},
2132
href: isExternal ? item.link : withBase(item.link),
@@ -36,4 +47,5 @@ function normalizePath(path: string): string {
3647
.replace(/#.*$/, '')
3748
.replace(/\?.*$/, '')
3849
.replace(/\.(html|md)$/, '')
50+
.replace(/\/index$/, '/')
3951
}

Diff for: ‎src/client/theme-default/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export namespace DefaultTheme {
8080
target?: string
8181
rel?: string
8282
ariaLabel?: string
83+
activeMatch?: string
8384
}
8485

8586
export interface NavItemWithLink extends NavItemBase {

0 commit comments

Comments
 (0)