mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-25 06:56:56 +02:00
fix(v2): redirect plugin: use siteConfig.trailingSlash (#4988)
This commit is contained in:
parent
80b6d9728e
commit
b54ec72389
15 changed files with 443 additions and 73 deletions
|
@ -14,10 +14,20 @@ describe('applyTrailingSlash', () => {
|
|||
expect(applyTrailingSlash('', undefined)).toEqual('');
|
||||
});
|
||||
|
||||
test('should apply to /', () => {
|
||||
test('should not apply to /', () => {
|
||||
expect(applyTrailingSlash('/', true)).toEqual('/');
|
||||
expect(applyTrailingSlash('/', false)).toEqual('');
|
||||
expect(applyTrailingSlash('/', false)).toEqual('/');
|
||||
expect(applyTrailingSlash('/', undefined)).toEqual('/');
|
||||
|
||||
expect(applyTrailingSlash('/?query#anchor', true)).toEqual(
|
||||
'/?query#anchor',
|
||||
);
|
||||
expect(applyTrailingSlash('/?query#anchor', false)).toEqual(
|
||||
'/?query#anchor',
|
||||
);
|
||||
expect(applyTrailingSlash('/?query#anchor', undefined)).toEqual(
|
||||
'/?query#anchor',
|
||||
);
|
||||
});
|
||||
|
||||
test('should not apply to #anchor links ', () => {
|
||||
|
|
|
@ -9,8 +9,8 @@ export default function applyTrailingSlash(
|
|||
path: string,
|
||||
trailingSlash: boolean | undefined,
|
||||
): string {
|
||||
// Never apply trailing slash to an anchor link
|
||||
if (path.startsWith('#')) {
|
||||
// Never apply trailing slash to an anchor link
|
||||
return path;
|
||||
}
|
||||
|
||||
|
@ -29,8 +29,13 @@ export default function applyTrailingSlash(
|
|||
|
||||
// The trailing slash should be handled before the ?search#hash !
|
||||
const [pathname] = path.split(/[#?]/);
|
||||
const newPathname = trailingSlash
|
||||
? addTrailingSlash(pathname)
|
||||
: removeTrailingSlash(pathname);
|
||||
|
||||
// Never transform '/' to ''
|
||||
const newPathname =
|
||||
pathname === '/'
|
||||
? '/'
|
||||
: trailingSlash
|
||||
? addTrailingSlash(pathname)
|
||||
: removeTrailingSlash(pathname);
|
||||
return path.replace(pathname, newPathname);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue