From f926178e63f617e8955a3ccd571b832e862a9a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Fri, 24 Jul 2020 12:40:24 +0200 Subject: [PATCH] fix(v2): baseUrl is wrongly appended to anchor links (#3112) * fix baseurl being wrongly appended to anchor links * fix baseurl being wrongly appended to anchor links --- .../docusaurus/src/client/exports/__tests__/useBaseUrl.ts | 4 ++++ packages/docusaurus/src/client/exports/useBaseUrl.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.ts b/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.ts index eb5a7f1b6ed2..28a7def3b2b4 100644 --- a/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.ts +++ b/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.ts @@ -40,6 +40,7 @@ describe('useBaseUrl', () => { expect(useBaseUrl('/hello/byebye', {absolute: true})).toEqual( 'https://v2.docusaurus.io/hello/byebye', ); + expect(useBaseUrl('#hello')).toEqual('#hello'); }); test('non-empty base URL', () => { @@ -69,6 +70,7 @@ describe('useBaseUrl', () => { ); expect(useBaseUrl('/docusaurus/')).toEqual('/docusaurus/'); expect(useBaseUrl('/docusaurus/hello')).toEqual('/docusaurus/hello'); + expect(useBaseUrl('#hello')).toEqual('#hello'); }); }); @@ -99,6 +101,7 @@ describe('useBaseUrlUtils().withBaseUrl()', () => { expect(withBaseUrl('/hello/byebye', {absolute: true})).toEqual( 'https://v2.docusaurus.io/hello/byebye', ); + expect(withBaseUrl('#hello')).toEqual('#hello'); }); test('non-empty base URL', () => { @@ -129,5 +132,6 @@ describe('useBaseUrlUtils().withBaseUrl()', () => { ); expect(withBaseUrl('/docusaurus/')).toEqual('/docusaurus/'); expect(withBaseUrl('/docusaurus/hello')).toEqual('/docusaurus/hello'); + expect(withBaseUrl('#hello')).toEqual('#hello'); }); }); diff --git a/packages/docusaurus/src/client/exports/useBaseUrl.ts b/packages/docusaurus/src/client/exports/useBaseUrl.ts index cd2f3123ca8f..627d140141a5 100644 --- a/packages/docusaurus/src/client/exports/useBaseUrl.ts +++ b/packages/docusaurus/src/client/exports/useBaseUrl.ts @@ -23,6 +23,11 @@ function addBaseUrl( return url; } + // it never makes sense to add a base url to a local anchor url + if (url.startsWith('#')) { + return url; + } + // it never makes sense to add a base url to an url with a protocol if (hasProtocol(url)) { return url; @@ -32,8 +37,7 @@ function addBaseUrl( return baseUrl + url; } - // sometimes we try to add baseurl to an url that already has a baseurl - // we should avoid adding the baseurl twice + // We should avoid adding the baseurl twice if it's already there const shouldAddBaseUrl = !url.startsWith(baseUrl); const basePath = shouldAddBaseUrl ? baseUrl + url.replace(/^\//, '') : url;