feat(theme-default): nav.item.activeMatch

pull/198/head
Evan You 4 years ago
parent a41928ef83
commit e262ef63d8

@ -23,8 +23,12 @@ module.exports = {
},
nav: [
{ text: 'Guide', link: '/' },
{ text: 'Config Reference', link: '/config/basics' },
{ text: 'Guide', link: '/', activeMatch: '^/$|^/guide/' },
{
text: 'Config Reference',
link: '/config/basics',
activeMatch: '^/config/'
},
{
text: 'Release Notes',
link: 'https://github.com/vuejs/vitepress/releases'

@ -11,11 +11,22 @@ export function useNavLink(item: DefaultTheme.NavItemWithLink) {
const isExternal = isExternalCheck(item.link)
const props = computed(() => {
const routePath = normalizePath(route.path)
let active = false
if (item.activeMatch) {
active = new RegExp(item.activeMatch).test(routePath)
} else {
const itemPath = normalizePath(withBase(item.link))
active =
itemPath === '/'
? itemPath === routePath
: routePath.startsWith(itemPath)
}
return {
class: {
active: normalizePath(route.path).startsWith(
normalizePath(withBase(item.link))
),
active,
isExternal
},
href: isExternal ? item.link : withBase(item.link),
@ -36,4 +47,5 @@ function normalizePath(path: string): string {
.replace(/#.*$/, '')
.replace(/\?.*$/, '')
.replace(/\.(html|md)$/, '')
.replace(/\/index$/, '/')
}

@ -80,6 +80,7 @@ export namespace DefaultTheme {
target?: string
rel?: string
ariaLabel?: string
activeMatch?: string
}
export interface NavItemWithLink extends NavItemBase {

Loading…
Cancel
Save