From ddf178a170967527bafe7c9b262fb66aa10ec9de Mon Sep 17 00:00:00 2001 From: T <6601329+cookesan@users.noreply.github.com> Date: Sun, 28 Jun 2026 07:28:16 -0300 Subject: [PATCH] fix: preserve external sidebar links with base (#5243) --- .../theme-default/support/sidebar.test.ts | 32 +++++++++++++++++++ src/client/theme-default/support/sidebar.ts | 4 +-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/__tests__/unit/client/theme-default/support/sidebar.test.ts b/__tests__/unit/client/theme-default/support/sidebar.test.ts index f33dcbd94..91e8085bb 100644 --- a/__tests__/unit/client/theme-default/support/sidebar.test.ts +++ b/__tests__/unit/client/theme-default/support/sidebar.test.ts @@ -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', () => { diff --git a/src/client/theme-default/support/sidebar.ts b/src/client/theme-default/support/sidebar.ts index 13cd0aada..34dcfcf5e 100644 --- a/src/client/theme-default/support/sidebar.ts +++ b/src/client/theme-default/support/sidebar.ts @@ -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