Merge pull request #2793 from slorber/feature/client-side-redirects

feat(v2): docusaurus-plugin-client-redirects
This commit is contained in:
Sébastien Lorber 2020-06-10 17:36:57 +02:00 committed by GitHub
commit 68a1bb1ebf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 1762 additions and 0 deletions

View file

@ -12,6 +12,7 @@ import camelCase from 'lodash.camelcase';
import kebabCase from 'lodash.kebabcase';
import escapeStringRegexp from 'escape-string-regexp';
import fs from 'fs-extra';
import {URL} from 'url';
const fileHash = new Map();
export async function generate(
@ -349,3 +350,35 @@ export function getEditUrl(fileRelativePath: string, editUrl?: string) {
? normalizeUrl([editUrl, posixPath(fileRelativePath)])
: undefined;
}
export function isValidPathname(str: string): boolean {
if (!str.startsWith('/')) {
return false;
}
try {
return new URL(str, 'https://domain.com').pathname === str;
} catch (e) {
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`);
}