mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-25 20:48:50 +02:00
refactor(utils): remove duplicated function (#9972)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
parent
efbe474e9c
commit
821247142e
24 changed files with 160 additions and 141 deletions
|
@ -5,6 +5,7 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {addPrefix, removeSuffix} from './stringUtils';
|
||||
import type {DocusaurusConfig} from '@docusaurus/types';
|
||||
|
||||
export type ApplyTrailingSlashParams = Pick<
|
||||
|
@ -12,6 +13,10 @@ export type ApplyTrailingSlashParams = Pick<
|
|||
'trailingSlash' | 'baseUrl'
|
||||
>;
|
||||
|
||||
export function addTrailingSlash(str: string): string {
|
||||
return str.endsWith('/') ? str : `${str}/`;
|
||||
}
|
||||
|
||||
// Trailing slash handling depends in some site configuration options
|
||||
export default function applyTrailingSlash(
|
||||
path: string,
|
||||
|
@ -24,13 +29,6 @@ export default function applyTrailingSlash(
|
|||
return path;
|
||||
}
|
||||
|
||||
// TODO deduplicate: also present in @docusaurus/utils
|
||||
function addTrailingSlash(str: string): string {
|
||||
return str.endsWith('/') ? str : `${str}/`;
|
||||
}
|
||||
function removeTrailingSlash(str: string): string {
|
||||
return str.endsWith('/') ? str.slice(0, -1) : str;
|
||||
}
|
||||
function handleTrailingSlash(str: string, trailing: boolean): string {
|
||||
return trailing ? addTrailingSlash(str) : removeTrailingSlash(str);
|
||||
}
|
||||
|
@ -55,3 +53,13 @@ export default function applyTrailingSlash(
|
|||
|
||||
return path.replace(pathname, newPathname);
|
||||
}
|
||||
|
||||
/** Appends a leading slash to `str`, if one doesn't exist. */
|
||||
export function addLeadingSlash(str: string): string {
|
||||
return addPrefix(str, '/');
|
||||
}
|
||||
|
||||
/** Removes the trailing slash from `str`. */
|
||||
export function removeTrailingSlash(str: string): string {
|
||||
return removeSuffix(str, '/');
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue