-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v2): add trailingSlash config option (#4908)
* POC: add trailingSlash option * integrate the preferFoldersOutput option of fork @slorber/static-site-generator-webpack-plugin * Fix broken links when using trailing slash => using md links is more reliable * fix TS issue * minor polish * fix doc page being sensitive to trailing slashes * Add tests for applyTrailingSlash * rename test files * extract and test applyRouteTrailingSlash * update snapshot * add trailing slash config to serve command * fix getSidebar() => still sensitive to trailing slash setting * never apply trailing slash to an anchor link * Add documentation for trailingSlash setting
- Loading branch information
Showing
28 changed files
with
469 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/docusaurus/src/client/exports/__tests__/applyTrailingSlash.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import applyTrailingSlash from '../applyTrailingSlash'; | ||
|
||
describe('applyTrailingSlash', () => { | ||
test('should apply to empty', () => { | ||
expect(applyTrailingSlash('', true)).toEqual('/'); | ||
expect(applyTrailingSlash('', false)).toEqual(''); | ||
expect(applyTrailingSlash('', undefined)).toEqual(''); | ||
}); | ||
|
||
test('should apply to /', () => { | ||
expect(applyTrailingSlash('/', true)).toEqual('/'); | ||
expect(applyTrailingSlash('/', false)).toEqual(''); | ||
expect(applyTrailingSlash('/', undefined)).toEqual('/'); | ||
}); | ||
|
||
test('should not apply to #anchor links ', () => { | ||
expect(applyTrailingSlash('#', true)).toEqual('#'); | ||
expect(applyTrailingSlash('#', false)).toEqual('#'); | ||
expect(applyTrailingSlash('#', undefined)).toEqual('#'); | ||
expect(applyTrailingSlash('#anchor', true)).toEqual('#anchor'); | ||
expect(applyTrailingSlash('#anchor', false)).toEqual('#anchor'); | ||
expect(applyTrailingSlash('#anchor', undefined)).toEqual('#anchor'); | ||
}); | ||
|
||
test('should apply to simple paths', () => { | ||
expect(applyTrailingSlash('abc', true)).toEqual('abc/'); | ||
expect(applyTrailingSlash('abc', false)).toEqual('abc'); | ||
expect(applyTrailingSlash('abc', undefined)).toEqual('abc'); | ||
expect(applyTrailingSlash('abc/', true)).toEqual('abc/'); | ||
expect(applyTrailingSlash('abc/', false)).toEqual('abc'); | ||
expect(applyTrailingSlash('abc/', undefined)).toEqual('abc/'); | ||
expect(applyTrailingSlash('/abc', true)).toEqual('/abc/'); | ||
expect(applyTrailingSlash('/abc', false)).toEqual('/abc'); | ||
expect(applyTrailingSlash('/abc', undefined)).toEqual('/abc'); | ||
expect(applyTrailingSlash('/abc/', true)).toEqual('/abc/'); | ||
expect(applyTrailingSlash('/abc/', false)).toEqual('/abc'); | ||
expect(applyTrailingSlash('/abc/', undefined)).toEqual('/abc/'); | ||
}); | ||
|
||
test('should apply to path with #anchor', () => { | ||
expect(applyTrailingSlash('/abc#anchor', true)).toEqual('/abc/#anchor'); | ||
expect(applyTrailingSlash('/abc#anchor', false)).toEqual('/abc#anchor'); | ||
expect(applyTrailingSlash('/abc#anchor', undefined)).toEqual('/abc#anchor'); | ||
expect(applyTrailingSlash('/abc/#anchor', true)).toEqual('/abc/#anchor'); | ||
expect(applyTrailingSlash('/abc/#anchor', false)).toEqual('/abc#anchor'); | ||
expect(applyTrailingSlash('/abc/#anchor', undefined)).toEqual( | ||
'/abc/#anchor', | ||
); | ||
}); | ||
|
||
test('should apply to path with ?search', () => { | ||
expect(applyTrailingSlash('/abc?search', true)).toEqual('/abc/?search'); | ||
expect(applyTrailingSlash('/abc?search', false)).toEqual('/abc?search'); | ||
expect(applyTrailingSlash('/abc?search', undefined)).toEqual('/abc?search'); | ||
expect(applyTrailingSlash('/abc/?search', true)).toEqual('/abc/?search'); | ||
expect(applyTrailingSlash('/abc/?search', false)).toEqual('/abc?search'); | ||
expect(applyTrailingSlash('/abc/?search', undefined)).toEqual( | ||
'/abc/?search', | ||
); | ||
}); | ||
|
||
test('should apply to path with ?search#anchor', () => { | ||
expect(applyTrailingSlash('/abc?search#anchor', true)).toEqual( | ||
'/abc/?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc?search#anchor', false)).toEqual( | ||
'/abc?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc?search#anchor', undefined)).toEqual( | ||
'/abc?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/?search#anchor', true)).toEqual( | ||
'/abc/?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/?search#anchor', false)).toEqual( | ||
'/abc?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/?search#anchor', undefined)).toEqual( | ||
'/abc/?search#anchor', | ||
); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
packages/docusaurus/src/client/exports/applyTrailingSlash.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export default function applyTrailingSlash( | ||
path: string, | ||
trailingSlash: boolean | undefined, | ||
): string { | ||
// Never apply trailing slash to an anchor link | ||
if (path.startsWith('#')) { | ||
return path; | ||
} | ||
|
||
function addTrailingSlash(str: string): string { | ||
return str.endsWith('/') ? str : `${str}/`; | ||
} | ||
function removeTrailingSlash(str: string): string { | ||
return str.endsWith('/') ? str.slice(0, -1) : str; | ||
} | ||
// undefined = legacy retrocompatible behavior | ||
if (typeof trailingSlash === 'undefined') { | ||
return path; | ||
} | ||
|
||
// The trailing slash should be handled before the ?search#hash ! | ||
const [pathname] = path.split(/[#?]/); | ||
const newPathname = trailingSlash | ||
? addTrailingSlash(pathname) | ||
: removeTrailingSlash(pathname); | ||
return path.replace(pathname, newPathname); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/docusaurus/src/server/plugins/__tests__/applyRouteTrailingSlash.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import applyRouteTrailingSlash from '../applyRouteTrailingSlash'; | ||
import {RouteConfig} from '@docusaurus/types'; | ||
|
||
function route(path: string, subRoutes?: string[]): RouteConfig { | ||
const result: RouteConfig = {path, component: 'any'}; | ||
|
||
if (subRoutes) { | ||
result.routes = subRoutes.map((subRoute) => route(subRoute)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
describe('applyRouteTrailingSlash', () => { | ||
test('apply to empty', () => { | ||
expect(applyRouteTrailingSlash(route(''), true)).toEqual(route('/')); | ||
expect(applyRouteTrailingSlash(route(''), false)).toEqual(route('')); | ||
expect(applyRouteTrailingSlash(route(''), undefined)).toEqual(route('')); | ||
}); | ||
|
||
test('apply to /', () => { | ||
expect(applyRouteTrailingSlash(route('/'), true)).toEqual(route('/')); | ||
expect(applyRouteTrailingSlash(route('/'), false)).toEqual(route('/')); | ||
expect(applyRouteTrailingSlash(route('/'), undefined)).toEqual(route('/')); | ||
}); | ||
|
||
test('apply to /abc', () => { | ||
expect(applyRouteTrailingSlash(route('/abc'), true)).toEqual( | ||
route('/abc/'), | ||
); | ||
expect(applyRouteTrailingSlash(route('/abc'), false)).toEqual( | ||
route('/abc'), | ||
); | ||
expect(applyRouteTrailingSlash(route('/abc'), undefined)).toEqual( | ||
route('/abc'), | ||
); | ||
}); | ||
|
||
test('apply to /abc/', () => { | ||
expect(applyRouteTrailingSlash(route('/abc/'), true)).toEqual( | ||
route('/abc/'), | ||
); | ||
expect(applyRouteTrailingSlash(route('/abc/'), false)).toEqual( | ||
route('/abc'), | ||
); | ||
expect(applyRouteTrailingSlash(route('/abc/'), undefined)).toEqual( | ||
route('/abc/'), | ||
); | ||
}); | ||
|
||
test('apply to /abc?search#anchor', () => { | ||
expect(applyRouteTrailingSlash(route('/abc?search#anchor'), true)).toEqual( | ||
route('/abc/?search#anchor'), | ||
); | ||
expect(applyRouteTrailingSlash(route('/abc?search#anchor'), false)).toEqual( | ||
route('/abc?search#anchor'), | ||
); | ||
expect( | ||
applyRouteTrailingSlash(route('/abc?search#anchor'), undefined), | ||
).toEqual(route('/abc?search#anchor')); | ||
}); | ||
|
||
test('apply to /abc/?search#anchor', () => { | ||
expect(applyRouteTrailingSlash(route('/abc/?search#anchor'), true)).toEqual( | ||
route('/abc/?search#anchor'), | ||
); | ||
expect( | ||
applyRouteTrailingSlash(route('/abc/?search#anchor'), false), | ||
).toEqual(route('/abc?search#anchor')); | ||
expect( | ||
applyRouteTrailingSlash(route('/abc/?search#anchor'), undefined), | ||
).toEqual(route('/abc/?search#anchor')); | ||
}); | ||
|
||
test('apply to subroutes', () => { | ||
expect( | ||
applyRouteTrailingSlash(route('/abc', ['/abc/1', '/abc/2']), true), | ||
).toEqual(route('/abc/', ['/abc/1/', '/abc/2/'])); | ||
expect( | ||
applyRouteTrailingSlash(route('/abc', ['/abc/1', '/abc/2']), false), | ||
).toEqual(route('/abc', ['/abc/1', '/abc/2'])); | ||
expect( | ||
applyRouteTrailingSlash(route('/abc', ['/abc/1', '/abc/2']), undefined), | ||
).toEqual(route('/abc', ['/abc/1', '/abc/2'])); | ||
}); | ||
|
||
test('apply for complex case', () => { | ||
expect( | ||
applyRouteTrailingSlash( | ||
route('/abc?search#anchor', ['/abc/1?search', '/abc/2#anchor']), | ||
true, | ||
), | ||
).toEqual( | ||
route('/abc/?search#anchor', ['/abc/1/?search', '/abc/2/#anchor']), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.