fix: coerce a leading slash onto path bases

base: 'docs/' silently emitted relative-looking asset urls like
src="docs/assets/app.js".

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5406/head
Divyansh Singh 2 weeks ago
parent cfa9b902a0
commit 78f97f3bbc

@ -86,6 +86,15 @@ describe('node/config base normalization', () => {
expect(normalizeSiteBase('/docs/')).toBe('/docs/') expect(normalizeSiteBase('/docs/')).toBe('/docs/')
}) })
test('coerces a leading slash onto path bases', () => {
expect(normalizeSiteBase('docs')).toBe('/docs/')
expect(normalizeSiteBase('docs/')).toBe('/docs/')
expect(normalizeSiteBase('https://example.com/x')).toBe(
'https://example.com/x/'
)
expect(normalizeSiteBase('//cdn.example.com/')).toBe('//cdn.example.com/')
})
test('normalizes relative forms to ./', () => { test('normalizes relative forms to ./', () => {
expect(normalizeSiteBase('.')).toBe('./') expect(normalizeSiteBase('.')).toBe('./')
expect(normalizeSiteBase('./')).toBe('./') expect(normalizeSiteBase('./')).toBe('./')

@ -45,13 +45,20 @@ const resolve = (root: string, file: string) =>
normalizePath(path.resolve(root, `.vitepress`, file)) normalizePath(path.resolve(root, `.vitepress`, file))
export function normalizeSiteBase(base?: string): string { export function normalizeSiteBase(base?: string): string {
const normalized = base ? base.replace(/([^/])$/, '$1/') : '/' let normalized = base ? base.replace(/([^/])$/, '$1/') : '/'
if (normalized.startsWith('.') && !isRelativeBase(normalized)) { if (normalized.startsWith('.') && !isRelativeBase(normalized)) {
throw new Error( throw new Error(
`a relative base must be exactly './' (got: ${base}) — pages always ` + `a relative base must be exactly './' (got: ${base}) — pages always ` +
`reference the site root relative to their own depth` `reference the site root relative to their own depth`
) )
} }
if (
!isRelativeBase(normalized) &&
!EXTERNAL_URL_RE.test(normalized) &&
!normalized.startsWith('/')
) {
normalized = '/' + normalized
}
return normalized return normalized
} }

Loading…
Cancel
Save