migrate useful helper functions to docusaurus-utils

This commit is contained in:
slorber 2020-06-10 17:22:41 +02:00
parent c964a1e3b6
commit 6b507630e3
7 changed files with 77 additions and 95 deletions

View file

@ -339,3 +339,24 @@ export function isValidPathname(str: string): boolean {
return false;
}
}
export function addTrailingSlash(str: string) {
return str.endsWith('/') ? str : `${str}/`;
}
export function removeTrailingSlash(str: string) {
return removeSuffix(str, '/');
}
export function removeSuffix(str: string, suffix: string) {
if (suffix === '') {
return str; // always returns "" otherwise!
}
return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
}
export function getFilePathForRoutePath(routePath: string) {
const fileName = path.basename(routePath);
const filePath = path.dirname(routePath);
return path.join(filePath, `${fileName}/index.html`);
}