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: [ nav: [
{ text: 'Guide', link: '/' }, { text: 'Guide', link: '/', activeMatch: '^/$|^/guide/' },
{ text: 'Config Reference', link: '/config/basics' }, {
text: 'Config Reference',
link: '/config/basics',
activeMatch: '^/config/'
},
{ {
text: 'Release Notes', text: 'Release Notes',
link: 'https://github.com/vuejs/vitepress/releases' link: 'https://github.com/vuejs/vitepress/releases'

@ -11,11 +11,22 @@ export function useNavLink(item: DefaultTheme.NavItemWithLink) {
const isExternal = isExternalCheck(item.link) const isExternal = isExternalCheck(item.link)
const props = computed(() => { 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 { return {
class: { class: {
active: normalizePath(route.path).startsWith( active,
normalizePath(withBase(item.link))
),
isExternal isExternal
}, },
href: isExternal ? item.link : withBase(item.link), href: isExternal ? item.link : withBase(item.link),
@ -36,4 +47,5 @@ function normalizePath(path: string): string {
.replace(/#.*$/, '') .replace(/#.*$/, '')
.replace(/\?.*$/, '') .replace(/\?.*$/, '')
.replace(/\.(html|md)$/, '') .replace(/\.(html|md)$/, '')
.replace(/\/index$/, '/')
} }

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

Loading…
Cancel
Save