@docusaurus/utils-common
Advanced tools
Comparing version 2.0.0-beta.bfd33dc63 to 2.0.0-beta.c3127b65d
@@ -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,5 +9,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function applyTrailingSlash(path, trailingSlash) { | ||
// Never apply trailing slash to an anchor link | ||
// Trailing slash handling depends in some site configuration options | ||
function applyTrailingSlash(path, options) { | ||
const { trailingSlash, baseUrl } = options; | ||
if (path.startsWith('#')) { | ||
// Never apply trailing slash to an anchor link | ||
return path; | ||
@@ -22,2 +24,5 @@ } | ||
} | ||
function handleTrailingSlash(str, trailing) { | ||
return trailing ? addTrailingSlash(str) : removeTrailingSlash(str); | ||
} | ||
// undefined = legacy retrocompatible behavior | ||
@@ -29,7 +34,12 @@ if (typeof trailingSlash === 'undefined') { | ||
const [pathname] = path.split(/[#?]/); | ||
const newPathname = trailingSlash | ||
? addTrailingSlash(pathname) | ||
: removeTrailingSlash(pathname); | ||
// Never transform '/' to '' | ||
// 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,1 +8,3 @@ /** | ||
export { default as applyTrailingSlash } from './applyTrailingSlash'; | ||
export type { ApplyTrailingSlashParams } from './applyTrailingSlash'; | ||
export { default as uniq } from './uniq'; |
@@ -12,4 +12,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.applyTrailingSlash = void 0; | ||
exports.uniq = exports.applyTrailingSlash = void 0; | ||
var applyTrailingSlash_1 = require("./applyTrailingSlash"); | ||
Object.defineProperty(exports, "applyTrailingSlash", { enumerable: true, get: function () { return __importDefault(applyTrailingSlash_1).default; } }); | ||
var uniq_1 = require("./uniq"); | ||
Object.defineProperty(exports, "uniq", { enumerable: true, get: function () { return __importDefault(uniq_1).default; } }); |
{ | ||
"name": "@docusaurus/utils-common", | ||
"version": "2.0.0-beta.bfd33dc63", | ||
"version": "2.0.0-beta.c3127b65d", | ||
"description": "Common (Node/Browser) utility functions for Docusaurus packages.", | ||
@@ -21,3 +21,3 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"@docusaurus/types": "2.0.0-beta.bfd33dc63", | ||
"@docusaurus/types": "2.0.0-beta.c3127b65d", | ||
"tslib": "^2.2.0" | ||
@@ -28,3 +28,3 @@ }, | ||
}, | ||
"gitHead": "5fee403a1424933db3b5454886b71d64ecd16304" | ||
"gitHead": "42adccc80d7d0265013f4343cdc0c2f7159535a4" | ||
} |
@@ -8,82 +8,144 @@ /** | ||
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 apply to /', () => { | ||
expect(applyTrailingSlash('/', true)).toEqual('/'); | ||
expect(applyTrailingSlash('/', false)).toEqual(''); | ||
expect(applyTrailingSlash('/', undefined)).toEqual('/'); | ||
test('should not apply to /', () => { | ||
expect(applyTrailingSlash('/', params(true))).toEqual('/'); | ||
expect(applyTrailingSlash('/', params(false))).toEqual('/'); | ||
expect(applyTrailingSlash('/', params(undefined))).toEqual('/'); | ||
expect(applyTrailingSlash('/?query#anchor', params(true))).toEqual( | ||
'/?query#anchor', | ||
); | ||
expect(applyTrailingSlash('/?query#anchor', params(false))).toEqual( | ||
'/?query#anchor', | ||
); | ||
expect(applyTrailingSlash('/?query#anchor', params(undefined))).toEqual( | ||
'/?query#anchor', | ||
); | ||
}); | ||
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'); | ||
}); | ||
@@ -93,20 +155,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,1 +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
64826
16
369
+ Added@docusaurus/types@2.0.0-beta.c3127b65d(transitive)
+ Added@types/node@22.10.1(transitive)
+ Addedcaniuse-lite@1.0.30001685(transitive)
+ Addedelectron-to-chromium@1.5.68(transitive)
- Removed@docusaurus/types@2.0.0-beta.bfd33dc63(transitive)
- Removed@types/node@22.10.0(transitive)
- Removedcaniuse-lite@1.0.30001684(transitive)
- Removedelectron-to-chromium@1.5.65(transitive)