fix: preserve external sidebar links with base (#5243)

pull/5207/merge
T 1 month ago committed by GitHub
parent 8288785d5a
commit ddf178a170
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -103,6 +103,38 @@ describe('client/theme-default/support/sidebar', () => {
)
})
})
test('applies base only to internal links', () => {
expect(
getSidebar(
{
'/en/': {
base: '/en/',
items: [
{
text: 'Guide',
items: [
{ text: 'Intro', link: 'intro' },
{ text: 'Root', link: '/root' },
{ text: 'External', link: 'https://example.com/' }
]
}
]
}
},
'/en/intro'
)
).toStrictEqual([
{
text: 'Guide',
items: [
{ text: 'Intro', link: '/en/intro' },
{ text: 'Root', link: '/en/root' },
{ text: 'External', link: 'https://example.com/' }
]
}
])
})
})
describe('hasActiveLink', () => {

@ -1,5 +1,5 @@
import type { DefaultTheme } from 'vitepress/theme'
import { isActive } from '../../shared'
import { isActive, isExternal } from '../../shared'
import { ensureStartingSlash } from './utils'
export interface SidebarLink {
@ -112,7 +112,7 @@ function addBase(items: SidebarItem[], _base?: string): SidebarItem[] {
return [...items].map((_item) => {
const item = { ..._item }
const base = item.base || _base
if (base && item.link)
if (base && item.link && !isExternal(item.link))
item.link = base + item.link.replace(/^\//, base.endsWith('/') ? '' : '/')
if (item.items) item.items = addBase(item.items, base)
return item

Loading…
Cancel
Save