Skip to content

Commit

Permalink
feat(theme-default): nav.item.activeMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 1, 2021
1 parent a41928e commit e262ef6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 6 additions & 2 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 15 additions & 3 deletions src/client/theme-default/composables/navLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -36,4 +47,5 @@ function normalizePath(path: string): string {
.replace(/#.*$/, '')
.replace(/\?.*$/, '')
.replace(/\.(html|md)$/, '')
.replace(/\/index$/, '/')
}
1 change: 1 addition & 0 deletions src/client/theme-default/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export namespace DefaultTheme {
target?: string
rel?: string
ariaLabel?: string
activeMatch?: string
}

export interface NavItemWithLink extends NavItemBase {
Expand Down

0 comments on commit e262ef6

Please sign in to comment.