mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
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
This commit is contained in:
parent
08a726e154
commit
f926178e63
2 changed files with 10 additions and 2 deletions
|
@ -40,6 +40,7 @@ describe('useBaseUrl', () => {
|
||||||
expect(useBaseUrl('/hello/byebye', {absolute: true})).toEqual(
|
expect(useBaseUrl('/hello/byebye', {absolute: true})).toEqual(
|
||||||
'https://v2.docusaurus.io/hello/byebye',
|
'https://v2.docusaurus.io/hello/byebye',
|
||||||
);
|
);
|
||||||
|
expect(useBaseUrl('#hello')).toEqual('#hello');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('non-empty base URL', () => {
|
test('non-empty base URL', () => {
|
||||||
|
@ -69,6 +70,7 @@ describe('useBaseUrl', () => {
|
||||||
);
|
);
|
||||||
expect(useBaseUrl('/docusaurus/')).toEqual('/docusaurus/');
|
expect(useBaseUrl('/docusaurus/')).toEqual('/docusaurus/');
|
||||||
expect(useBaseUrl('/docusaurus/hello')).toEqual('/docusaurus/hello');
|
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(
|
expect(withBaseUrl('/hello/byebye', {absolute: true})).toEqual(
|
||||||
'https://v2.docusaurus.io/hello/byebye',
|
'https://v2.docusaurus.io/hello/byebye',
|
||||||
);
|
);
|
||||||
|
expect(withBaseUrl('#hello')).toEqual('#hello');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('non-empty base URL', () => {
|
test('non-empty base URL', () => {
|
||||||
|
@ -129,5 +132,6 @@ describe('useBaseUrlUtils().withBaseUrl()', () => {
|
||||||
);
|
);
|
||||||
expect(withBaseUrl('/docusaurus/')).toEqual('/docusaurus/');
|
expect(withBaseUrl('/docusaurus/')).toEqual('/docusaurus/');
|
||||||
expect(withBaseUrl('/docusaurus/hello')).toEqual('/docusaurus/hello');
|
expect(withBaseUrl('/docusaurus/hello')).toEqual('/docusaurus/hello');
|
||||||
|
expect(withBaseUrl('#hello')).toEqual('#hello');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -23,6 +23,11 @@ function addBaseUrl(
|
||||||
return url;
|
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
|
// it never makes sense to add a base url to an url with a protocol
|
||||||
if (hasProtocol(url)) {
|
if (hasProtocol(url)) {
|
||||||
return url;
|
return url;
|
||||||
|
@ -32,8 +37,7 @@ function addBaseUrl(
|
||||||
return baseUrl + url;
|
return baseUrl + url;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sometimes we try to add baseurl to an url that already has a baseurl
|
// We should avoid adding the baseurl twice if it's already there
|
||||||
// we should avoid adding the baseurl twice
|
|
||||||
const shouldAddBaseUrl = !url.startsWith(baseUrl);
|
const shouldAddBaseUrl = !url.startsWith(baseUrl);
|
||||||
|
|
||||||
const basePath = shouldAddBaseUrl ? baseUrl + url.replace(/^\//, '') : url;
|
const basePath = shouldAddBaseUrl ? baseUrl + url.replace(/^\//, '') : url;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue