@docusaurus/utils-common
Advanced tools
Comparing version 2.0.0-beta.41eaa690e to 2.0.0-beta.44225a3df
@@ -7,2 +7,4 @@ /** | ||
*/ | ||
export default function applyTrailingSlash(path: string, trailingSlash: boolean | undefined): string; | ||
import type { DocusaurusConfig } from '@docusaurus/types'; | ||
export declare type ApplyTrailingSlashParams = Pick<DocusaurusConfig, 'trailingSlash' | 'baseUrl'>; | ||
export default function applyTrailingSlash(path: string, options: ApplyTrailingSlashParams): string; |
@@ -9,3 +9,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function applyTrailingSlash(path, trailingSlash) { | ||
// Trailing slash handling depends in some site configuration options | ||
function applyTrailingSlash(path, options) { | ||
const { trailingSlash, baseUrl } = options; | ||
if (path.startsWith('#')) { | ||
@@ -22,2 +24,5 @@ // Never apply trailing slash to an anchor link | ||
} | ||
function handleTrailingSlash(str, trailing) { | ||
return trailing ? addTrailingSlash(str) : removeTrailingSlash(str); | ||
} | ||
// undefined = legacy retrocompatible behavior | ||
@@ -30,9 +35,11 @@ if (typeof trailingSlash === 'undefined') { | ||
// Never transform '/' to '' | ||
const newPathname = pathname === '/' | ||
? '/' | ||
: trailingSlash | ||
? addTrailingSlash(pathname) | ||
: removeTrailingSlash(pathname); | ||
// Never remove the baseUrl trailing slash! | ||
// If baseUrl = /myBase/, we want to emit /myBase/index.html and not /myBase.html ! | ||
// See https://github.com/facebook/docusaurus/issues/5077 | ||
const shouldNotApply = pathname === '/' || pathname === baseUrl; | ||
const newPathname = shouldNotApply | ||
? pathname | ||
: handleTrailingSlash(pathname, trailingSlash); | ||
return path.replace(pathname, newPathname); | ||
} | ||
exports.default = applyTrailingSlash; |
@@ -8,2 +8,3 @@ /** | ||
export { default as applyTrailingSlash } from './applyTrailingSlash'; | ||
export type { ApplyTrailingSlashParams } from './applyTrailingSlash'; | ||
export { default as uniq } from './uniq'; |
{ | ||
"name": "@docusaurus/utils-common", | ||
"version": "2.0.0-beta.41eaa690e", | ||
"version": "2.0.0-beta.44225a3df", | ||
"description": "Common (Node/Browser) utility functions for Docusaurus packages.", | ||
@@ -21,3 +21,3 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"@docusaurus/types": "2.0.0-beta.41eaa690e", | ||
"@docusaurus/types": "2.0.0-beta.44225a3df", | ||
"tslib": "^2.2.0" | ||
@@ -28,3 +28,3 @@ }, | ||
}, | ||
"gitHead": "2c55b3a650d826c87c1d4a6c52dcca9309ccdcb6" | ||
"gitHead": "4dac50d01bba4072bf369f9a39a71f7df8af02c1" | ||
} |
@@ -8,23 +8,32 @@ /** | ||
import applyTrailingSlash from '../applyTrailingSlash'; | ||
import applyTrailingSlash, { | ||
ApplyTrailingSlashParams, | ||
} from '../applyTrailingSlash'; | ||
function params( | ||
trailingSlash: boolean | undefined, | ||
baseUrl: string = '/', | ||
): ApplyTrailingSlashParams { | ||
return {trailingSlash, baseUrl}; | ||
} | ||
describe('applyTrailingSlash', () => { | ||
test('should apply to empty', () => { | ||
expect(applyTrailingSlash('', true)).toEqual('/'); | ||
expect(applyTrailingSlash('', false)).toEqual(''); | ||
expect(applyTrailingSlash('', undefined)).toEqual(''); | ||
expect(applyTrailingSlash('', params(true))).toEqual('/'); | ||
expect(applyTrailingSlash('', params(false))).toEqual(''); | ||
expect(applyTrailingSlash('', params(undefined))).toEqual(''); | ||
}); | ||
test('should not apply to /', () => { | ||
expect(applyTrailingSlash('/', true)).toEqual('/'); | ||
expect(applyTrailingSlash('/', false)).toEqual('/'); | ||
expect(applyTrailingSlash('/', undefined)).toEqual('/'); | ||
expect(applyTrailingSlash('/', params(true))).toEqual('/'); | ||
expect(applyTrailingSlash('/', params(false))).toEqual('/'); | ||
expect(applyTrailingSlash('/', params(undefined))).toEqual('/'); | ||
expect(applyTrailingSlash('/?query#anchor', true)).toEqual( | ||
expect(applyTrailingSlash('/?query#anchor', params(true))).toEqual( | ||
'/?query#anchor', | ||
); | ||
expect(applyTrailingSlash('/?query#anchor', false)).toEqual( | ||
expect(applyTrailingSlash('/?query#anchor', params(false))).toEqual( | ||
'/?query#anchor', | ||
); | ||
expect(applyTrailingSlash('/?query#anchor', undefined)).toEqual( | ||
expect(applyTrailingSlash('/?query#anchor', params(undefined))).toEqual( | ||
'/?query#anchor', | ||
@@ -34,67 +43,110 @@ ); | ||
test('should not apply to /baseUrl/', () => { | ||
const baseUrl = '/baseUrl/'; | ||
expect(applyTrailingSlash('/baseUrl/', params(true, baseUrl))).toEqual( | ||
'/baseUrl/', | ||
); | ||
expect(applyTrailingSlash('/baseUrl/', params(false, baseUrl))).toEqual( | ||
'/baseUrl/', | ||
); | ||
expect(applyTrailingSlash('/baseUrl/', params(undefined, baseUrl))).toEqual( | ||
'/baseUrl/', | ||
); | ||
expect( | ||
applyTrailingSlash('/baseUrl/?query#anchor', params(true, baseUrl)), | ||
).toEqual('/baseUrl/?query#anchor'); | ||
expect( | ||
applyTrailingSlash('/baseUrl/?query#anchor', params(false, baseUrl)), | ||
).toEqual('/baseUrl/?query#anchor'); | ||
expect( | ||
applyTrailingSlash('/baseUrl/?query#anchor', params(undefined, baseUrl)), | ||
).toEqual('/baseUrl/?query#anchor'); | ||
}); | ||
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'); | ||
expect(applyTrailingSlash('#', params(true))).toEqual('#'); | ||
expect(applyTrailingSlash('#', params(false))).toEqual('#'); | ||
expect(applyTrailingSlash('#', params(undefined))).toEqual('#'); | ||
expect(applyTrailingSlash('#anchor', params(true))).toEqual('#anchor'); | ||
expect(applyTrailingSlash('#anchor', params(false))).toEqual('#anchor'); | ||
expect(applyTrailingSlash('#anchor', params(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/'); | ||
expect(applyTrailingSlash('abc', params(true))).toEqual('abc/'); | ||
expect(applyTrailingSlash('abc', params(false))).toEqual('abc'); | ||
expect(applyTrailingSlash('abc', params(undefined))).toEqual('abc'); | ||
expect(applyTrailingSlash('abc/', params(true))).toEqual('abc/'); | ||
expect(applyTrailingSlash('abc/', params(false))).toEqual('abc'); | ||
expect(applyTrailingSlash('abc/', params(undefined))).toEqual('abc/'); | ||
expect(applyTrailingSlash('/abc', params(true))).toEqual('/abc/'); | ||
expect(applyTrailingSlash('/abc', params(false))).toEqual('/abc'); | ||
expect(applyTrailingSlash('/abc', params(undefined))).toEqual('/abc'); | ||
expect(applyTrailingSlash('/abc/', params(true))).toEqual('/abc/'); | ||
expect(applyTrailingSlash('/abc/', params(false))).toEqual('/abc'); | ||
expect(applyTrailingSlash('/abc/', params(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( | ||
expect(applyTrailingSlash('/abc#anchor', params(true))).toEqual( | ||
'/abc/#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc#anchor', params(false))).toEqual( | ||
'/abc#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc#anchor', params(undefined))).toEqual( | ||
'/abc#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/#anchor', params(true))).toEqual( | ||
'/abc/#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/#anchor', params(false))).toEqual( | ||
'/abc#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/#anchor', params(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( | ||
expect(applyTrailingSlash('/abc?search', params(true))).toEqual( | ||
'/abc/?search', | ||
); | ||
expect(applyTrailingSlash('/abc?search', params(false))).toEqual( | ||
'/abc?search', | ||
); | ||
expect(applyTrailingSlash('/abc?search', params(undefined))).toEqual( | ||
'/abc?search', | ||
); | ||
expect(applyTrailingSlash('/abc/?search', params(true))).toEqual( | ||
'/abc/?search', | ||
); | ||
expect(applyTrailingSlash('/abc/?search', params(false))).toEqual( | ||
'/abc?search', | ||
); | ||
expect(applyTrailingSlash('/abc/?search', params(undefined))).toEqual( | ||
'/abc/?search', | ||
); | ||
}); | ||
test('should apply to path with ?search#anchor', () => { | ||
expect(applyTrailingSlash('/abc?search#anchor', true)).toEqual( | ||
expect(applyTrailingSlash('/abc?search#anchor', params(true))).toEqual( | ||
'/abc/?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc?search#anchor', false)).toEqual( | ||
expect(applyTrailingSlash('/abc?search#anchor', params(false))).toEqual( | ||
'/abc?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc?search#anchor', undefined)).toEqual( | ||
expect(applyTrailingSlash('/abc?search#anchor', params(undefined))).toEqual( | ||
'/abc?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/?search#anchor', true)).toEqual( | ||
expect(applyTrailingSlash('/abc/?search#anchor', params(true))).toEqual( | ||
'/abc/?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/?search#anchor', false)).toEqual( | ||
expect(applyTrailingSlash('/abc/?search#anchor', params(false))).toEqual( | ||
'/abc?search#anchor', | ||
); | ||
expect(applyTrailingSlash('/abc/?search#anchor', undefined)).toEqual( | ||
'/abc/?search#anchor', | ||
); | ||
expect( | ||
applyTrailingSlash('/abc/?search#anchor', params(undefined)), | ||
).toEqual('/abc/?search#anchor'); | ||
}); | ||
@@ -104,20 +156,26 @@ | ||
expect( | ||
applyTrailingSlash('https://xyz.com/abc?search#anchor', true), | ||
applyTrailingSlash('https://xyz.com/abc?search#anchor', params(true)), | ||
).toEqual('https://xyz.com/abc/?search#anchor'); | ||
expect( | ||
applyTrailingSlash('https://xyz.com/abc?search#anchor', false), | ||
applyTrailingSlash('https://xyz.com/abc?search#anchor', params(false)), | ||
).toEqual('https://xyz.com/abc?search#anchor'); | ||
expect( | ||
applyTrailingSlash('https://xyz.com/abc?search#anchor', undefined), | ||
applyTrailingSlash( | ||
'https://xyz.com/abc?search#anchor', | ||
params(undefined), | ||
), | ||
).toEqual('https://xyz.com/abc?search#anchor'); | ||
expect( | ||
applyTrailingSlash('https://xyz.com/abc/?search#anchor', true), | ||
applyTrailingSlash('https://xyz.com/abc/?search#anchor', params(true)), | ||
).toEqual('https://xyz.com/abc/?search#anchor'); | ||
expect( | ||
applyTrailingSlash('https://xyz.com/abc/?search#anchor', false), | ||
applyTrailingSlash('https://xyz.com/abc/?search#anchor', params(false)), | ||
).toEqual('https://xyz.com/abc?search#anchor'); | ||
expect( | ||
applyTrailingSlash('https://xyz.com/abc/?search#anchor', undefined), | ||
applyTrailingSlash( | ||
'https://xyz.com/abc/?search#anchor', | ||
params(undefined), | ||
), | ||
).toEqual('https://xyz.com/abc/?search#anchor'); | ||
}); | ||
}); |
@@ -9,2 +9,4 @@ /** | ||
export {default as applyTrailingSlash} from './applyTrailingSlash'; | ||
export type {ApplyTrailingSlashParams} from './applyTrailingSlash'; | ||
export {default as uniq} from './uniq'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65098
369
+ Added@docusaurus/types@2.0.0-beta.44225a3df(transitive)
- Removed@docusaurus/types@2.0.0-beta.41eaa690e(transitive)