diff --git a/__tests__/unit/node/config.test.ts b/__tests__/unit/node/config.test.ts index 3fd32610..de1eb2f8 100644 --- a/__tests__/unit/node/config.test.ts +++ b/__tests__/unit/node/config.test.ts @@ -86,6 +86,15 @@ describe('node/config base normalization', () => { 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 ./', () => { expect(normalizeSiteBase('.')).toBe('./') expect(normalizeSiteBase('./')).toBe('./') diff --git a/src/node/config.ts b/src/node/config.ts index ff548a67..f60663f6 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -45,13 +45,20 @@ const resolve = (root: string, file: string) => normalizePath(path.resolve(root, `.vitepress`, file)) export function normalizeSiteBase(base?: string): string { - const normalized = base ? base.replace(/([^/])$/, '$1/') : '/' + let normalized = base ? base.replace(/([^/])$/, '$1/') : '/' if (normalized.startsWith('.') && !isRelativeBase(normalized)) { throw new Error( `a relative base must be exactly './' (got: ${base}) — pages always ` + `reference the site root relative to their own depth` ) } + if ( + !isRelativeBase(normalized) && + !EXTERNAL_URL_RE.test(normalized) && + !normalized.startsWith('/') + ) { + normalized = '/' + normalized + } return normalized }